Writing Derivatives at a Point Using Leibniz Notat...

Maple

Maple users often want to write a derivative evaluated at a point using Leibniz notation, as a matter of presentation, with appropriate variables and coordinates. For instance:

Now, Maple uses the D operator for evaluating derivatives at a point, but this can be a little clunky:

```p := D[1,2,2,3](f)(a,b,c);

q := convert( p, Diff );```

```u := D[1,2,2,3](f)(5,10,15);

v := convert( u, Diff );```

How can we tell Maple, programmatically, to print this in a nicer way? We amended the print command (see below) to do this. For example:

```print( D[1,2,2,3](f)(a,b,c), [x,y,z] );

print( D[1,2,2,3](f)(5,10,15), [x,y,z] );```

`print( 'D(sin)(Pi/6)', theta );`

Here's the definition of the custom version of print:

```# Type to check if an expression is a derivative using 'D', e.g. D(f)(a) and D[1,2](f)(a,b).

TypeTools:-AddType(

'Dexpr',

proc( f )

if op( [0,0], f ) <> D and op( [0,0,0], f ) <> D then

return false;

end if;

if not type( op( [0,1], f ), 'name' ) or not type( { op( f ) }, 'set(algebraic)' ) then

return false;

end if;

if op( [0,0,0], f ) = D and not type( { op( [0,0,..], f ) }, 'set(posint)' ) then

return false;

end if;

return true;

end proc

):

# Create a local version of 'print', which will print expressions like D[1,2](f)(a,b) in a custom way,

# but otherwise print in the usual fashion.

local print := proc()

local A, B, f, g, L, X, Y, Z;

# Check that a valid expression involving 'D' is passed, along with a variable name or list of variable names.

if ( _npassed < 2 ) or ( not _passed[1] :: 'Dexpr' ) or ( not passed[2] :: 'Or'('name','list'('name')) ) then

return :-print( _passed );

end if;

# Extract important variables from the input.

g := _passed[1]; # expression

X := _passed[2]; # variable name(s)

f := op( [0,1], g ); # function name in expression

A := op( g ); # point(s) of evaluation

# Check that the number of variables is the same as the number of evaluation points.

if nops( X ) <> nops( [A] ) then

return :-print( _passed );

end if;

# The differential operator.

L := op( [0,0], g );

# Find the variable (univariate) or indices (multivariate) for the derivative(s).

B := `if`( L = D, X, [ op( L ) ] );

# Variable name(s) as expression sequence.

Y := op( X );

# Check that the point(s) of evaluation is/are distinct from the variable name(s).

if numelems( {Y} intersect {A} ) > 0 then

return :-print( _passed );

end if;

# Find the expression sequence of the variable names.

Z := `if`( L = D, X, X[B] );

return print( Eval( Diff( f(Y), Z ), (Y) = (A) ) );

end proc:

```

Do you use Leibniz Notation often? Or do you have an alternate method? We’d love to hear from you!

Announcing Maple 2019

by: Maple 2019

Last year, I read a fascinating paper that presented evidence of an exoplanet, inferred through the “wobble” (or radial velocity) of the star it orbits, HD 3651. A periodogram of the radial velocity revealed the orbital period of the exoplanet – about 62.2 days.

I found the experimental data and attempted to reproduce the periodogram. However, the data was irregularly sampled, as is most astronomical data. This meant I couldn’t use the standard Fourier-based tools from the signal processing package.

I started hunting for the techniques used in the spectral analysis of irregularly sampled data, and found that the Lomb Scargle approach was often used for astronomical data. I threw together some simple prototype code and successfully reproduced the periodogram in the paper.

After some (not so) gentle prodding, Erik Postma’s team wrote their own, far faster and far more robust, implementation.

This new functionality makes its debut in Maple 2019 (and the final worksheet is here.)

From a simple germ of an idea, to a finished, robust, fully documented product that we can put in front of our users – that, for me, is incredibly satisfying.

That’s a minor story about a niche I’m interested in, but these stories are repeated time and time again.  Ideas spring from users and from those that work at Maplesoft. They’re filtered to a manageable set that we can work on. Some projects reach completion in under a year, while other, more ambitious, projects take longer.

The result is software developed by passionate people invested in their work, and used by passionate people in universities, industry and at home.

We always pack a lot into each release. Maple 2019 contains improvements for the most commonly used Maple functions that nearly everyone uses – such as solve, simplify and int – as well features that target specific groups (such as those that share my interest in signal processing!)

I’d like to to highlight a few new of the new features that I find particularly impressive, or have just caught my eye because they’re cool.

Of course, this is only a small selection of the shiny new stuff – everything is described in detail on the Maplesoft website.

Edgardo, research fellow at Maplesoft, recently sent me a recent independent comparison of Maple’s PDE solver versus those in Mathematica (in case you’re not aware, he’s the senior developer for that function). He was excited – this test suite demonstrated that Maple was far ahead of its closest competitor, both in the number of PDEs solved, and the time taken to return those solutions.

He’s spent another release cycle working on pdsolve – it’s now more powerful than before. Here’s a PDE that Maple now successfully solves.

Maplesoft tracks visits to our online help pages - simplify is well-inside the top-ten most visited pages. It’s one of those core functions that nearly everyone uses.

For this release, R&D has made many improvements to simplify. For example, Maple 2019 better simplifies expressions that contain powers, exponentials and trig functions.

Everyone who touches Maple uses the same programming language. You could be an engineer that’s batch processing some data, or a mathematical researcher prototyping a new algorithm – everyone codes in the same language.

Maple now supports C-style increment, decrement, and assignment operators, giving you more concise code.

We’ve made a number of improvements to the interface, including a redesigned start page. My favorite is the display of large data structures (or rtables).

You now see the header (that is, the top-left) of the data structure.

For an audio file, you see useful information about its contents.

I enjoy creating new and different types of visualizations using Maple's sandbox of flexible plots and plotting primitives.

Here’s a new feature that I’ll use regularly: given a name (and optionally a modifier), polygonbyname draws a variety of shapes.

In other breaking news, I now know what a Reuleaux hexagon looks like.

Since I can’t resist talking about another signal processing feature, FindPeakPoints locates the local peaks or valleys of a 1D data set. Several options let you filter out spurious peaks or valleys

I’ve used this new function to find the fundamental frequencies and harmonics of a violin note from its periodogram.

Speaking of passionate developers who are devoted to their work, Edgardo has written a new e-book that teaches you how to use tensor computations using Physics. You get this e-book when you install Maple 2019.

The new LeastTrimmedSquares command fits data to an equation while not being signficantly influenced by outliers.

In this example, we:

• Artifically generate a noisy data set with a few outliers, but with the underlying trend Y =5 X + 50
• Fit straight lines using CurveFitting:-LeastSquares and Statistics:-LeastTrimmedSquares

LeastTrimmedSquares function correctly predicts the underlying trend.

We try to make every release faster and more efficient. We sometimes target key changes in the core infrastructure that benefit all users (such as the parallel garbage collector in Maple 17). Other times, we focus on specific functions.

For this release, I’m particularly impressed by this improved benchmark for factor, in which we’re factoring a sparse multivariate polynomial.

On my laptop, Maple 2018 takes 4.2 seconds to compute and consumes 0.92 GiB of memory.

Maple 2019 takes a mere 0.27 seconds, and only needs 45 MiB of memory!

I’m a visualization nut, and I always get a vicarious thrill when I see a shiny new plot, or a well-presented application.

I was immediately drawn to this new Maple 2019 app – it illustrates the transition between day and night on a world map. You can even change the projection used to generate the map. Shiny!

So that’s my pick of the top new features in Maple 2019. Everyone here at Maplesoft would love to hear your comments!

The Maple Conference is Back!

by: Maple Maple Toolboxes

It is my pleasure to announce the return of the Maple Conference! On October 15-17th, in Waterloo, Ontario, Canada, we will gather a group of Maple enthusiasts, product experts, and customers, to explore and celebrate the different aspects of Maple.

Specifically, this conference will be dedicated to exploring Maple’s impact on education, new symbolic computation algorithms and techniques, and the wide range of Maple applications. Attendees will have the opportunity to learn about the latest research, share experiences, and interact with Maple developers.

In preparation for the conference we are welcoming paper and extended abstract submissions. We are looking for presentations which fall into the broad categories of “Maple in Education”, “Algorithms and Software”, and “Applications of Maple” (a more extensive list of topics can be found here).

You can learn more about the event, plus find our call-for-papers and abstracts, here: https://www.maplesoft.com/mapleconference/

How MapleSim Helped Canada’s Paralympic Curling...

by: Maple MapleSim

Recently, my research team at the University of Waterloo was approached by Mark Ideson, the skip for the Canadian Paralympic men’s curling team, about developing a curling end-effector, a device to give wheelchair curlers greater control over their shots. A gold medalist and multi-medal winner at the Paralympics, Mark has a passion to see wheelchair curling performance improve and entrusted us to assist him in this objective. We previously worked with Mark and his team on a research project to model the wheelchair curling shot and help optimize their performance on the ice. The end-effector project was the next step in our partnership.

The use of technology in the sports world is increasing rapidly, allowing us to better understand athletic performance. We are able to gather new types of data that, when coupled with advanced engineering tools, allow us to perform more in-depth analysis of the human body as it pertains to specific movements and tasks. As a result, we can refine motions and improve equipment to help athletes maximize their abilities and performance. As a professor of Systems Design Engineering at the University of Waterloo, I have overseen several studies on the motor function of Paralympic athletes. My team focuses on modelling the interactions between athletes and their equipment to maximize athletic performance, and we rely heavily on Maple and MapleSim in our research and project development.

The end-effector project was led by my UW students Borna Ghannadi and Conor Jansen. The objective was to design a device that attaches to the end of the curler’s stick and provides greater command over the stone by pulling it back prior to release.  Our team modeled the end effector in Maple and built an initial prototype, which has undergone several trials and adjustments since then. The device is now on its 7th iteration, which we felt appropriate to name the Mark 7, in recognition of Mark’s inspiration for the project. The device has been a challenge, but we have steadily made improvements with Mark’s input and it is close to being a finished product.

Currently, wheelchair curlers use a device that keeps the stone static before it’s thrown. Having the ability to pull back on the stone and break the friction prior to release will provide great benefit to the curlers. As a curler, if you can only push forward and the ice conditions aren’t perfect, you’re throwing at a different speed every time. If you can pull the stone back and then go forward, you’ve broken that friction and your shot is far more repeatable. This should make the game much more interesting.

For our team, the objective was to design a mechanism that not only allowed curlers to pull back on the stone, but also had a release option with no triggers on the curler’s hand. The device we developed screws on to the end of the curler’s stick, and is designed to rest firmly on the curling handle. Once the curler selects their shot, they can position the stone accordingly, slide the stone backward and then forward, and watch the device gently separate from the stone.

For our research, the increased speed and accuracy of MapleSim’s multibody dynamic simulations, made possible by the underlying symbolic modelling engine, Maple, allowed us to spend more time on system design and optimization. MapleSim combines principles of mechanics with linear graph theory to produce unified representations of the system topology and modelling coordinates. The system equations are automatically generated symbolically, which enables us to view and share the equations prior to a numerical solution of the highly-optimized simulation code.

The Mark 7 is an invention that could have significant ramifications in the curling world. Shooting accuracy across wheelchair curling is currently around 60-62%, and if new technology like the Mark 7 is adopted, that number could grow to 70 or 75%. Improved accuracy will make the game more enjoyable and competitive. Having the ability to pull back on the stone prior to release will eliminate some instability for the curlers, which can help level the playing field for everyone involved. Given the work we have been doing with Mark’s team on performance improvements, it was extremely satisfying for us to see them win the bronze medal in South Korea. We hope that our research and partnership with the team can produce gold medals in the years to come.

Some Interesting Applications from 2018

by: Maple

Throughout the course of a year, Maple users create wildly varying applications on all sorts of subjects. To mark the end of 2018, I thought I’d share some of the 2018 submissions to the Maple Application Center that I personally found particularly interesting.

Solving the 15-puzzle, by Curtis Bright. You know those puzzles where you have to move the pieces around inside a square to put them in order, and there’s only one free space to move into?  I’m not good at those puzzles, but it turns out Maple can help. This is one of collection of new, varied applications using Maple’s SAT solvers (if you want to solve the world’s hardest Sudoku, Maple’s SAT solvers can help with that, too).

Romeo y Julieta: Un clasico de las historias de amor... y de las ecuaciones diferenciales [Romeo and Juliet: A classic story of love..and differential equations], by Ranferi Gutierrez. This one made me laugh (and even more so once I put some of it in google translate, which is more than enough to let you appreciate the application even if you don’t speak Spanish). What’s not to like about modeling a high drama love story using DEs?

Prediction of malignant/benign of breast mass with DNN classifier, by Sophie Tan. Machine learning can save lives.

Hybrid Image of a Cat and a Dog, by Samir Khan. Signal processing can be more fun that I realized. This is one of those crazy optical illusions where the picture changes depending on how far away you are.

Beyond the 8 Queens Problem, by Yury Zavarovsky. In true mathematical fashion, why have 8 queens when you can have n?  (If you are interested in this problem, you can also see a different solution that uses SAT solvers.)

Gödel's Universe, by Frank Wang.  Can’t say I understood much of it, but it involves Gödel, Einstein, and Hawking, so I don’t need to understand it to find it interesting.

How to Complete the Square in Maple

Maple

A common question to our tech support team is about completing the square for a univariate polynomial of even degree, and how to do that in Maple. We’ve put together a solution that we think you’ll find useful. If you have any alternative methods or improvements to our code, let us know!

```restart;

# Procedure to complete the square for a univariate
# polynomial of even degree.

CompleteSquare := proc( f :: depends( 'And'( polynom, 'satisfies'( g -> ( type( degree(g,x), even ) ) ) ) ), x :: name )

local a, g, k, n, phi, P, Q, r, S, T, u:

# Degree and parameters of polynomial.
n := degree( f, x ):
P := indets( f, name ) minus { x }:

# General polynomial of square plus constant.
g := add( a[k] * x^k, k=0..n/2 )^2 + r:

# Solve for unknowns in g.
Q := indets( g, name ) minus P:

S := map( expand, { solve( identity( expand( f - g ) = 0, x ), Q ) } ):

if numelems( S ) = 0 then
return NULL:
end if:

# Evaluate g at the solution, and re-write square term
# so that the polynomial within the square is monic.

phi := u -> lcoeff(op(1,u),x)^2 * (expand(op(1,u)/lcoeff(op(1,u),x)))^2:
T := map( evalindets, map( u -> eval(g,u), S ), `^`(anything,identical(2)), phi ):

return `if`( numelems(T) = 1, T[], T ):

end proc:

# Examples.

CompleteSquare( x^2 + 3 * x + 2, x );
CompleteSquare( a * x^2 + b * x + c, x );
CompleteSquare( 4 * x^8 + 8 * x^6 + 4 * x^4 - 246, x );

m, n := 4, 10;
r := rand(-10..10):
for i from 1 to n do
CompleteSquare( r() * ( x^(m/2) + randpoly( x, degree=m-1, coeffs=r ) )^2 + r(), x );
end do;

# Compare quadratic examples with Student:-Precalculus:-CompleteSquare()
# (which is restricted to quadratic expressions).

Student:-Precalculus:-CompleteSquare( x^2 + 3 * x + 2 );
Student:-Precalculus:-CompleteSquare( a * x^2 + b * x + c );
```

For a higher-order example:

```f := 5*x^4 - 70*x^3 + 365*x^2 - 840*x + 721;
g := CompleteSquare( f, x ); # 5 * ( x^2 - 7 * x + 12 )^2 + 1
h := evalindets( f, `*`, factor ); 4 * (x-3)^2 * (x-4)^2 + 1
p1 := plot( f, x=0..5, y=-5..5, color=blue ):
p2 := plots:-pointplot( [ [3,1], [4,1] ], symbol=solidcircle, symbolsize=20, color=red ):
plots:-display( p1, p2 );
```

tells us that the minimum value of the expression is 1, and it occurs at x=3 and x=4.

Improve Your Chess Rating with Math

by:

Over the holidays I reconnected with an old friend and occasional
chess partner who, upon hearing I was getting soundly thrashed by run
of the mill engines, recommended checking out the ChessTempo site.  It
has online tools for training your chess tactics.  As you attempt to
solve chess problems your rating is computed depending on how well you
do.  The chess problems, too, are rated and adjusted as visitors
attempt them.  This should be familar to any chess or table-tennis
player.  Rather than the Elo rating system, the Glicko rating system is
used.

You have a choice of the relative difficulty of the problems.
After attempting a number of easy puzzles and seeing my rating slowly
climb, I wondered what was the most effective technique to raise my
rating (the classical blunder).  Attempting higher rated problems would lower my
solving rate, but this would be compensated by a smaller loss and
larger gain.  Assuming my actual playing strength is greater than my
current rating (a misconception common to us patzers), there should be a
rating that maximizes the rating gain per problem.

The following Maple module computes the expected rating change
using the Glicko system.

```Glicko := module()

export DeltaRating
,  ExpectedDelta
,  Pwin
;

# Return the change in rating for a loss and a win
# for player 1 against player2
DeltaRating := proc(r1,rd1,r2,rd2)
local E, K, g, g2, idd, q;

q := ln(10)/400;
g := rd -> 1/sqrt(1 + 3*q^2*rd^2/Pi^2);
g2 := g(rd2);
E := 1/(1+10^(-g2*(r1-r2)/400));
idd := q^2*(g2^2*E*(1-E));

K := q/(1/rd1^2+idd)*g2;

(K*(0-E), K*(1-E));

end proc:

# Compute the probability of a win
# for a player with strength s1
# vs a player with strength s2.

Pwin := proc(s1, s2)
local p;
p := 10^((s1-s2)/400);
p/(1+p);
end proc:

# Compute the expected rating change for
# player with strength s1, rating r1 vs a player with true rating r2.
# The optional rating deviations are rd1 and rd2.

ExpectedDelta := proc(s1,r1,r2,rd1 := 35, rd2 := 35)
local P, l, w;
P := Pwin(s1,r2);
(l,w) := DeltaRating(r1,rd1,r2,rd2);
P*w + (1-P)*l;
end proc:

end module:
```

Assume a player has a rating of 1500 but an actual playing strength of 1700.  Compute the expected rating change for a given puzzle rating, then plot it.  As expected the graph has a peak.

```Ept := Glicko:-ExpectedDelta(1700,1500,r2):
plot(Ept,r2 = 1000...2000);
```

Compute the optimum problem rating

```fsolve(diff(Ept,r2));

{r2 = 1599.350691}
```

As your rating improves, you'll want to adjust the rating of the problems (the site doesn't allow that fine tuning). Here we plot the optimum puzzle rating (r2) for a given player rating (r1), assuming the player's strength remains at 1700.

```Ept := Glicko:-ExpectedDelta(1700, r1, r2):
dEpt := diff(Ept,r2):
r2vsr1 := r -> fsolve(eval(dEpt,r1=r)):
plot(r2vsr1, 1000..1680);
```

Here is a Maple worksheet with the code and computations.

Glicko.mw

Later

After pondering this, I realized there is a more useful way to present the results. The shape of the optimal curve is independent of the user's actual strength. Showing that is trivial, just substitute a symbolic value for the player's strength, offset the ratings from it, and verify that the result does not depend on the strength.

```Ept := Glicko:-ExpectedDelta(S, S+r1, S+r2):
has(Ept, S);
false
```

Here's the general curve, shifted so the player's strength is 0, r1 and r2 are relative to that.

```r2_r1 := r -> rhs(Optimization:-Maximize(eval(Ept,r1=r), r2=-500..0)[2][]):
p1 := plot(r2_r1, -500..0, 'numpoints'=30);
```

Compute and plot the expected points gained when playing the optimal partner and your rating is r-points higher than your strength.

```EptMax := r -> eval(Ept, [r1=r, r2=r2_r1(r)]):
plot(EptMax, -200..200, 'numpoints'=30, 'labels' = ["r","Ept"]);
```

When your playing strength matches your rating, the optimal opponent has a relative rating of

```r2_r1(0);
-269.86
```

The expected points you win is

```evalf(EptMax(0));
0.00956
```

Visualizing Knots

by: Maple

From time to time, people ask me about visualizing knots in Maple. There's no formal "Knot Theory" package in Maple per se, but it is certainly possible to generate many different knots using a couple of simple commands. The following shows various examples of knots visualized using the plots:-tubeplot and algcurves:-plot_knot commands.

The unknot can be defined by the following parametric equations:

x=sin(t)

y=cos(t)

z=0

plots:-tubeplot([cos(t),sin(t),0,t=0..2*Pi],
radius=0.2, axes=none, color="Blue", orientation=[60,60], scaling=constrained, style=surfacecontour);

The trefoil knot can be defined by the following parametric equations:

x = sin(t) + 2*sin(2*t)

y = cos(t) + 2*sin(2*t)

z = sin(3*t)

plots:-tubeplot([sin(t)+2*sin(2*t),cos(t)-2*cos(2*t),-sin(3*t), t= 0..2*Pi],
radius=0.2, axes=none, color="Green", orientation=[90,0], style=surface);

The figure-eight can be defined by the following parametric equations:

x = (2 + cos(2*t)) * cos(3*t)

y = (2 + cos(2*t)) * sin(3*t)

z = sin(4*t)

plots:-tubeplot([(2+cos(2*t))*cos(3*t),(2+cos(2*t))*sin(3*t),sin(4*t),t=0..2*Pi],
numpoints=100, radius=0.1, axes=none, color="Red", orientation=[75,30,0], style=surface);

The Lissajous knot can be defined by the following parametric equations:

Where n[x], n[y], and n[z] are integers and the phase shifts phi[x], phi[y], and phi[z] are any real numbers.
The 8 21 knot ( n[x] = 3, n[y] = 4, and n[z] = 7) appears as follows:

plots:-tubeplot([cos(3*t+Pi/2),cos(4*t+Pi/2),cos(7*t),t=0..2*Pi],
radius=0.05, axes=none, color="Brown", orientation=[90,0,0], style=surface);

A star knot can be defined by using the following polynomial:

f := -x^5+y^2
algcurves:-plot_knot(f,x,y,epsilon=0.7,
radius=0.25, tubepoints=10, axes=none, color="Orange", orientation=[60,0], style=surfacecontour);

By switching x and y, different visualizations can be generated:

g:=(y^3-x^7)*(y^2-x^5)+y^3;
plots:-display(<
algcurves:-plot_knot(g,y,x, epsilon=0.8, radius=0.1, axes=none, color="CornflowerBlue", orientation=[75,30,0])|
algcurves:-plot_knot(g,x,y, epsilon=0.8, radius=0.1, axes=none, color="OrangeRed", orientation=[75,0,0])>);

f:=(y^3-x^7)*(y^2-x^5);
algcurves:-plot_knot(f,x,y,
epsilon=0.8, radius=0.1, axes=none, orientation=[35,0,0]);

h:=(y^3-x^7)*(y^3-x^7+100*x^13)*(y^3-x^7-100*x^13);

algcurves:-plot_knot(h,x,y,
epsilon=0.8, numpoints=400, radius=0.03, axes=none, color=["Blue","Red","Green"], orientation=[60,0,0]);

Please feel free to add more of your favourite knot visualizations in the comments below!

You can interact with the examples or download a copy of these examples from the MapleCloud here: https://maple.cloud/app/5654426890010624/Examples+of+Knots

How Do I Consolidate and Spread-Out Values Evenly...

Maple

Problem:

Suppose you have a bunch of 2D data points which:

1. May include points with the same x-value but different y-values; and
2. May be unevenly-spaced with respect to the x-values.

How do you clean up the data so that, for instance, you are free to construct a connected data plot, or perform a Discrete Fourier Transform? Please note that Curve Fitting and the Lomb–Scargle Method, respectively, are more effective techniques for these particular applications. Let's start with a simple example for illustration. Consider this Matrix:

`A := < 2, 5; 5, 8; 2, 1; 7, 8; 10, 10; 5, 7 >;`

Consolidate:

First, sort the rows of the Matrix by the first column, and extract the sorted columns separately:

```P := sort( A[..,1], output=permutation ); # permutation to sort rows by the values in the first column
U := A[P,1]; # sorted column 1
V := A[P,2]; # sorted column 2```

We can regard the sorted bunches of distinct values in U as a step in a stair case, and the goal is replace each step with the average of the y-values in V located on each step.

Second, determine the indices for the first occurrences of values in U, by selecting the indices which give a jump in x-value:

```m := numelems( U );
K := [ 1, op( select( i -> ( U[i] > U[i-1] ), [ seq( j, j=2..m ) ] ) ), m+1 ];
n := numelems( K );```

The element m+1 is appended for later convenience. Here, we can quickly define the first column of the consolidated Matrix:

`X1 := U[K[1..-2]];`

Finally, to define the second column of the consolidated Matrix, we take the average of the values in each step, using the indices in K to tell us the ranges of values to consider:

`Y1 := Vector[column]( n-1, i -> add( V[ K[i]..K[i+1]-1 ] ) / ( K[i+1] - K[i] ) );`

Thus, the consolidated Matrix is given by:

`B := < X1 | Y1 >;`

Spread Evenly:

To spread-out the x-values, we can use a sequence with fixed step size:

`X2 := evalf( Vector[column]( [ seq( X1[1]..X1[-1], (X1[-1]-X1[1])/(m-1) ) ] ) );`

For the y-values, we will interpolate:

`Y2 := CurveFitting:-ArrayInterpolation( X1, Y1, X2, method=linear );`

This gives us a new Matrix, which has both evenly-spaced x-values and consolidated y-values:

`C := < X2 | Y2 >;`

Plot:

```plots:-display( Array( [
plots:-pointplot( A, view=[0..10,0..10], color=green, symbol=solidcircle, symbolsize=15, title="Original Data", font=[Verdana,15] ),
plots:-pointplot( B, view=[0..10,0..10], color=red, symbol=solidcircle, symbolsize=15, title="Consolidated Data", font=[Verdana,15] ),
plots:-pointplot( C, view=[0..10,0..10], color=blue, symbol=solidcircle, symbolsize=15, title="Spread-Out Data", font=[Verdana,15] )
] ) );```

Sample Data with Noise:

For another example, let’s take data points from a logistic curve, and add some noise:

```# Noise generators
f := 0.5 * rand( -1..1 ):
g := ( 100 - rand( -15..15 ) ) / 100:

# Actual x-values
X := [ seq( i/2, i=1..20 ) ];

# Actual y-values
Y := evalf( map( x -> 4 / ( 1 + 3 * exp(-x) ), X ) );

# Matrix of points with noise
A := Matrix( zip( (x,y) -> [x,y], map( x -> x + f(), X ), map( y -> g() * y, Y ) ) );```

Using the method outlined above, and the general procedures defined below, define:

```B := ConsolidatedMatrix( A );
C := EquallySpaced( B, 21, method=linear );```

Visually:

```plots:-display( Array( [
plots:-pointplot( A, view=[0..10,0..5], symbol=solidcircle, symbolsize=15, color=green, title="Original Data", font=[Verdana,15] ),
plots:-pointplot( B, view=[0..10,0..5], symbol=solidcircle, symbolsize=15, color=red, title="Consolidated Data", font=[Verdana,15]  ),
plots:-pointplot( C, view=[0..10,0..5], symbol=solidcircle, symbolsize=15, color=blue, title="Spread-Out Data", font=[Verdana,15] )
] ) );```

Generalization:

Below are more generalized custom procedures, which are used in the above example. These also account for special cases.

```# Takes a matrix with two columns, and returns a new matrix where the new x-values are unique and sorted,
# and each new y-value is the average of the old y-values corresponding to the x-value.
ConsolidatedMatrix := proc( A :: 'Matrix'(..,2), \$ )

local i, j, K, m, n, P, r, U, V, X, Y:

# The number of rows in the original matrix.
r := LinearAlgebra:-RowDimension( A ):

# Return the original matrix should it only have one row.
if r = 1 then
return A:
end if:

# Permutation to sort first column of A.
P := sort( A[..,1], ':-output'=permutation ):

# Sorted first column of A.
U := A[P,1]:

# Corresponding new second column of A.
V := A[P,2]:

# Return the sorted matrix should all the x-values be distinct.
if numelems( convert( U, ':-set' ) ) = r then
return < U | V >:
end if:

# Indices of first occurrences for values in U. The element m+1 is appended for convenience.
m := numelems( U ):
K := [ 1, op( select( i -> ( U[i] > U[i-1] ), [ seq( j, j=2..m ) ] ) ), m+1 ]:
n := numelems( K ):

# Consolidated first column.
X := U[K[1..-2]]:

# Determine the consolidated second column, using the average y-value.
Y := Vector[':-column']( n-1, i -> add( V[ K[i]..K[i+1]-1 ] ) / ( K[i+1] - K[i] ) ):

return < X | Y >:

end proc:

# Procedure which takes a matrix with two columns, and returns a new matrix of specified number of rows
# with equally-spaced x-values, and interpolated y-values.
# It accepts options that can be passed to ArrayInterpolation().
EquallySpaced := proc( M :: 'Matrix'(..,2), m :: posint )

local A, i, r, U, V, X, Y:

# Consolidated matrix, the corresponding number of rows, and the columns.
A := ConsolidatedMatrix( M ):
r := LinearAlgebra:-RowDimension( A ):
U, V := evalf( A[..,1] ), evalf( A[..,2] ):

# If the consolidated matrix has only one row, return it.
if r = 1 then
return A:
end if:

# If m = 1, i.e. only one equally-spaced point is requested, then return a matrix of the averages.
if m = 1 then
return 1/r * Matrix( [ [ add( U ), add( V ) ] ] ):
end if:

# Equally-spaced x-values.
X := Vector[':-column']( [ seq( U[1]..U[-1], (U[-1]-U[1])/(m-1), i=1..m ) ] ):

# Interpolated y-values.
Y := CurveFitting:-ArrayInterpolation( U, V, X, _rest ):

return < X | Y >:

end proc:```

Worth Checking Out:

How do I assign functions from expressions in...

Maple

Maple users frequently solve differential equations. If you want to use the results later in Maple, you need to deconstruct the solution, and then assign the functions -- something that isn't done automatically in Maple. We wrote a multi-purpose routine to help you out. For instance, suppose you solve a simple linear system of equations:

```restart;

eqs := { x + y = 3, x - y = 1 };
soln := solve( eqs ); # { x = 2, y = 1 }
x, y; # plain x and y```

To assign the values from the solution to the corresponding variables:

```assign( soln );
x, y; # 2, 1```

This won't work for solutions of differential equations:

```restart;

sys := { D(x)(t) = y(t), D(y)(t) = -x(t), x(0) = 1, y(0) = 0 };
soln := dsolve( sys ); # { x(t) = cos(t), y(t) = -sin(t) }
assign( soln );
x(s), y(s); # plain x(s) and y(s)```

To make this work, we wrote this multi-purpose routine:

```restart;

# Type for a variable expression, e.g. x=5.
TypeTools:-AddType( 'varexpr', u -> type( u, 'And'('name','Non'('constant'))='algebraic' ) ):

# Type for a function expression, e.g. f(x)=x^2.
TypeTools:-AddType( 'funcexpr', u -> type( u, 'function'('And'('name','Non'('constant')))='algebraic' ) ):

# Procedure to assign variable and function expressions.
my_assign := proc( u :: {
varexpr, 'list'(varexpr), 'rtable'(varexpr), 'set'(varexpr),
funcexpr, 'list'(funcexpr), 'rtable'(funcexpr), 'set'(funcexpr)
}, \$ )

local F, L, R, V:

# Map the procedure if input is a data container, or apply regular assign(), where applicable.
if not u :: {'varexpr','funcexpr'} then
map( procname, u ):
return NULL:
elif u :: 'varexpr' then
assign( u ):
return NULL:
end if:

L, R := lhs(u), rhs(u):
F := op(0,L):
V := [ indets( L, 'And'( 'name', 'Non'('constant') ) )[] ]:

map( assign, F, unapply( R, V ) ):
return NULL:

end proc:

# Example 1.

eqs := { x + y = 3, x - y = 1 };
my_assign( solve( eqs ) );
'x' = x, 'y' = y; # x=1, y=2

# Example 2.

unassign( 'x', 'y' ):
E := [ f(x,y) = x + y, g(x,y) = x - y ];
my_assign( E );
'f(u,v)' = f(u,v), 'g(u,v)' = g(u,v); # f(u,v)=u+v, g(u,v)=u-v

# Example 3.

sys := { D(x)(t) = y(t), D(y)(t) = -x(t), x(0) = 1, y(0) = 0 };
soln := dsolve( sys );
my_assign( soln ):
'x(s)' = x(s); # x(s)=cos(s)
'y(s)' = y(s); # y(s)=-sin(s)```

Making Math Fun – the Teenage Way!

by: Maple

Fourteen year old Lazar Paroski is an exceptional student. Not only is he an overachiever academically, but he has a passion to help struggling students, and to think of innovative ways to help them learn. Lazar is particularly fond of Math, and in his interactions with other students, he noticed how students have a hard time with Math.

Putting on his creative cap, Lazar came up with the idea of an easily accessible “Math Wall” that explains simple math concepts for students in a very visual way.

“The Music Wall on Pinterest was my inspiration,” says Lazar. “I thought I can use the same idea for Math, and why not a Math Wall”?

"The math wall is basically all the tools you'll have on the wall in your classroom outside," said Lazar. Making the Math Wall and getting it set up, took time and effort. But he had help along the way, which, fueled by his passion and enthusiasm, helped turn his creative dream into reality. Lazar received a grant of \$6000 from the local government to implement the project; his teachers, principal and family helped promote it; and the community of parents provided encouragement.

The Math Wall covers fundamental math concepts learnt in grades 1 to 6. Lazar engaged with over 450 students in the community to understand what would really be helpful for students to see in this Math Wall, and then he carefully picked the top themes he wanted to focus on.

The three meter Math Wall is located in the Morrison community park, and was officially inaugurated by the Mayor of Kitchener in July 2018. Many students have already found it to be useful and educative. Parents who bring their children to the park stop by to give their kids a quick math lesson.

At Maplesoft, we love a math story like this! And that too in our backyard! We wanted to appreciate and encourage Lazar and his efforts in making math fun and easy and accessible to students. So we invited Lazar to our offices, gifted him a copy of Maple, and heard more about his passion and future plans. “In many ways, Lazar embodies the same qualities that Maplesoft stands for – making it easy for students to understand and grasp complex STEM concepts,” said Laurent Bernardin, Maplesoft’s Chief Operating Officer. “We try to impress upon students that math matters, even in everyday life, and they can now use advanced, sophisticated technology tools to make math learning fun and efficient.”

We wish Lazar all the very best as he thinks up new and innovative ways to spread his love for math to other kids. Well done, Lazar!

Hybrid Images, Visual Perception, and Distance ...

by: Maple

You might recall this image being shared on social media some time ago.

Source: http://cvcl.mit.edu/hybrid_gallery/monroe_einstein.html

Look closely and you see Albert Einstein. However, if you move further away (or make the image smaller), you see Marilyn Monroe.

To create the image, the high spatial frequency data from an image of Albert Einstein was added to the low spatial frequency data from an image of Marilyn Monroe. This approach was pioneered by Oliva et al. (2006) and is influenced by the multiscale processing of human vision.

• When we view objects near us, we see fine detail (that is, higher spatial frequencies dominate).

• However, when we view objects at a distance, the broad outline has greater influence (that is, lower spatial frequencies dominate).

I thought I'd try to create a similar image in Maple (get the complete application here).

Here's an overview of the approach (as outlined in Oliva et al., 2006). I used different source images of Einstein and Monroe.

Let's start by loading some packages and defining a few procedures.

```restart:
with(ImageTools):
with(SignalProcessing):

fft_shift := proc(M)
local nRows, nCols, quad_1, quad_2, quad_3, quad_4, cRows, cCols;
nRows, nCols := LinearAlgebra:-Dimensions(M):
cRows, cCols := ceil(nRows/2), ceil(nCols/2):
quad_1 := M[1..cRows,      1..cCols]:
quad_2 := M[1..cRows,      cCols + 1..-1]:
quad_3 := M[cRows + 1..-1, cCols + 1..-1]:
quad_4 := M[cRows + 1..-1, 1..cCols]:
return <<quad_3, quad_2 |quad_4, quad_1>>:
end proc:

PowerSpectrum2D := proc(M)
return sqrt~(abs~(M))
end proc:

gaussian_filter := (a, b, sigma) -> Matrix(2 * a, 2 * b, (i, j) -> evalf(exp(-((i - a)^2 + (j - b)^2) / (2 * sigma^2))), datatype = float[8]):```

fft_shift() swaps quadrants of a 2D Fourier transform around so that the zero frequency components are in the center.

PowerSpectrum2D() returns the spectra of a 2D Fourier transform

gaussian_filter() will be used to apply a high or low-pass filter in the frequency domain (a and b are the number of rows and columns in the 2D Fourier transform, and sigma is the cut-off frequency.

Now let's import and display the original Einstein and Monroe images (both are the same size).

``````einstein_img := Read("einstein.png")[..,..,1]:
Embed(einstein_img)``````

```marilyn_img  := Read("monroe.png")[..,..,1]:
Embed(monroe_img)```

Let's convert both images to the spatial frequency domain (not many people know that SignalProcessing:-FFT can calculate the Fourier transform of matrices).

```einstein_fourier := fft_shift(FFT(einstein_img)):
monroe_fourier   := fft_shift(FFT(monroe_img)):```

Visualizing the power spectra of the unfiltered and filtered images isn't necessary, but helps illustrate what we're doing in the frequency domain.

First the spectra of the Einstein image. Lower frequency data is near the center, while higher frequency data is further away from the center.

`Embed(Create(PowerSpectrum2D(einstein_fourier)))`

Now the spectra of the Monroe image.

`Embed(Create(PowerSpectrum2D(monroe_fourier)))`

Now we need to filter the frequency content of both images.

First, define the cutoff frequencies for the high and low pass Gaussian filters.

```sigma_einstein := 25:
sigma_monroe   := 10:```

In the frequency domain, apply a high pass filter to the Einstein image, and a low pass filter to the Monroe image.

```nRows, nCols := LinearAlgebra:-Dimension(einstein_img):

einstein_fourier_high_pass := einstein_fourier *~ (1 -~ gaussian_filter(nRows/2, nCols/2, sigma_einstein)):
monroe_fourier_low_pass    := monroe_fourier   *~ gaussian_filter(nRows/2, nCols/2, sigma_monroe):```

Here's the spectra of the Einstein and Monroe images after the filtering (compare these to the pre-filtered spectra above).

`Embed(Create(PowerSpectrum2D(einstein_fourier_high_pass)))`

`Embed(Create(PowerSpectrum2D(monroe_fourier_low_pass)))`

Before combining both images in the Fourier domain, let's look the individual filtered images.

```einstein_high_pass_img := Re~(InverseFFT(fft_shift(einstein_fourier_high_pass))):
monroe_low_pass_img    := Re~(InverseFFT(fft_shift(monroe_fourier_low_pass))):```

We're left with sharp detail in the Einstein image.

`Embed(FitIntensity(Create(einstein_high_pass_img)))`

But the Monroe image is blurry, with only lower spatial frequency data.

`Embed(FitIntensity(Create(monroe_low_pass_img)))`

For the final image, we're simply going to add the Fourier transforms of both filtered images, and invert to the spatial domain.

```hybrid_image := Create(Re~(InverseFFT(fft_shift(monroe_fourier_low_pass + einstein_fourier_high_pass)))):
Embed(hybrid_image)```

So that's our final image, and has a similar property to the hybrid image at the top of this post.

• Move close to the computer monitor and you see Albert Einstein.
• Move to the other side of the room, and Marilyn Monroe swims into vision (if you're myopic, just take off your glasses and don't move back as much).

To simulate this, here, I've successively reduced the size of the hybrid image

And just because I can, here's a hybrid image of a cat and a dog, generated by the same worksheet.

Can I Programmatically Modify Batch Scripts in...

Maple

Hi Primes Users,

We’re back with another tech support challenge that we wanted to share with you!

A customer had been having issues launching Maplets using the standard double-clicking method. This is a known issue that rarely occurs, but is being addressed for a future release. In the meantime, we were able to provide this person with a command-prompt-based way of opening the Maplet, and we thought it would be great to share in case you run into the same kind of problem.

After suggesting a few workarounds, our Team Lead was able to offer a command-prompt based way of solving the problem. Since command prompts are the target of batch scripts, which we had already used as a workaround for another issue, we just needed a way of programmatically creating scripts based on the command prompt code for each file.

Using various commands from the FileTools package (including a technique suggested by our Team Lead), we were able to put together code that takes all files in a particular folder (namely, a folder named “Maplets” on the Desktop), and creates a new batch script from a template that is based on the command prompt code (provided that the template is saved in the same Maplets folder under the file name “Maplet script”):

```restart; with(FileTools): username := kernelopts(username):

directory := cat("C:\\Users\\", username, "\\Desktop\\Maplets"):

dir := ListDirectory(directory): dir := Basename~(dir):

main := cat(directory, "\\Maplet script.txt"): body := Import(main):

n := numelems(dir):

for i to n do

script := cat(directory, "\\launch ", dir[i], ".txt");

batch := cat(directory, "\\launch ", dir[i], ".bat");

newbody := StringTools:-Substitute(body, "name", dir[i]);

Export(script, newbody);

Rename(script, batch);

end do:

Script template:

if not "%minimized%"=="" goto :minimized

set minimized=true

start /min cmd /C "%~dpnx0"

goto :EOF

:minimized

"C:\Program Files\Maple 2018\bin.X86_64_WINDOWS\mapletviewer.exe" "C:\Users\%USERNAME%\Desktop\Maplets\name.maplet"```

Before using the Maplet script:

After using the Maplet script:

If the appropriate executable is referenced, and the relevant file paths are adjusted accordingly, one should be able to adapt this process to other programs and their corresponding files.  In fact, any batch script that needs to be modified programmatically can be modified using these techniques.  Does anyone have other useful batch scripts that they’ve modified programmatically in Maple using similar techniques?

*(including dragging the files to the executable directly, which only seemed to work when the executable was in its original directory)

Meet Your Developers - Margaret Hinchcliffe

We’re excited to bring you another Meet Your Developers post. This one comes from Senior Developer, Margaret Hinchcliffe.

Enjoy!

1. What do you do at Maplesoft?
I work on the software team that develops the user interface for Maplesoft products. In my time at Maple, I’ve worked on Maple, MapleSim, and MapleNet.

1. What did you study in school?
I studied Computer Science at the University of Waterloo, in the Co-op program.

1. What area(s) of Maple or MapleSim are you currently focusing on in your development?
I’m currently working on MapleNet and on ways to bring Maple functionality to the web.

1. What’s the coolest feature of Maple or MapleSim that you’ve had a hand in developing?
I helped develop the feature that lets you embed videos in a Maple worksheet. I thought that was pretty cool.

1. What do you like most about working at Maplesoft? How long have you worked here?
I celebrated my twentieth anniversary at Maplesoft this spring. Obviously, I like working here. What I like most is the opportunity to learn new skills in a supportive environment. Our company gym is pretty awesome, too.

1. Favourite hobby?
I took up boxing a couple of years ago and I really enjoy it. It’s a great workout and there’s always something new to learn.

1. What do you like on your pizza?
Pepperoni, mushrooms, and fresh basil.

1. What’s your favourite movie?
The Wizard of Oz. When people say “The book is always better”, I point to this movie as a counterexample.

1. What skill would you love to learn? Why?
I’d like to try archery. If nothing else, it would come in handy in a zombie apocalypse.

1. Who’s your favourite mathematician?
Alan Turing. He made important contributions to computer science and he helped fight the Nazis.

Thanks Margaret!

MagicPuzzles Package for Puzzles Like Magic Squares...

by: Maple 2018

Hello MaplePrime users,

Yesterday, I posted my MagicPuzzles package to the MapleCloud. It is a collection of tools I have written for manipulating, solving, and visualizing puzzles like Magic Squares and Magic Stars. Here's a sample solution for each:

For the Magic Square, the numbers on each horizontal and vertical line, along with the numbers on each of the two diagonals, add up to 65.

The inaugural version has separate sub-packages for:

• Magic Annulai (my own name)
• Magic Hexagons
• Magic Squares
• Magic Stars

Moreover, each sub-package contains these commands:

• Equations(), to return the linear equations for the variables based on the Magic Sum;
• Constraints(), to return the conditions that prevent redundant solutions found by reflections and rotations;
• VerifySolution(), to confirm if a list of numbers is a solution;
• EquivalentSolutions(), to determine solutions equivalent to a given solution;
• PrimarySolution(), which takes a solution and returns the associated primary solution;
• Reflection() and Rotation(), to reflect and rotate a solution; and
• Draw(), to provide a visualization (like the ones above).

There is also a command, MagicSolve(), which is used to find solutions, which take the form of permutations of [1,...n] for some positive integer n, to the equations. Essentially, it solves the linear equations, and cycles through all permutations for the free variables, and selects those that give "magic" solutions.

In future versions, I intend to add:

• Other specific classes of problems;
• More sample solutions; and
• Known algorithms for finding particular solutions.

To install the package, you can do so from here, or just execute the following within Maple 2017+:

`PackageTools:-Install( 5755630338965504, 'overwrite' );`

There are many examples in the help pages.

I think others will find this package interesting and useful, and I encourage you to check it out.

 1 2 3 4 5 6 7 Last Page 2 of 24
﻿