Skip to main content
23-Emerald V
November 26, 2021
Question

Mathcad Prime fold function

  • November 26, 2021
  • 2 replies
  • 5878 views

 

Whilst playing around trying to write a function to remove text rows from a mixed string/numeric matrix (*), I discovered that the undocumented (but very useful) function fold doesn't just work on vectors but also works on matrices, scanning down each column in turn, in the normal Mathcad fashion.

 

This behaviour opens up the possibility of some straightforward implementations of useful functions, such as converting a (potentially arbitrarily nested) array into a vector and summing the elements of an array.

 

Here are some Mathcad Express 7 functions.

 

2021 11 26 a.png

 

2021 11 26 b.png

 

2021 11 26 c.png

 

Or we could define matsum to sum just the numeric elements of an array.

 

2021 11 26 d.png

 

Alternatively, we could shift the burden of removing non-numbers to a specialized version of vec.

 

2021 11 26 e.png

 

We can even count the elements in nested arrays.

 

2021 11 26 f.png

 

The full version of Mathcad Prime allows the user to write programs to create functions.  This facility opens up the possibility of writing a more generic filter function using fold by creating functions containing tailored boolean predicates.

 

Stuart

 

(*) https://community.ptc.com/t5/PTC-Mathcad/READ-FILE-RANGE-OF-ROWS-AND-COLUMNS/m-p/760410#M198424

2 replies

25-Diamond I
November 27, 2021

Interesting! "fold" seems not to be available in real Mathcad, though but I can confirm its available in Prime 6, too..

And as it seems not to be a premium function, this opens a lot of possibilities for Express workarounds.

How did you find out about its existence?

23-Emerald V
November 27, 2021

@Werner_E wrote:

Interesting! "fold" seems not to be available in real Mathcad, though but I can confirm its available in Prime 6, too..


I think that's the version I found fold in.

 

And as it seems not to be a premium function, this opens a lot of possibilities for Express workarounds.


Indeed, it does.  Bear in mind though, that it's only a left fold. 

 


How did you find out about its existence?

By accident.  As a Mathcad Express user, I'd been playing around with list functions in an attempt to emulate bits of Haskell, Scala, F#, C#, Maple, Mathematica, MATLAB, Kotlin, Julia, Python, R, J etc (folds also occur under different names, such as aggregate, accumulate and reduce (#)).

 

I'd got in the habit of copy-pasting a few primitives (such as fold, foldr & foldl) into worksheets and seeing what I could do.  One time I just typed fold, got a result,  and didn't think much about it until I realized I'd forgotten to define fold

 

I haven't found anything else of interest.  Nor have I found an empty array(*) ... such arrays normally go hand in hand with folds in functional programming languages.  OTOH, I've only tried the usual suspects; there may be some other useful functions or objects lurking beneath the worksheet surface.

 

Stuart

 

(#). I used "aggregate" in my multi-dimensional array library - I didn't know about "fold" at the time I wrote the MDA library.

 

(*). Life would have been sooooo much easier, pleasant and worthwhile if Mathcad had that empty array I've been requesting for 20+ years.

25-Diamond I
November 27, 2021

I found a few more undocumented Prime functions. Not sure about their value, though.

update1 and update2 can be used to add values at arbitrary positions in vectors or matrices and the function is not ORIGIN-aware.

mapvector seems to allow us to avoid vectorization, but then, I may not be able to see its potential right now. Just playing around with the functions for a few minutes so far.

 

I found those function by looking for "mc_fold" in all files in the PTC program directory and found it in some dll's (not surprising). I looked at those files with a simple text editor and noticed other mc_*** mentioned which I wasn't aware of.

OK, I also was not aware of the functions "foldover" and "fullfact2", but they are documented functions 😉

But I guess that further digging into the dll's may bring up a few more undocumented functions as well.

Werner_E_0-1638021138584.png

 

24-Ruby III
November 27, 2021

Can you upload your worksheet?

23-Emerald V
November 27, 2021

@VladimirN wrote:

Can you upload your worksheet?


Umm. I knew you'd ask me that. 

 

Unfortunately, I'd only been throwing stuff at random onto the worksheet as something that just occurred to me whilst I was tackling a different problem.   I was too busy doing other stuff to make it presentable, so I'll need to do so ... why is it all my ideas take 2 minutes to put down on paper, then another 2 years to put into a readable form?

 

Stuart

24-Ruby III
November 27, 2021

@StuartBruff wrote:


Umm. I knew you'd ask me that. 

 


😀

 

 


@StuartBruff wrote:

Unfortunately, I'd only been throwing stuff at random onto the worksheet as something that just occurred to me whilst I was tackling a different problem.   I was too busy doing other stuff to make it presentable, so I'll need to do so ... why is it all my ideas take 2 minutes to put down on paper, then another 2 years to put into a readable form?

 

Stuart


 

Have you mastered the new Arduino plate again 🤔?