Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6765197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:41:03+00:00 2026-05-26T14:41:03+00:00

I am trying to port a MATLAB program to C++. And I want to

  • 0

I am trying to port a MATLAB program to C++.
And I want to implement a left matrix division between a matrix A and a column vector B.

A is an m-by-n matrix with m is not equal to n and B is a column vector with m components.

And I want the result X = A\B is the solution in the least squares sense to the under- or overdetermined system of equations AX = B. In other words, X minimizes norm(A*X - B), the length of the vector AX - B.
That means I want it has the same result as the A\B in MATLAB.

I want to implement this feature in GSL-GNU (GNU Science Library) and I don’t know too much about math, least square fitting or matrix operation, can somebody told me how to do this in GSL? Or if implement them in GSL is too complicate, can someone suggest me a good open source C/C++ library that provides the above matrix operation?


Okay, I finally figure out by my self after spend another 5 hours on it.. But still thanks for the suggestions to my question.

Assuming we have a 5 * 2 matrix

A = [1 0           
     1 0
     0 1
     1 1
     1 1] 

and a vector b = [1.8388,2.5595,0.0462,2.1410,0.6750]

The solution to the A \ b would be

 #include <stdio.h>
 #include <gsl/gsl_linalg.h>

 int
 main (void)
 {
   double a_data[] = {1.0, 0.0,1.0, 0.0, 0.0,1.0,1.0,1.0,1.0,1.0};

   double b_data[] = {1.8388,2.5595,0.0462,2.1410,0.6750};

   gsl_matrix_view m
     = gsl_matrix_view_array (a_data, 5, 2);

   gsl_vector_view b
     = gsl_vector_view_array (b_data, 5);

   gsl_vector *x = gsl_vector_alloc (2); // size equal to n
   gsl_vector *residual = gsl_vector_alloc (5); // size equal to m
   gsl_vector *tau = gsl_vector_alloc (2); //size equal to min(m,n)
   gsl_linalg_QR_decomp (&m.matrix, tau); // 
   gsl_linalg_QR_lssolve(&m.matrix, tau, &b.vector, x, residual);

   printf ("x = \n");
   gsl_vector_fprintf (stdout, x, "%g");
   gsl_vector_free (x);
   gsl_vector_free (tau);
   gsl_vector_free (residual);
   return 0;
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:41:03+00:00Added an answer on May 26, 2026 at 2:41 pm

    In addition to the one you gave, a quick search revealed other GSL examples, one using QR decomposition, the other LU decomposition.

    There exist other numeric libraries capable of solving linear systems (a basic functionality in every linear algebra library). For one, Armadillo offers a nice and readable interface:

    #include <iostream>
    #include <armadillo>
    using namespace std;
    using namespace arma;
    
    int main()
    {
        mat A = randu<mat>(5,2);
        vec b = randu<vec>(5);
    
        vec x = solve(A, b);
        cout << x << endl;
    
        return 0;
    }
    

    Another good one is the Eigen library:

    #include <iostream>
    #include <Eigen/Dense>
    using namespace std;
    using namespace Eigen;
    
    int main()
    {
        Matrix3f A;
        Vector3f b;
        A << 1,2,3,  4,5,6,  7,8,10;
        b << 3, 3, 4;
    
        Vector3f x = A.colPivHouseholderQr().solve(b);
        cout << "The solution is:\n" << x << endl;
    
        return 0;
    }
    

    Now, one thing to remember is that MLDIVIDE is a super-charged function and has multiple execution paths. If the coefficient matrix A has some special structure, then it is exploited to obtain faster or more accurate result (can choose from substitution algorithm, LU and QR factorization, ..)

    MATLAB also has PINV which returns the minimal norm least-squares solution, in addition to a number of other iterative methods for solving systems of linear equations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to port a program written in Microsoft Visual Studio (in c, c++
I'm trying to port a program which uses a hand-rolled interpolator (developed by a
I'm trying to port a Matlab code to Java and C++. It's quite straightforward
I am trying to port some code written in MATLAB to C, so that
I'm trying to port a program/game that was developed in linux/g++ so that it
I am trying to port a script from Matlab to Octave. The one function
I'm trying to port a Python program to Ruby but I'm completely ignorant about
I'm trying to port a LabView program to C++, and the OLE calls it
I am trying to port a Linux program written in C to Windows. In
I am trying to port an Oauth2 client based on Oauth for Spring Security

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.