Maple Questions and Posts

These are Posts and Questions associated with the product, Maple


Code:

Graph := NULL; for i from 0 to N-1 do for j from 0 to 10 do if j <= finite_element_xi[i] and finite_element_xi[i] <= j+1 then finite_element_sigma[i] := evalf(finite_element_epsilon[i]*R(j)); p := plot(finite_element_sigma[i](x), x = finite_element_xi[i] .. finite_element_xi[i+1]); Graph := display(Graph, p) end if end do end do;
Error, (in plot) illegal use of an object as a name

I don't understand how to draw a graph for sigma

Why this post
This work was intended to be a simple reply to a question asked a few days ago.
At some point, I realised that the approach I was using could have a more general interest which, in my opinion, was worth a post.
In a few words, this post is about solving an algebra problem using a method originally designed to tackle statistical problems.

The Context
Recently @raj2018 submitted a question I'm going to resume this way:

Let S(phi ;  beta, f) a function of phi parameterized by beta and f.
Here is the graph of S(phi ;  0.449, 0.19)  @raj2018 provided

@raj2018 then asked how we can find other values (A, B)  of values for (beta, f) such that the graph of S(phi, A, B) has the same aspect of the graph above.
More precisely, let phi_0 the largest strictly negative value of phi such that  S(phi_0, A, B) = 0.
Then  S(phi, A, B) must be negative (strictly negative?) in the open interval (phi_0, 0), and must have exactly 3 extrema in this range.
I will said the point  (A, B) is admissible is S(phi, A, B) verifies thess conditions

The expression of S(phi, A, B) is that complex that it is likely impossible to find an (several?, all?) admissible point using analytic developments.

The approach

When I began thinking to this problem I early thought to find the entire domain of admissible points: was it something possible, at least with some reasonable accuracy? 

Quite rapidly I draw an analogy with an other type of problems whose solution is part of my job: the approximate construction of the probability density function (PDF) of multivariate random variables (obviously this implies that no analytical expression of this PDF is available). This is a very classical problem in Bayesian Statistics, for instance when we have to construt an approximation of a posterior PDF.

To stick with this example and put aside the rare situations where this PDF can be derived analytically, building a posterior PDF is largely based on specific numerical methods. 
The iconic one is known under the generic name MCMC  which stands for Markov Chain Monte Carlo.

Why am I speaking about MCMC or PDF or even random variables?
Let us consider some multivariate random variable R whose PDF as a constant on some bounded domain D and is equal to 0 elsewhere. R is then a uniform random variable with support supp(R) = D.
Assuming the domain Adm of admissible (beta, f) is bounded, we may  think of it as the support of some uniform random variable. Following this analogy we may expect to use some MCMC method to "build the PDF of the bivariate random variable (beta, f)", otherwise stated "to capture​​​​​​ the boundary of​ Adm".

The simplest MCMC method is the Metropolis-Hastings algorithm (MH).
In a few simple words MH builds a Markov chain this way:

  1. Let us assume that the chain already contains elements e1, ..., en.
    Let  f  some suitable "fitness" function (whose nature is of no importance right now).
  2. A potential new element c is randomly picked in some neighborhood or en.
  3. If the ratio (c) / (en) is larger than 1, we decide to put c into the chain (thus en+1 = c) otherwise we leave it to chance to decide whether or not c iis put into the chain.
    If chance decides the contrary,  then en is duclicated (thus en+1 = en).


MH is not the most efficient MCMC algorithm but it is efficient enough for what we want to achieve.
The main difficulty here is that there is no natural way to build the fitness function  f , mainly because the equivalent random variable I talked about is a purely abstract construction.

A preliminary observation is that if S(phi, beta, f) < 0 whatever phi in (phi_0, 0), then S has an odd number of extrema in (phi_0, 0). The simplest way to find these extrema is to search for the zeros of the derivative S' of S with respect to phi, while discardinq those where the second derivative can reveal "false" extrema where both S'' of S is null (I emphasize this last point because I didn't account for it in attached file).
The algorithm designed in this file probably misses a few points for not checking if S''=0, but it is important to keep in mind that we don't want a complete identification of  Adm but just the capture of its boundary.
Unless we are extremely unlucky there is only a very small chance that omitting to check if S''=0 will deeply modify this boundary.


How to define function f  ?
What we want is that  f (c) / (en) represents the probability to decide wether c is an admissible point or not. In a Markov chain this  ratio represents how better or worse c is relatively to en, and this is essential for the chain to be a true Markov chain.
But as our aim is not to build a true Markov chain but simply a chain which looks like a Markov chain, we we can take some liberties and replace  f (c) / (en) by some function  g(c) which quantifies the propability for c to be an admissible couple. So we want that  g(c) = 1 if  S(phi, c) has exactly M=3 negative extrema and  g(c) < 1 if M <> 3.
The previous algorihm transforms into:

  1. Let us assume that the chain already contains elements e1, ..., en.
    Let  g  a function which the propability that element is admissible
  2. A potential new element c is randomly picked in some neighborhood or en.
  3. If the ratio g(c) is larger than 1, we decide to put c into the chain (thus en+1 = c) otherwise we leave it to chance to decide whether or not c iis put into the chain.
    If chance decides the contrary,  then en is duclicated (thus en+1 = en).

This algorithm can also be seen as a kind of genetic algorithm.

A possible choice is  g(c)= exp(-|3-M|).
In the attached file I use instead the expression g(c) = (M + 1) / 4 fo several reasons:

  • It is less sharp at M=3 and thus enables more often to put c into the chain, which increases its exploratory capabilities.
  • The case M > 3, which no preliminary investigation was able to uncover, is by construction eliminated in the procedure Extrema which use an early stopping strategy (if as soon as more than M=3 negative extrema are found the procedure stops).


The algorithm I designed basically relies upon two stages:

  1. The first one is aimed to construct a "long" Markov-like chain ("long" and not long because Markov chains are usually much longer than those I use).
    There are two goals here:
    1. Check if Adm is or not simply-connected or not (if it has holes or not).
    2. Find a first set of admissible points that can be used as starting points for subsequent chains.
       
  2. Run several independent Markov-like chains from a reduced set of admissible points.
    The way this reduced set is constructed depends on the goal to achieve:
    1. One may think of adding points among those already known in order to assess the connectivity of Adm,
    2. or refinining the boundary of Adm.

These two concurent objectives are mixed in an ad hoc way depending on the observation of the results already in hand.


We point here an important feature of MCMC methods: behind their apparent algorithmic simplicity, it is common that high-quality results can only be obtained efficiently at the cost of problem-dependent tuning.

A last word to say that after several trials and failures I found it simpler to reparameterize the problems in terms of (phi_0, f) instead of (beta, f).

Codes and results

Choice g(c) = (M + 1) / 4 
The code : Extrema_and_MCMC.mw

To access the full results I got load this m file (do not bother its extension, Mapleprimes doesn't enable uploading m files) MCMC_20231209_160249.mw (save it and change it's extension in to m instead mw)

EDITED: choice  g(c)= exp(-|3-M|)
Here are the files contzining the code and the results:
Extrema_and_MCMC_g2.mw
MCMC_20231211_084053.mw

To ease the comparison of the two sets of results I used the same random seeds inn both codes.
Comparing the results got around the first admissible point is straightforward.
It's more complex for @raj2018's solution because the first step of the algorithim (drawing of a sibgle chain of length 1000) finds six times more admissible point with g(c)= exp(-|3-M|) than with g(c) = (M + 1) / 4.                                 

I wanted to debug some code from worksheet A.  So added DEBUG(); command in the code where I want to start the GUI debugger from, and then run the command from the worksheet. All is working OK. the debugger GUI comes up and I can step in. 

Now I wanted to debug some other code from worksheet B. But without closing the currect debugger which is open and running.

It turned out this is not possible.  When running debugger from worksheet B, it uses the same debugger GUI that was up and running. I think it closed that session automtically also.

So basically using same Maple process, one can't open two debgging sessions at same time? Is there a way around this.

I run each worksheet using its own math engine. So each is separated from each other.

But this is first time I wanted to debug two things at same time, i.e. side by side, thinking I will be able to open two GUI debuggers at same time.

I know ofcourse I can open two separate Maple processes and then I will be able to do this. I think I am allowed to have two Maple's open at same time. Will try that next.

But it will be better if one is able to open two debuggers from same Maple at same time. I do not see why this should not be possible.

Any suggestions if there is a workaround? May be some hidden setting that allows this?

Maple 2023.2.1 on windows 10

Plots of physical quantities has significantly improved with Maple 2022. The updated useunits option makes unit conversion errors in plots very unlikely. A lot of time is saved when creating plots of physical quantities where values and units must be correct.

One final source of user errors remains: The manual entry of incorrect units in labels.

Below is a way to avoid such errors by computing labels with units for three prevalent axis labeling schemes.


Other desireable labels are given as a suggestion for future plot label enhancements where plot commands could provide formating functionality.

The rendering on this website adds double brakets ⟦ ⟧ wherever units are used. You have to open the document to see how Maple renders.

NULL

Simple plot example: Solar irradiance in space

G__0 := 1361*Unit('W'/'m'^2)

1361*Units:-Unit(W/m^2)

(1)

G__0*sin(2*Pi*t/(24*Unit('h')))

1361*Units:-Unit(W/m^2)*sin((1/12)*Pi*t/Units:-Unit(h))

(2)

plot(1361*Units:-Unit(W/m^2)*sin((1/12)*Pi*t/Units:-Unit(h)), t = 0 .. 12*Unit('h'))

 

This plot has inconsistent axis labeling:

• 

The vertical axis has units but no name

• 

The horizontal axis has a name and units but they are not easily distinguishable. Misinterpretation is possible. Due to the close spacing the label could be read as a product of the dimension "time squared" (the time t times hours h is of the dimension time squared). Or the reader confounds name and units. (The use of italic fonts for names and roman fonts for units might not be noticeable and is a convention that is not used everywhere.)

 

The above labeling should be improved for communication, documentation or publication purposes.

 

A quick attempt using strings and the options useuints and labels.

plot(1361*Units:-Unit(W/m^2)*sin((1/12)*Pi*t/Units:-Unit(h)), t = 0 .. 12*Unit('h'), useunits = ['d', kW/m^2], labels = ["Time t in days", "Exposure G in kV/m^2"])

 

Axes are now consistent and can be interpreted unambiguously. Formatting can still be improved.

 

Unfortunately, using the options useunits (for unit conversion) and labels this way introduces a new source of user error when labels are entered with the wrong units.

 

A way to address this and to ensure unit error-free plotting of expressions of physical quantities is the following:

 

Step1: Define two lists, one for the units to display and the other for the names to display

a := [Unit('s'), Unit('W'/'cm'^2)]; b := [t, G]

[t, G]

(3)

Step2: Compute labels from the lists

This step avoids the labeling error: No manual entry of units in labels required.

c := [b[1]/a[1], typeset(b[2]/a[2])]; d := [typeset(b[1], "  ", "&lobrk;", a[1], "&robrk;"), typeset(b[2], "  &lobrk;", a[2], "&robrk;")]; e := [typeset(b[1], "  ", "(", a[1], ")"), typeset(b[2], "  (", a[2], ")")]

[typeset(t, "  ", "(", Units:-Unit(s), ")"), typeset(G, "  (", Units:-Unit(W/cm^2), ")")]

(4)

NULL

 

Dimensionless labels

 Double brackets

Parenthesis

plot(1361*Units:-Unit(W/m^2)*sin((1/12)*Pi*t/Units:-Unit(h)), t = 0 .. 12*Unit('h'), useunits = a, labels = c)

 

plot(1361*Units:-Unit(W/m^2)*sin((1/12)*Pi*t/Units:-Unit(h)), t = 0 .. 12*Unit('h'), useunits = a, labels = d)

 

plot(1361*Units:-Unit(W/m^2)*sin((1/12)*Pi*t/Units:-Unit(h)), t = 0 .. 12*Unit('h'), useunits = a, labels = e)

 

The axis values equal physical quantities divided by their units. The algebraic equation G*cm^2/W = 0.8e-1, for example, is physically speaking correct. Most functions of Maple can process dimensionless expression of the kind G*cm^2/W if G is given with appropriate physical units.

This way of using physical quantities is consistent with ISO 80000.  

Used in Maple to enter units in 2D-Math input mode

Can be confounded with functional notation. Units are therefore often written as a whole word (e.g. seconds instead of s).

 

 

NULL

The time to produce the above three plots was about 10 Minutes. The most part was spent to get the typesetting of the second and third plot correct.

 

What takes significant more time (more a question of hours when Typesetting is used for the first time) are

 

Labels with "/ cm^(2) "or 1/cm^2 formatting.

 

This formatting might be preferred but is unfortunately again not free from user errors. (I would probably use it if there was a simple and safe way).

f := [b[1]/a[1], b[2]/`#mrow(mo("W "),mo(" "),mo(" / "),msup(mo("cm"),mn("2")))`]; g := [typeset(b[1], "  ", "&lobrk;", a[1], "&robrk;"), typeset(b[2], "  &lobrk;", (`@`(`@`(Units:-Unit, numer), op))(a[2]), "/", (`@`(`@`(Units:-Unit, denom), op))(a[2]), "&robrk;")]; h := [typeset(b[1], "  ", "(", Unit('s'), ")"), typeset(b[2], "  (", `#mrow(mo("W"),mo(" "),msup(mo("cm"),mn("-2")))`, ")")]

[typeset(t, "  ", "(", Units:-Unit(s), ")"), typeset(G, "  (", `#mrow(mo("W"),mo(" "),msup(mo("cm"),mn("-2")))`, ")")]

(5)

 

plot(1361*Units:-Unit(W/m^2)*sin((1/12)*Pi*t/Units:-Unit(h)), t = 0 .. 12*Unit('h'), useunits = a, labels = f)

 

plot(1361*Units:-Unit(W/m^2)*sin((1/12)*Pi*t/Units:-Unit(h)), t = 0 .. 12*Unit('h'), useunits = a, labels = g)

 

plot(1361*Units:-Unit(W/m^2)*sin((1/12)*Pi*t/Units:-Unit(h)), t = 0 .. 12*Unit('h'), useunits = a, labels = h)

 

NULL

 

 

 

Remarks

• 

For two reasons, I have not given an example with the often used square brackets [ ] because:
    
    Maple uses square brackets already for lists and indexing purposes,
    and ISO 80000 uses square brackets as an operator that extracts the unit from a physical quantity (e.g.       [G] = Unit('W'/'cm'^2)).

• 

Adding a unit to each value at axes ticks would definitely be a nice labeling feature for simple units.

• 

Programmatically analyzing the units defined in list a above and converting them in a generic way to a typesetting structure is not possible with a few high-level commands.

 

• 

For inline quotients like in 1/2, an additional backslash must be entered in 2D-Math: \/  

Unit('W')/Unit('cm')^2

Units:-Unit(W)/Units:-Unit(cm)^2

(6)

     This will not prevent evaluation to a normal quotient but the input can be used to create an atomic variable (select with mouse -> 2-D Math -> Atomic Variable)

`#mrow(mfenced(mi("W",fontstyle = "normal"),open = "&lobrk;",close = "&robrk;"),mo("&sol;"),mo("&InvisibleTimes;"),msup(mfenced(mi("cm",fontstyle = "normal"),open = "&lobrk;",close = "&robrk;"),mn("2")),mo("&InvisibleTimes;"))`

`#mrow(mfenced(mi("W",fontstyle = "normal"),open = "&lobrk;",close = "&robrk;"),mo("&sol;"),mo("&InvisibleTimes;"),msup(mfenced(mi("cm",fontstyle = "normal"),open = "&lobrk;",close = "&robrk;"),mn("2")),mo("&InvisibleTimes;"))`

(7)

     This makes labeling much easier as compared to typesetting commands (compare to the above statements).

f := [b[1]/a[1], b[2]/`#mrow(mfenced(mi("W",fontstyle = "normal"),open = "&lobrk;",close = "&robrk;"),mo("&sol;"),mo("&InvisibleTimes;"),msup(mfenced(mi("cm",fontstyle = "normal"),open = "&lobrk;",close = "&robrk;"),mn("2")),mo("&InvisibleTimes;"))`]

[t/Units:-Unit(s), G/`#mrow(mfenced(mi("W",fontstyle = "normal"),open = "&lobrk;",close = "&robrk;"),mo("&sol;"),mo("&InvisibleTimes;"),msup(mfenced(mi("cm",fontstyle = "normal"),open = "&lobrk;",close = "&robrk;"),mn("2")),mo("&InvisibleTimes;"))`]

(8)

In any case it is a good idea to read ?plot,typesetting before experimenting with typesetting.

 

Axes_with_unit_labels.mw

My personal preference is for dimensionless labels.

Note:

The solution to avoid labeling errors works only for Maple 2022 and higher.

Some plot commands do not support plotting with units, or they do not fully support it yet.

Good day.

I am working on a time series problem that uses 107 data sets (historic) and I wish to obtain a forecast for the next successive 12 events. I have obtained the time series plot for the predicted values and the associated dates separately (see attached), however - I am looking to get the solution in a more user-friendly format and was hoping someone could help me out.

Can someone tell me how to 

1. Express the data values only as whole number values (decimal-free)

2. Construct a table of data values and dates for the average forecast as well as the 2nd and 98th percentile forecasts?

Thanks for reading!

MaplePrimes_TS_Example.mw

Here is the layout challenge: Whenever the value of a physical quantity is one as in

mass = 1* Unit('kg')

automatic simplification removes the one and the output displays

Desired would be which reads better (in particular when used inline in text passages).

Using the Empty Symbol

mass = ``(1)*Unit('kg')

an expression with parenthesis is obtained (that can be removed in subsequent calculations with the expand command) which looks worse than my current workaround of using floats

(Introducing floats in expressions is not always acceptable)
So the idea would be to use the empty symbol for initial parameter definition in textbook style layout and remove it with expand in subsequent calculations.

Any other symbol that prints as "1" that can be removed in later calculations by Maple commands is welcome as well. It should be better than

mass=`#mrow(mo("1"));`*Unit('kg');
subs(`#mrow(mo("1"));` = 1, %);

 

Hi all,

I wrote a little simple minded procedure for finding the sum of divisors for a positive integer.  I thought I would share.

sum_of_proper_divisors.mw

sum_of_proper_divisors.pdf

Has someone written better code for this task?

Regards,
Matt

When there are print commands in a loop their content is printed as soon as this command is executed.
This is not the case with printf whose displays are delayed (buffered?).
Is there a way to force the display of printf when the command is executed?

TIA

Motivation: I want to display intermediate execution times in a prettier way than print offers.

Is it possible to enlarge the sliders in Explore(plot(...), ...) and increase their "resolution" (meaning to have a higher precision when the slider is moved)?
If Maple does offer this option, could you tell me from what version this is the case

TIA

For a project I need to construct a large symbolic adjoint matrix, hoping it can be factored afterward into nice expressions.
In the worksheet, I present an adjoint matrix using permuted Hadamard products. What puzzles me is that only for even dimensions, I need to multiply it (elementwise) with a parity matrix. Okay, not a specific Maple question, but maybe someone can help me out.

Download Adjoint.mw

The attached worksheet shows that Maple 2023 produces an incomplete plot of a function.  Maple 2021, however, produces the full graph.  I wonder if Maple 2023's behavior is due to a bad setting in my environment or a plotting bug in Maple.

restart;

kernelopts(version);

`Maple 2023.2, X86 64 LINUX, Oct 25 2023, Build ID 1753458`

y := -cos(sqrt(x))*x^3/(-x^2 + 24*cos(sqrt(x)) + 12*x - 24);

-cos(x^(1/2))*x^3/(-x^2+24*cos(x^(1/2))+12*x-24)

plot(y, x=0..1);

Here is the graph of the same function plotted correctly in Maple 2021:

Download cannot-plot.mw

 

A new “Sudoku Puzzle” document is now on Maple Learn! Sudoku is one of the world’s most popular puzzle games and it is now ready to play on our online platform. 

This document is a great example of how Maple scripts can be used to create complex and interactive content. Using Maple’s built-in DocumentTools:-Canvas package, anyone can build and share content in Maple Learn. If you are new to scripting, a great place to start is with one of the scripting templates, which are accessible through the Build Interactive Content help page. In fact, I built the Sudoku document script by starting from the “Clickable Plots” template.

A Sudoku puzzle is a special type of Latin Square. The concept of a Latin Square was introduced to the mathematical community by Leonard Euler in his 1782 paper, Recherches sur une nouvelle espèce de Quarrés, which translates to “Research on a new type of square”. A Latin Square is an n by n square array composed of n symbols, each repeated exactly once in every row and column. The Sudoku board is a Latin Square where n=9, the symbols are the digits from 1 to 9,  and there is an additional requirement that each 3 by 3 subgrid contains each digit exactly once. 

Mathematical research into Sudoku puzzles is still ongoing. While the theory about Latin Squares is extensive, the additional parameters and relative novelty of Sudoku means that there are still many open questions about the puzzle. For example, a 2023 paper from Peter Dukes and Kate Nimegeers examines Sudoku boards through the lenses of graph theory and linear algebra.

The modern game of Sudoku was created by a 74-year-old Indiana retiree named Howard Garnes in 1979 and published under the name “Number Place”. The game had gained popularity in Japan by the mid-1980s, where it was named “Sudoku,” an abbreviation of the phrase “Sūji wa dokushin ni kagiru,” which means “the numbers must be single”.

Today, Sudoku is a worldwide phenomenon. This number puzzle helps players practice using their logical reasoning, short-term memory, time management, and decision-making skills, all while having fun. Furthermore, research from the International Journal of Geriatric Psychiatry concluded that doing regular brain exercises, like solving a Sudoku, is correlated with better brain health for adults over 50 years old. Additionally, research published in the BMJ medical journal suggests that playing Sudoku can help your brain build and maintain cognition, meaning that mental decline due to degenerative conditions like Alzheimer’s would begin from a better initial state, and potentially delay severe symptoms. However, playing Sudoku will by no means cure or prevent such conditions.

If you are unfamiliar with the game of Sudoku, need a refresher on the rules, or want to improve your approach, the “Sudoku Rules and Strategies” document is the perfect place to start. This document will teach you essential strategies like Cross Hatching:

And Hidden Pairs:

After reading through this document, you will have all the tools you need to start solving puzzles with the “Sudoku Puzzle” document on Maple Learn. 

Have fun solving!

I would like to plot graph of 2d function f(x, y) = (-ax/(1+y^2), x+by) where x from -5 to 5 and y from -5 to 5 and parameter bar of a and b. Thank you 

As I am new to using the statistics package, I have some doubts about how to perform certain operations in Maple.

For example, let sigma be a variable containing any real number, finding the average is easy, just use the command A0:=Median(sigma) and if you want A (see image below) just take A1:=A0^2. However, the terms that make up <sigma^2> cause a certain difficulty, how to do this? In other words, calculate the B of the image?

sigma := [2, 4, 0, 6]

AMedia := (2 + 4 + 0 + 6)/4;
AMedia1 := AMedia^2;

 

A0 := Median(sigma)

A1 := A0^2

BMedia := (2^2 + 4^2 + 0^2 + 6^2)/4

sigma0 := sigma^2

sigma1 := Mean(sigma0)

.......................................................................................

First 26 27 28 29 30 31 32 Last Page 28 of 2097