Skip to main content
1-Visitor
June 27, 2018
Solved

How to vectorise

  • June 27, 2018
  • 2 replies
  • 2256 views

Hallo,

can anyone help me with the question how to vectorise this function?

Thank you,

br

Stefan

Best answer by Werner_E

Guess you are looking for something like this:

B1.png

While this works I would much more favor an approach, where all functions are written just for scalar arguments and have all variables they depend on as function argument. After all all the calculations your functions are doing are basically intended to be done with scalars, not with vectors. So the functions should be set up that way and should use no vectorization.

Later those function could be be called vectorized with vectors as arguments and you can always call those function also with simple scalar arguments to check or to quickly play with different values.

Doing so also avoids problems like the one you ran into.

 B2.png

Reason for the effect you experience is, that your vectorized call of sigma.rRf  not only vectorizes F.b but also vectorizes D. So sigma.rRA is not fed with the vector D, as you intended, but with a single scalar, a component of D. The result is a vector, as sigma.rRA unfortunately uses vectors for its calculations. This vector is multiplied by the appropriate component of F.b and the result for every of these five calls is a vector. Thats the reason your function presents you with a nested vector as result.

2 replies

21-Topaz II
June 27, 2018

Hi,

As a result, what do you want? a vector? or a scalar?

Maybe so?

maybeso.jpg

1-Visitor
June 27, 2018

the result should be a Vector. What failure is there in my function?

 

Best thankx

Werner_E25-Diamond IAnswer
25-Diamond I
June 27, 2018

Guess you are looking for something like this:

B1.png

While this works I would much more favor an approach, where all functions are written just for scalar arguments and have all variables they depend on as function argument. After all all the calculations your functions are doing are basically intended to be done with scalars, not with vectors. So the functions should be set up that way and should use no vectorization.

Later those function could be be called vectorized with vectors as arguments and you can always call those function also with simple scalar arguments to check or to quickly play with different values.

Doing so also avoids problems like the one you ran into.

 B2.png

Reason for the effect you experience is, that your vectorized call of sigma.rRf  not only vectorizes F.b but also vectorizes D. So sigma.rRA is not fed with the vector D, as you intended, but with a single scalar, a component of D. The result is a vector, as sigma.rRA unfortunately uses vectors for its calculations. This vector is multiplied by the appropriate component of F.b and the result for every of these five calls is a vector. Thats the reason your function presents you with a nested vector as result.

1-Visitor
June 28, 2018

Werner is my hero 😃

Made my day it works

 

Thank you