MaplePrimes Announcement

With Maple Flow, we’re regularly rolling out exciting updates. Each offers new features, as well as resolving many user-reported issues.

Flow 2024.2 lives up to that track record. Two major new features - drop-down menus and phasors - make their debut. We've also made many other quality-of-life enhancements.

Drop-down list boxes make your worksheets more tactile and interactive. The menu items can be defined in a matrix or vector, or in a table. You can return the index of the selected item, or return an entire row or column of a matrix.

An important use case is populating the contents of a drop-down menu with data from an external file. In movie below, we

  • import a database of steel shapes and their associated properties
  • populate the drop-down menu with the steel shapes in the first column of the imported data
  • based upon the selected shape, use ArrayTools:-Lookup to return a property
  • and finally perform a design analysis.

If you look carefully, you'll see that you can hide commands on a per-container basis to make your worksheets cleaner.

We have a large number of power system engineers who want to model electrical systems in phasor notation.

Entering a phasor is easy - just enter the magnitude, the angle character and then the angle. Phasors evaluate to rectangular complex numbers but can be recast to phasor format with the Context Panel.

You can also associate a unit with the magnitude and angle. On output, you can change units inline or via the Context Panel.

You can also prevent floating point approximation of phasors using the symbolic toggle on each container.

We've also made a raft of other improvements. Extracting slices of matrices is faster, and you can now enter units in 2d notation in the Context Panel (particularly useful when you want to change the units of everything inside a matrix).

As ever, the new features are driven by you. The only way you can point us the right direction is by telling us what you want. Don't be shy!

Featured Post

Every December since 2015, software engineer and puzzle enthusiast Eric Wastl has created an Advent Calender of code challenges called Advent of Code. These puzzles can be solved in any programming language, since you are only required to submit a string or integer solution, and tend to increase in difficulty over the course of the 25 days. Each puzzle has two parts each using the same input (which is different for each user), the second part being revealed only after the first part is solved.

I started participating in this puzzle challenge in 2021 and I like to solve them using Maple right when they are unlocked at 12am EST each day. I post my my solutions to my github as Maple worksheets, and (usually) later as cleaned up Maple text files: https://github.com/johnpmay/AdventOfCode2024

I typically work in a Maple worksheet since I find the worksheet interface ideal to quickly play with an evolving piece of code to solve a puzzle (it is kind of a race if you want it to be, Advent of Code tells you your place every day and gives you "points" if you are one of the first 100). Also I find the nature of the Maple language and its variety of libraries pretty ideal for approaching these problems.

Today, I am writing about this because 2025 Day 5 was a cool puzzle that really asked to be analyzed further in Maple. (Note, if you are participating in Advent of Code and haven't solved Day 5, there are heavy spoilers ahead).

Once you decypher the silly story about the elves, the problem comes down to a set of pairwise ordering rules, and a collection of lists (called updates) that need to be checked if they are sorted according to the rule. This seems straightforward, but as always you have a (big!) input file to handle.  In this case the input looks like this (the actual input not included here, but it's much much longer)

75|13
53|13
[...elided...]


75,47,61,53,29
97,61,53,29,13
[...elided...]

For this step, StringTools is your best friend (I typically read in the whole input as a string using FileTools:-Text:-ReadFile).  I first use StringSplit on the double line break "\n\n" to seperate the rules from the updates.  Then I map an ordinary Split over those to make everything into nested lists of integer.

(rules, updates) := StringSplit(Trim(input), "\n\n")[]:
rules := map(s->map(s2i,Split(s,"|")), Split(rules,"\n")): 

        rules := [[75, 13], [53, 13], ...]

updates := map(s->(map(s2i,Split(s,","))), Split(updates, "\n")):

        updates := [[75, 47, 61, 53, 29], [97, 61, 53, 29, 13], ...]

(the function s2i is a "string to integer" helper function since it's considered bad practice to just call parse on input handed to you by a stranger on the internet.  In older version of maple I used: s2i := s->sscanf(s,"%d")[1] in newever versions you can use the way less cryptic s2i := s->convert(s,'integer') )

Okay, now that we have two nice lists, it's easy to check which of the updates is sorted according to the rules with a simple nested loop.

goodups := DEQueue():

for u in updates do
    for r in rules do
        if member(r[1],u,'i') and member(r[2],u,'j') then
           if j < i then next 2; end if;
        end if;
     end do;
     push_back(goodups, u);
end do:

For each update, check that if a rule applies to it then that rule is obeyed. If the rule r is violated, advance the outer loop with the relatively new and useful next 2; syntax (it also works with break #) and if not check the next rule. If no rules were violated then push u into our stack of Good Updates.  This is a perfectly fine solution to part 1 of the puzzle, but if you thing about it a little why not just use sort with a custom comparision function built from the rules?

V := ListTools:-MakeUnique(map2(op,1,rules)): # all the numbers that can appear
N := [seq(map2(op,2,select(n->m=n[1], rules)), m in V)]: # all their neighbors in the graph induced by rules
T := table([seq(V[i]={N[i][]}, i=1..nops(V))]): # table of neighbors
comp := (x,y)->evalb(y in T[x]): # "less than" according the rules

Now with that comp function we can just do

goodups := select(u->sort(u, comp)=u, updates);

to find all the correctly sorted updates, this will seem like a better idea when you get the [SPOILER] part 2 of the problem which is to sort all the incorrectly sorted updates.

NOW WAIT A MINUTE.  If you look at the fine help page for ?sort you'll see it very clearly says about the comp function F (emphasis added):

custom boolean procedure

 When F does not match one of the symbols listed above, it must be a Boolean-
 valued function of two arguments.  Specifically, F(a,b) returns false if and  
 only if b must precede a in the sorted output.  That is F(a,b) is a non-
 strict less than comparison function.  In addition, F(a,b) must be defined  
 for all pairs a for a and b in the input structure and F(a,b) must be  
 transitive
, that is, if F(a,b) = true and F(b,c) = true then F(a,c) = true.

Dear reader, I guess we should check if our rules from the puzzle create a relation that is transitive.  The good news is that the sample input given in the problem description is indeed transitive. The easiest way I could think to check that was to use GraphTheory and see if there were any cycles in the directed graph given by the rules. So, let's make a graph with an edge for every comparison rule a<b, b<c, etc.  If there is a cycle in the graph, that will be a case where a < b < c < a in our rules and therefore non-transitivity in the comparison.  When we do that, we see it's cycle-free.

with(GraphTheory):
G := Graph({rules[]});
FindCycle(G); # returns []

That graph is really nice, it looks like this:

If you count the in-degree of each vertex, you can see it actually induced a total order on the numbers going linearly from smallest to largest numbers.

So, is that also true of the actual input you are given? Of course not. When you create the graph of your actual puzzle input you find there is a 3-cycle.

with(GraphTheory):
G := Graph({rules[]});
FindCycle(G); # returns a 3-cycle
seq(FindCycle(G,v),v in Vertices(V)); # returns a 3-cycle for every v!

In fact, there is a 3-cycle from every single vertex.  But at least the graph is pleasing to contemplate. Let it calm you:

So, does this mean sort with out comp function doesn't work? Is all lost? In fact, I solved both parts of the puzzle using a custom comparision for sort before stopping to consider that maybe the comparison wasn't transitive. And... it just works! So, WHY does it work? (the comparison is very very non-transitive). The answer is of course that the lists you need to sort don't contain all of the numbers covered by the rules.  In fact, your clever/cruel puzzlemaster has in fact constructed each of those (200 or so) lists so that it misses every one of the 3-cycles in the above graph. You can carefully check that by restricting the relation graph to the subset of numbers in each list and check for cycles:

G := Graph({rules[]});
H := InducedSubgraph(G, updates[i]); # try for i=1..nops(updates)
FindCycles(H); # returns [] for every element of update

In fact, each of those induced subgraphs looks like this

and you can maybe observe that this induced a total order on the numbers in the problem. Or if you are not careful, and you didn't bother to check, you would get the right answer anyway. Because it's only Day 5, and Eric Wastl hasn't decided to make thing really challenging yet.

I hope you've enjoyed my deep dive into this problem. And I hope it's inspired you to go try out Advent of Code. It's a fun challenge. Let me know in a comment if you're participating! Maybe we can start a leaderboard for MaplePrimes folks working on the puzzles.

Featured Post

I was working in my living room.  My computer was upstairs, but I had my phone and tablet.  I'm working on The Book ("Perturbation methods using backward error", with Nic Fillion, which will be published by SIAM next year some time).

I've discovered something quite cool, historically, about the WKB method and George Green's original invention of the idea (that bears other people's names, or, well, initials, anyway).  (As usual.)  Green had written down a PDE modelling waves in a long narrow canal of slowly varying breadth 2*beta(x) and slowly varying depth 2*gamma(x).  Turns out his "approximate" solution is actually an exact solution to an equation of a very similar kind, with an extra term E(x)*phi(x,t).  The extra term depends in a funny way on beta(x) and gamma(x), and only on those.  So a natural kind of question is, "is there a canal shape for which Green's solution is the exact solution with E(x)==0?"  Can we find beta(x) and gamma(x) for which this works?

Yes.  Lots of cases.  In particular, if the breadth beta(x) is constant, you can write down a differential equation for gamma(x).  I wrote it in my notebook using y and not gamma.  I wrote it pretty neatly.  Then I fired up the Maple Calculator on my little tablet, opened the camera, and pow!  Solved.

I wrote the solution down underneath the equation.  It checks out, too.  See the attached image.

Now, after the fact, I figured out how to solve it myself (using Ricatti's trick: put y' = v, then y'' = v*dv/dy, and the resulting first order equation is separable).  But that whole "take a picture of the equation and write down the solution" thing is pretty impressive.

 

So: kudos to the designers and implementers of the Maple Calculator.  Three cheers!

 



Extract data from graphs

Maple asked by imparter 175 Yesterday

questions about mapole

Maple 2024 asked by StudentOne... 0 December 10