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

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

23-Emerald II

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.

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

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

We can even count the elements in nested arrays.

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

13 REPLIES 13
24-Ruby V
(To:StuartBruff)

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

24-Ruby V
(To:StuartBruff)

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.

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.

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

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.

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

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:

Then there is the function sleep(t) that wastes computer time by calculating for t seconds and then returning the value of t.

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.

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

Success!
Luc

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:

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.

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

Stuart

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.

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

where

Stuart

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.

Stuart

24-Ruby II
(To:StuartBruff)

23-Emerald II

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 II
(To:StuartBruff)

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

23-Emerald II

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

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

Announcements
Top Tags