0

Right now I want to use C++ Boost to solve a matrix function: A*P=X, P=A\X. I have matrix A and matrix X, so I need to do P=A\X to get matrix P. That's a matrix division problem, right?

My C++ code is

#include "stdafx.h" 
#include <boost\mat2cpp-20130725/mat2cpp.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/io.hpp>
using namespace boost::numeric::ublas;
using namespace std;

int main() {
    using namespace mat2cpp;
    matrix<double> x(2,2); // initialize a matrix
    x(0, 0) = 1;  // assign value
    x(1, 1) = 1;
    matrix<double> y(2, 1);
    y(0, 0) = 1;
    y(1, 0) = 1;

    size_t rank;
    matrix<double> z = matrix_div(x, y, rank);
}

But it has errors Error figure, please help me! Thanks!

Yahui Sun
  • 11
  • 3
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – The Quantum Physicist Mar 07 '17 at 09:51
  • I can't find `boost\mat2cpp-20130725/mat2cpp.hpp` in my Boost distribution (1.63). What version do you use? – TobiMcNamobi Mar 07 '17 at 09:56
  • Google finds three (3) pages with the string "mat2cpp-20130725". One of them is this question. The other two suggest that this directory is not a part of boost. – n. m. could be an AI Mar 07 '17 at 16:38

1 Answers1

1

First of all there is no such thing as matrix division. If you have this equation A * P=X and you want to find P, than the solution is: inv(A) * A * P=inv(A) * X, where inv(A) is the inverse of the A matrix. Because we know that the inv(A) * A is equal to the identity matrix, than we can conclude that P=inv(A) * X.

Now your problem is to calculate the inverse of the A matrix. There are several ways to do this, my suggestion would be to use LU factorization.

Honestly, I don't know if the boost library has such thing as mat2cpp. If you want to use boost, I would recommend using boost/numeric/ublas/matrix.hpp.

Ervin Szilagyi
  • 14,274
  • 2
  • 25
  • 40
  • Good answer. I like to add "calculate the inverse ... *if it exists*". Also in addition to [uBLAS](http://www.boost.org/doc/libs/1_63_0/libs/numeric/ublas/doc/index.html) Boost contains a library called [QVM](http://www.boost.org/doc/libs/1_63_0/libs/qvm/doc/index.html) which I used recently to calculate the inverse of a (small) matrix. – TobiMcNamobi Mar 08 '17 at 07:50