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

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

Vectorizing SIUnitsOf over array containing NaN

StuartBruff
23-Emerald III

Vectorizing SIUnitsOf over array containing NaN

Whenever I think I might be making progress, I hit something that causes me to scratch my head slightly.

 

This time, it's one of NaN's apparent idiosyncrasies.

 

SIUnitsOf returns NaN when applied to mixed unit matrices, which is somewhat limiting.

 

So, having spent a few happy moments programming Yet Another New Version of a function, this time SIUnitsOf, I was reasonably happy with its behaviour. 

 

Then I thought I ought to keep my hand in writing Express-friendly functions and sort of succeeded.

 

I say "sort of" because I noticed an anomaly: vectorizing SIUnitsOf over a mixed-unit matrix with a pure NaN threw in a spare unit where none was supposed to be.

 

2024 11 04 C.png

 

I've played around with various positions of NaN within the matrix, and it looks as though SIUnitsOf returns the unit of the first element to the NaN under vectorization.

 

The first question is clearly, "Is this 'anomaly' to be expected?", in which case, what am I not understanding?  If it is unexpected behaviour, is it a bug?

 

Mathcad Prime 10 worksheet attached.  I've checked in Mathcad Prime Express 7, and the same behaviour is evident.

 

Stuart

3 REPLIES 3
Werner_E
25-Diamond I
(To:StuartBruff)

I wouldn't have expected these effects, either, but I am not really sure if its a bug but possibly it is.

 

Prime got rid of SUC but it looks like they tried to make it a bit more comfortable by implementing ways so that Prime would guess a necessary missing unit. Maybe the root for some anomalies lies in the attempt to make strict dynamic unit checking a bit less troublesome?

 

NaN seems to behave similar to Zero (with capital "Z").

BTW, I find it strange that zero and Zero are not covered in the help pages but only in the Migration Guide.
According to this, zero should be dimensionless 0, but zero seems to be more strict than 'real' dimensionless 0, which sometimes (but not always as many questions in this forum prove) also behaves as Zero, as the following screenshot shows.

Werner_E_0-1730766002416.png

 

In case of a matrix with mixed units the unit of the first non-unitless-zero and non-NaN entry when the matrix is read columnwise as usual seems to "win"

Werner_E_1-1730766891870.png

What really surprises me is that I thought that vectorization would feed one value after the other into the function and so no value would be aware of any other. But this seems not to be the case.

As we know  there are some functions which cannot be vectorized and the usual way around is to use a wrapper function and call this vectorized.

But this does not work as expected in case of SIUnitsOf

Werner_E_2-1730767057579.png

 

So I ask myself what did i not understand about how vectorization would be implemented?

 

Trying to be clever:

Werner_E_3-1730767550719.png

🙂

 

Werner_E
25-Diamond I
(To:Werner_E)

Arrgh!!

 

Its not an anomaly of SIUnitsOf and it has nothing to do with vectorization.

 

Whenever a matrix is created, the 'appropriate' units seem to be added automatically!!

Werner_E_5-1730768088762.png

So as soon as you type in the matrix element, the NaN is not a simple NaN but Prime would automatically have added a unit 😞

 

In case of NaN this can be avoided. If you really want to store a simple NaN without any (automatically added) unit in a mixed units matrix, you have to make it a calculation with result NaN. The picture shows that 1*NaN works. 1+NaN would work as well as would NaN^2.

NaN + NaN does not work and also NaN^0  doesn't have the desired effect. Actually in NaN^n the only value which works OK is n=2 ! Strange!

 

Werner_E_4-1730768048201.png

 

So your example I could only fix by using 1*NaN instead of just NaN

Werner_E_6-1730769686206.png

 

StuartBruff
23-Emerald III
(To:Werner_E)

I suspect, in the light of that additional information, Sherlock Holmes would declare the hand of Professor Bugiarty to be behind the Case of the Added Unit. 

 

Multiplying the NaN by 1 only works if you know ahead of time that you will have a NaN of dimension one in a mixed-unit (OK, mixed SI+money base dimension) array.  Once it's defined, you're snookered.  Even non-clever programming doesn't help because the NaN has already acquired mixed base dimensionhood.

 

I'm now puzzled as to where the function sin picked up its base dimensionality ... ah, looks like the evaluation of the result, whereas the NaN picks up the unit during assignment.

 

2024 11 05 C.png

 

Interestingly, if you take a nested array whose deepest (base) elements are scalars, multiply it by 0, and evaluate it, then you get the nested array structure with 0 for the numerical values but the original units.  Off the top of my head, I can't think of a neat way of making use of this structure without having to do some recursion anyway.  Plus, extra work will still be involved in dealing with non-scalar base elements.

 

Stuart

Announcements

Top Tags