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

Mathcad Prime fold function

StuartBruff
23-Emerald II

Mathcad Prime fold function

 

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

13 REPLIES 13

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?

StuartBruff
23-Emerald II
(To:Werner_E)


@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.

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

 

StuartBruff
23-Emerald II
(To:Werner_E)


@Werner_E wrote:

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

 


Nice work, Werner! 

 

update potentially solve a problem I commonly encounter.   

 

One of the problems with passing arrays around in Express is that there's no simple way to set an array element's value without going through some hoops.  Yes, it can be done, but it can be awkward (I have a function, but ... ).  update looks like it might be a neater, and more efficient, solution to the problem.

 

mapvector could be useful for those odd occasions when I want to pass an operator as an argument to another function.

 

Stuart

 

 

LucMeekes
23-Emerald III
(To:StuartBruff)

All these undocumented functions work as from Prime 2, Express. Can't check Prime 1, but expect it will work there as well.

LucMeekes_0-1638098665410.png

Update1 and 2 will (also) help in creating vectors and matrices with constant data:

update1(0,5,0) creates a 6 element vector of zero's, what otherwise matrix(6,1,max)*0 would do.

 

Prime 2 file attached.

Success!
Luc

LucMeekes
23-Emerald III
(To:LucMeekes)

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 is the function sleep(t) that wastes computer time by calculating for t seconds and then returning the value of t.

LucMeekes_1-1638309751195.png

Very useful if Prime is too fast.

 

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

 

Finally there are getmaxthreads() and setnumthreads(), which give (so far) unpredictable results:

LucMeekes_3-1638310650782.png

Success!
Luc

 

StuartBruff
23-Emerald II
(To:LucMeekes)


@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

StuartBruff
23-Emerald II
(To:Werner_E)


@Werner_E wrote:

mapvector seems to allow us to avoid vectorization, but then, I may not be able to see its potential right now.

 


Naturally, having scratched my head in vain doing likewise, I thought of several cases where I'd needed to avoid the blunt tool of vectorization (sometimes a sledgehammer is what you need).   mapvector is useful when creating an array argument in situ.

 

2021 11 28 a.png2021 11 28 b.png

 

Most of the time, I need a polyadic map, though. 

 

2021 11 28 c.png

 

where

 

2021 11 28 d.png

 

Stuart

StuartBruff
23-Emerald II
(To:Werner_E)


@Werner_E wrote:

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.


And here's a demo for update1.  I sometimes need to use a list of indices to either get or set values from a vector.   This is, of course, a good cat-skinning exercise, but this version demonstrates the advantage of being able to directly update and return an array.

 

2021 11 28 e.png

 

Stuart

Can you upload your worksheet?

StuartBruff
23-Emerald II
(To:VladimirN)


@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


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

StuartBruff
23-Emerald II
(To:VladimirN)


@VladimirN wrote:

@StuartBruff wrote:


Umm. I knew you'd ask me that. 

 


😀


I thought that might amuse you.  🙂

 

Anyway, please find attached a quickly edited worksheet.  It's probably full of errors, so caveat emptor.   

 

Could you also please tell me why I'm doing work on a weekend evening when I'm not even getting paid for it, let alone at time and a half?

 

A brief summary of some of the highlights:

 

2021 11 27 d.png

 

2021 11 27 e.png

 


Have you mastered the new Arduino plate again 🤔?

Нет!!  It appears as if there was poor contact inside one of the header sockets.  I suspect that this might have induced a hardware failure on either the (SAM-based) Feather RP2040 or the RGB Matrix.  Just to show I haven't lost my magical effect on physical equipment, the Feather's no longer working as a CircuitPython device.  I guess I'm going to have to try the Arduino IDE and C++, or take the road to eternal damnation and try using the debug facility.  ☹️

 

Stuart 

Top Tags