Product Tips & Techniques

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

The presentation below is on undergrad Quantum Mechanics. Tackling this topic within a computer algebra worksheet in the way it's done below, however, is an exciting novelty and illustrates well the level of abstraction that is now possible using the Physics package.

 

Quantum Mechanics: Schrödinger vs Heisenberg picture

Pascal Szriftgiser1 and Edgardo S. Cheb-Terrab2 

(1) Laboratoire PhLAM, UMR CNRS 8523, Université Lille 1, F-59655, France

(2) Maplesoft

 

Within the Schrödinger picture of Quantum Mechanics, the time evolution of the state of a system, represented by a Ket "| psi(t) >", is determined by Schrödinger's equation:

I*`ℏ`*(diff(Ket(psi, t), t)) = H*Ket(psi, t)

where H, the Hamiltonian, as well as the quantum operators O__S representing observable quantities, are all time-independent.

 

Within the Heisenberg picture, a Ket Ket(psi, 0) representing the state of the system does not evolve with time, but the operators O__H(t)representing observable quantities, and through them the Hamiltonian H, do.

 

Problem: Departing from Schrödinger's equation,

  

a) Show that the expected value of a physical observable in Schrödinger's and Heisenberg's representations is the same, i.e. that

Bra(psi, t)*O__S*Ket(psi, t) = Bra(psi, 0)*O__H(t)*Ket(psi, 0)

  

b) Show that the evolution equation of an observable O__H in Heisenberg's picture, equivalent to Schrödinger's equation,  is given by:

diff(O__H(t), t) = (-I*Physics:-Commutator(O__H(t), H))*(1/`ℏ`)

where in the right-hand-side we see the commutator of O__H with the Hamiltonian of the system.

Solution: Let O__S and O__H respectively be operators representing one and the same observable quantity in Schrödinger's and Heisenberg's pictures, and H be the operator representing the Hamiltonian of a physical system. All of these operators are Hermitian. So we start by setting up the framework for this problem accordingly, including that the time t and Planck's constant are real. To automatically combine powers of the same base (happening frequently in what follows) we also set combinepowersofsamebase = true. The following input/output was obtained using the latest Physics update (Aug/31/2016) distributed on the Maplesoft R&D Physics webpage.

with(Physics):

Physics:-Setup(hermitianoperators = {H, O__H, O__S}, realobjects = {`ℏ`, t}, combinepowersofsamebase = true, mathematicalnotation = true)

[combinepowersofsamebase = true, hermitianoperators = {H, O__H, O__S}, mathematicalnotation = true, realobjects = {`ℏ`, t}]

(1)

Let's consider Schrödinger's equation

I*`ℏ`*(diff(Ket(psi, t), t)) = H*Ket(psi, t)

I*`ℏ`*(diff(Physics:-Ket(psi, t), t)) = Physics:-`*`(H, Physics:-Ket(psi, t))

(2)

Now, H is time-independent, so (2) can be formally solved: psi(t) is obtained from the solution psi(0) at time t = 0, as follows:

T := exp(-I*H*t/`ℏ`)

exp(-I*t*H/`ℏ`)

(3)

Ket(psi, t) = T*Ket(psi, 0)

Physics:-Ket(psi, t) = Physics:-`*`(exp(-I*t*H/`ℏ`), Physics:-Ket(psi, 0))

(4)

To check that (4) is a solution of (2), substitute it in (2):

eval(I*`ℏ`*(diff(Physics[Ket](psi, t), t)) = Physics[`*`](H, Physics[Ket](psi, t)), Physics[Ket](psi, t) = Physics[`*`](exp(-I*H*t/`ℏ`), Physics[Ket](psi, 0)))

Physics:-`*`(H, exp(-I*t*H/`ℏ`), Physics:-Ket(psi, 0)) = Physics:-`*`(H, exp(-I*t*H/`ℏ`), Physics:-Ket(psi, 0))

(5)

Next, to relate the Schrödinger and Heisenberg representations of an Hermitian operator O representing an observable physical quantity, recall that the value expected for this quantity at time t during a measurement is given by the mean value of the corresponding operator (i.e., bracketing it with the state of the system Ket(psi, t)).

So let O__S be an observable in the Schrödinger picture: its mean value is obtained by bracketing the operator with equation (4):

Dagger(Ket(psi, t) = Physics[`*`](exp(-I*H*t/`ℏ`), Ket(psi, 0)))*O__S*(Ket(psi, t) = Physics[`*`](exp(-I*H*t/`ℏ`), Ket(psi, 0)))

Physics:-`*`(Physics:-Bra(psi, t), O__S, Physics:-Ket(psi, t)) = Physics:-`*`(Physics:-Bra(psi, 0), exp(I*t*H/`ℏ`), O__S, exp(-I*t*H/`ℏ`), Physics:-Ket(psi, 0))

(6)

The composed operator within the bracket on the right-hand-side is the operator O in Heisenberg's picture, O__H(t)

Dagger(T)*O__S*T = O__H(t)

Physics:-`*`(exp(I*t*H/`ℏ`), O__S, exp(-I*t*H/`ℏ`)) = O__H(t)

(7)

Analogously, inverting this equation,

(T*(Physics[`*`](exp(I*H*t/`ℏ`), O__S, exp(-I*H*t/`ℏ`)) = O__H(t)))*Dagger(T)

O__S = Physics:-`*`(exp(-I*t*H/`ℏ`), O__H(t), exp(I*t*H/`ℏ`))

(8)

As an aside to the problem, we note from these two equations, and since the operator T = exp((-I*H*t)*(1/`ℏ`)) is unitary (because H is Hermitian), that the switch between Schrödinger's and Heisenberg's pictures is accomplished through a unitary transformation.

 

Inserting now this value of O__S from (8) in the right-hand-side of (6), we get the answer to item a)

lhs(Physics[`*`](Bra(psi, t), O__S, Ket(psi, t)) = Physics[`*`](Bra(psi, 0), exp(I*H*t/`ℏ`), O__S, exp(-I*H*t/`ℏ`), Ket(psi, 0))) = eval(rhs(Physics[`*`](Bra(psi, t), O__S, Ket(psi, t)) = Physics[`*`](Bra(psi, 0), exp(I*H*t/`ℏ`), O__S, exp(-I*H*t/`ℏ`), Ket(psi, 0))), O__S = Physics[`*`](exp(-I*H*t/`ℏ`), O__H(t), exp(I*H*t/`ℏ`)))

Physics:-`*`(Physics:-Bra(psi, t), O__S, Physics:-Ket(psi, t)) = Physics:-`*`(Physics:-Bra(psi, 0), O__H(t), Physics:-Ket(psi, 0))

(9)

where, on the left-hand-side, the Ket representing the state of the system is evolving with time (Schrödinger's picture), while on the the right-hand-side the Ket `ψ__0`is constant and it is O__H(t), the operator representing an observable physical quantity, that evolves with time (Heisenberg picture). As expected, both pictures result in the same expected value for the physical quantity represented by O.

 

To complete item b), the derivation of the evolution equation for O__H(t), we take the time derivative of the equation (7):

diff((rhs = lhs)(Physics[`*`](exp(I*H*t/`ℏ`), O__S, exp(-I*H*t/`ℏ`)) = O__H(t)), t)

diff(O__H(t), t) = I*Physics:-`*`(H, exp(I*t*H/`ℏ`), O__S, exp(-I*t*H/`ℏ`))/`ℏ`-I*Physics:-`*`(exp(I*t*H/`ℏ`), O__S, H, exp(-I*t*H/`ℏ`))/`ℏ`

(10)

To rewrite this equation in terms of the commutator  Physics:-Commutator(O__S, H), it suffices to re-order the product  H  exp(I*H*t/`ℏ`) placing the exponential first:

Library:-SortProducts(diff(O__H(t), t) = I*Physics[`*`](H, exp(I*H*t/`ℏ`), O__S, exp(-I*H*t/`ℏ`))/`ℏ`-I*Physics[`*`](exp(I*H*t/`ℏ`), O__S, H, exp(-I*H*t/`ℏ`))/`ℏ`, [exp(I*H*t/`ℏ`), H], usecommutator)

diff(O__H(t), t) = I*Physics:-`*`(exp(I*t*H/`ℏ`), H, O__S, exp(-I*t*H/`ℏ`))/`ℏ`-I*Physics:-`*`(exp(I*t*H/`ℏ`), Physics:-`*`(H, O__S)+Physics:-Commutator(O__S, H), exp(-I*t*H/`ℏ`))/`ℏ`

(11)

Normal(diff(O__H(t), t) = I*Physics[`*`](exp(I*H*t/`ℏ`), H, O__S, exp(-I*H*t/`ℏ`))/`ℏ`-I*Physics[`*`](exp(I*H*t/`ℏ`), Physics[`*`](H, O__S)+Physics[Commutator](O__S, H), exp(-I*H*t/`ℏ`))/`ℏ`)

diff(O__H(t), t) = -I*Physics:-`*`(exp(I*t*H/`ℏ`), Physics:-Commutator(O__S, H), exp(-I*t*H/`ℏ`))/`ℏ`

(12)

Finally, to express the right-hand-side in terms of  Physics:-Commutator(O__H(t), H) instead of Physics:-Commutator(O__S, H), we take the commutator of the equation (8) with the Hamiltonian

Commutator(O__S = Physics[`*`](exp(-I*H*t/`ℏ`), O__H(t), exp(I*H*t/`ℏ`)), H)

Physics:-Commutator(O__S, H) = Physics:-`*`(exp(-I*t*H/`ℏ`), Physics:-Commutator(O__H(t), H), exp(I*t*H/`ℏ`))

(13)

Combining these two expressions, we arrive at the expected result for b), the evolution equation of a given observable O__H in Heisenberg's picture

eval(diff(O__H(t), t) = -I*Physics[`*`](exp(I*H*t/`ℏ`), Physics[Commutator](O__S, H), exp(-I*H*t/`ℏ`))/`ℏ`, Physics[Commutator](O__S, H) = Physics[`*`](exp(-I*H*t/`ℏ`), Physics[Commutator](O__H(t), H), exp(I*H*t/`ℏ`)))

diff(O__H(t), t) = -I*Physics:-Commutator(O__H(t), H)/`ℏ`

(14)


Download:    Schrodinger_vs_Heisenberg_picture.mw     Schrodinger_vs_Heisenberg_picture.pdf

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

Hanze University of Applied Sciences Groningen has created 105 questions related to engineering mechanics for structures (statics/construction). These 105 randomised questions with graphics are used for first year students in civil engineering, structural engineering, architectural engineering and building engineering.

The topics of the course modules are as follows:
- Force Vectors (10)
- Support Reactions (26)
- Internal Forces (31)
- Stress (21)
- Trusses (17)

All questions have a translation button which makes it easy to switch from English to any other language. The questions are first shown in Dutch [NL] but by clicking [UK] in the Preview, the English version is shown. The text can easily be edited and changed into the language of choice in the Maple T.A. question editor. Only the button needs adjustment in the question source.

60 questions are “exercises“ which means that these questions have extended feedback. The remaining questions (45) are “tests” meaning that the questions include no feedback.

Cone.zip - construction exercises (60 questions)

Cont.zip - construction tests (45 questions)

Jonny
Maplesoft Product Manager, Online Education Products

In order to change Maple for the better, I use to submit SCRs. However, as i was kindly
informed by Bryon (a copy of his e-letter on demand), MaplePrimes are under reconstruction and do not
work properly. At least my three messages sent through the Contact button were lost.
I have  unsuccessfully tried to reach beta.maplesoft.com (see the result of ping in the screen screen.29.08.16.docx).
Please, help me!

A more honest and specific version of lemma 3.

CONGRUENT_FUNCTIONS_OF_THE_FRACTIONAL_PART_OVER_Q_LEMMA_4.mw

Maple Worksheet - Error

Failed to load the worksheet /maplenet/convert/CONGRUENT_FUNCTIONS_OF_THE_FRACTIONAL_PART_OVER_Q_LEMMA_4.mw .

Download CONGRUENT_FUNCTIONS_OF_THE_FRACTIONAL_PART_OVER_Q_LEMMA_4.mw

This post is about the relationship between the number of processors used in parallel processing with the Threads package and the resultant real times and cpu times for a computation.

In the worksheet below, I perform the same computation using each possible number of processors on my machine, one thru eight. The computation is adding a list of 32 million pre-selected random integers. The real times and cpu times are collected from each run, and these are analyzed with a variety of metrics that I devised. Note that garbage-collection (gc) time is not an issue in the timings; as you can see below, the gc times are zero throughout.

My conclusion is that there are severely diminishing returns as the number of processors increases. There is a major benefit in going from one processor to two; there is a not-as-great-but-still-substantial benefit in going from two processors to four. But the real-time reduction in going from four processors to eight is very small compared to the substantial increase in resource consumption.

Please discuss the relevance of my six metrics, the soundness of my test technique, and how the presentation could be better. If you have a computer capable of running more than eight threads, please modify and run my worksheet on it.

Diminishing Returns from Parallel Processing: Is it worth using more than four processors with Threads?

Author: Carl J Love, 2016-July-30 

Set up tests

 

restart:

currentdir(kernelopts(homedir)):
if kernelopts(numcpus) <> 8 then
     error "This worksheet needs to be adjusted for your number of CPUs."
end if:
try fremove("ThreadsData.m") catch: end try:
try fremove("ThreadsTestData.m") catch: end try:
try fremove("ThreadsTest.mpl") catch: end try:

#Create and save random test data
L:= RandomTools:-Generate(list(integer, 2^25)):
save L, "ThreadsTestData.m":

#Create code file to be read for the tests.
fd:= FileTools:-Text:-Open("ThreadsTest.mpl", create):
fprintf(
     fd,
     "gc();\n"
     "read \"ThreadsTestData.m\":\n"
     "CodeTools:-Usage(Threads:-Add(x, x= L)):\n"
     "fd:= FileTools:-Text:-Open(\"ThreadsData.m\", create, append):\n"
     "fprintf(\n"
     "     fd, \"%%m%%m%%m\\n\",\n"
     "     kernelopts(numcpus),\n"
     "     CodeTools:-Usage(\n"
     "          Threads:-Add(x, x= L),\n"
     "          iterations= 8,\n"
     "          output= [realtime, cputime]\n"
     "     )\n"
     "):\n"
     "fclose(fd):"
):
fclose(fd):

#Code review
fd:= FileTools:-Text:-Open("ThreadsTest.mpl"):
while not feof(fd) do
     printf("%s\n", FileTools:-Text:-ReadLine(fd))
end do:

fclose(fd):

gc();
read "ThreadsTestData.m":
CodeTools:-Usage(Threads:-Add(x, x= L)):
fd:= FileTools:-Text:-Open("ThreadsData.m", create, append):
fprintf(
     fd, "%m%m%m\n",
     kernelopts(numcpus),
     CodeTools:-Usage(
          Threads:-Add(x, x= L),
          iterations= 8,
          output= [realtime, cputime]
     )
):
fclose(fd):

 

Run the tests

restart:

kernelopts(numcpus= 1):
currentdir(kernelopts(homedir)):
read "ThreadsTest.mpl":

memory used=0.79MiB, alloc change=0 bytes, cpu time=2.66s, real time=2.66s, gc time=0ns

memory used=0.78MiB, alloc change=0 bytes, cpu time=2.26s, real time=2.26s, gc time=0ns

 

Repeat above test using numcpus= 2..8.

 

restart:

kernelopts(numcpus= 2):
currentdir(kernelopts(homedir)):
read "ThreadsTest.mpl":

memory used=0.79MiB, alloc change=2.19MiB, cpu time=2.73s, real time=1.65s, gc time=0ns

memory used=0.78MiB, alloc change=0 bytes, cpu time=2.37s, real time=1.28s, gc time=0ns

 

restart:

kernelopts(numcpus= 3):
currentdir(kernelopts(homedir)):
read "ThreadsTest.mpl":

memory used=0.79MiB, alloc change=4.38MiB, cpu time=2.98s, real time=1.38s, gc time=0ns

memory used=0.78MiB, alloc change=0 bytes, cpu time=2.75s, real time=1.05s, gc time=0ns

 

restart:

kernelopts(numcpus= 4):
currentdir(kernelopts(homedir)):
read "ThreadsTest.mpl":

memory used=0.80MiB, alloc change=6.56MiB, cpu time=3.76s, real time=1.38s, gc time=0ns

memory used=0.78MiB, alloc change=0 bytes, cpu time=3.26s, real time=959.75ms, gc time=0ns

 

restart:

kernelopts(numcpus= 5):
currentdir(kernelopts(homedir)):
read "ThreadsTest.mpl":

memory used=0.80MiB, alloc change=8.75MiB, cpu time=4.12s, real time=1.30s, gc time=0ns

memory used=0.78MiB, alloc change=0 bytes, cpu time=3.74s, real time=910.88ms, gc time=0ns

 

restart:

kernelopts(numcpus= 6):
currentdir(kernelopts(homedir)):
read "ThreadsTest.mpl":

memory used=0.81MiB, alloc change=10.94MiB, cpu time=4.59s, real time=1.26s, gc time=0ns

memory used=0.78MiB, alloc change=0 bytes, cpu time=4.29s, real time=894.00ms, gc time=0ns

 

restart:

kernelopts(numcpus= 7):
currentdir(kernelopts(homedir)):
read "ThreadsTest.mpl":

memory used=0.81MiB, alloc change=13.12MiB, cpu time=5.08s, real time=1.26s, gc time=0ns

memory used=0.78MiB, alloc change=0 bytes, cpu time=4.63s, real time=879.00ms, gc time=0ns

 

restart:

kernelopts(numcpus= 8):
currentdir(kernelopts(homedir)):
read "ThreadsTest.mpl":

memory used=0.82MiB, alloc change=15.31MiB, cpu time=5.08s, real time=1.25s, gc time=0ns

memory used=0.78MiB, alloc change=0 bytes, cpu time=4.69s, real time=845.75ms, gc time=0ns

 

Analyze the data

restart:

currentdir(kernelopts(homedir)):

(R,C):= 'Vector(kernelopts(numcpus))' $ 2:
N:= Vector(kernelopts(numcpus), i-> i):

fd:= FileTools:-Text:-Open("ThreadsData.m"):
while not feof(fd) do
     (n,Tr,Tc):= fscanf(fd, "%m%m%m\n")[];
     (R[n],C[n]):= (Tr,Tc)
end do:

fclose(fd):

plot(
     (V-> <N | 100*~V>)~([R /~ max(R), C /~ max(C)]),
     title= "Raw timing data (normalized)",
     legend= ["real", "CPU"],
     labels= [`number of processors\n`, `%  of  max`],
     labeldirections= [HORIZONTAL,VERTICAL],
     view= [DEFAULT, 0..100]
);

The metrics:

 

R[1] /~ R /~ N:          Gain: The gain from parallelism expressed as a percentage of the theoretical maximum gain given the number of processors

C /~ R /~ N:               Evenness: How evenly the task is distributed among the processors

1 -~ C[1] /~ C:           Overhead: The percentage of extra resource consumption due to parallelism

R /~ R[1]:                   Reduction: The percentage reduction in real time

1 -~ R[2..] /~ R[..-2]:  Marginal Reduction: Percentage reduction in real time by using one more processor

C[2..] /~ C[..-2] -~ 1:  Marginal Consumption: Percentage increase in resource consumption by using one more processor

 

plot(
     [
          (V-> <N | 100*~V>)~([
               R[1]/~R/~N,             #gain from parallelism
               C/~R/~N,                #how evenly distributed
               1 -~ C[1]/~C,           #overhead
               R/~R[1]                 #reduction
          ])[],
          (V-> <N[2..] -~ .5 | 100*~V>)~([
               1 -~ R[2..]/~R[..-2],   #marginal reduction rate
               C[2..]/~C[..-2] -~ 1    #marginal consumption rate        
          ])[]
     ],
     legend= typeset~([
          'r[1]/r/n',
          'c/r/n',
          '1 - c[1]/c',
          'r/r[1]',
          '1 - `Delta__%`(r)',
          '`Delta__%`(c) - 1'       
     ]),
     linestyle= ["solid"$4, "dash"$2], thickness= 2,
     title= "Efficiency metrics\n", titlefont= [HELVETICA,BOLD,16],
     labels= [`number of processors\n`, `% change`], labelfont= [TIMES,ITALIC,14],
     labeldirections= [HORIZONTAL,VERTICAL],
     caption= "\nr = real time,  c = CPU time,  n = # of processors",
     size= combinat:-fibonacci~([16,15]),
     gridlines
);

 

 

Download Threads_dim_ret.mw

In a recent conversation I explained whyLSODE was giving wrong results (http://www.mapleprimes.com/questions/210948-Can-We-Trust-Maple#comment230167). After a lot of confusions and weird infinite loops for answers, it turned out that Newton Raphson was not properly done.

Both LSODE and MEBDFI are currently incompletely implemented (only one iteration is done instead of Newton Raphson till convergence). Maplesoft should update the help files accordingly.

The post below explains how better results are obtained with method = mgear. To run the command mgear you will need Maple 6 or earlier versions. For lsode, any current version is fine.  Unfortunately Maple deprecated an algorithm that worked fine. From Maple 8, the algorithm moved to Rosenbrock methods for stiff equations. This is still not ideal.

If Maple had a working algorithm, I am hoping that Maplesoft folks would consider bringing it back in future versions. (At least with the same functionality as in Maple 6).

PLEASE NOTE, the issue is not with solving this example (Very simple). This example is chosen to show how a popular algorithm in the literature is wrongly implemented.

 

Here Maple's lsode is forced to take only one step and use first order back ward difference formula to integrate from 0 to 1.  LSODE mimics Eulerbackward using the options given below. The post shows that LSODE does not do Newton Raphson and just performs only iteration for nonlinear equations.

restart;

Digits:=15;

Digits := 15

(1)

eq:=diff(y(t),t)=-y(t);

eq := diff(y(t), t) = -y(t)

(2)

C:=array([0$22]);

C := Vector[row](22, {(1) = 0, (2) = 0, (3) = 0, (4) = 0, (5) = 0, (6) = 0, (7) = 0, (8) = 0, (9) = 0, (10) = 0, (11) = 0, (12) = 0, (13) = 0, (14) = 0, (15) = 0, (16) = 0, (17) = 0, (18) = 0, (19) = 0, (20) = 0, (21) = 0, (22) = 0})

(3)

C[9]:=1;

C[9] := 1

(4)

sol:=dsolve({eq,y(0)=1},type=numeric,method=lsode[backfull],ctrl=C,initstep=0.1,minstep=0.1,abserr=1,relerr=1):

sol(0.1);

[t = .1, y(t) = .909090909090834]

(5)

subs(diff(y(t),t)=(y1-1)/0.1,y(t)=y1,eq);

0.1e2*y1-0.1e2 = -y1

(6)

fsolve(%,y1=0.5);

.909090909090909

(7)

 While for linear it gave the expected result, it gives wrong results for nonlinear problems.

sol1:=dsolve({eq,y(0)=1},type=numeric):

sol1(0.1);

[t = .1, y(t) = .904837355407810]

(8)

eq:=diff(y(t),t)=-y(t)^2*exp(-y(t))-10*y(t)*(1+0.01*exp(y(t)));

eq := diff(y(t), t) = -y(t)^2*exp(-y(t))-10*y(t)*(1+0.1e-1*exp(y(t)))

(9)

sol:=dsolve({eq,y(0)=1},type=numeric,method=lsode[backfull],ctrl=C,initstep=0.1,minstep=0.1,abserr=1,relerr=1):

sol(0.1);

[t = .1, y(t) = .501579294869466]

(10)

subs(diff(y(t),t)=(y1-1)/0.1,y(t)=y1,eq);

0.1e2*y1-0.1e2 = -y1^2*exp(-y1)-10*y1*(1+0.1e-1*exp(y1))

(11)

fsolve(%,y1=1);

.488691779256025

(12)

sol1:=dsolve({eq,y(0)=1},type=numeric):

 the expected answer is correctly obtained with default tolerance as

sol1(0.1);

[t = .1, y(t) = .349614721994122]

(13)

 The results obtained are worse than single iteration using jacobian.

eq2:=(lhs-rhs)(subs(diff(y(t),t)=(y1-1)/0.1,y(t)=y1,eq));

eq2 := 0.1e2*y1-0.1e2+y1^2*exp(-y1)+10*y1*(1+0.1e-1*exp(y1))

(14)

jac:=unapply(diff(eq2,y1),y1);

jac := proc (y1) options operator, arrow; 20.+2*y1*exp(-y1)-y1^2*exp(-y1)+.10*exp(y1)+.10*y1*exp(y1) end proc

(15)

f:=unapply(eq2,y1);

f := proc (y1) options operator, arrow; 0.1e2*y1-0.1e2+y1^2*exp(-y1)+10*y1*(1+0.1e-1*exp(y1)) end proc

(16)

y0:=1;

y0 := 1

(17)

dy:=-evalf(f(y0)/jac(y0));

dy := -.508796088545793

(18)

ynew:=y0+dy;

ynew := .491203911454207

(19)

 Following procedures confirm that it is indeed calling the procedure only at 0 and 0.1, with backdiag giving slightly better results.

myfun:= proc(x,y) if not type(x,'numeric') or not type(evalf(y),numeric)then 'procname'(x,y);
    else lprint(`Request at x=`,x); -y^2*exp(-y(x))-10*y*(1+0.01*exp(y)); end if; end proc;

myfun := proc (x, y) if not (type(x, 'numeric') and type(evalf(y), numeric)) then ('procname')(x, y) else lprint(`Request at x=`, x); -y^2*exp(-y(x))-10*y*(1+0.1e-1*exp(y)) end if end proc

(20)

sol1:=dsolve({diff(y(x),x)=myfun(x,y(x)),y(0)=1},numeric,method=lsode[backfull],ctrl=C,initstep=0.1,minstep=0.1,abserr=1,relerr=1,known={myfun}):

sol1(0.1);

`Request at x=`, 0.

`Request at x=`, 0.

`Request at x=`, .1

`Request at x=`, .1

[x = .1, y(x) = .501579304183583]

(21)

sol2:=dsolve({diff(y(x),x)=myfun(x,y(x)),y(0)=1},numeric,method=lsode[backdiag],ctrl=C,initstep=0.1,minstep=0.1,abserr=1,relerr=1,known={myfun}):

sol2(0.1);

`Request at x=`, 0.

`Request at x=`, 0.

`Request at x=`, .1

`Request at x=`, .1

[x = .1, y(x) = .497831388424072]

(22)

 

Download Lsodeanalysistrunc.mws

 

Next see how dsolve method = mgear works just fine in Maple 6 (gives the expected answer upto 3 Digits accuracy). To run this code you will need Maple 6 or earlier versions. Maple 7 has this algorithm, but I don't know to use it as it is hidden. I would like to get support from other members to get Maplesoft's attention to bring this algorithm back.

If Mdy/dt = f(y) is solved using mgear algorithm (instead of dy/dt =f ), then one can have a good DAE solver based on this (M being singular). 

 

restart;

myfun:= proc(x,y) if not type(x,'numeric') or not type(evalf(y),numeric)then 'procname'(x,y);
    else lprint(`Request at x=`,x); -y^2*exp(-y(x))-10*y*(1+0.01*exp(y)); end if; end proc;

myfun := proc (x, y) if not (type(x, 'numeric') and type(evalf(y), numeric)) then ('procname')(x, y) else lprint(`Request at x=`, x); -y^2*exp(-y(x))-10*y*(1+0.1e-1*exp(y)) end if end proc

(1)

sol2:=dsolve({diff(y(x),x)=myfun(x,y(x)),y(0)=1},{y(x)},numeric,method=mgear[mstepnum],stepsize=0.1,minstep=0.1,errorper=1):

sol2(0.1);

`Request at x=`, 0.

`Request at x=`, .1

`Request at x=`, .1

`Request at x=`, .1

[x = .1, y(x) = .4887165263]

(2)

 

 

Download Mgearworks.mws

Run the following command in Maple:

Explore(plot(x^k), k = 1 .. 3);

 

Once you’ve run the command, move the slider from side to side. Neat, isn’t it?

With this single line of code, you have built an interactive application that shows the graph of x to the power of various exponent powers.

 

The Explore command is an application builder. More specifically, the Explore command can programmatically generate interactive content in Maple worksheets.

Programmatically generated content is inserted into a Maple worksheet by executing Maple commands. For example, when you run the Explore command on an expression, it inserts a collection of input and output controllers, called Embedded Components, into your Maple worksheet. In the preceding example, the Explore command inserts a table containing:

  • a Slider component, which corresponds to the value for the exponent k
  • a Plot component, which shows the graph of x raised to the power for k

Together these components form an interactive application that can be used to visualize the effect of changing parameter values.

Explore can be viewed as an easy application creator that generates simple applications with input and output components. Recently added packages for programmatic content generation broaden Maple’s application authoring abilities to form a full development framework for creating customized interactive content in a Maple worksheet. The DocumentTools package contains many of these new tools. Components and Layout are two sub-packages that generate XML using function calls that represents GUI elements, such as embedded components, tables, input, or output. For example, the DocumentTools:-Components:-Plot command creates a new Plot component. These key pieces of functionality provide all of the building blocks needed to create customizable interfaces inside of the Maple worksheet. For me, this new functionality has completely altered my approach to building Maple worksheets and made it much easier to create new applications that can explore hundreds of data sets, visualize mathematical functions, and more.

I would go so far as to say that the ability to programmatically generate content is one of the most important new sources of functionality over the past few years, and is something that has the potential to significantly alter the way in which we all use Maple. Programmatic content generation allows you to create applications with hundreds of interactive components in a very short period of time when compared to building them manually using embedded components. As an illustration of this, I will show you how I easily created a table with over 180 embedded components—and the logic to control them.

 

Building an interface for exploring data sets:

In my previous blog post on working with data sets in Maple, I demonstrated a simple customized interface for exploring country data sets. That post only hinted at the much bigger story of how the Maple programming language was used to author the application. What follows is the method that I used, and a couple of lessons that I learned along the way.

When I started building an application to explore the country data sets, I began with an approach that I had used to build several MathApps in the past. I started with a blank Maple worksheet and manually added embedded components for controlling input and output. This included checkbox components for each of the world’s countries, drop down boxes for available data sets, and a couple of control buttons for retrieving data to complete my application.

This manual, piece-by-piece method seemed like the most direct approach, but building my application by hand proved time-consuming, given that I needed to create 180 checkboxes to house all available countries with data. What I really needed was a quicker, more scriptable way to build my interface.

 

So jumping right into it, you can view the code that I wrote to create the country data application here:PECCode.txt

Note that you can download a copy of the associated Maple worksheet at the bottom of this page.

 

I won’t go into too much detail on how to write this code, but the first thing to note is the length of the code; in fewer than 70 lines, this code generates an interface with all of the required underlying code to drive interaction for 180+ checkboxes, 2 buttons and a plot. In fact, if you open up the application, you’ll see that every check box has several lines of code behind it. If you tried to do this by hand, the amount of effort would be multiplied several times over.

This is really the key benefit to the world of programmatic content generation. You can easily build and rebuild any kind of interactive application that you need using the Maple programming language. The possibilities are endless.

 

Some tips and tricks:

There are a few pitfalls to be aware of when you learn to create content with Maple code. One of the first lessons I learned was that it is always important to consider embedded component name collision and name resolution.

For those that have experimented with embedded components, you may have noticed that Maple’s GUI gives unique names to components that are copied (or added) in a Maple worksheet. For example, the first TextArea component that you add to a worksheet usually has the default name TextArea0. If you next add another TextArea, this new TextArea gets the name TextArea1, so as to not collide with the first component. Similar behaviour can be observed with any other component and even within some component properties such as ‘group’ name.

Many of the options for commands in the DocumentTools sub-packages can have “action code”, or code that is run when the component is interacted with. When building action code for a generated component, the action code is specified using a long string that encapsulates all of the code. Due to this code being provided as a long string, one trick that I quickly picked up is that it is important to separate out the names for any components into sub-strings inside of a longer cat statement.

For example, here is a line that is contained within a longer cat statement in the preceding code:

cat( "DocumentTools:-SetProperty( \"", "ComboBox_0", "\", 'value', \"Internet Users\" );\n" )

It is necessary to enclose “ComboBox_0” in quotes, as well as to add in escaped quotes in order to have the resulting action code look like (also note the added new line at the end):

“DocumentTools:-SetProperty( “ComboBox_0”, ‘value’, “Internet Users” );”

Doing so ensures that when the components are created, the names are not hard-coded to always just look for a given name. This means that the GUI can scrape through the code and update any newly generated components with a new name when needed. This is important if “ComboBox_0” already exists so that the GUI can instead create “ComboBox_1”.

 

Another challenge for coding applications is adding a component state. One of the most common problems encountered with running any interactive content in Maple is that if state is not persistent, errors can occur when, for example, a play button is clicked but the required procedures have not been run. This is a very challenging problem, which often require solutions like the use of auto-executing start-up code or more involved component programming. Some features in Maple 2016 have started working to address this, but state is still something that usually needs to be considered on an application by application basis.

In my example, I needed to save the state of a table containing country names so that the interface retains the information for check box state (checked or unchecked) after restart. That is, if I saved the application with two countries selected, I wanted to ensure that when I opened the file again those same two countries would still be selected, both in the interface as well as in the table that is used to generate the plot. Now accomplishing this was a more interesting task: my hack was to insert a DataTable component, which stored my table as an entry of a 1x1 Matrix rtable. Since the rtable that underlies a DataTable is loaded into memory on Maple load, this gave me a way to ensure that the checked country table was loaded on open.

Here, for example, is the initial creation of this table:

"if not eval( :-_SelectedCountries )::Matrix then\n",
"    :-_SelectedCountries := Matrix(1,1,[table([])]):\n",
"end if;\n",

For more details, just look for the term: “:-_SelectedCountries” in the preceding code.

I could easily devote separate posts to discussing in detail each of these two quick tips. Similarly, there’s much more that can be discussed with respect to authoring an interface using programmatic tools from the DocumentTools packages, but I found the best way to learn more about a command is to try it out yourself. Once you do, you’ll find that there are an endless number of combinations for the kinds of interfaces that can be quickly built using programmatic content generation. Several commands in Maple have already started down the path of inserting customized content for their output (see DataSets:-InsertSearchBox and AudioTools:-Preview as a couple of examples) and I can only see this trend growing.

Finally, I would like to say that getting started with programmatic content generation was intimidating at first, but with a little bit of experimentation, it was a rewarding experience that has changed the way in which I work in Maple. In many cases, I now view output as something that can be customized for any command. More often than not, I turn to commands like ‘Explore’ to create an interface to see how sweeping through parameters effects my results, and any time I want to perform a more complex analysis or visualization for my data, I write code to create interfaces that can more easily be customized and re-used for other applications.

If you are interested in learning more about this topic, some good examples to get started with are the examples page for programmatic content generation as well as the help pages for the DocumentTools:-Components and DocumentTools:-Layout sub-packages.

To download a copy of the worksheet used in this post, click here (note that the code can be found in the start-up code of this worksheet): CountryDataPEC.mw To create the datasets interface, simply run the CountrySelection(); command.

        General description of the method of solving underdetermined systems of equations. As a particular application of the idea proposed a universal method  kinematic analysis for all kinds of  spatial and planar link mechanisms with any number degrees of freedom.  The method can be used for powerful CAD linkages.   
         http://www.maplesoft.com/applications/view.aspx?SID=154228

       


      Some examples of a much larger number calculated by the proposed method. Examples gathered here not to look for them on the forum and opportunity to demonstrate the method.  Among the examples, I think, there are very complicated.

https://vk.com/doc242471809_408704758
https://vk.com/doc242471809_408704572
https://vk.com/doc242471809_376439263
https://vk.com/doc242471809_402619761
https://vk.com/doc242471809_402610228
https://vk.com/doc242471809_401188803
https://vk.com/doc242471809_400465891
https://vk.com/doc242471809_400711315
https://vk.com/doc242471809_387358164
https://vk.com/doc242471809_380837279
https://vk.com/doc242471809_379935473
https://vk.com/doc242471809_380217387
https://vk.com/doc242471809_363266817
https://vk.com/doc242471809_353980472
https://vk.com/doc242471809_375452868
https://vk.com/doc242471809_353988163 
https://vk.com/doc242471809_353986884 
https://vk.com/doc242471809_353987119
https://vk.com/doc242471809_324249241
https://vk.com/doc242471809_324102889
https://vk.com/doc242471809_322219275
https://vk.com/doc242471809_437298137
https://vk.com/doc242471809_437308238
https://vk.com/doc242471809_437308241
https://vk.com/doc242471809_437308243
https://vk.com/doc242471809_437308245
https://vk.com/doc242471809_437308246
https://vk.com/doc242471809_437401651
https://vk.com/doc242471809_437664558

 

 

Aggregate statistics are calculated by splitting the rows of a DataFrame by each factor in a given column into subsets and computing summary statistics for each of these subsets.

The following is a short example of how the Aggregate command is used to compute aggregate statistics for a DataFrame with housing data:

To begin, we construct a DataFrame with housing data: The first column has number of bedrooms, the second has the area in square feet, the third has price.
 

bedrooms := <3, 4, 2, 4, 3, 2, 2, 3, 4, 4, 2, 4, 4, 3, 3>:
area := <1130, 1123, 1049, 1527, 907, 580, 878, 1075, 
         1040, 1295, 1100, 995, 908, 853, 856>:
price := <114700, 125200, 81600, 127400, 88500, 59500, 96500, 113300, 
          104400, 136600, 80100, 128000, 115700, 94700, 89400>:
HouseSalesData := DataFrame([bedrooms, area, price], columns = [Bedrooms, Area, Price]);

Note that the Bedrooms column has three distinct levels: 2, 3, and 4.

convert(HouseSalesData[Bedrooms], set);

The following returns the mean of all other columns for each distinct level in the column, Bedrooms:

Aggregate(HouseSalesData, Bedrooms);

Adding the columns option controls which columns are returned.

Aggregate(HouseSalesData, Bedrooms, columns = [Price])

Additionally, the tally option returns a tally for each of the levels.

Aggregate(HouseSalesData, Bedrooms, tally)

The function option allows for the specification of any command that can be applied to a DataSeries. For example, the Statistics:-Median command computes the median for each of the levels of Bedrooms.

Aggregate(HouseSalesData, Bedrooms, function = Statistics:-Median);

By default, Aggregate uses the SplitByColumn command to creates a separate sub-DataFrame for every discrete level in the column given by bycolumn.

with(Statistics);
ByRooms := SplitByColumn(HouseSalesData, Bedrooms);

We can create box plots of the price for subgroups of sales defined by number of bedrooms.

BoxPlot( map( (m)->m[Price], ByRooms), 
             deciles=false, 
             datasetlabels=["2 bdrms", "3 bdrms", "4 bdrms"], 
             color=["Red", "Purple", "Blue"]);

 

I have recorded a short video that walks through this example here:

The worksheet for this example can be downloaded here: Aggregate.mw

We have just released a new version of the Multivariate Calculus Study Guide.  It provides a new section on Vector Calculus, with over 100 additional worked problems, and makes extensive use of Maple’s Clickable Math tools as well as commands.

Existing study guide customers can get the new content via a free update, available through the Check for Updates system or from our website. See Multivariate Calculus Study Guide 2016 Update for details.

For more information about this guide, including a full table of contents, visit Multivariate Calculus Study Guide.

 

eithne

A few people have asked me how I created the sections in the Maple application in this video: https://youtu.be/voohdmfTRn0?t=572

Here's the worksheet (Maple 2016 only). As you can see, the “sections” look different what you would normally expect (I often like to experiment with small changes in presentation!)

These aren't, however, sections in the traditional Maple sense; they're a demonstration of Maple 2016's new tools for programmatically changing the properties of a table (including the visibility of its rows and columns). @dskoog gets the credit for showing me the technique.

Each "section" consists of a table with two rows.

  • The table has a name, specified in its properties.
  • The first row (colored blue) contains (1) a toggle button and (2) the title of each section (with the text in white)
  • The second row (colored white) is visible or invisible based upon the state of the toggle button, and contains the content of my section.

Each toggle button has

  • a name, specified in its properties
  • + and - images associated with its on and off states (with the image background color matching the color of the first table row)
  • Click action code that enables or disables the visibility of the second row

The Click action code for the toggle button in the "Pure Fluid Properties" section is, for example,

tableName:="PureFluidProperties_tb":
buttonName:="PureFluidProperties_tbt":
if DocumentTools:-GetProperty(buttonName, 'value') = "false" then   
     DocumentTools:-SetProperty([tableName, 'visible[2..]', true]);
else
     DocumentTools:-SetProperty([tableName, 'visible[2..]', false]);
end if;

As I said at the start, I often try to make worksheets look different to the out-of-the-box defaults. Programmatic table properties have simply given me one more option to play about with.

Disclaimer: This blog post has been contributed by Prof. Nicola Wilkin, Head of Teaching Innovation (Science), College of Engineering and Physical Sciences and Jonathan Watkins from the University of Birmingham Maple T.A. user group*. 

Written for Maple T.A. 2016. For Maple T.A. 10 users, this question can be written using the queston designer.

 

This is the second of three blog posts about working with data sets in Maple.

In my previous post, I discussed how to use Maple to access a large number of data sets from Quandl, an online data aggregator. In this post, I’ll focus on exploring built-in data sets in Maple.

Data is being generated at an ever increasing rate. New data is generated every minute, adding to an expanding network of online information. Navigating through this information can be daunting. Simply preparing a tabular data set that collects information from several sources is often a difficult and time consuming effort. For example, even though the example in my previous post only required a couple of lines of Maple code to merge 540 different data sets from various sources, the effort to manually search for and select sources for data took significantly more time.

In an attempt to make the process of finding data easier, Maple’s built-in country data set collects information on country-specific variables including financial and economic data, as well as information on country codes, population, area, and more.

The built-in database for Country data can be accessed programmatically by creating a new DataSets Reference:

CountryData := DataSets:-Reference( "builtin", "country" );

This returns a Reference object, which can be further interrogated. There are several commands that are applicable to a DataSets Reference, including the following exports for the Reference object:

exports( CountryData, static );

The list of available countries in this data set is given using the following:

GetElementNames( CountryData );

The available data for each of these countries can be found using:

GetHeaders( CountryData );

There are many different data sets available for country data, 126 different variables to be exact. Similar to Maple’s DataFrame, the columns of information in the built-in data set can be accessed used the labelled name.

For example, the three-letter country codes for each country can be returned using:

CountryData[.., "3 Letter Country Code"];

The three-letter country code for Denmark is:

CountryData["Denmark", "3 Letter Country Code"];

Built-in data can also be queried in a similar manner to DataFrames. For example, to return the countries with a population density less than 3%:

pop_density := CountryData[ .., "Population Density" ]:
pop_density[ `Population Density` < 3 ];

At this time, Maple’s built-in country data collection contains 126 data sets for 185 countries. When I built the example from my first post, I knew exactly the data sets that I wanted to use and I built a script to collect these into a larger data container. Attempting a similar task using Maple’s built-in data left me with the difficult decision of choosing which data sets to use in my next example.

So rather than choose between these available options, I built a user interface that lets you quickly browse through all of Maple’s collection of built-in data.

Using a couple of tricks that I found in the pages for Programmatic Content Generation, I built the interface pictured above. (I’ll give more details on the method that I used to construct the interface in my next post.)

This interface allows you to select from a list of countries, and visualize up to three variables of the country data with a BubblePlot. Using the preassigned defaults, you can select several countries and then visualize how their overall number of internet users has changed along with their gross domestic product. The BubblePlot visualization also adds a third dimension of information by adjusting the bubble size according to the relative population compared with the other selected countries.

Now you may notice that the list of available data sets is longer than the list of available options in each of the selection boxes. In order to be able to generate BubblePlot animations, I made an arbitrary choice to filter out any of the built-in data sets that were not of type TimeSeries. This is something that could easily be changed in the code. The choice of a BubblePlot could also be updated to be any other type of Statistical visualization with some additional modifications.

You can download a copy of this application here: VisualizingCountryDataSets.mw

You can also interact with it via the MapleCloud: http://maplecloud.maplesoft.com/application.jsp?appId=5743882790764544

I’ll be following up this post with an in-depth post on how I authored the country selector interface using programmatic content generation.

     Example of the equidistant surface at a distance of 0.25 to the surface
x3
-0.1 * (sin (4 * x1) + sin (3 * x2 + x3) + sin (2 * x2)) = 0
Constructed on the basis of universal parameterization of surfaces.

equidistant_surface.mw 


First 16 17 18 19 20 21 22 Last Page 18 of 64