Maple 2019 Questions and Posts

These are Posts and Questions associated with the product, Maple 2019

I thought I remembed how to do this once in Maple, or asking something like this here, but may be it was something similar. But I am not able to figure it now or remember.

Given an expression, I want to find all occuranes of a pattern in it.  Not just one.  So this is like using patmatch but over and over again untill all patterns found. I'll give an example to make it easy to explain.

Given

expr := y^2*sin(1/y) + y^(3/2) + y + x*y^7;

I want to find all patterns of form y^n  so the result should be 

{y^(3/2), y^7, y^2, 1/y, y}

This below is how it is done in Mathematica, but having hard time translating this code to Maple.

The last line below does the actual repeated pattern matching. That line was not written by me. It is something from Mathematica forum at stackexchange and I use it all the time and it works well.

ClearAll[x,y,n]
expr = y^2*Sin[1/y] + y^(3/2) + y + x*y^7;
pat = y^n_.;
Last@Reap[expr /. a : pat :> Sow[a], _, Sequence @@ #2 &]

I looked at hastype also. But hastype will only tell me if the pattern is there or not. May be I did not use it right.

restart;
expr := y^2*sin(1/y) + y^(3/2) + y + x*y^7;
hastype(expr,symbol^(anything));

Gives true

I tried patmatch, but again, it only find one:

restart;
expr := y^2*sin(1/y) + y^(3/2) + y + x*y^7;
if patmatch(expr,a::anything+y^(n::anything)+b::anything,'la') then
   assign(la);
   n;
fi;

And the above is not even robust for finding one. Since I have to change the pattern again to account for multiplication/addition cases between terms. 

Is it possible to do in Maple the same thing I show in Mathematica? I am sure it is possible, but can't now find the right function in Maple.

Maple 2019.1

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 <carl.j.love@gmail.com> 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:

1. 

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

2. 

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.

restart
:

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)->
      evalhf(
         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
            od
         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:
   Grid:-Set(Init)
   ;
   #Run Init on each node:
   Nodes:= Grid:-Run(Init, [n1], 'wait');
time__init_Grid:= time[real]() - st;

Array(%id = 18446745861500764518)

1.109

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);

8

#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

4.440

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

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

0.16e-1

24.483

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

plots:-display(
   Statistics:-Histogram~(
      <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;

5.36319824753560

Express that as a fraction of the theoretical maximum speedup:

efficiency:= speedup_factor / num_nodes;

.670399780941950

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.

 


 

Download GridRandSample.mw

Here are the histograms:

I do not understand why the following is not working. I have a package which has a module inside it. The module is exported by the package.

Inside that module, there is a proc, which is also exported by the module.

So why can't one call the proc from outside the package? What Am I doing wrong? and how to correct it? I'd like to be able to call the proc directly. 

A:=module()
  option package;
  export B;
  B := module()
      export foo;

      foo:=proc()
           print("in foo");
      end proc;

  end module;
end module;   

Now when typing (A::B):-foo() Maple is not happy and says Error, module does not export `foo`

I tried different syntax from the above, but can't get it to work. For example A::B:-foo() gives Error, `B` does not evaluate to a module

 

Maple 2019.1

When making a proc that accepts positional and keyword based arguments, Maple allows the caller to put the keyword arguments before or after the positional arguments, or in between.

Is there a way to force the keyword arguments (if they are used) to only be placed after the (required) positional arguments during the call? 

An example will make it more clear. Given this proc

foo := proc(a::integer, b::integer, {c::integer:= 1, d::integer:= 1},$)
    print("a=",a," b=",b," c=",c," d=",d);
end proc:

It can be called as any one of these cases

foo(1,2);                                #case 1                                             
foo(1, 2,'c' = 1, 'd' = 2);              #case 2
foo('c' = 1, 1, 'd' = 2, 2);             #case 3
foo('c' = 1, 1, 2);                      #case 4
foo(1, 'c'=1, 2);                        #case 5
#more combinations possible

Is it possible to change how proc is defined, so that Maple will accept case1, and case 2 only in the above when the call is made and give an error otherwise?

i.e. only allow keyword arguments after all positional arguments.  I read https://www.maplesoft.com/support/help/Maple/view.aspx?path=argument_processing  and it says

After all arguments matching keyword parameters have been processed, matching of required positional and optional or expected ordered parameters is carried out

So from above, it looks like what I am asking for is not really possible. But I thought to ask, in case there is some way.

I find ability to put keyword arguments in the call anywhere and in-between required positional arguments confusing that is all.

Maple 2019.1

Is there a command or method in Maple to list all initially protected names?

This page https://www.maplesoft.com/support/help/Maple/view.aspx?path=UndocumentedNames list undocumented protected names, and this page https://www.maplesoft.com/support/help/Maple/view.aspx?path=initialconstants  lists initially known names, which I assume are all protected.

But what about a list of all protected names? sin,cos, eval, uneval, etc.. any name that can't be assigned to. In my search so far, I could not find how to find these names. There are 100's of such names. Can one get them all in a list to look at?

Using Maple 2019.1

Could someone confirm if this is a problem in this sample application?

In 2019, in the folder 

    C:\Program Files\Maple 2019\samples\ProgrammingGuide\RandomnessTests

There is sample application. Opening the main module file RandomnessTests.mpl  and scrolling down a little bit to the include statements show

$include "WaldWolfowitz.mm"
$include "BitFrequency.mm"
$include "SequenceFrequency.mm"
$include "Compressibility.mm"
$include "BinaryRank.mm"
$include "Entropy.mm"

$include "Data.mm"
$include "Visualization.mm"

However, in the same folder, there is no Data.mm file. There is only Data.mpl file.

So how could this sample application work?  

This sample app have more problems. The file Visualization.mm  say

Data := module()
option package;
...

And the file Data.mpl has

Data := module()
....

This is all so confusing. Seems someone made some typos and not tested this app? Why put a sample application which makes learning packages in Maple more confusing?

ps. I did not yet try to figure how to locad it and run it myself, I was just browsing it.

 

 

I have the following expression

I would like to know if is it possible avoid negative exponent goes to denominator when I use expand().

Thank you!

 

 

 

 

 

 

Maple always starts a new document with a 2D input style and Times New Roman font. How to change the boot style? (default).
Oliveira

I have to solve the equation rH''(r)+H'(r)+(rk^2-r^2*b^2/R^2)=0 where k, b, and R are real constant positive number, with condition H(R)=0 and H(1/R)=R to be solved into series of power. I know from the literature that xy''+y'+xy=0, can't be solved in terms of elementary function(see G.Nagy-ODE-November 29, 2017) that's why I'm interested in an approximate solution based on series, or any results as long as it satisfied the too condition H(R)=0 and H(1/R)=R of the real function H(r). 

Please advice!.

    I have encountered a peculiar behavior in Maple 2019 worksheets. I have attached a worksheet which illustrates a reversal of the coefficient and the blade in the following types of expression.  My Maple installation is set in options to use maple input and output (1D notation).  I am using build ID 1399874.

The procedure I was testing used the add function to expand a multivector over basis blades and coefficients represented by indexed names; for example
    add(a[indx[]]*e[indx[]], indx = indxes); where indxes:={[1],[2],[3],[1,2],[1,3],[1,4],[2,3],[2,4]};

The expected result
a[1]*e[1]+a[2]*e[2]+a[3]*e[3]+a[4]*e[1, 2]+a[5]*e[1, 3]+a[6]*e[1, 4]+a[7]*e[2, 3]+a[8]*e[2, 4] in 1D notation but instead I got

e[1]*xx[1]+e[2]*xx[2]+e[3]*xx[3]+e[1, 2]*xx[4]+e[1, 3]*xx[5]+e[1, 4]*xx[6]+ e[2, 3]*xx[7]+e[2, 4]*xx[8]

(Note and the different coefficient names illustrates the exchange is caused by sorting.)

Initially, I assumed that this resulted from the add command, but after I directly entered an indexed expression into the worksheet and obtained a similar result I realized it must be occurring during the output.  Apparently, before the expression is written, it is first sorted. I know this because, if the coefficient is named a,b,c,d ( less than e), the expression is not exchanged. In addition, if I convert the expression to 2D output, the expression is written in the normal order specified in the add command.

I also confirmed this behavior in Maple 2018.2 as well.  Hopefully, the example worksheet attached will illustrate this behavior.  Am I correct in assuming that when the code I am using in the worksheet with is incorporated into a module in an mpl file and loaded, this will not be an issue.

exchangeproblem.mw

Hello,

How I can extract coefficients from and by calculating determinant for Eigenvalue problem, the value of omega.

For more details please see attached PDF file.

Thanks so much.

eign.pdf

 

We solve Laplace's equation in the domain a < r and r < b, c < t and t < d
in polar coordinates subject to prescribed Dirichlet data.

Maple produces a solution in the form of an infinite sum,
but that solution fails to satisfy the boundary condition
on the domain's outer arc.  Is this a bug or am I missing
something?

restart;

kernelopts(version);

`Maple 2019.1, X86 64 LINUX, May 21 2019, Build ID 1399874`

with(plots):

pde := diff(u(r,t),r,r) + diff(u(r,t),r)/r + diff(u(r,t),t,t)/r^2 = 0;

diff(diff(u(r, t), r), r)+(diff(u(r, t), r))/r+(diff(diff(u(r, t), t), t))/r^2 = 0

a, b, c, d := 1, 2, Pi/6, Pi/2;

1, 2, (1/6)*Pi, (1/2)*Pi

bc := u(r,c)=c, u(r,d)=0, u(a,t)=0, u(b,t)=t;

u(r, (1/6)*Pi) = (1/6)*Pi, u(r, (1/2)*Pi) = 0, u(1, t) = 0, u(2, t) = t

We plot the boundary data on the domain's outer arc:

p1 := plots:-spacecurve([b*cos(t), b*sin(t), t], t=c..d, color=red, thickness=5);

Solve the PDE:

pdsol := pdsolve({pde, bc});

u(r, t) = Sum((1/6)*cos(3*signum(n1-1/4)*(-1+4*n1)*t)*(2*Pi*sin((1/2)*signum(n1-1/4)*Pi)*abs(n1-1/4)-6*Pi*sin((3/2)*signum(n1-1/4)*Pi)*abs(n1-1/4)+cos((3/2)*signum(n1-1/4)*Pi)-cos((1/2)*signum(n1-1/4)*Pi))*signum(n1-1/4)*8^(signum(n1-1/4)*(4*n1+1))*(r^((-3+12*n1)*signum(n1-1/4))-r^((3-12*n1)*signum(n1-1/4)))/(abs(n1-1/4)*Pi*(-1+4*n1)*(16777216^(signum(n1-1/4)*n1)-64^signum(n1-1/4))), n1 = 0 .. infinity)+Sum(-(1/3)*((-1)^n-1)*sin(n*Pi*ln(r)/ln(2))*(exp((1/6)*Pi*n*(Pi+6*t)/ln(2))-exp((1/6)*Pi*n*(7*Pi-6*t)/ln(2)))/(n*(exp((1/3)*n*Pi^2/ln(2))-exp(n*Pi^2/ln(2)))), n = 1 .. infinity)

Truncate the infinite sum at 20 terms, and plot the result:

eval(rhs(pdsol), infinity=20):
value(%):
p2 := plot3d([r*cos(t), r*sin(t), %], r=a..b, t=c..d);

Here is the combined plot of the solution and the boundary condition.
We see that the proposed solution completely misses the boundary condition.

plots:-display([p1,p2], orientation=[25,72,0]);


 

Download mw.mw

I spend some time searching and reading help. But not able to find if this is possible.

I use worksheet only (i.e. not 2D document). I have my display set as

 

I'd like diff(y(x),x) to display as y'(x) in output.

I know I can do this 

PDEtools:-declare(y(x), prime = x);

And that will make diff(y(x),x) display as y'  but I want y'(x). And the same for diff(y(x),x$2) to display as y''(x). And to be clear, y(x) will still display as y(x).  I am mainly interested in making the derivative display a little nicer if possible.

Is there a way to do this?

I am using 2019.1 on windows 10.

 

pde := (diff(u(r, theta), r) + r * diff(u(r, theta), r, r) + diff(u(r, theta), theta, theta) / r ) / r:
iv := u( 1, theta) = 0, u( 3, theta) = theta, u( r, 0) = 10, u( r, Pi/2) = 0:
           Maple 2019 returns a symbolic solution for PDE:
pdsolve([pde, iv], u(r, theta));
   But for the numeric option, it returns a message saying that Maple is unable to handle elliptical PDEs.
pdsolve(pde, {iv}, numeric, time = t, range = 1 .. 3);

Error, (in pdsolve/numeric) unable to handle elliptic PDEs
I found it strange.

Oliveira.

In answers given in 

In https://www.mapleprimes.com/questions/227546-How-To-Make-Odetest-Verify-Dsolve

It shows that odetest() did not verify a solution to ODE becuase solution was using hypergeom special functions. If the solution to the ODE was in integral form, then odetest() will verify it OK.

But what to do if the solution I want to verify is already in hypergoem? If I try odetest() it will fail to verify now. Then I can try to convert the solution to integral form and try again.

But when  using convert(sol,Int) followed by odetest() it did not work.

The solutions I try to verify are hand solutions or book solutions, and not coming from dsolve. 

But some of them are the same solution that comes from dsolve() when not using the useInt option. 

Also, I am doing this all inside a Maple program. It is not an interactive process. So I can't do plots and look at them to decide on anything. So verification must all be implemented in code.

The question is: Why did convert(hand_solution,Int) not give the same result as dsolve(ode,useInt)? Is there another way around this? (May be I am asking for too much in this one based on answers in the above link, So that is OK if not possible. But I really like the solution given when using "useInt" option. Much more clear than otherwise).
 

restart;

ode := diff(y(x), x)*(x^3 + 1)^(2/3) + (1 + y(x)^3)^(2/3) = 0;
sol_int:=dsolve(ode,useInt);
odetest(sol_int,ode); #OK now, since solution in integral form

(diff(y(x), x))*(x^3+1)^(2/3)+(1+y(x)^3)^(2/3) = 0

Int(1/(x^3+1)^(2/3), x)+Intat(1/(_a^3+1)^(2/3), _a = y(x))+_C1 = 0

0

hand_solution:= x*hypergeom([1/3, 2/3], [4/3], -x^3) + y(x)*hypergeom([1/3, 2/3], [4/3], -y(x)^3) + _C1 = 0;
convert(hand_solution,Int); #Why this did not give same result as ABOVE?

x*hypergeom([1/3, 2/3], [4/3], -x^3)+y(x)*hypergeom([1/3, 2/3], [4/3], -y(x)^3)+_C1 = 0

(2/9)*x*Pi*3^(1/2)*(Int(1/(_t1^(1/3)*(1-_t1)^(1/3)*(x^3*_t1+1)^(1/3)), _t1 = 0 .. 1))/GAMMA(2/3)^3+(2/9)*y(x)*Pi*3^(1/2)*(Int(1/(_t1^(1/3)*(1-_t1)^(1/3)*(y(x)^3*_t1+1)^(1/3)), _t1 = 0 .. 1))/GAMMA(2/3)^3+_C1 = 0

odetest(%,ode); #does not give zero

-y(x)^3*(1+y(x)^3)^(2/3)*(Int(_t1^(2/3)/((1-_t1)^(1/3)*(y(x)^3*_t1+1)^(4/3)), _t1 = 0 .. 1))+(x^3+1)^(2/3)*(Int(_t1^(2/3)/((1-_t1)^(1/3)*(x^3*_t1+1)^(4/3)), _t1 = 0 .. 1))*x^3-(x^3+1)^(2/3)*(Int(1/(_t1^(1/3)*(1-_t1)^(1/3)*(x^3*_t1+1)^(1/3)), _t1 = 0 .. 1))+(1+y(x)^3)^(2/3)*(Int(1/(_t1^(1/3)*(1-_t1)^(1/3)*(y(x)^3*_t1+1)^(1/3)), _t1 = 0 .. 1))

 

 

Maple 2019.1

Download 072619_2.mw

 

 

 

First 38 39 40 41 42 43 44 Page 40 of 47