See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Arguments for custom unary/binaryExpr() eigen3

General Tech Bugs & Fixes

Max. 2000 characters



( 6 months ago )


I'm trying to eliminate nested for loops by making use of coefficient-wise operations on eigen3 objects. In order to achieve this I have to generalize an already existing function such that I can make us of custom coefficient-wise operations.

I found that eigen provides two functions, unaryExpr() and binaryExpr() (documentation), that allow to implement a custom coefficient-wise operation on eigen Arrays. However, as far as I understand, you can only give one or two arguments to these functions which represent the coefficients from the array itself. I would like to pass other arguments as well to this function since I need these other arguments to complete the calculation.

I would like to generalize the following function

inline Complex expValue(int twoMS, int twoMSPrime, const Matrix2cd& mat)
  const Vector2cd& bra = getSpinBasisState(twoMSPrime);
  const Vector2cd& ket = getSpinBasisState(twoMS);

  return bra.adjoint()*mat*ket;

All the possible combinations of values for twoMS and twoMSPrime I have stored in an array like this

Eigen::ArrayXXd spinCGPart(16, 2);

So, 16 different combinations and two columns, one for twoMS and one for twoMSPrime. Instead of looping over all the different combinations, I would like to implement a coefficient-wise operation like so

Eigen::ArrayXXcd result(16, 1);
result = spinCGPart.col(0).binaryExpr(spinCGPart.col(1), generalExpVal);

Where generalExpVal should be something like

complex generalExpVal(int a, int b, const Matrix2cd& mat) const 
  const Vector2cd& bra = getSpinBasisState(b);
  const Vector2cd& ket = getSpinBasisState(a);

  return bra.adjoint()*mat*ket;

I'm stuck with implementing this last function. The documentation for the binaryExpr() looks like it doesn't allow extra parameters to be given to the function. Is this the case? I need to pass mat as an argument since it changes constantly throughout the calculation. Any suggestion regarding eigen or another way of thinking about the problem would be very helpful and appreciated!

what's your interest