ANAGHA S GOURI
ANAGHA S GOURI

Reputation: 41

How to perform matrix matrix division using eigen library in C++

I did a MATLAB code and it had to perform

B3=abs(B2/max(B2));

where B2 is an 181 x 238 matrix , max(B2) should give me a matrix of 1 x 238 comprising of maximum value in each column and B3 should be 181x1matrix. What should be the equivalent C++ code using Eigen library? Please help. On modifying my code, with simpler dimension say with 2 x 2 matrix

//problem
#include <iostream>
#include<complex.h>
#include <eigen3/Eigen/Dense>
#include <eigen3/Eigen/Core>

using namespace Eigen;
using namespace std;
using Eigen::MatrixXd;

int main()
{
    MatrixXd A(2,2);MatrixXd B(2,1);MatrixXd C(1,2);
    A<<4,12,
       6,8;
            C=A.colwise().maxCoeff();
        //B=(A*(1.0/C)).cwiseAbs();
             B=A.array()/C.array();
   cout << "The solution is A :\n" << B.cwiseAbs()<< endl;

    return 0;
}

But I am not able to execute this code.

hp@hp-HP-Notebook:~/beamforming/programs/eigen_prog$ g++ mm_t.cpp -o mm_t

hp@hp-HP-Notebook:~/beamforming/programs/eigen_prog$ ./mm_t mm_t: /usr/local/include/eigen3/Eigen/src/Core/CwiseBinaryOp.h:110: Eigen::CwiseBinaryOp::CwiseBinaryOp(const Lhs&, const Rhs&, const BinaryOp&) [with BinaryOp = Eigen::internal::scalar_quotient_op; LhsType = const Eigen::ArrayWrapper >; RhsType = const Eigen::ArrayWrapper >; Eigen::CwiseBinaryOp::Lhs = Eigen::ArrayWrapper >; Eigen::CwiseBinaryOp::Rhs = Eigen::ArrayWrapper >]: Assertion `aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols()' failed. Aborted (core dumped)

Any idea what is wrong?? I did simple execution in my MATLAB command window to simplify what I want to get as output.

m=[4,12;6,8]

m =

 4    12
 6     8

max(m)

ans = 6 12

abs(m/max(m))

ans =

0.9333
0.7333

I am stuck with this problem for a long time. Please help.

Upvotes: 0

Views: 3653

Answers (1)

norio
norio

Reputation: 3892

I interpret B3=abs(B2/max(B2)) as folllowing.

  • b = max(B2) is a row vector containing the largest elements of the respective columns of B2.

  • q = B2/b means the least-squares solution to the overdetermined linear equations q b = B2. (There are nrow independent problems, where nrow is the number of rows of B2). This equation is equivalent to b^T q^T = B2^T, where ^T is my notation for transpose, and this form is more frequently implemented in many libraries, I guess.

  • abs(q) means the elementwise absolute value of q.

So, the required result is x below. Maybe.

#include <iostream>
#include <eigen3/Eigen/Dense>
#include <eigen3/Eigen/Core>

using namespace Eigen;
using namespace std;

int main()
{
  MatrixXd A(2,2), Atr(2,2);
  VectorXd b(2), x(2);
  A<<4,12,
    6,8;
  cout << "A :\n" << A << endl;

  Atr=A.transpose();
  cout << "Atr :\n" << Atr << endl;

  b=A.colwise().maxCoeff();
  cout << "b :\n" << b << endl;

  x = b.colPivHouseholderQr().solve(Atr).cwiseAbs();
  cout << "x :\n" << x << endl;

  return 0;
}

Output is

A :
 4 12
 6  8
Atr :
 4  6
12  8
b :
 6
12
x :
0.933333
0.733333

cf.

https://eigen.tuxfamily.org/dox/group__LeastSquares.html


  • Below is my old answer based on misunderstanding of the definition of A/v in Matlab.

Maybe the result B3 in the quection corresponds to the vector x below.

#include <iostream>
#include <eigen3/Eigen/Dense>
#include <eigen3/Eigen/Core>

using namespace Eigen;
using namespace std;

int main()
{
  MatrixXd A(2,2);
  VectorXd b(2), x(2);
  A<<4,12,
    6,8;
  cout << "A :\n" << A << endl;

  b=A.colwise().maxCoeff();
  cout << "b :\n" << b << endl;

  x = A.colPivHouseholderQr().solve(b).cwiseAbs();
  cout << "x :\n" << x << endl;

  return 0;
}

cf

http://eigen.tuxfamily.org/dox/group__TutorialLinearAlgebra.html


Below is old and wrong answer based on my misunderstanding of max(A) in matlab.

In Matlab, max(A) is the maximum element of the matrix A, and abs(A) returns a matrix taking the absolute values of the respective element of A. So, if B2 is a Matrix object of eigen, maybe

B2=(B2*(1.0/B2.maxCoeff())).cwiseAbs()

cf. https://www.mathworks.com/help/matlab/ref/abs.html?searchHighlight=abs&s_tid=gn_loc_drop http://eigen.tuxfamily.org/dox/group__QuickRefPage.html

Upvotes: 0

Related Questions