This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Eigen friendly form for: (scaler1*(vector - scaler2))^2

Tags: None
(comma "," separated)
mehrdadh
Registered Member
Posts
26
Karma
0
Hi,

I would like to do either of this (which ever is faster to calculate):

1) vector2 += scaler1 * ((vector - scaler2))^2
or
2) vector2 += (scaler1 * (vector - scaler2))^2

What I have done so far is this like of code:
Code: Select all
Vec1.array() +=  scaler2*((vec2.array() - scaler1).array().square();         

could you please tell me what is the best way to do that ? so to eliminate temporaries as much as possible.
User avatar
bjacob
Registered Member
Posts
658
Karma
3
Your Eigen code is already perfect and won't cause any temporary to be created. It also should vectorize perfectly.

The only comment that I have to make is: if you are interested in this kind of operations, are you sure that you really want vectors (matrices) and not Arrays ? Eigen3 allows you to declare your objects as Arrays so that you don't need to call .array() to get Array behavior. This is just to make your code a bit simpler:

Code: Select all
ArrayXf array1, array2;
array1 +=  scalar2 * (array2 - scalar1).square();         


but of course, if you want matrix arithmetic elsehwere in your code, then your current code was the right choice.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
FMD
Registered Member
Posts
25
Karma
0
A quick thought: Wouldn't it be nice to have something like type conversion (aka mapping) in the line of

Code: Select all
VectorXf v1, v2;
ArrayXf_Map am1(v1), am2(v2);

am1 +=  scalar2 * (am2 - scalar1).square();


Cheers
Frank
User avatar
bjacob
Registered Member
Posts
658
Karma
3
Oh you mean naming and storing the expression returned by .array() ? You can do this today; .array() returns a ArrayWrapper object that you can store for later use. See class ArrayWrapper.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
FMD
Registered Member
Posts
25
Karma
0
OK, shame on me :z .
Isn't it nice to have expressions? Yes, it is!


Bookmarks



Who is online

Registered users: abc72656, Bing [Bot], daret, Google [Bot], Sogou [Bot], Yahoo [Bot]