cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community email notifications are disrupted. While we are working to resolve, please check on your favorite boards regularly to keep up with your conversations and new topics.

This value must be a vector

Hans_Westerweel
7-Bedrock

This value must be a vector

Hi,

 

Could someone explain why I can't vectorize method 2 in the attached work sheet? It returns error message "this value must be a vector". The output of both functions being multiplied seem to be vectors, so I don't understand why I can't use the vectorize function to do an element per element multiplication.

 

Hans_Westerweel_0-1697204079564.png

Thank you,

 

1 ACCEPTED SOLUTION

Accepted Solutions

Your function a2 was already written with vectors in mind. Its assumes that e.g. EL1 ist a vector. When you call M3 this fuction calls a2 vectorized which means that its first argument EL.1 (and the others as well) is a scalar, not a vector and therefore "last(EL.1)" finally throws the error because the function "last" needs a vector as its argument.

 

Basically you should write every function so that it works just with scalars and then just later use vectorization when you call them with vector arguments.

 

So you function a2 may simply look that way:

Werner_E_0-1697206771811.png

and so your function M3 works as expected

Werner_E_1-1697206826838.png

 

P.S.: You can use the "Error Tracing" facility on the "Calculation" Ribbon to find out where the error actually stems from

 

View solution in original post

6 REPLIES 6

Your function a2 was already written with vectors in mind. Its assumes that e.g. EL1 ist a vector. When you call M3 this fuction calls a2 vectorized which means that its first argument EL.1 (and the others as well) is a scalar, not a vector and therefore "last(EL.1)" finally throws the error because the function "last" needs a vector as its argument.

 

Basically you should write every function so that it works just with scalars and then just later use vectorization when you call them with vector arguments.

 

So you function a2 may simply look that way:

Werner_E_0-1697206771811.png

and so your function M3 works as expected

Werner_E_1-1697206826838.png

 

P.S.: You can use the "Error Tracing" facility on the "Calculation" Ribbon to find out where the error actually stems from

 

Very helpful, thank you!

Hi Werner,

 

The solution works for M3, but evaluation of a2 gives an error because the arguments are vectors and not scalars.

 

Is there a way to rewrite the functions that allows evaluation of both? I'm trying to avoid having to write for-loops for every function that requires if-else statements.

 

Hans_Westerweel_0-1697209102786.png

 

Thank you,

 

Rename  the function a2(...) as it was suggested by me to a2_aux(....) or anything else.

Then define a2(....) to call a2_aux vectorized.

This function a2 should work with scalars and with vectors alike.

 

EDIT: If you dislike the idea of having an extra auxiliary function, you can also make it a local function to a2

Werner_E_0-1697210401095.png

Doing so is necessary because Prime does not allow to vectorize a comparison operator like >= directly.

 

Personally I would prefer that my functions work with scalars only and just if I want to apply them to vectors I would call them explicitly vectorized. I find this approach clearer and straightforward for me .

 

 

 

Interesting idea to add local functions, thanks for showing that.

 

It appears Prime also allows the following, where you vectorize the function prior to evaluation. This seems to be the simplest approach so far. Not sure if I'll run into any surprises later on though.

 

Hans_Westerweel_0-1697211013487.png

 

Yes, thats exactly what I was talking about. a2 here is a scalar-only function and when you want to feed it with  vectors, you use vectorization.

Actually thats what my last suggestion is doing anyway - aaux is the (local) scalar-only function and is called vectorized. I only provided this because you demanded a function which would work with scalars AND with vectors WITHOUT using explicit vectorization. At least thats what I understood.

Top Tags