From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW Idea Exchange

cancel
Showing results for 
Search instead for 
Did you mean: 
0 Kudos
Bob_Schor

Fix (logical) inconsistency in Square function

Status: Declined

Moved to CAR Database: CAR 629759

Quite by accident, I stumbled upon an inconsistency in LabVIEW's "Square" function.  For almost all operations, you get the same result if you wire a numeric or "structured" numeric (like an Array or Cluster) to either Square or to both inputs of Multiply.  Furthermore, you get the same (or a close approximation to the same) result if you compare the input to Square to the output from Square -> Square Root.  Here's an example for a 10x10 Array of 1's -- if you run this, both Booleans will return True.  Note that I'm testing for "Equality of Dbls" by subtracting them, taking their absolute value, and saying they're equal if their difference is <= 0.000000001.

Square Works for Array.png

 

However, if you transform the Square Array to a Square Matrix, Multiply still does what (mathematically) Multiply should do for a Matrix, namely do a Matrix multiplication (giving a 10x10 Matrix of 10), but Square, instead, simply squares each Matrix entry, giving a 10x10 Matrix of 1.  Curiously, the Square Root function does do a proper Matrix Square Root, i.e. it produces the Matrix that, when multiplied by itself, gives the argument to the Square Root (shown by "Square Root is Inverse of Multiply").

Square Fails for Matrices.png

So here we have a double-inconsistency.  We have a Square function that does not always produce the same quantity as "Multiply something by itself", and a Square function that is not the inverse of the Square Root function, meaning you don't get back the same thing if you Square (a Matrix) then take its Square Root, or vice versa.

 

There is no "warning" (that I've seen) that Square has this anomalous behavior.  It was pointed out to me (by an NI AE) that there is a listing of functions that "work" for Matrices, listing Square Root, but not Square.

 

At the risk of potentially causing code written with the "old" mathematically-wrong Square function to fail if migrated to a function that (mathematically) works properly for Matrices (i.e. is the inverse of Square Root and gives the same thing as "multiply by yourself"), I recommend that NI:

  • Fix the Square function for Matrices so that it multiplies the matrix by itself, and
  • Prominently note that the functionality of Square has been "corrected in this Version of LabVIEW", including on the Help for the Square function.

Bob Schor

 

P.S. -- I searched the Idea Exchange for "Matrix" and found no other mention of this "bug".  It occurs to me that while NI might be reluctant to admit this is a clear bug and to fix it, potentially causing "old code moved to a new Version" to break, I strongly suspect that anyone actually using matrices in LabVIEW would already "know" that Square is broken and therefore not use it for fear of having code that "doesn't do what it seems to say it is doing", a further argument that this should really be fixed!

4 Comments
AristosQueue (NI)
NI Employee (retired)

This is now filed as CAR 629759.

Darren
Proven Zealot
Status changed to: Declined

Moved to CAR Database: CAR 629759

X.
Trusted Enthusiast
Trusted Enthusiast

Actually, if you look at what the Square operation does on a matrix, it could be an interesting function to have: it does square each element of the matrix. In fact, I'd argue that it would be helpful to be able to exponentiate all elements of a matrix.

However, "unfortunately", the xˆy function does in fact behave as a matrix multiple product. In particular y = 2 give the same result as x*x.

 

Bob_Schor
Knight of NI

Thank you for making my point even stronger.  I had not checked the behavior of x^y when x is a matrix and y = 2, which I would predict (mathematically) should give x*x and Square(x) (where "Square" is the function x^2).

 

If you want to square the Matrix elements (or, for that matter, raise them each to the nth power with x^y), you can write that (non-standard) function in LabVIEW by doing Matrix-to-Array, x^y, Array to Matrix.  But if you want to do simple mathematics, where x*x = x^2, then the Square function has to be fixed.

 

Bob "Pedantic" Schor