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

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

Vectors and proraming in a fuction

athurin
4-Participant

Vectors and proraming in a fuction

Hi,

I have a function that is defined by different equations, based on an "if" statement. both the equations and the conditions depend on coefficients stored in vectors. I expect, as a result, a function that returns a vector.

I have tried several syntaxes, but I can't find one that works. Either i don't put the index, and the condition fails (comparing a scalar to a vector), or I put the index and the function fails to find a result (although, if I set the function parameter as a fixed variable, the content of the function is evaluated correctly).

Is there a proper syntax to get it to work ?

Thank you very much.

1 ACCEPTED SOLUTION

Accepted Solutions

This is what Werner's example does (thank you very much !). I didn't know about the vectorization tool, and it seems to do the trick. I am just a bit puzzled why I can't apply it with k and y being global variables rather than function parameters.

This is because of the if statement. Its argument has to be a scalar (0 means false, any other number is interpreted as true). If you use global vectors in your comparison, the result is a vector consisting of 0's and/or 1's. Obvisously Mathcad can't decide if it should interprete such a vector as true or false - its no scalar and so the error is thrown. This applies to the programming if statement and to the if function likewise.

With my approach the call of the function g(x,k,y) ist vectorized, Mathcad loops through the involved vectors, feeds the called function g() with the single elements and collects the various results in a vector. So the function g() never sees a vector but is just fed with scalars and so the if construct works.

I generally find it the preferred approach that a function should be self contained - that all thats needed for the calculation should be either hard coded or provided via function argument. Its less failure prone and more versatile that way.

But if you insist on defining g(x) with just one argument and using global y and k you can do that if you replace the if statement by a sum using boolean expressions. We use here that a false statement yields zero and a true one yields 1. See g1() and g2().

1.png

And of course you are free to program the looping through your vectors yourself and use either the if function or the if statement. See g3() and g4().

2.png

View solution in original post

6 REPLIES 6
RichardJ
19-Tanzanite
(To:athurin)

You are trying to compare a scalar, x, to a two element vector, y. There is no way to do that. I assume you want to compare x to each element in y. If so, when should the condition be true?

When x is less than y1 and x is less than y2 (AND)

When x is less than y1 or x is less than y2 or x is less than y1 and x is less than y2 (OR)

When x is less than y1 or x is less than y2 but not when x is less than y1 and x is less than y2 (XOR)

Are you looking for something like the attached?

athurin
4-Participant
(To:athurin)

Richard, I don't particularly want to compare a scalar to a vector. I would like to have a function result that is a vector, who's result depends on the comparison on of each value in the vector.

Basically, what I want is :

f(x)_1 = if(x<y1) (k1 * x) else (2* k1*x)

f(x)_2 = if(x<y2) (k2 * x) else (2* k2*x)

This is what Werner's example does (thank you very much !). I didn't know about the vectorization tool, and it seems to do the trick. I am just a bit puzzled why I can't apply it with k and y being global variables rather than function parameters.

I find it also quite strange that, outside of a function, operations involving scalars and vectors seem to "just work". y-3 or y>3 the expected answer, with operations applied element-wise. What is different in this example ? Is it the programing functions who don't like vector results ?

This is what Werner's example does (thank you very much !). I didn't know about the vectorization tool, and it seems to do the trick. I am just a bit puzzled why I can't apply it with k and y being global variables rather than function parameters.

This is because of the if statement. Its argument has to be a scalar (0 means false, any other number is interpreted as true). If you use global vectors in your comparison, the result is a vector consisting of 0's and/or 1's. Obvisously Mathcad can't decide if it should interprete such a vector as true or false - its no scalar and so the error is thrown. This applies to the programming if statement and to the if function likewise.

With my approach the call of the function g(x,k,y) ist vectorized, Mathcad loops through the involved vectors, feeds the called function g() with the single elements and collects the various results in a vector. So the function g() never sees a vector but is just fed with scalars and so the if construct works.

I generally find it the preferred approach that a function should be self contained - that all thats needed for the calculation should be either hard coded or provided via function argument. Its less failure prone and more versatile that way.

But if you insist on defining g(x) with just one argument and using global y and k you can do that if you replace the if statement by a sum using boolean expressions. We use here that a false statement yields zero and a true one yields 1. See g1() and g2().

1.png

And of course you are free to program the looping through your vectors yourself and use either the if function or the if statement. See g3() and g4().

2.png

athurin
4-Participant
(To:Werner_E)

This is a lot clearer. Thank you very much!

I particularly like the g4 function, because it is more readable, especially with more than one condition (or more complex conditions).

Adrien Thurin wrote:

This is a lot clearer. Thank you very much!

I particularly like the g4 function, because it is more readable, especially with more than one condition (or more complex conditions).

Yes, I sure wouldn't recommend g1 and g2.

And with all those functions you are free to add k and y as function arguments so they don't rely on global variables with a specific name but are self-contained 😉

Top Tags