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

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

program in function definition corrupts vector?

NM_10460738
4-Participant

program in function definition corrupts vector?

"This value must be a scalar"
Why does the standard lognormal (Pr_lnorm) work
but when I introduce a program into the function definition (Pr_tlnorm), I get the above error?

1 ACCEPTED SOLUTION

Accepted Solutions
JKT
12-Amethyst
12-Amethyst
(To:Werner_E)

... or

JKT_0-1674053189767.png

if you expect only vectors are arrays as input for id..

View solution in original post

5 REPLIES 5

If you want to call a function which, like yours, expects a scalar argument "id" using a vector instead and expect this vector to be fed element by element as argument of the function and the various results to be collected in a result vector, you have to use vectorization!

Werner_E_0-1674052627518.png

 

JKT
12-Amethyst
12-Amethyst
(To:Werner_E)

... or

JKT_0-1674053189767.png

if you expect only vectors are arrays as input for id..

NM_10460738
4-Participant
(To:JKT)

Thanks @Werner_E and @JKT 

I'm not familiar enough with Mathcad (or perhaps maths) to be sure what the difference is between the two solutions, but the latter solution by @JKT does appear to offer the benefit of baking in the vectorization at the function definition stage.

There should be no difference regarding the result you get. The solution of JKT works OK because vectorization doesn not complain if no vector is involved at all. So the function S.tr can be called with a vector as argument as well as with a single scalar.

Personally I find it more clear to set up my functions to work with scalars only and use explicit vectorization only when I call those function with a vector as argument. That way its always clear that I am nor using a vector function but a scalar function which is evaluated for the set of scalar values in the vector argument. Personally I feel that that clearness justifies the extra effort of having to explicitly apply vectorization when the function is called with a vector argument.

But I also understand the argument concerning convenience in case of JKT's approach where vectorization has to be applied only once at definition stage. The choice is up to you and you should experience no problems regardless which approach you prefer.

 

NM_10460738
4-Participant
(To:Werner_E)

Thanks for the clarification. In my case I will always pass a vector so it's easier to define it like that.

Top Tags