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

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

Vectorization Function Performance Comparison

StuartBruff
23-Emerald III

Vectorization Function Performance Comparison

Following @DJNewman 's message on the topic of implicit vectorization of range variable being removed from Mathcad Prime 10, I looked at the referenced support article CS416474.   The resolution to the "problem" was to use a range-to-vector conversion program.

 

Note that I use the term "vectorize" in the mathematical sense of "convert a matrix to a vector" rather than the programming sense of carrying out operations in parallel (e.g., Mathcad's vectorize operator).

 

The suggested function Range2Vec is similar to one I usually use (vec) except that instead of stack it uses indexing to add new elements to the constructed vector.   I vaguely recall that stacking is inefficient time-wise compared to indexing, so I thought it would be interesting to see how the two variants compared.

 

Then I thought, "Hey, why stop at two mousetraps? Let's build some more to see if anything hits paydirt".

 

So, I played with a few variants and concluded yet again that stacking is probably slower than indexing.  However, there isn't much objectively in it even for a few million elements, even in nested arrays.  

 

Note that most of the variants tested fully convert nested arrays to vectors, whilst Range2Vec and vec` do not - they're only targeted at vectorizing the first level of an array.

 

Take the results with several pinches of salt.  The times depend upon what else your computer is doing.  In the example below Range2Vec is shown to perform worse than the rest.  This is very unusual.  Range2Vec holds Gold or Silver during most runs, as it should, given it doesn't have to have code to deal with nesting. .  Besides, the worksheet's probably full of bugs, anyway.  🙂

 

Having said that, the times are reasonably consistent in showing "bands" of times.  The slowest two functions are usually vec (uses stack to add elements) and vec3 (uses trim rather than submatrix to remove the dummy empty array values).

 

2024 10 29 F.png

 

Stuart

 

References:

 

https://community.ptc.com/t5/Mathcad/Exclude-some-values/m-p/980366/highlight/true#M214627

 

Article - CS416474 : Inline evaluation to convert range into vector worked in Mathcad Prime 1.0-9.0, but now fails in Mathcad Prime 10.0

 

https://en.wikipedia.org/wiki/Vectorization_(mathematics)

5 REPLIES 5

Something to (possibly, no promises) think about for Mathcad Prime 11: We're strongly pushing R&D to put in a built-in vec() function that should also do this. So you may wish to repeat your testing once that's out. (If it happens.)

I manage the Creo and PTC Mathcad YouTube channels for PTC, as well as all PTC Mathcad marketing in general.
StuartBruff
23-Emerald III
(To:DJNewman)

That sounds great, Dave. I certainly look forward to doing that round of tests again.

 

If it is implemented, please consider my comments about naming the function. The naming style for other functions that create and manipulate arrays tends to be short, lowercase, and meaningful. Thank you.

 

As an aside, will the function be limited in scope or capable of vectorizing any type of array (including arbitrarily-deeply nested ones)?  As you (PTC) can write functions that take optional arguments, perhaps a depth limit could be incorporated - there are occasions when it's only required to go so far down a data structure.  Here's a quick and brutal version of vec that takes a (non-optional) depth argument.

 

vec7 definition:

2024 10 30 B.png

 

vec7 applied to a slightly nested data structure

 

2024 10 30 A.png

 

Oh, as it's a closely related issue, could you please ask the development team to include an IsRange function (that doesn't evaluate a range if evaluated rather than used in a definition!).

 

Stuart

 

 

Werner_E
25-Diamond I
(To:DJNewman)


@DJNewman wrote:

Something to (possibly, no promises) think about for Mathcad Prime 11: We're strongly pushing R&D to put in a built-in vec() function that should also do this. So you may wish to repeat your testing once that's out. (If it happens.)


Please keep on pushing .... 😉

 

DJNewman
17-Peridot
(To:Werner_E)

I also just used my powers of email sending to direct attention to Stuart's technical implementation suggestion above.

I manage the Creo and PTC Mathcad YouTube channels for PTC, as well as all PTC Mathcad marketing in general.
StuartBruff
23-Emerald III
(To:DJNewman)


@DJNewman wrote:

I also just used my powers of email sending to direct attention to Stuart's technical implementation suggestion above.


 

Thanks, Dave.  I'll keep my fingers and other digits crossed that the email falls on fertile ground.

 

Stuart

Announcements

Top Tags