0

H, W, R,V are matrices and are already initialized with respective sizes. "beta" is an int, "myeps" is a float. For the matrix implementation I have currently used Eigen library. However I'm not sure of the syntax to convert this Matlab code successfully to Eigen based C++ code.

Matlab Code

H = H .* ( (W'*(R.^(beta-2) .* V)) ./ max(W'*R.^(beta-1), myeps) ); 

C++ Code (What I tried so far)

WH = W_ * H_;

Eigen::MatrixXf j=(W_.transpose().array()*(WH.array().pow((beta2)).cwiseProduct(V.array())));

Eigen::MatrixXf k=(W_.transpose().array()*((WH.array().pow(beta-1))));

float m=max(k.maxCoeff(),0.001);

H_ = H_.cwiseProduct(j/m);

Is this code correct?

FYI - This is a step in a NMF Algorithm(Non-negative matrix Factorization based on beta divergence).

Any help would be greatly appreciated.

Miyuru Sagarage
  • 1,305
  • 1
  • 11
  • 15
  • 2
    So, what have you tried? What do you have issues with? The component-wise operations? – PeterT Oct 26 '13 at 19:34
  • Up to now I have this code... is it correct? WH = W_ * H_; Eigen::MatrixXf j=(W_.transpose().array()*(WH.array().pow((beta-2)).cwiseProduct(V.array()))); Eigen::MatrixXf k=(W_.transpose().array()*((WH.array().pow(beta-1)))); float m=max(k.maxCoeff(),0.001); H_ = H_.cwiseProduct(j/m); – Miyuru Sagarage Oct 27 '13 at 04:25

2 Answers2

1

This does not seem correct. * on arrays is equivalent to a cwiseProduct while you want a matrix product with W'. Proposition:

Eigen::MatrixXf j = (W.transpose() * (R.array().pow(beta-2)*V.array()).matrix());
Eigen::MatrixXf k = (W.transpose() *  R.array().pow(beta-1).matrix());
H = H.cwiseProduct(j/max(k.maxCoeff(),myeps));
ggael
  • 28,425
  • 2
  • 65
  • 71
0

I recommend you have a look at this quick reference:

http://eigen.tuxfamily.org/dox/AsciiQuickReference.txt

It contains mappings between Eigen and Matlab.

For example:

R = P.cwiseProduct(Q);    // R = P .* Q

and

R.array().square()        // P .^ 2

As you can see, this Rosetta Stone can help you translate your expressions.

Escualo
  • 40,844
  • 23
  • 87
  • 135