#### Arguments for custom unary/binaryExpr() eigen3

##### Replies

User

( 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);

}``````

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);

}``````

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!

25-Sep-2019

1 Replies

27-Aug-2019

2 Replies

06-Sep-2019

1 Replies