LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Array manipulation

Solved!
Go to solution

I have a main 1D array Ambient Temperatures [1 2 3 4 5 6 7 8... i] and another 1D array A_corrections (5 elements) [a b c d e].
The equation is this: Result = a * i + b * i^2 + c * i^3 + d * i^4 + e * i^5. where i is an element of the array.

How do I configure this in Labview? 

I tried setting up a for loop, with N = 5. The power function takes i as an index, and an element from the array. Then I am stuck..

 

Programming_beginner_0-1690430149457.png

Thank you.

0 Kudos
Message 1 of 9
(1,368 Views)

My pseudocode/logic is:

Within the loop, the array element i is powered by the index 1 and multiplied with the first element of the 1D array (a). The output is x.

Then, the index is incremented by 1 and the array element i is powered by this index and multiplied with the second element (b) This output is added to x.
This process repeats until it reaches the last element of the 1D array(e). The final output is the array.

0 Kudos
Message 2 of 9
(1,352 Views)

I am at this:

Programming_beginner_0-1690436639185.png

now for each inner loop, I need to add to the previous loop. Not sure how to proceed.

 

0 Kudos
Message 3 of 9
(1,332 Views)

Hi beginner,

 


@Programming_beginner wrote:

I have a main 1D array Ambient Temperatures [1 2 3 4 5 6 7 8... i] and another 1D array A_corrections (5 elements) [a b c d e].
The equation is this: Result = a * i + b * i^2 + c * i^3 + d * i^4 + e * i^5. where i is an element of the array.


So you want to calculate a polynomial evaluation for each input array element?

Then you should do just that:

That function is polymorph and can handle scalar and 1D inputs…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 9
(1,318 Views)
Solution
Accepted by topic author Programming_beginner

Programming_beginner_1-1690440025792.png

Got it. The value is correct. How do I repeat this for each value of an array? [1,2,3 ...... , ith element]

 

^The polynomial trick works but I manage to code this.

Edit:

Programming_beginner_0-1690441071149.png

 

 

0 Kudos
Message 5 of 9
(1,305 Views)
Solution
Accepted by topic author Programming_beginner

Hi beginner,

 


@Programming_beginner wrote:

^The polynomial trick works but I manage to code this.

Edit:

Programming_beginner_0-1690441071149.png


  • Why don't you use autoindexing for the inner loop? No need to wire the "5" to N terminal!
  • When using autoindexing your VI would easily adapt to other polynomial orders…
  • Why is the "0" set to wrong datatype? Why do you introduce a coercion dot?
  • Why don't you hold the powers of the input value in the shift register instead of calling the (rather expensive) x^y function?
  • Why don't you sum the polynomial terms after the loop? (I guess you don't care about floating point precision for your 5th order polynomial…)

Like this:

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 9
(1,281 Views)

Programming_beginner_0-1690509816224.png

This is my final solution.

 

The shift register is to hold the previous value for summation (like a * i), then for next loop, this is summed with ( b * i ^2), which is then summed with ( c * i ^3) ....

 

It's pretty precise to 4d.p. I follow strictly with regards to the powers. (instead of just multiply again, though I will try to use your method for the future, GerdW)

 

0 Kudos
Message 7 of 9
(1,223 Views)

@Programming_beginner wrote:

Programming_beginner_0-1690509816224.png

This is my final solution.


No, just no!!! (The term "final" should not exist here!)

 

First of all the A array belongs before the outer loop because it is not supposed to change during the run (make the tunnel non-indexing!). If it is before the loop, the compiler can assume that is does not change during the execution and can apply further optimization!

 

Gerd already suggested the polynomial evaluation, but forgot to mention that you need to include the 0th term (i.e. i^0), which is zero here because your expansion does not have an offset. (maybe it should!)

 

So that's the first choice! (top in image)

If you want to do it explicit, one loop is sufficient! (middle, yes it can be more optimized))

Compare that with the cleaned up solution of your version (bottom).

 

Of course all are much more precise than just four decimal digits and all give exactly the same result (within the limits of DBL math)!

 

altenbach_0-1690517711529.png

 

 

0 Kudos
Message 8 of 9
(1,212 Views)

A few more words about performance:

 

If [# of temps] is M and [# of factors] is N

 

  • The middle code iterates N times doing array operations
  • Your outer loop iterates M times, so the inner loop is called NxM times, doing all scalar operations

Array operations have a significantly higher chance to take advantage of SIMD instructions, and will thus perform better. Of course polynomial evaluation is the most optimized for large inputs.

 

(These results are for a very old laptop. Result might be slightly different on other hardware. YMMV)

 

Some real world benchmarks (5M temps):

 

  • My single loop version is only about 5% slower than the polynomial evaluation version.
  • The double loop (your draft!) is about 20x slower than the polynomial evaluation version! (It can be sped up by parallelizing the outer loop but it still won't come close!)

Curiously, for smaller inputs (e.g. 500 temps):

 

  • My single loop version is twice as fast and the polynomial evaluation!
  • Your is still 10+x slower

For even smaller inputs (e.g. 10temps)

 

  • My single loop version is 5+x faster than the polynomial evaluation!!!
  • Your is still more than twice slower (or 10x slower than mine!)
0 Kudos
Message 9 of 9
(1,186 Views)