LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Working with Vectors -- What's the best way?

My algorithm involves the following vector mathematics:

(1) Define two vectors.
(2) Cross product the two vectors to create a normal vector.
(3) Add three vectors.

It will, in the midst of the above steps, involve declarations of vectors like:

A=rcos(pi/2)i + rsin(pi/2)j where r is some scalar that I define.

It seems like using native LabView functions will be a burden to do this a bunch of times. In Mathscript, such a thing is easy!

The data that MathScript will use will be continuously changing, and coming from another algorithm that I programmed.

I'm sure MathScript is capable of doing this, but I'm not sure if it's the most efficient. I read that MathScript is quite slow in some conditions, and I'm not sure if this is one of them. Is there another way of doing such operations in LabView that will be quicker?

Thank you,
Alex

0 Kudos
Message 1 of 9
(7,990 Views)
Hi Alex,

a vector is just an array of 3 elements. You can easily define and add arays (InitArray, Add). In the math palette->linear algebra you find find a function for calculating the cross product...
It's all predefined and available... What more do you need?
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 9
(7,982 Views)
I suppose my biggest worry is the fact that I need to use trigonometric functions and variables within the parameters of the array/vector. In MathScript code, this seems easier.

Am I just deluded?

ps. I apologize, but I double posted: link


Message Edited by ap8888 on 07-10-2008 12:35 PM
0 Kudos
Message 3 of 9
(7,979 Views)
I don't have enough information to help w/ all of the parts you mention but the cross product and vector adds are a good start.

First, you should know that the cross product function is only provided with MathScript, not as a VI in the Linear Algebra palette. However, it's very easy to code up in G. I've attached a sample here that models the function using two VIs - one of which handles the computation of one vector component. Note that I did nothing to deal w/ badly scaled cases (i.e. when the two vectors are extremely close together or very close to the zero vector).

The top-level VI, CrossProduct.vi, represents the function and offers both G and MathScript versions of the cross product function. Use the switch on the front panel to select the algorithm you want to use. This is not really a fair comparison between G and MathScript because one is code and the other is built-in. I would reserve judgment until you have a chance to investigate a larger algorithm.

Second, I attached an add operation that adds two vectors with their cross product. This shows the polymorphism in LabVIEW's G - how a function can operate on both scalar and array data. The compound arithmetic node doing the add is performing scalar addition on each element of the three vectors. This behavior is also true for unary functions like sin() where inputting a 1D array of numerics performs sin() on each element of the array. I think this is the feature you were hoping to have to efficiently implement your vector operations.

If you are not familiar with G, even simple tasks will seem hard at first. However, it has many nice features worth exploiting. As for MathScript, your work can be done w/ that language also. Using vectorized operations (such as element-wise multiply: x .* y) rather than indexing each element in a for loop will avoid many of the performance penalities. In general, this is true for most high-level scripting languages - not just MathScript. Also, you should know that MathScript compiles to G code. So, chances are high that a G implementation will outperform a MathScript one especially when significant indexing is performed.


Message Edited by MathGuy on 07-10-2008 05:59 PM
Download All
Message 4 of 9
(7,943 Views)
Please ignore the attachments from the prior post - they were renamed during upload and so they will be broken when you try to load them. Support is looking into correcting the problem. In the meantime, I have attached those VIs in an LLB here.


0 Kudos
Message 5 of 9
(7,935 Views)
Hi MathGuy.

Thank you for your help.

I have one more question:

  • What runs faster, a VI (like the one you attached) that does the cross product with LV native functions, or a MathScript node with a "cross(a,b)" command? I know you mentioned this, but I'm still a tad confused. My cross-product function (whether LV native or MathScript) will receive continous data.



0 Kudos
Message 6 of 9
(7,910 Views)
Hi ap,

why not make a small test? For me G is >20 times faster...
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 7 of 9
(7,904 Views)
And if you want to go about 4x faster than the version by Mathguy, here's a quick alternative. I am sure it could be improved further. 😄
 
(The code alone gives about 2-3x and setting subroutine priority/no debugging gains another small factor).
 
 
The reason is tigher code with fewer subVI calls. According to my casual benchmarks, it is about 150-200x faster than the mathscript version.
 


Message Edited by altenbach on 07-11-2008 02:03 PM
Download All
Message 8 of 9
(7,878 Views)

Wow Altenbach.

If I ever need to write a complex math script, I will post it on this forum 😉

R

0 Kudos
Message 9 of 9
(7,853 Views)