Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X
Hello everyone,
I'm trying to square the the values in a non-square matrix in a function. In order to do this, I use the vectorize operator above the operation (squaring or multiplying the radii together). However, I'm getting an error in this step, which appears due to the definition of the function. I'm not able to determine the cause, as if I were to perform the operation on a copy of the matricies, it works just fine. Furthermore, I've used the operation in a fuction definition other places in the file.
Thank you!
Solved! Go to Solution.
It seems to me that the problem is that if you vectorize the squaring you automatically also vectorize the call to your function R(). So Mathcad sees itself confronted to do a vectorization with a 4x1 vector AND a 4x5 matrix and refuses to work as of different dimensions. It shouldn't do so in my opinion but I am not sure if its a bug or just an undesired behaviour.
So the only solution I can think of is to separate the call to your function R() and the squaring of the result. You found two ways to do it and the second one looks quite OK to me. But for whatever it may be worth here is a third one using a two-liner and a temporary local variable:
Send the sheet pleace!
At the end of the document, there are three definitions for A.
The first uses the definition for R in the function, which is silly since it isn't using the function for which R is defined.
The second uses the matrix R as passed in an argument, and seems to work OK
The third is how I've defined in the image above, where I reference the function definition for R as defined above.
It seems to me that the problem is that if you vectorize the squaring you automatically also vectorize the call to your function R(). So Mathcad sees itself confronted to do a vectorization with a 4x1 vector AND a 4x5 matrix and refuses to work as of different dimensions. It shouldn't do so in my opinion but I am not sure if its a bug or just an undesired behaviour.
So the only solution I can think of is to separate the call to your function R() and the squaring of the result. You found two ways to do it and the second one looks quite OK to me. But for whatever it may be worth here is a third one using a two-liner and a temporary local variable:
Many thanks Werner!
Matthew Paragano wrote:
Many thanks Werner!
You are welcome!
Here is a fourth way to achieve the goal using an auxiliary function sqr() which simply does a vectorized squaring of the argument. Again its important to separate the function call toR() and the squaring as we must avoid to vectorize the call to R().
So you have a couple a methods to chose from. I personally would stay with the second method you came up with, A(R(myg))=...
EDIT: I think my first explanation of why the error is thrown was wrong. If you vectorize the squaring of R(gain) all calculations are vectorized, beginning with the function call R(gain). The function R() is now feeded four times with a scalar and returns at the end a 4x1 vector consisting of 1x5 row vectors. Now, because of the vectorization, Mathcad would try to square each element of that 4x1 vector one after the other. As those element are 1x5 matrices, the squaring consequently fails with the errormessage because vectorization does not resolve nested matrices (and thats OK).
So the way how vectorization is implemented at the time, there is no way to tell Mathcad in the case of two nested function that it should vectorize the outer function call only but not the inner one. In my opinion this behaviour should be changed and vectorization only be applied to the function the operator clings to.
This sure would mean that we would have to type more vectorization operators in some situations.
As a simple example the following would/should fail with an error
and would have to be written using two vectorizations
Hi Werner,
Werner Exinger wrote:
EDIT: I think my first explanation of why the error is thrown was wrong. If you vectorize the squaring of R(gain) all calculations are vectorized, beginning with the function call R(gain). The function R() is now feeded four times with a scalar and returns at the end a 4x1 vector consisting of 1x5 row vectors. Now, because of the vectorization, Mathcad would try to square each element of that 4x1 vector one after the other. As those element are 1x5 matrices, the squaring consequently fails with the errormessage because vectorization does not resolve nested matrices (and thats OK).
I'm not sure that vectorization not applying to nested matrices is OK. Why do you think it is? My reasons for wanting nested vectorization is that it makes it a lot easier to operate on multi-dimensional array (MDA) representations of data (with a view towards a proper MDA capability). For example, if I have a 3D array where each 2D 'slice' is an image then there are a number of operations, such as normalization and scaling, that I would like to apply to the array as a whole, rather than having to operate on the array a slice at a time. If I use a 3D representation using a vector to hold the 2D slices, then it often proves useful to extend vectorization to provide this global capability. Note that this is also the way that Mathematica and Matlab operate on data, see documentation Mathematica Lists Matlab Computing with Multidimensional Arrays. I created a simple nested vectorization for use in standard Mathcad operations whilst my MDA library implements a set of functions that mirror the built-in functions but operate on the whole array (at an abstract level ... my MDA solution uses a single vector to hold the data so I can use the built-in vectorization operator). See attached worksheet for the vectorization functions
So the way how vectorization is implemented at the time, there is no way to tell Mathcad in the case of two nested function that it should vectorize the outer function call only but not the inner one. In my opinion this behaviour should be changed and vectorization only be applied to the function the operator clings to.
This sure would mean that we would have to type more vectorization operators in some situations.
As a simple example the following would/should fail with an error
and would have to be written using two vectorizations
I agree with you here. I often write expressions of the form f(g(x,y),h(a,b)) where any of x,y, a or b may be arrays. However, as you note, vectorizing function f also vectorizes functions g and h which is decidedly what I *don't* want. Consequently, I've got to write A:=g(x,y) B:= h(a,b) result:=f(A,B). It's probably more readable, but offends my sense of minimalism and the functional flow I normally view such processes as. Being able to limit the vectorization process to just the top level arguments would be nice. It might be interesting to modify the operator to include a placeholder that allows the user to specify the nesting depth with, say, 0 being total vectorization, 1 being top level and so on
I'm not sure that vectorization not applying to nested matrices is OK. Why do you think it is? M
I guess I think/thought it is OK because automatic nested vectorization gives me less control about what exactly I want to vectorize. So the point was the I would rather like the vectorization be applied exactly to the expression its put on. But I agree it would be convenient and useful in many respects if it would work on nested arrays.
Maybe I got confused about nested arrays vs. nested function calls 😉
Thanks for your attached sheet - interesting and useful as usual.
My reasons for wanting nested vectorization is that it makes it a lot easier to operate on multi-dimensional array (MDA) representations of data (with a view towards a proper MDA capability).
You still keep on dreaming?! I wouldn't hold my breath.
Hi Werner,
I guess my problem is that I was born an optimist ... one who is clearly incapable of learning from experience! 🙂
I do hope they do something about data structure support in the near future, because Mathcad is totally outclassed in this respect by just about all its competitors (eg Matlab and Mathematica). It's like pitting a pampered Miniature Poodle against an irritated Rottweiler.
Stuart
StuartBruff wrote:
Hi Werner,
I guess my problem is that I was born an optimist ... one who is clearly incapable of learning from experience! 🙂
I do hope they do something about data structure support in the near future, because Mathcad is totally outclassed in this respect by just about all its competitors (eg Matlab and Mathematica). It's like pitting a pampered Miniature Poodle against an irritated Rottweiler.
Stuart
Hi Stuart,
I like your comparisons and analogies. 🙂
Its a really pity that ever so often when it comes to Mathcad/Prime and its development and advancement we have to finsh with a "hope dies last".
Werner