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

Mathcad Prime fold function

  • November 26, 2021
  • 2 replies
  • 5958 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.

23-Emerald V
December 1, 2021

@LucMeekes wrote:

Found just a few more undocumented Prime stuff.

 

The most interesting is the contradictio in terminis constant undefined, which isn't undefined:

LucMeekes_0-1638309714462.png

Then there are functions ceilMc11, floorMc11, truncMc11, CeilMc11, FloorMc11 and TruncMc11, which appear to do nothing else then ceil, floor, trunc, Ceil Floor and Trunc respectively.

LucMeekes_2-1638310100305.png


I was aware of undefined, but the Mc11 functions are interesting,  Playing around ...

 

2021 12 01 a.png

 

Stuart

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 🤔?