Product Tips & Techniques

Tips and Tricks on how to get the most about Maple and MapleSim

Feynman Diagrams
The scattering matrix in coordinates and momentum representation


Mathematical methods for particle physics was one of the weak spots in the Physics package. There existed a FeynmanDiagrams command, but its capabilities were too minimal. People working in the area complained about that. These diagrams are the cornerstone of calculations in particle physics (collisions involving from the proton to the Higgs boson), for example at the CERN. As an introduction for people curious, not working in the area, see "Why Feynman Diagrams are so important".


This post is thus about a new development in Physics: a full rewriting of the FeynmanDiagrams command, now including a myriad of new capabilities (mainly a. b. and c. in the Introduction below), reversing the previous status of things entirely. This is work in collaboration with Davide Polvara from Durham University, Centre for Particle Theory.


The introduction to the presentation below is as brief as it can get, emphasizing the examples instead. This material is reproducible in Maple 2019.2 after installing the Physics Updates, v.598 or higher.




At the end it is attached the worksheet corresponding to this presentation, as well as the new FeynmanDiagrams help page with all the explanatory details.



A scattering matrix S relates the initial and final states, `#mfenced(mrow(mo("⁢"),mi("i"),mo("⁢")),open = "|",close = "⟩")` and `#mfenced(mrow(mo("⁢"),mi("f"),mo("⁢")),open = "|",close = "⟩")`, of an interacting system. In an 4-dimensional spacetime with coordinates X, S can be written as:

S = T(exp(i*`#mrow(mo("∫"),mi("L"),mo("⁡"),mfenced(mi("X")),mo("ⅆ"),msup(mi("X"),mn("4")))`))


where i is the imaginary unit  and L is the interaction Lagrangian, written in terms of quantum fields  depending on the spacetime coordinates  X. The T symbol means time-ordered. For the terminology used in this page, see for instance chapter IV, "The Scattering Matrix", of ref.[1].


This exponential can be expanded as

S = 1+S[1]+S[2]+S[3]+`...`



S[n] = `#mrow(mo("⁡"),mfrac(msup(mi("i"),mi("n")),mrow(mi("n"),mo("!")),linethickness = "1"),mo("⁢"),mo("∫"),mi("…"),mo("⁢"),mo("∫"),mi("T"),mo("⁡"),mfenced(mrow(mi("L"),mo("⁡"),mfenced(mi("\`X__1\`")),mo(","),mi("…"),mo(","),mi("L"),mo("⁡"),mfenced(mi("\`X__n\`")))),mo("⁢"),mo("ⅆ"),msup(mi("\`X__1\`"),mn("4")),mo("⁢"),mi("…"),mo("⁢"),mo("ⅆ"),msup(mi("\`X__n\`"),mn("4")))`


and T(L(X[1]), `...`, L(X[n])) is the time-ordered product of n interaction Lagrangians evaluated at different points. The S matrix formulation is at the core of perturbative approaches in relativistic Quantum Field Theory, where exact solutions are known only for some 2-dimensional models.


 In brief, the new functionality includes computing:


The expansion S = 1+S[1]+S[2]+S[3]+`...` in coordinates representation up to arbitrary order (the limitation is now only the hardware)


The S-matrix element `#mfenced(mrow(mo("⁢"),mi("f"),mo("⁢"),mo("|"),mo("⁢"),mi("S"),mo("⁢"),mo("|"),mo("⁢"),mi("i"),mo("⁢")),open = "⟨",close = "⟩")` in momentum representation up to arbitrary order for given number of loops and initial and final particles (the contents of the `#mfenced(mrow(mo("⁢"),mi("i"),mo("⁢")),open = "|",close = "⟩")` and `#mfenced(mrow(mo("⁢"),mi("f"),mo("⁢")),open = "|",close = "⟩")` states); optionally, also the transition probability density, constructed using the square of the scattering matrix element abs(`#mfenced(mrow(mo("⁢"),mi("f"),mo("⁢"),mo("|"),mo("⁢"),mi("S"),mo("⁢"),mo("|"),mo("⁢"),mi("i"),mo("⁢")),open = "⟨",close = "⟩")`)^2, as shown in formula (13) of sec. 21.1 of ref.[1].


The Feynman diagrams (drawings) related to the different terms of the expansion of S or of its matrix elements `#mfenced(mrow(mo("⁢"),mi("f"),mo("⁢"),mo("|"),mo("⁢"),mi("S"),mo("⁢"),mo("|"),mo("⁢"),mi("i"),mo("⁢")),open = "⟨",close = "⟩")`.


Interaction Lagrangians involving derivatives of fields, typically appearing in non-Abelian gauge theories, are also handled, and several options are provided enabling restricting the outcome in different ways, regarding the incoming and outgoing particles, the number of loops, vertices or external legs, the propagators and normal products, or whether to compute tadpoles and 1-particle reducible terms.






[1] Bogoliubov, N.N., and Shirkov, D.V. Quantum Fields. Benjamin Cummings, 1982.

[2] Weinberg, S., The Quantum Theory Of Fields. Cambridge University Press, 2005.




Edgardo S. Cheb-Terrab
Physics, Differential Equations and Mathematical Functions, Maplesoft

The ideas here are to allow 3D plotting commands such as plot3d to handle a `size` option similarly to how 2D plotting commands do so, and for the plots:-display command to also handle it for 3D plots.

The size denotes the dimensions of the inlined plotting window, and not the relative lengths of the three axes.

I'd be interested in any new problems introduced with this, eg. export, etc.


# Using ToInert/FromInert
# This might go in an initialzation file.
  if __ver>=18.0 and __ver<=2019.2 then
    if :-has(:-op([5,2,2,2,1],__KK),:-_Inert_PARAM(__NN)) then
      :-print("3D size patch done");
      :-print("3D size patch not appropriate; possibly already done");
    end if;
    :-print(sprintf("3D size patch not appropriate for version %a"),__ver);
  end if;
  :-print("3D size patch failed");
end try:

"3D size patch done"


P := plot3d(sin(x)*y^2, x=-Pi..Pi, y=-1..1, size=[150,150],
            font=[Times,5], labels=["","",""]):

plots:-display(P, size=[300,300], font=[Times,10]);

# inherited from the contourplot3d (the plot3d is unset).
  plots:-contourplot3d(sin(x)*y^2, x=-Pi..Pi, y=-1..1,
                       thickness=3, contours=20, size=[800,800]),
  plot3d(sin(x)*y^2, x=-Pi..Pi, y=-1..1, color="Gray",
         transparency=0.1, style=surface)

# Some options should still act as 2D-plot-specific.
try plot3d(sin(x)*y^2, x=-Pi..Pi, y=-1..1, legend="Q");
    print("Not OK");
if StringTools:-FormatMessage(lastexception[2..-1])
   ="the legend option is not available for 3-D plots"
then print("OK"); else print("Not OK"); error; end if; end try;




If this works fine then it might be a candidate for inclusion in an initialization file, so that it's
automatically available.

I was trying to display a Physics[Vectors] vector name in a 3dplot with an up arrow
on it. I found that this old 2008 trick still works in MAPLE 2018.





# Using MAPLE 2018.2


t:= textplot3d([-1.1,1.1,1,v_]):




# I found this on an old 2008 post
t:= textplot3d([-1.1,1.1,1,typeset(`#mover(mi(` || v ||  `),mo("→"))`)]):




This update fixes the problems inadvertently introduced in Maple 2019.2, namely:

  • Maple failed to run the code in the maple.ini/.mapleinit initialization files when loading existing worksheets containing a restart() command
  • Installing some packages from the MapleCloud was unsuccessful

For anyone who installed the 2019.2 update, installing 2019.2.1 will fix these problems.

If you are at Maple 2019.1 or earlier, installing this update will bring you straight to Maple 2019.2.1.

This update is available through Tools>Check for Updates in Maple, and is also available from our website on the Maple 2019.2.1 download page.

If you are a MapleSim user, please note that these problems do not affect your use of MapleSim. If you use Maple on its own, and if you use Maple command initialization files and/or you need to install a package from the MapleCloud that does not work, please contact Maplesoft Technical Support for assistance.

We sincerely apologize for the inconvenience and thank you for your patience as we worked through this issue.

I'm only just hearing (haven't experienced) about some serious issues with the 2019.2 updates.  I would recommend waiting for Maplesoft to release an emergency 2019.3 fix update - Maplesoft can NOT leave the last update of 2019 in this state.

Splitting PDE parameterized symmetries

and Parameter-continuous symmetry transformations

The determination of symmetries for partial differential equation systems (PDE) is relevant in several contexts, the most obvious of which is of course the determination of the PDE solutions. For instance, generally speaking, the knowledge of a N-dimensional Lie symmetry group can be used to reduce the number of independent variables of PDE by N. So if PDE depends only on N independent variables, that amounts to completely solving it. If only N-1 symmetries are known or can be successfully used then PDE becomes and ODE; etc., all advantageous situations. In Maple, a complete set of symmetry commands, to perform each step of the symmetry approach or several of them in one go, is part of the PDEtools  package.


Besides the dependent and independent variables, PDE frequently depends on some constant parameters, and besides the PDE symmetries for arbitrary values of those parameters, for some particular values of them, PDE transforms into a completely different problem, admitting different symmetries. The question then is: how can you determine those particular values of the parameters and the corresponding different symmetries? That was the underlying subject of a recent question in Mapleprimes. The answer to those questions is relatively simple and yet not entirely obvious for most of us, motivating this post, organized briefly around one example.


To reproduce the input/output below you need Maple 2019 and to have installed the Physics Updates v.449 or higher.


Consider the family of Korteweg-de Vries equation for u(x, t)involving three constant parameters a, b, q. For convenience (simpler input and more readable output) use the diff_table  and declare  commands


U := diff_table(u(x, t))

pde := b*U[]*U[x]+a*U[x]+q*U[x, x, x]+U[t] = 0

b*u(x, t)*(diff(u(x, t), x))+a*(diff(u(x, t), x))+q*(diff(diff(diff(u(x, t), x), x), x))+diff(u(x, t), t) = 0



` u`(x, t)*`will now be displayed as`*u


This pde admits a 4-dimensional symmetry group, whose infinitesimals - for arbitrary values of the parameters a, b, q- are given by

I__1 := Infinitesimals(pde, [u], specialize_Cn = false)

[_xi[x](x, t, u) = (1/3)*_C1*x+_C3*t+_C4, _xi[t](x, t, u) = _C1*t+_C2, _eta[u](x, t, u) = (1/3)*((-2*b*u-2*a)*_C1+3*_C3)/b]


Looking at pde (1) as a nonlinear problem in u, a, b and q, it splits into four cases for some particular values of the parameter:

pde__cases := casesplit(b*u(x, t)*(diff(u(x, t), x))+a*(diff(u(x, t), x))+q*(diff(diff(diff(u(x, t), x), x), x))+diff(u(x, t), t) = 0, parameters = {a, b, q}, caseplot)

`========= Pivots Legend =========`


p1 = q


p2 = b*u(x, t)+a


p3 = b



`casesplit/ans`([diff(diff(diff(u(x, t), x), x), x) = -(b*u(x, t)*(diff(u(x, t), x))+a*(diff(u(x, t), x))+diff(u(x, t), t))/q], [q <> 0]), `casesplit/ans`([diff(u(x, t), x) = -(diff(u(x, t), t))/(b*u(x, t)+a), q = 0], [b*u(x, t)+a <> 0]), `casesplit/ans`([u(x, t) = -a/b, q = 0], [b <> 0]), `casesplit/ans`([diff(u(x, t), t) = 0, a = 0, b = 0, q = 0], [])


The legend above indicates the pivots and the tree of cases, depending on whether each pivot is equal or different from 0. At the end there is the algebraic sequence of cases. The first case is the general case, for which the symmetry infinitesimals were computed as I__1 above, but clearly the other three cases admit more general symmetries. Consider for instance the second case, pass the ignoreparameterizingequations to ignore the parameterizing equation q = 0, and you get

I__2 := Infinitesimals(pde__cases[2], ignore)

`* Partial match of  'ignore' against keyword 'ignoreparameterizingequations'`


[_xi[x](x, t, u) = _F3(x, t, u), _xi[t](x, t, u) = Intat(((b*u+a)*(D[1](_F3))(_a, ((b*u+a)*t-x+_a)/(b*u+a), u)-_F1(u, ((b*u+a)*t-x)/(b*u+a))*b+(D[2](_F3))(_a, ((b*u+a)*t-x+_a)/(b*u+a), u))/(b*u+a)^2, _a = x)+_F2(u, ((b*u+a)*t-x)/(b*u+a)), _eta[u](x, t, u) = _F1(u, ((b*u+a)*t-x)/(b*u+a))]


These infinitesimals are indeed much more general than I__1, in fact so general that (5) is almost unreadable ... Specialize the three arbitrary functions into something "easy" just to be able follow - e.g. take _F1 to be just the + operator, _F2 the * operator and _F3 = 1

eval(I__2, [_F1 = `+`, _F2 = `*`, _F3 = 1])

[_xi[x](x, t, u) = 1, _xi[t](x, t, u) = Intat(-(u+((b*u+a)*t-x)/(b*u+a))*b/(b*u+a)^2, _a = x)+u*((b*u+a)*t-x)/(b*u+a), _eta[u](x, t, u) = u+((b*u+a)*t-x)/(b*u+a)]


simplify(value([_xi[x](x, t, u) = 1, _xi[t](x, t, u) = Intat(-(u+((b*u+a)*t-x)/(b*u+a))*b/(b*u+a)^2, _a = x)+u*((b*u+a)*t-x)/(b*u+a), _eta[u](x, t, u) = u+((b*u+a)*t-x)/(b*u+a)]))

[_xi[x](x, t, u) = 1, _xi[t](x, t, u) = (b^3*t*u^4+((3*a*t-x)*u^3-u^2*x-t*x*u)*b^2+((3*a^2*t-2*a*x)*u^2-a*u*x-a*t*x+x^2)*b+a^2*u*(a*t-x))/(b*u+a)^3, _eta[u](x, t, u) = (b*u^2+(b*t+a)*u+a*t-x)/(b*u+a)]


This symmetry is of course completely different than [_xi[x](x, t, u) = (1/3)*_C1*x+_C3*t+_C4, _xi[t](x, t, u) = _C1*t+_C2, _eta[u](x, t, u) = ((-2*b*u-2*a)*_C1+3*_C3)/(3*b)]computed for the general case.


The symmetry (7) can be verified against pde__cases[2] or directly against pde after substituting q = 0.

[_xi[x](x, t, u) = (1/3)*_C1*x+_C3*t+_C4, _xi[t](x, t, u) = _C1*t+_C2, _eta[u](x, t, u) = (1/3)*((-2*b*u-2*a)*_C1+3*_C3)/b]


SymmetryTest([_xi[x](x, t, u) = 1, _xi[t](x, t, u) = (b^3*t*u^4+((3*a*t-x)*u^3-u^2*x-t*x*u)*b^2+((3*a^2*t-2*a*x)*u^2-a*u*x-a*t*x+x^2)*b+a^2*u*(a*t-x))/(b*u+a)^3, _eta[u](x, t, u) = (b*u^2+(b*t+a)*u+a*t-x)/(b*u+a)], pde__cases[2], ignore)

`* Partial match of  'ignore' against keyword 'ignoreparameterizingequations'`




SymmetryTest([_xi[x](x, t, u) = 1, _xi[t](x, t, u) = (b^3*t*u^4+((3*a*t-x)*u^3-u^2*x-t*x*u)*b^2+((3*a^2*t-2*a*x)*u^2-a*u*x-a*t*x+x^2)*b+a^2*u*(a*t-x))/(b*u+a)^3, _eta[u](x, t, u) = (b*u^2+(b*t+a)*u+a*t-x)/(b*u+a)], subs(q = 0, pde))



Summarizing: "to split PDE symmetries into cases according to the values of the PDE parameters, split the PDE into cases with respect to these parameters (command PDEtools:-casesplit ) then compute the symmetries for each case"


Parameter continuous symmetry transformations


A different, however closely related question, is whether pde admits "symmetries with respect to the parameters a, b and q", so whether exists continuous transformations of the parameters a, b and q that leave pde invariant in form.


Beforehand, note that since the parameters are constants with regards to the dependent and independent variables (here u(x, t)), such continuous symmetry transformations cannot be used directly to compute a solution for pde. They can, however, be used to reduce the number of parameters. And in some contexts, that is exactly what we need, for example to entirely remove the splitting into cases due to their presence, or to proceed applying a solving method that is valid only when there are no parameters (frequently the case when computing exact solutions to "PDE & Boundary Conditions").


To compute such "continuous symmetry transformations of the parameters" that leave pde invariant one can always think of these parameters as "additional independent variables of pde". In terms of formulation, that amounts to replacing the dependency in the dependent variable, i.e. replace u(x, t) by u(x, t, a, b, q)


pde__xtabq := subs((x, t) = (x, t, a, b, q), pde)

b*u(x, t, a, b, q)*(diff(u(x, t, a, b, q), x))+a*(diff(u(x, t, a, b, q), x))+q*(diff(diff(diff(u(x, t, a, b, q), x), x), x))+diff(u(x, t, a, b, q), t) = 0


Compute now the infinitesimals: note there are now three additional ones, related to continuous transformations of "a,b,"and q - for readability, avoid displaying the redundant functionality x, t, a, b, q, u on the left-hand sides of these infinitesimals

Infinitesimals(pde__xtabq, displayfunctionality = false)

[_xi[x] = (1/3)*(_F4(a, b, q)*q+_F3(a, b, q))*x/q+_F6(a, b, q)*t+_F7(a, b, q), _xi[t] = _F4(a, b, q)*t+_F5(a, b, q), _xi[a] = _F1(a, b, q), _xi[b] = _F2(a, b, q), _xi[q] = _F3(a, b, q), _eta[u] = (1/3)*((b*u+a)*_F3(a, b, q)-2*((b*u+a)*_F4(a, b, q)+(3/2)*u*_F2(a, b, q)+(3/2)*_F1(a, b, q)-(3/2)*_F6(a, b, q))*q)/(b*q)]


This result is more general than what is convenient for algebraic manipulations, so specialize the seven arbitrary functions of a, b, q and keep only the first symmetry that result from this specialization: that suffices to illustrate the removal of any of the three parameters a, b, or q

S := Library:-Specialize_Fn([_xi[x] = (1/3)*(_F4(a, b, q)*q+_F3(a, b, q))*x/q+_F6(a, b, q)*t+_F7(a, b, q), _xi[t] = _F4(a, b, q)*t+_F5(a, b, q), _xi[a] = _F1(a, b, q), _xi[b] = _F2(a, b, q), _xi[q] = _F3(a, b, q), _eta[u] = (1/3)*((b*u+a)*_F3(a, b, q)-2*((b*u+a)*_F4(a, b, q)+(3/2)*u*_F2(a, b, q)+(3/2)*_F1(a, b, q)-(3/2)*_F6(a, b, q))*q)/(b*q)])[1 .. 1]

[_xi[x] = 0, _xi[t] = 0, _xi[a] = 1, _xi[b] = 0, _xi[q] = 0, _eta[u] = -1/b]


To remove the parameters, as it is standard in the symmetry approach, compute a transformation to canonical coordinates, with respect to the parameter a. That means a transformation that changes the list of infinitesimals, or likewise its infinitesimal generator representation,

InfinitesimalGenerator(S, [u(x, t, a, b, q)])

proc (f) options operator, arrow; diff(f, a)-(diff(f, u))/b end proc


into [_xi[x] = 0, _xi[t] = 0, _xi[a] = 1, _xi[b] = 0, _xi[q] = 0, _eta[u] = 0] or its equivalent generator representation  proc (f) options operator, arrow; diff(f, a) end proc

That same transformation, when applied to pde__xtabq, entirely removes the parameter a.

The transformation is computed using CanonicalCoordinates and the last argument indicates the "independent variable" (in our case a parameter) that the transformation should remove. We choose to remove a

CanonicalCoordinates(S, [u(x, t, a, b, q)], [upsilon(xi, tau, alpha, beta, chi)], a)

{alpha = a, beta = b, chi = q, tau = t, xi = x, upsilon(xi, tau, alpha, beta, chi) = (b*u(x, t, a, b, q)+a)/b}


declare({alpha = a, beta = b, chi = q, tau = t, xi = x, upsilon(xi, tau, alpha, beta, chi) = (b*u(x, t, a, b, q)+a)/b})

` u`(x, t, a, b, q)*`will now be displayed as`*u


` upsilon`(xi, tau, alpha, beta, chi)*`will now be displayed as`*upsilon


Invert this transformation in order to apply it

solve({alpha = a, beta = b, chi = q, tau = t, xi = x, upsilon(xi, tau, alpha, beta, chi) = (b*u(x, t, a, b, q)+a)/b}, {a, b, q, t, x, u(x, t, a, b, q)})

{a = alpha, b = beta, q = chi, t = tau, x = xi, u(x, t, a, b, q) = (upsilon(xi, tau, alpha, beta, chi)*beta-alpha)/beta}


The next step is not necessary, but just to understand how all this works, verify its action over the infinitesimal generator proc (f) options operator, arrow; diff(f, a)-(diff(f, u))/b end proc

ChangeSymmetry({a = alpha, b = beta, q = chi, t = tau, x = xi, u(x, t, a, b, q) = (upsilon(xi, tau, alpha, beta, chi)*beta-alpha)/beta}, proc (f) options operator, arrow; diff(f, a)-(diff(f, u))/b end proc, [upsilon(xi, tau, alpha, beta, chi), xi, tau, alpha, beta, chi])

proc (f) options operator, arrow; diff(f, alpha) end proc


Now that we see the transformation (17) is the one we want, just use it to change variables in pde__xtabq

PDEtools:-dchange({a = alpha, b = beta, q = chi, t = tau, x = xi, u(x, t, a, b, q) = (upsilon(xi, tau, alpha, beta, chi)*beta-alpha)/beta}, pde__xtabq, [upsilon(xi, tau, alpha, beta, chi), xi, tau, alpha, beta, chi], simplify)

upsilon(xi, tau, alpha, beta, chi)*(diff(upsilon(xi, tau, alpha, beta, chi), xi))*beta+chi*(diff(diff(diff(upsilon(xi, tau, alpha, beta, chi), xi), xi), xi))+diff(upsilon(xi, tau, alpha, beta, chi), tau) = 0


As expected, this result depends only on two parameters, beta, and chi, and the one equivalent to a (that is alpha, see the transformation used (17)), is not present anymore.

To remove b or q we use the same steps, (15), (17) and (19), just changing the parameter to be removed, indicated as the last argument  in the call to CanonicalCoordinates . For example, to eliminate b (represented in the new variables by beta), input

CanonicalCoordinates(S, [u(x, t, a, b, q)], [upsilon(xi, tau, alpha, beta, chi)], b)

{alpha = b, beta = a, chi = q, tau = t, xi = x, upsilon(xi, tau, alpha, beta, chi) = (b*u(x, t, a, b, q)+a)/b}


solve({alpha = b, beta = a, chi = q, tau = t, xi = x, upsilon(xi, tau, alpha, beta, chi) = (b*u(x, t, a, b, q)+a)/b}, {a, b, q, t, x, u(x, t, a, b, q)})

{a = beta, b = alpha, q = chi, t = tau, x = xi, u(x, t, a, b, q) = (upsilon(xi, tau, alpha, beta, chi)*alpha-beta)/alpha}


PDEtools:-dchange({a = beta, b = alpha, q = chi, t = tau, x = xi, u(x, t, a, b, q) = (upsilon(xi, tau, alpha, beta, chi)*alpha-beta)/alpha}, pde__xtabq, [upsilon(xi, tau, alpha, beta, chi), xi, tau, alpha, beta, chi], simplify)

upsilon(xi, tau, alpha, beta, chi)*(diff(upsilon(xi, tau, alpha, beta, chi), xi))*alpha+chi*(diff(diff(diff(upsilon(xi, tau, alpha, beta, chi), xi), xi), xi))+diff(upsilon(xi, tau, alpha, beta, chi), tau) = 0


and as expected this result does not contain "beta. "To remove a second parameter, the whole cycle is repeated starting with computing infinitesimals, for instance for (22). Finally, the case of function parameters is treated analogously, by considering the function parameters as additional dependent variables instead of independent ones.



Download How_to_split_symmetries_into_cases_(II).mw

Edgardo S. Cheb-Terrab
Physics, Differential Equations and Mathematical Functions, Maplesoft

Integral Transforms (revamped) and PDEs


Integral transforms, implemented in Maple as the inttrans  package, are special integrals that appear frequently in mathematical-physics and that have remarkable properties. One of the main uses of integral transforms is for the computation of exact solutions to ordinary and partial differential equations with initial/boundary conditions. In Maple, that functionality is implemented in dsolve/inttrans  and in pdsolve/boundary conditions .


During the last months, we have been working heavily on several aspects of these integral transform functions and this post is about that. This is work in progress, in collaboration with Katherina von Bulow


The integral transforms are represented by the commands of the inttrans  package:


[addtable, fourier, fouriercos, fouriersin, hankel, hilbert, invfourier, invhilbert, invlaplace, invmellin, laplace, mellin, savetable, setup]


Three of these commands, addtable, savetable, and setup (this one is new, only present after installing the Physics Updates) are "administrative" commands while the others are computational representations for integrals. For example,

FunctionAdvisor(integral_form, fourier)

[fourier(a, b, z) = Int(a/exp(I*b*z), b = -infinity .. infinity), MathematicalFunctions:-`with no restrictions on `(a, b, z)]


FunctionAdvisor(integral_form, mellin)

[mellin(a, b, z) = Int(a*b^(z-1), b = 0 .. infinity), MathematicalFunctions:-`with no restrictions on `(a, b, z)]


For all the integral transform commands, the first argument is the integrand, the second one is the dummy integration variable of a definite integral and the third one is the evaluation point. (also called transform variable). The integral representation is also visible using the convert network

laplace(f(t), t, s); % = convert(%, Int)

laplace(f(t), t, s) = Int(f(t)*exp(-s*t), t = 0 .. infinity)


Having in mind the applications of these integral transforms to compute integrals and exact solutions to PDE with boundary conditions, five different aspects of these transforms received further development:


Compute Derivatives: Yes or No


Numerical Evaluation


Two Hankel Transform Definitions


More integral transform results


Mellin and Hankel transform solutions for Partial Differential Equations with boundary conditions

The project includes having all these tranforms available at user level (not ready), say as FourierTransform for inttrans:-fourier, so that we don't need to input with(inttrans) anymore. Related to these changes we also intend to have Heaviside(0) not return undefined anymore, and return itself instead, unevaluated, so that one can set its value according to the problem/preferred convention (typically 0, 1/2 or 1) and have all the Maple library following that choice.

The material presented in the following sections is reproducible already in Maple 2019 by installing the latest Physics Updates (v.435 or higher),

Compute derivatives: Yes or No.


For historical reasons, previous implementations of these integral transform commands did not follow a standard paradigm of computer algebra: "Given a function f(x), the input diff(f(x), x) should return the derivative of f(x)". The implementation instead worked in the opposite direction: if you were to input the result of the derivative, you would receive the derivative representation. For example, to the input laplace(-t*f(t), t, s) you would receive d*laplace(f(t), t, s)/ds. This is particularly useful for the purpose of using integral transforms to solve differential equations but it is counter-intuitive and misleading; Maple knows the differentiation rule of these functions, but that rule was not evident anywhere. It was not clear how to compute the derivative (unless you knew the result in advance).


To solve this issue, a new command, setup, has been added to the package, so that you can set "whether or not" to compute derivatives, and the default has been changed to computederivatives = true while the old behavior is obtained only if you input setup(computederivatives = false). For example, after having installed the Physics Updates,


`The "Physics Updates" version in the MapleCloud is 435 and is the same as the version installed in this computer, created 2019, October 1, 12:46 hours, found in the directory /Users/ecterrab/maple/toolbox/2019/Physics Updates/lib/`


the current settings can be queried via


computederivatives = true


and so differentiating returns the derivative computed

(%diff = diff)(laplace(f(t), t, s), s)

%diff(laplace(f(t), t, s), s) = -laplace(f(t)*t, t, s)


while changing this setting to work as in previous releases you have this computation reversed: you input the output (1.3) and you get the corresponding input

setup(computederivatives = false)

computederivatives = false


%diff(laplace(f(t), t, s), s) = -laplace(t*f(t), t, s)

%diff(laplace(f(t), t, s), s) = diff(laplace(f(t), t, s), s)


Reset the value of computederivatives

setup(computederivatives = true)

computederivatives = true


%diff(laplace(f(t), t, s), s) = -laplace(t*f(t), t, s)

%diff(laplace(f(t), t, s), s) = -laplace(f(t)*t, t, s)


In summary: by default, derivatives of integral transforms are now computed; if you need to work with these derivatives as in  previous releases, you can input setup(computederivatives = false). This setting can be changed any time you want within one and the same Maple session, and changing it does not have any impact on the performance of intsolve, dsolve and pdsolve to solve differential equations using integral transforms.


Numerical Evaluation


In previous releases, integral transforms had no numerical evaluation implemented. This is in the process of changing. So, for example, to numerically evaluate the inverse laplace transform ( invlaplace  command), three different algorithms have been implemented: Gaver-Stehfest, Talbot and Euler, following the presentation by Abate and Whitt, "Unified Framework for Numerically Inverting Laplace Transforms", INFORMS Journal on Computing 18(4), pp. 408–421, 2006.


For example, consider the exact solution to this partial differential equation subject to initial and boundary conditions

pde := diff(u(x, t), x) = 4*(diff(u(x, t), t, t))

iv := u(x, 0) = 0, u(0, t) = 1


Note that these two conditions are not entirely compatible: the solution returned cannot be valid for x = 0 and t = 0 simultaneously. However, a solution discarding that point does exist and is given by

sol := pdsolve([pde, iv])

u(x, t) = -invlaplace(exp(-(1/2)*s^(1/2)*t)/s, s, x)+1


Verifying the solution, one condition remains to be tested

pdetest(sol, [pde, iv])

[0, 0, -invlaplace(exp(-(1/2)*s^(1/2)*t)/s, s, 0)]


Since we now have numerical evaluation rules, we can test that what looks different from 0 in the above is actually 0.

zero := [0, 0, -invlaplace(exp(-(1/2)*s^(1/2)*t)/s, s, 0)][-1]

-invlaplace(exp(-(1/2)*s^(1/2)*t)/s, s, 0)


Add a small number to the initial value of t to skip the point t = 0

plot(zero, t = 0+10^(-10) .. 1)


The default method used is the method of Euler sums and the numerical evaluation is performed as usual using the evalf command. For example, consider

F := sin(sqrt(2*t))


The Laplace transform of F is given by

LT := laplace(F, t, s)



and the inverse Laplace transform of LT in inert form is

ILT := %invlaplace(LT, s, t)

%invlaplace((1/2)*2^(1/2)*Pi^(1/2)*exp(-(1/2)/s)/s^(3/2), s, t)


At t = 1 we have

eval(ILT, t = 1)

%invlaplace((1/2)*2^(1/2)*Pi^(1/2)*exp(-(1/2)/s)/s^(3/2), s, 1)


evalf(%invlaplace((1/2)*2^(1/2)*Pi^(1/2)*exp(-(1/2)/s)/s^(3/2), s, 1))



This result is consistent with the one we get if we first compute the exact form of the inverse Laplace transform at t = 1:

%invlaplace((1/2)*2^(1/2)*Pi^(1/2)*exp(-(1/2)/s)/s^(3/2), s, 1) = value(%invlaplace((1/2)*2^(1/2)*Pi^(1/2)*exp(-(1/2)/s)/s^(3/2), s, 1))

%invlaplace((1/2)*2^(1/2)*Pi^(1/2)*exp(-(1/2)/s)/s^(3/2), s, 1) = sin(2^(1/2))


evalf(%invlaplace((1/2)*2^(1/2)*Pi^(1/2)*exp(-(1/2)/s)/s^(3/2), s, 1) = sin(2^(1/2)))

.9877659460 = .9877659459


In addition to the standard use of evalf to numerically evaluate inverse Laplace transforms, one can invoke each of the three different methods implemented using the MathematicalFunctions:-Evalf  command

with(MathematicalFunctions, Evalf)



Evalf(%invlaplace((1/2)*2^(1/2)*Pi^(1/2)*exp(-(1/2)/s)/s^(3/2), s, 1), method = Talbot)



MF:-Evalf(%invlaplace((1/2)*2^(1/2)*Pi^(1/2)*exp(-(1/2)/s)/s^(3/2), s, 1), method = GaverStehfest)



MF:-Evalf(%invlaplace((1/2)*2^(1/2)*Pi^(1/2)*exp(-(1/2)/s)/s^(3/2), s, 1), method = Euler)



Regarding the method we use by default: from a numerical experiment with varied problems we have concluded that our implementation of the Euler (sums) method is faster and more accurate than the other two.


Two Hankel transform definitions


In previous Maple releases, the definition of the Hankel transform was given by

hankel(f(t), t, s, nu) = Int(f(t)*sqrt(s*t)*BesselJ(nu, s*t), t = 0 .. infinity)

where BesselJ(nu, s*t) is the BesselJ(nu, s*t) function. This definition, sometimes called alternative definition of the Hankel transform, has the inconvenience of the square root sqrt(s*t) in the integrand, complicating the form of the hankel transform for the Laplacian in cylindrical coordinates. On the other hand, the definition more frequently used in the literature is

 hankel(f(t), t, s, nu) = Int(f(t)*t*BesselJ(nu, s*t), t = 0 .. infinity)

With it, the Hankel transform of diff(u(r, t), r, r)+(diff(u(r, t), r))/r+diff(u(r, t), t, t) is given by the simple ODE form d^2*`&Hopf;`(k, t)/dt^2-k^2*`&Hopf;`(k, t). Not just this transform but several other ones acquire a simpler form with the definition that does not have a square root in the integrand.

So the idea is to align Maple with this simpler definition, while keeping the previous definition as an alternative. Hence, by default, when you load the inttrans package, the new definition in use for the Hankel transform is

hankel(f(t), t, s, nu); % = convert(%, Int)

hankel(f(t), t, s, nu) = Int(f(t)*t*BesselJ(nu, s*t), t = 0 .. infinity)


You can change this default so that Maple works with the alternative definition as in previous releases.  For that purpose, use the new inttrans:-setup command (which you can also use to query about the definition in use at any moment):


alternativehankeldefinition = false


This change in definition is automatically taken into account by other parts of the Maple library using the Hankel transform. For example, the differentiation rule with the new definition is

(%diff = diff)(hankel(f(t), t, z, nu), z)

%diff(hankel(f(t), t, z, nu), z) = -hankel(t*f(t), t, z, nu+1)+nu*hankel(f(t), t, z, nu)/z


This differentiation rule resembles (is connected to) the differentiation rule for BesselJ, and this is another advantage of the new definition.

(%diff = diff)(BesselJ(nu, z), z)

%diff(BesselJ(nu, z), z) = -BesselJ(nu+1, z)+nu*BesselJ(nu, z)/z


Furthermore, several transforms have acquired a simpler form, as for example:

`assuming`([(%hankel = hankel)(exp(I*a*r)/r, r, k, 0)], [a > 0, k < a])

%hankel(exp(I*a*r)/r, r, k, 0) = 1/(-a^2+k^2)^(1/2)


Let's compare: make the definition be as in previous releases.

setup(alternativehankeldefinition = true)

alternativehankeldefinition = true


hankel(f(t), t, s, nu); % = convert(%, Int)

hankel(f(t), t, s, nu) = Int(f(t)*(s*t)^(1/2)*BesselJ(nu, s*t), t = 0 .. infinity)


The differentiation rule with the previous (alternative) definition was not as simple:

(%diff = diff)(hankel(f(t), t, s, nu), s)

%diff(hankel(f(t), t, s, nu), s) = -hankel(t*f(t), t, s, nu+1)+nu*hankel(f(t), t, s, nu)/s+(1/2)*hankel(f(t), t, s, nu)/s


And the transform (3.5) was also not so simple:

`assuming`([(%hankel = hankel)(exp(I*a*r)/r, r, k, 0)], [a > 0, k < a])

%hankel(exp(I*a*r)/r, r, k, 0) = (I*a*hypergeom([3/4, 3/4], [3/2], a^2/k^2)*GAMMA(3/4)^4+Pi^2*k*hypergeom([1/4, 1/4], [1/2], a^2/k^2))/(k*Pi*GAMMA(3/4)^2)


Reset to the new default value of the definition.

setup(alternativehankeldefinition = false)

alternativehankeldefinition = false


hankel(f(t), t, s, nu); % = convert(%, Int)

hankel(f(t), t, s, nu) = Int(f(t)*t*BesselJ(nu, s*t), t = 0 .. infinity)


More integral transform results



The revision of the integral transforms includes also filling gaps: previous transforms that were not being computed are now computed. Still with the Hankel transform, consider the operators

`D/t` := proc (u) options operator, arrow; (diff(u, t))/t end proc
formula_plus := t^(-nu)*(`D/t`@@m)(t^(m+nu)*u(t))

formula_minus := t^nu*(`D/t`@@m)(t^(m-nu)*u(t))


Being able to transform these operators into algebraic expressions or differential equations of lower order is key for solving PDE problems with Boundary Conditions.


Consider, for instance, this ODE

setup(computederivatives = false)

computederivatives = false


simplify(eval(formula_minus, [nu = 6, m = 3]))

((diff(diff(diff(u(t), t), t), t))*t^3-12*(diff(diff(u(t), t), t))*t^2+57*(diff(u(t), t))*t-105*u(t))/t^3


Its Hankel transform is a simple algebraic expression

hankel(((diff(diff(diff(u(t), t), t), t))*t^3-12*(diff(diff(u(t), t), t))*t^2+57*(diff(u(t), t))*t-105*u(t))/t^3, t, s, 6)

-s^3*hankel(u(t), t, s, 3)


An example with formula_plus

simplify(eval(formula_plus, [nu = 7, m = 4]))

((diff(diff(diff(diff(u(t), t), t), t), t))*t^4+38*(diff(diff(diff(u(t), t), t), t))*t^3+477*(diff(diff(u(t), t), t))*t^2+2295*(diff(u(t), t))*t+3465*u(t))/t^4


hankel(((diff(diff(diff(diff(u(t), t), t), t), t))*t^4+38*(diff(diff(diff(u(t), t), t), t))*t^3+477*(diff(diff(u(t), t), t))*t^2+2295*(diff(u(t), t))*t+3465*u(t))/t^4, t, s, 7)

s^4*hankel(u(t), t, s, 11)


In the case of hankel , not just differential operators but also several new transforms are now computable

hankel(1, r, k, nu)

piecewise(nu = 0, Dirac(k)/k, nu/k^2)


hankel(r^m, r, k, nu)

piecewise(And(nu = 0, m = 0), Dirac(k)/k, 2^(m+1)*k^(-m-2)*GAMMA(1+(1/2)*m+(1/2)*nu)/GAMMA((1/2)*nu-(1/2)*m))



Mellin and Hankel transform solutions for Partial Differential Equations with Boundary Conditions


In previous Maple releases, the Fourier and Laplace transforms were used to compute exact solutions to PDE problems with boundary conditions. Now, Mellin and Hankel transforms are also used for that same purpose.



pde := x^2*(diff(u(x, y), x, x))+x*(diff(u(x, y), x))+diff(u(x, y), y, y) = 0

iv := u(x, 0) = 0, u(x, 1) = piecewise(0 <= x and x < 1, 1, 1 < x, 0)

sol := pdsolve([pde, iv])

u(x, y) = invmellin(sin(s*y)/(sin(s)*s), s, x)


As usual, you can let pdsolve choose the solving method, or indicate the method yourself:

pde := diff(u(r, t), r, r)+(diff(u(r, t), r))/r+diff(u(r, t), t, t) = -Q__0*q(r)
iv := u(r, 0) = 0

pdsolve([pde, iv])

u(r, t) = Q__0*(-hankel(exp(-s*t)*hankel(q(r), r, s, 0)/s^2, s, r, 0)+hankel(hankel(q(r), r, s, 0)/s^2, s, r, 0))


It is sometimes preferable to see these solutions in terms of more familiar integrals. For that purpose, use

convert(u(r, t) = Q__0*(-hankel(exp(-s*t)*hankel(q(r), r, s, 0)/s^2, s, r, 0)+hankel(hankel(q(r), r, s, 0)/s^2, s, r, 0)), Int, only = hankel)

u(r, t) = Q__0*(-(Int(exp(-s*t)*(Int(q(r)*r*BesselJ(0, r*s), r = 0 .. infinity))*BesselJ(0, r*s)/s, s = 0 .. infinity))+Int((Int(q(r)*r*BesselJ(0, r*s), r = 0 .. infinity))*BesselJ(0, r*s)/s, s = 0 .. infinity))


An example where the hankel transform is computable to the end:

pde := c^2*(diff(u(r, t), r, r)+(diff(u(r, t), r))/r) = diff(u(r, t), t, t)
iv := u(r, 0) = A*a/(a^2+r^2)^(1/2), (D[2](u))(r, 0) = 0

`assuming`([pdsolve([pde, iv], method = Hankel)], [r > 0, t > 0, a > 0])

u(r, t) = (1/2)*A*a*((-c^2*t^2+(2*I)*a*c*t+a^2+r^2)^(1/2)+(-c^2*t^2-(2*I)*a*c*t+a^2+r^2)^(1/2))/((-c^2*t^2-(2*I)*a*c*t+a^2+r^2)^(1/2)*(-c^2*t^2+(2*I)*a*c*t+a^2+r^2)^(1/2))




Download Integral_Transforms_(revamped).mw

Edgardo S. Cheb-Terrab
Physics, Differential Equations and Mathematical Functions, Maplesoft

Although the graph of a parametrized surface can be viewed and manipulated on the computer screen as a surface in 3D, it is not quite suitable for printing on a 3D printer since such a surface has zero thickness, and thus it does not correspond to physical object.

To produce a 3D printout of a surface, it needs to be endowed with some "thickness".  To do that, we move every point from the surface in the direction of that point's nomral vector by the amount ±T/2, where T is the desired thickness.  The locus of the points thus obtained forms a thin shell of thickness T around the original surface, thus making it into a proper solid. The result then may be saved into a file in the STL format and be sent to a 3D printner for reproduction.

The worksheet attached to this post provides a facility for translating a parametrized surface into an STL file.  It also provides a command for viewing the thickened object on the screen.  The details are documented within that worksheet.

Here are a few samples.  Each sample is shown twice—one as it appears within Maple, and another as viewed by loading the STL file into MeshLab which is a free mesh viewing/manipulation software.


Here is the worksheet that produced these:



This post is closely related to the previous one  which presents the procedure  NumbrixPuzzle   that allows you to effectively solve these puzzles (the text of this procedure is also available in the worksheet below).  
This post is about generating these puzzles. To do this, we need the procedure  SerpentinePaths  (see below) , which allows us to generate a large number of serpentine paths in a matrix of a specified size, starting with a specified matrix element. Note that for a square matrix of the order  n , the number of such paths starting from [1,1] - position is the sequence .

The required parameter of  SerpentinePaths procedure is the list  S , which defines the dimensions of the matrix. The optional parameter is the list  P  - this is the position of the number 1 (by default P=[1,1] ).
As an example below, we generate 20 puzzles of size 6 by 6. In exactly the same way, we can generate the desired number of puzzles for matrices of other sizes.



SerpentinePaths:=proc(S::list, P::list:=[1,1])
local OneStep, A, m, F, B, T, a;

local s, L, B, T, k, l;

T:=table(); k:=0;
for l in L do
if l[1]>=1 and l[1]<=S[1] and l[2]>=1 and l[2]<=S[2] and A[op(l)]=0 then k:=k+1; B:=subsop(l=a+1,A);
T[k]:=B fi;
convert(T, list);
end proc;
A:=convert(Matrix(S[], {(P[])=1}), listlist);
B:=[$ 1..m];
F:=LM->ListTools:-FlattenOnce(map(OneStep, `if`(nops(LM)<=30000,LM,LM[-30000..-1])));
for a in B do
map(convert, T, Matrix);

end proc:

local A1, L, N, S, MS, OneStepLeft, OneStepRight, F1, F2, m, L1, p, q, a, b, T, k, s1, s, H, n, L2, i, j, i1, j1, R;
uses ListTools;
S:=upperbound(A); N:=nops(op(A)[3]); MS:=`*`(S);
A1:=convert(A, listlist);
for i from 1 to S[1] do
for j from 1 to S[2] do
for i1 from i to S[1] do
for j1 from 1 to S[2] do
if A1[i,j]<>0 and A1[i1,j1]<>0 and abs(A1[i,j]-A1[i1,j1])<abs(i-i1)+abs(j-j1) then return `no solutions` fi;
od; od; od; od;
L:=sort(select(e->e<>0, Flatten(A1)));
L1:=[`if`(L[1]>1,seq(L[1]-k, k=0..L[1]-2),NULL)];
L2:=[seq(seq(`if`(L[i+1]-L[i]>1,L[i]+k,NULL),k=0..L[i+1]-L[i]-2), i=1..nops(L)-1), `if`(L[-1]<MS,seq(L[-1]+k,k=0..MS-L[-1]-1),NULL)];
local s, M, m, k, T;
uses ListTools;
s:=Search(a, Matrix(A1));   
T:=table(); k:=0;
for m in M do
if m[1]>=1 and m[1]<=S[1] and m[2]>=1 and m[2]<=S[2] and A1[op(m)]=0 then k:=k+1; T[k]:=subsop(m=a-1,A1);
convert(T, list);
end proc;
local s, M, m, k, T, s1;
uses ListTools;
s:=Search(a, Matrix(A1));  s1:=Search(a+2, Matrix(A1));  
T:=table(); k:=0;
for m in M do
if m[1]>=1 and m[1]<=S[1] and m[2]>=1 and m[2]<=S[2] and A1[op(m)]=0 and `if`(a+2 in L, `if`(is(abs(s1[1]-m[1])+abs(s1[2]-m[2])>1),false,true),true) then k:=k+1; T[k]:=subsop(m=a+1,A1);
convert(T, list);   
end proc;
F1:=LM->ListTools:-FlattenOnce(map(OneStepLeft, LM));
F2:=LM->ListTools:-FlattenOnce(map(OneStepRight, LM));
for a in L1 do
for a in L2 do
R:=map(t->convert(t,Matrix), T);
if nops(R)=0 then return `no solutions` else R fi;
end proc:

Simple examples

SerpentinePaths([3,3]);  # All the serpentine paths for the matrix  3x3, starting with [1,1]-position
SerpentinePaths([3,3],[1,2]);  # No solutions if the start with [1,2]-position
SerpentinePaths([4,4]):  # All the serpentine paths for the matrix  4x4, starting with [1,1]-position
nops(SerpentinePaths([4,4],[1,2]));  # The number of all the serpentine paths for the matrix  4x4, starting with [1,2]-position
nops(SerpentinePaths([4,4],[2,2]));  # The number of all the serpentine paths for the matrix  4x4, starting with [2,2]-position

[Matrix(3, 3, {(1, 1) = 1, (1, 2) = 6, (1, 3) = 7, (2, 1) = 2, (2, 2) = 5, (2, 3) = 8, (3, 1) = 3, (3, 2) = 4, (3, 3) = 9}), Matrix(3, 3, {(1, 1) = 1, (1, 2) = 8, (1, 3) = 7, (2, 1) = 2, (2, 2) = 9, (2, 3) = 6, (3, 1) = 3, (3, 2) = 4, (3, 3) = 5}), Matrix(3, 3, {(1, 1) = 1, (1, 2) = 8, (1, 3) = 9, (2, 1) = 2, (2, 2) = 7, (2, 3) = 6, (3, 1) = 3, (3, 2) = 4, (3, 3) = 5}), Matrix(3, 3, {(1, 1) = 1, (1, 2) = 4, (1, 3) = 5, (2, 1) = 2, (2, 2) = 3, (2, 3) = 6, (3, 1) = 9, (3, 2) = 8, (3, 3) = 7}), Matrix(3, 3, {(1, 1) = 1, (1, 2) = 2, (1, 3) = 9, (2, 1) = 4, (2, 2) = 3, (2, 3) = 8, (3, 1) = 5, (3, 2) = 6, (3, 3) = 7}), Matrix(3, 3, {(1, 1) = 1, (1, 2) = 2, (1, 3) = 3, (2, 1) = 8, (2, 2) = 7, (2, 3) = 4, (3, 1) = 9, (3, 2) = 6, (3, 3) = 5}), Matrix(3, 3, {(1, 1) = 1, (1, 2) = 2, (1, 3) = 3, (2, 1) = 8, (2, 2) = 9, (2, 3) = 4, (3, 1) = 7, (3, 2) = 6, (3, 3) = 5}), Matrix(3, 3, {(1, 1) = 1, (1, 2) = 2, (1, 3) = 3, (2, 1) = 6, (2, 2) = 5, (2, 3) = 4, (3, 1) = 7, (3, 2) = 8, (3, 3) = 9})]










Below we find 12,440 serpentine paths in the matrix  6x6 starting from various positions (the set  L )

k:=0:  n:=6:
for i from 1 to n do
for j from i to n do
k:=k+1; S[k]:=SerpentinePaths([n,n],[i,j])[];
od: od:
L1:={seq(S[i][], i=1..k)}:
L2:=map(A->A^%T, L1):
L:=L1 union L2:



Further, using the list  L, we generate 20 examples of Numbrix puzzles with the unique solutions

M:=[seq(L[i], i=combinat:-randcomb(nops(L),N))]:
for i from 1 to N do
for k from floor(n^2/4) do
T[i]:=Matrix(n,{seq(op(M[i])[3][j], j=combinat:-randcomb(n^2,k))});
if nops(NumbrixPuzzle(T[i]))=1 then break; fi;
od:  od:
DocumentTools:-Tabulate(Matrix(4,5, (i,j)->T1[i,j]), fillcolor = "LightYellow", width=95):

The solutions of these puzzles

DocumentTools:-Tabulate(Matrix(4,5, (i,j)->NumbrixPuzzle(T1[i,j])[]), fillcolor = "LightYellow", width=95):


For some reason, these 20 examples and their solutions did not load here.

 Edit. I separately inserted these generated 20 puzzles as a picture:




We are currently in the process of updating the support FAQs at We’ve been working on updating the existing content for clarity, and have added several new articles already.


The majority of our FAQs are from questions people ask us in Technical Support at, but we’d also like to like to add content from other sources.

Since we have such a great community here at MaplePrimes, we wanted to reach out and ask if there are any articles or questions that you'd like to see added to our FAQ.


We look forward to hearing your feedback!

I experienced a significant obstacle while trying to generate independent random samples with Statistics:-Sample on different nodes of a Grid multi-processing environment. After many hours of trial-and-error, I discovered an astonishing workaround, and I achieved excellent time and memory performance. Since this seems like a generally useful computation, I thought that it was worthy of a Post.

This Post is also worth reading to learn how to use Grid when you need to initialize a substantial environment on each node before using Grid:-Map or Grid:-Seq.

All remaining details are in the following worksheet.

How to use Statistics:-Sample in the `Grid` environment

Author: Carl Love <> 1 August 2019


I experienced a significant obstacle while trying to generate indenpendent random samples with Statistics:-Sample on the nodes of a multi-processor Grid (on a single computer). After several hours of trial-and-error, I discovered that two things are necessary to do this:


The random number generator needs to be seeded differently in each node. (The reason for this is easy to understand.)


The random variables generated by Statistics:-RandomVariable need to have different names in each node. This one is mind-boggling to me. Afterall, each node has its own kernel and so its own memory It's as if the names of random variables are stored in a disk file which all kernels access. And also the generator has been seeded differently in each node.


Once these things were done, the time and memory performance of the computation were excellent.


Digits:= 15

#Specify the size of the computation:
(n1,n2,n3):= (100, 100, 1000):
# n1 = size of each random sample;
# n2 = number of samples in a batch;
# n3 = number of batches.

#Procedure to initialize needed globals on each node:
Init:= proc(n::posint)
local node:= Grid:-MyNode();
   #This is wrapped in parse so that it'll act globally. Otherwise, an environment
   #variable would be reset when this procedure ends.
   parse("Digits:= 15;", 'statement');

   randomize(randomize()+node); #Initialize independent RNG for this node.
   #If repeatability of results is desired, remove the inner randomize().

   (:-X,:-Y):= Array(1..n, 'datatype'= 'hfloat') $ 2;

   #Perhaps due to some oversight in the design of Statistics, it seems necessary that
   #r.v.s in different nodes **need different names** in order to be independent:
   N||node:= Statistics:-RandomVariable('Normal'(0,1));
   :-TRS:= (X::rtable)-> Statistics:-Sample(N||node, X);
   #To verify that different names are needed, change N||node to N in both lines.
   #Doing so, each node will generate identical samples!

   #Perform some computation. For the pedagogical purpose of this worksheet, all that
   #matters is that it's some numeric computation on some Arrays of random Samples.
   :-GG:= (X::Array, Y::Array)->
         proc(X::Array, Y::Array, n::posint)
         local s, k, S:= 0, p:= 2*Pi;
            for k to n do
               s:= sin(p*X[k]);  
               S:= S + X[k]^2*cos(p*Y[k])/sqrt(2-sin(s)) + Y[k]^2*s
         end proc
         (X, Y, n)
   #Perform a batch of the above computations, and somehow numerically consolidate the
   #results. Once again, pedagogically it doesn't matter how they're consolidated.  
   :-TRX1:= (n::posint)-> add(GG(TRS(X), TRS(Y)), 1..n);
   #It doesn't matter much what's returned. Returning `node` lets us verify that we're
   #actually running this on a grid.
   return node
end proc

The procedure Init above uses the :- syntax to set variables globally for each node. The variables set are X, Y, N||node, TRS, GG, and TRX1. Names constructed by concatenation, such as N||node, are always global, so :- isn't needed for those.

#Time the initialization:
st:= time[real]():
   #Send Init to each node, but don't run it yet:
   #Run Init on each node:
   Nodes:= Grid:-Run(Init, [n1], 'wait');
time__init_Grid:= time[real]() - st;

Array(%id = 18446745861500764518)


The only purpose of array Nodes is that it lets us count the nodes, and it lets us verify that Grid:-MyNode() returned a different value on each node.

num_nodes:= numelems(Nodes);


#Time the actual execution:
st:= time[real]():
   R1:= [Grid:-Seq['tasksize'= iquo(n3, num_nodes)](TRX1(k), k= [n2 $ n3])]:
time__run_Grid:= time[real]() - st


#Just for comparison, run it sequentially:
st:= time[real]():
time__init_noGrid:= time[real]() - st;

st:= time[real]():
   R2:= [seq(TRX1(k), k= [n2 $ n3])]:
time__run_noGrid:= time[real]() - st;



R1 and R2 will be different because different random numbers were used, but they should have similar histograms.

      <R1 | R2>, #side-by-side plots
      'title'=~ <<"With Grid\n"> | <"Without Grid\n">>,
      'gridlines'= false

(Plot output deleted because MaplePrimes cannot handle side-by-side plots!)

They look similar enough to me!


Let's try to quantify the benefit of using Grid:

speedup_factor:= time__run_noGrid / time__run_Grid;


Express that as a fraction of the theoretical maximum speedup:

efficiency:= speedup_factor / num_nodes;


I think that that's really good!


The memory usage of this code is insignificant, which can be verified from an external memory monitor such as Winodws Task Manager. It's just a little bit more than that needed to start a kernel on each node. It's also possible to measure the memory usage programmatically. Doing so for a Grid:-Seq computation is a little bit beyond the scope of this worksheet.




Here are the histograms:


Animated 3-D cascade of dolls




We occasionally get asked questions about methods of Perturbation Theory in Maple, including the Lindstedt-Poincaré Method. Presented here is the most famous application of this method.


During the dawn of the 20th Century, one problem that bothered astronomers and astrophysicists was the precession of the perihelion of Mercury. Even when considering the gravity from other planets and objects in the solar system, the equations from Newtonian Mechanics could not account for the discrepancy between the observed and predicted precession.

One of the early successes of Einstein's General Theory of Relativity was that the new model was able to capture the precession of Mercury, in addition to the orbits of all the other planets. The Einsteinian model, when applied to the orbit of Mercury, was in fact a non-negligible perturbation of the old model. In this post, we show how to use Maple to compute the perturbation, and derive the formula for calculating the precession.

In polar coordinates, the Einsteinian model can be written in the following form, where u(theta)=a(1-e^2)/r(theta), with theta being the polar angle, r(theta) being the radial distance, a being the semi-major axis length, and e being the eccentricity of the orbit:

# Original system.
f := (u,epsilon) -> -1 - epsilon * u^2;
omega := 1;
u0, du0 := 1 + e, 0;
de1 := diff( u(theta), theta, theta ) + omega^2 * u(theta) + f( u(theta), epsilon );
ic1 := u(0) = u0, D(u)(0) = du0;

The small parameter epsilon (along with the amount of precession) can be found in terms of the physical constants, but for now we leave it arbitrary:

# Parameters.
P := [
    a = 5.7909050e10 * Unit(m),
    c = 2.99792458e8 * Unit(m/s),
    e = 0.205630,
    G = 6.6740831e-11 * Unit(N*m^2/kg^2), 
    M = 1.9885e30 * Unit(kg), 
    alpha = 206264.8062, 
    beta = 415.2030758 
epsilon = simplify( eval( 3 * G * M / a / ( 1 - e^2 ) / c^2, P ) ); # approximately 7.987552635e-8

Note that c is the speed of light, G is the gravitational constant, M is the mass of the sun, alpha is the number of arcseconds per radian, and beta is the number of revolutions per century for Mercury.

We will show that the radial distance, predicted by Einstein's model, is close to that for an ellipse, as predicted by Newton's model, but the perturbation accounts for the precession (42.98 arcseconds/century). During one revolution, the precession can be determined to be approximately

sigma = simplify( eval( 6 * Pi * G * M / a / ( 1 - e^2 ) / c^2, P ) ); # approximately 5.018727337e-7

and so, per century, it is alpha*beta*sigma, which is approximately 42.98 arcseconds/century.
It is worth checking out this question on Stack Exchange, which includes an animation generated numerically using Maple for a similar problem that has a more pronounced precession.

Lindstedt-Poincaré Method in Maple

In order to obtain a perturbation solution to the perturbed differential equation u'+omega^2*u=1+epsilon*u^2 which is periodic, we need to write both u and omega as a series in the small parameter epsilon. This is because otherwise, the solution would have unbounded oscillatory terms ("secular terms"). Using this Lindstedt-Poincaré Method, we substitute arbitrary series in epsilon for u and omega into the initial value problem, and then choose the coefficient constants/functions so that both the initial value problem is satisfied and there are no secular terms. Note that a first-order approximation provides plenty of agreement with the measured precession, but higher-order approximations can be obtained.

To perform this in Maple, we can do the following:

# Transformed system, with the new independent variable being the original times a series in epsilon.
de2 := op( PDEtools:-dchange( { theta = phi/b }, { de1 }, { phi }, params = { b, epsilon }, simplify = true ) );
ic2 := ic1;

# Order and series for the perturbation solutions of u(phi) and b. Here, n = 1 is sufficient.
n := 1;
U := unapply( add( p[k](phi) * epsilon^k, k = 0 .. n ), phi );
B := omega + add( q[k] * epsilon^k, k = 1 .. n );

# DE in terms of the series.
de3 := series( eval( de2, [ u = U, b = B ] ), epsilon = 0, n + 1 );

# Successively determine the coefficients p[k](phi) and q[k].
for k from 0 to n do

    # Specify the initial conditions for the kth DE, which involves p[k](phi).
    # The original initial conditions appear only in the coefficient functions with index k = 0,
    # and those for k > 1 are all zero.
    if k = 0 then
        ic3 := op( expand( eval[recurse]( [ ic2 ], [ u = U, epsilon = 0 ] ) ) );
        ic3 := p[k](0), D(p[k])(0);
    end if:

    # Solve kth DE, which can be found from the coefficients of the powers of epsilon in de3, for p[k](phi).
    # Then, update de3 with the new information.
    soln := dsolve( { simplify( coeff( de3, epsilon, k ) ), ic3 } );
    p[k] := unapply( rhs( soln ), phi );
    de3 := eval( de3 );

    # Choose q[k] to eliminate secular terms. To do this, use the frontend() command to keep only the terms in p[k](phi)
    # which have powers of t, and then solve for the value of q[k] which makes the expression zero. 
    # Note that frontend() masks the t-dependence within the sine and cosine terms.
    # Note also that this method may need to be amended, based on the form of the terms in p[k](phi).
    if k > 0 then
        q[1] := solve( frontend( select, [ has, p[k](phi), phi ] ) = 0, q[1] );
        de3 := eval( de3 );
    end if;

end do:

# Final perturbation solution.
'u(theta)' = eval( eval( U(phi), phi = B * theta ) ) + O( epsilon^(n+1) );

# Angular precession in one revolution.
sigma := convert( series( 2 * Pi * (1/B-1), epsilon = 0, n + 1 ), polynom ):
epsilon := 3 * G * M / a / ( 1 - e^2 ) / c^2;
'sigma' = sigma;

# Precession per century.
xi := simplify( eval( sigma * alpha * beta, P ) ); # returns approximately 42.98

Maple Worksheet:


Solving a Numbrix Puzzle with Logic




Parade magazine, a filler in the local Sunday newspaper, contains a Numbrix puzzle, the object of which is to find a serpentine path of consecutive integers, 1 through 81, through a nine by nine grid.  The puzzle typically specifies the content of every other border cell.


The Maple Logic  package has a procedure, Satisfy , that can be used to solve this puzzle.  Satisfy is a SAT-solver; given a boolean expression it attempts to find a set of equations of the form {x__1 = b__1, x__2 = b__2, () .. ()}, where x__i are the boolean variables in the given expression and b__i are boolean values (true or false) that satisfy the expression (cause it to evaluate true).


A general technique to solve this and other puzzles with Satisfy is to select boolean-values variables that encode the state of the puzzle (a trial solution, whether valid or not), generate a boolean-expression of these variables that is satisfied (true) if and only if the variables are given values that correspond to a solution, pass this expression to Satisfy, then translate the returned set of boolean values (if any) to the puzzle solution.



Assign a matrix that defines the grid and the initial position.  Use zeros to indicate the cells that need values. To make it easy to inspect the expressions, a small 2 x 3 matrix is used for this demo---a full size example is given at the end.

M := Matrix(2,3, {(1,1) = 1, (1,3) = 5});

Matrix(2, 3, {(1, 1) = 1, (1, 2) = 0, (1, 3) = 5, (2, 1) = 0, (2, 2) = 0, (2, 3) = 0})



Extract the dimensions of the Matrix

(m,n) := upperbound(M);

2, 3


Boolean Variables


Let the boolean variable x[i,j,k] mean that cell (i,j) has value k. For example, x[2,3,6] is true when cell (2,3) contains 6, otherwise it is false. There are (m*n)^2 boolean variables.

Initial Position


The initial position can be expressed as the following and-clause.

initial := &and(seq(seq(ifelse(M[i,j] = 0, NULL, x[i,j,M[i,j]]), i = 1..m), j = 1..n));

`&and`(x[1, 1, 1], x[1, 3, 5])


Adjacent Cells


The requirement that an interior cell with value k is adjacent to the cell with value k+1 can be expressed as the implication

   x[i,j,k] &implies &or(x[i-1,j,k+1], x[i+1,j,k+1], x[i,j-1,k+1], x[i,j+1,k+1])


Extending this to handle all cells results in the following boolean expression.

adjacent := &and(seq(seq(seq(
         x[i,j,k] &implies &or(NULL
                               , ifelse(1<i, x[i-1, j, k+1], NULL)
                               , ifelse(i<m, x[i+1, j, k+1], NULL)
                               , ifelse(1<j, x[i, j-1, k+1], NULL)
                               , ifelse(j<n, x[i, j+1, k+1], NULL)
                            , i = 1..m)
                        , j = 1..n)
                    , k = 1 .. m*n-1));

`&and`(`&implies`(x[1, 1, 1], `&or`(x[2, 1, 2], x[1, 2, 2])), `&implies`(x[2, 1, 1], `&or`(x[1, 1, 2], x[2, 2, 2])), `&implies`(x[1, 2, 1], `&or`(x[2, 2, 2], x[1, 1, 2], x[1, 3, 2])), `&implies`(x[2, 2, 1], `&or`(x[1, 2, 2], x[2, 1, 2], x[2, 3, 2])), `&implies`(x[1, 3, 1], `&or`(x[2, 3, 2], x[1, 2, 2])), `&implies`(x[2, 3, 1], `&or`(x[1, 3, 2], x[2, 2, 2])), `&implies`(x[1, 1, 2], `&or`(x[2, 1, 3], x[1, 2, 3])), `&implies`(x[2, 1, 2], `&or`(x[1, 1, 3], x[2, 2, 3])), `&implies`(x[1, 2, 2], `&or`(x[2, 2, 3], x[1, 1, 3], x[1, 3, 3])), `&implies`(x[2, 2, 2], `&or`(x[1, 2, 3], x[2, 1, 3], x[2, 3, 3])), `&implies`(x[1, 3, 2], `&or`(x[2, 3, 3], x[1, 2, 3])), `&implies`(x[2, 3, 2], `&or`(x[1, 3, 3], x[2, 2, 3])), `&implies`(x[1, 1, 3], `&or`(x[2, 1, 4], x[1, 2, 4])), `&implies`(x[2, 1, 3], `&or`(x[1, 1, 4], x[2, 2, 4])), `&implies`(x[1, 2, 3], `&or`(x[2, 2, 4], x[1, 1, 4], x[1, 3, 4])), `&implies`(x[2, 2, 3], `&or`(x[1, 2, 4], x[2, 1, 4], x[2, 3, 4])), `&implies`(x[1, 3, 3], `&or`(x[2, 3, 4], x[1, 2, 4])), `&implies`(x[2, 3, 3], `&or`(x[1, 3, 4], x[2, 2, 4])), `&implies`(x[1, 1, 4], `&or`(x[2, 1, 5], x[1, 2, 5])), `&implies`(x[2, 1, 4], `&or`(x[1, 1, 5], x[2, 2, 5])), `&implies`(x[1, 2, 4], `&or`(x[2, 2, 5], x[1, 1, 5], x[1, 3, 5])), `&implies`(x[2, 2, 4], `&or`(x[1, 2, 5], x[2, 1, 5], x[2, 3, 5])), `&implies`(x[1, 3, 4], `&or`(x[2, 3, 5], x[1, 2, 5])), `&implies`(x[2, 3, 4], `&or`(x[1, 3, 5], x[2, 2, 5])), `&implies`(x[1, 1, 5], `&or`(x[2, 1, 6], x[1, 2, 6])), `&implies`(x[2, 1, 5], `&or`(x[1, 1, 6], x[2, 2, 6])), `&implies`(x[1, 2, 5], `&or`(x[2, 2, 6], x[1, 1, 6], x[1, 3, 6])), `&implies`(x[2, 2, 5], `&or`(x[1, 2, 6], x[2, 1, 6], x[2, 3, 6])), `&implies`(x[1, 3, 5], `&or`(x[2, 3, 6], x[1, 2, 6])), `&implies`(x[2, 3, 5], `&or`(x[1, 3, 6], x[2, 2, 6])))



All Values Used


The following expression is true when each integer k, from 1 to m*n, is assigned to one or more cells.

allvals := &and(seq(seq(&or(seq(x[i,j,k], k=1..m*n)), i=1..m), j=1..n));

`&and`(`&or`(x[1, 1, 1], x[1, 1, 2], x[1, 1, 3], x[1, 1, 4], x[1, 1, 5], x[1, 1, 6]), `&or`(x[2, 1, 1], x[2, 1, 2], x[2, 1, 3], x[2, 1, 4], x[2, 1, 5], x[2, 1, 6]), `&or`(x[1, 2, 1], x[1, 2, 2], x[1, 2, 3], x[1, 2, 4], x[1, 2, 5], x[1, 2, 6]), `&or`(x[2, 2, 1], x[2, 2, 2], x[2, 2, 3], x[2, 2, 4], x[2, 2, 5], x[2, 2, 6]), `&or`(x[1, 3, 1], x[1, 3, 2], x[1, 3, 3], x[1, 3, 4], x[1, 3, 5], x[1, 3, 6]), `&or`(x[2, 3, 1], x[2, 3, 2], x[2, 3, 3], x[2, 3, 4], x[2, 3, 5], x[2, 3, 6]))


Single Value


The following expression is satisfied when each cell has no more than one value.

 single := &not &or(seq(seq(seq(seq(x[i,j,k] &and x[i,j,kk], kk = k+1..m*n), k = 1..m*n-1), i = 1..m), j = 1..n));