acer

32373 Reputation

29 Badges

19 years, 334 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

It is not necessary to write M:-Pn instead of just Pn (for each n), in the situation you describe.

In my experience is far more common to write the references as just Pn in equivalent situations.

You wrote that using the form M:-Pn somestimes "...saved me a lot of trouble". What trouble was that?

If you store them properly (in a .mla Maple Library Archive file) then you wouldn't need to read any of them in each session you want to access them, and they could all get seen as easily as each other.

To access any of them you'd simply need libname to be augmented with the location of the new personal .mla Library file.

- You could augment libname in a worksheet's Startup code, which would affect just that worksheet.
- Or you could augment libname in a personal initialization file.
- Alternatively you could put the .mla Library file under a new folder with a name like whatever the following returns, and have Maple automatically augment libname upon relaunch:
    cat(kernelopts(':-homedir'),"/maple/toolbox/mypackage/lib")

Personally, I think that reading in the plaintext source files each time you want to run a session or worksheet is an unnecessary, overly complicated burden.

But if for some strange reason I was unable to use an .mla file, then I'd probably just create a single .mpl text file that contained the means to input all the individual procedures. That could contain various (say, five) read calls for each of the individual source files, or it could contain the definition of a package module that accessed the individual sources with, say, the $include directive. In this scenario I could subsequently issue one read call of that master (build) file, and all would be avaliable.

[edit] There is an example of writing a module using the $include directive (and the preprocessor) for its member procedures in Section 11.4 of the Programming Guide.

You may well be satisfied with the right-click context-menu approach.

By the way, there is a Help page for that (it takes a little while to fully load and focus on the approriate section in my webbrowser).

Using the result in the left-hand side of an assignment requires mouse actions to copy&paste, so using the mouse to convert to Atomic Variable may be fine.

If you're interested, your expression can also be converted programmatically:

restart;

kernelopts(version);

`Maple 2022.1, X86 64 LINUX, May 26 2022, Build ID 1619613`

 

`convert/identifier`:=proc(x)
   cat(`#`,convert(:-Typesetting:-Typeset(x),
                   ':-compose',`global`,name));
end proc:

 

 

expr := abs(A[0])^2;

abs(A[0])^2

convert(expr, identifier);

`#msup(mfenced(msub(mi("A"),mn("0")),open = "|",close = "|",msemantics = "abs"),mn("2"))`

lprint(%);

`#msup(mfenced(msub(mi("A"),mn("0")),open = "|",close = "|",msema\
ntics = "abs"),mn("2"))`

 

I'll use the right-click context menu conversion (explained by Robert)
on the next 2D Input

 

`#msup(mrow(mo("|"),msub(mi("A"),mn("0")),mo("|")),mn("2"))`

`#msup(mrow(mo("|"),msub(mi("A"),mn("0")),mo("|")),mn("2"))`

lprint(%);

`#msup(mrow(mo("|"),msub(mi("A"),mn("0")),mo("|")),mn("2"))`

 

Both of these results are Maple names, to which you can assign values.

 

You should be able to copy&paste their pretty-printed output and use
that as the left-hand side of an assignment.

 

Download atomicvar_constr.mw

ps. In versions before Maple 2017 an Atomic Variable was called an Atomic Identifier.

Try

   ':-b' = b

using single right-ticks (a.k.a. uneval quotes).

The LHS of that equation is an unevaluated reference to the global name b (and not the procedure test1's parameter by a similar name). The RHS evaluates to the value you want to pass along.

test1:=proc(a::posint, {b::posint:=1})::posint:
	return a + b:
end proc:

test2:=proc(a::posint, {b::posint:=1} )::posint:
	return a*test1(a, ':-b' = b);
end proc:

test2(2, b=3);
             10

[edit] I much prefer this approach over using _options['b'], which Joe has suggested.

I would not use remove(has, ..., unit) since that would act quite differently on expressions other than a product. (Your expression assigned to a is of type `*`, but it's unclear that is all you want to be able to handle robustly.)

I would suggest these preliminary steps be part of the process:
1) combine addends into common units
2) optionally convert to a preferred unit (or accept the base unit for the current system, default=SI)
3) use convert(...,unit_free) to strip off the remaning units, as suitable

For example, quoting unprotected global versions of option names for robustness,

restart;

kernelopts(version);

`Maple 2015.2, X86 64 LINUX, Dec 20 2015, Build ID 1097895`

a := 3*Unit(m);

3*Units:-Unit('m')

convert(combine(a, ':-units'), ':-unit_free');

3

b := 7*Unit(mm)+3*Unit(m);

7*Units:-Unit('mm')+3*Units:-Unit('m')

convert(combine(b, ':-units'), ':-unit_free');

3007/1000

Download remove_units.mw

Maple was originally designed for efficient symbolic computation, as opposed to pretty mathematical typesetting.

A key aspect of that original design was the storing in a session (by the Maple engine, a.k.a. "kernel") of only one representation of any sum or product of terms (commuting under usual addition and multiplication). Any other input of an equivalent form of an expression -- with terms commuted -- gets uniquified to the stored form.

Hence, if the form
   -b + a
is first formed in a session then construction/entry of the alternate form,
  a - b
gets automatically recognized as the earlier, stored form.

However, for this kind of expression the terms can be reordered by the sort command. For example,

restart;

expr := -b + a;

-b+a

The order in which I type the entries does not matter.
What matters is that the originally existing form -b+a
is already stored in Maple's internal memory (ie. in its
unquification table of expressions).

a - b;

-b+a

It is possible to resort the items in this sum, forcing a
particular ordering of names. This causes the stored
(uniquified) expression to be replaced in internal memory.

sort(expr, order=plex(a,b));

a-b

a - b;

a-b

-b + a;

a-b

restart;


We can also go the other way...

alt := a - b;

a-b

-b + a;

a-b

sort(alt, order=plex(b,a));

-b+a

-b + a;

-b+a

a - b;

-b+a

Download simpl_examp.mw

As far as mathematical typesetting goes, one could also display the expression (in marked up "2D" math), without the above issues interfering. Here I use so-called inert operators, so avoid those uniquification issues,

restart;

-b + a;

-b+a

a - b;

-b+a

Here I use prefix syntax for the inert operator,

foo := `%+`(a, -b);

`%+`(a, -b)

InertForm:-Display(foo);

0, "%1 is not a command in the %2 package", _Hold, Typesetting

I can also use this infix syntax,

bar := a %+ (-b);

`%+`(a, -b)

InertForm:-Display(bar);

0, "%1 is not a command in the %2 package", _Hold, Typesetting

Download inert_add_ex.mw

If you have other, particular usage scenarios then you should describe them precisely, as a best bet on getting adequate assistance.

You asked about plotting the elements of a sequence, so I'll address that aspect.

You can use the plot command to show a discrete collection of x-y points.

The data can be formed in a list of lists, or a Matrix. (I'll do it as a list of lists, below.)

You can adjust other qualities, such as the symbol, the style, the color, etc. See the Help page plot,coptions for more details.

restart;

L := [seq([n,(-2)^n], n=-2..2)];

[[-2, 1/4], [-1, -1/2], [0, 1], [1, -2], [2, 4]]

plot(L, style=point, symbolsize=15,
        symbol=solidcircle, color=red);

plot(L, style=pointline, symbolsize=15,
        symbol=solidcircle, color=red);

Download point_plot_ex.mw

The plot shows that the local maximum is the global maximum over that range t=0..2.

So you can use the Optimization:-Maximize command.

In my opinion this is more straightforward, flexible, and likely more accurate in general than either examining the discrete data points from a plot structure, or using an interpolated spline.

I use the output=listprocedure option for dsolve, as a convenient way to get at procedures for xn(t) and zn(t), similarly to what I showed in your previous Question.

restart

q1 := 3b1 := 20l1 := 22r1 := .5m1 := 900g1 := -11
u0 := -10

v0 := -10x0 := 0z0 := 20xa := 0za := z0+5mu1 := .1omega0 := -24

mn := q1*g1*b1*za

fz := mn/za

TM1 := (2/5)*m1*r1^2

``

"Lp(t):=sqrt((xn(t)-xa)^(2)+(zn(t)-za)^(2)) :"

"Lr(t):=piecewise(sqrt(zn(t)^(2)+A1(t)^(2))>0,sqrt(zn(t)^(2)+A1(t)^(2)),0)  :"

"A1(t):=piecewise((xa-xn(t))>0,xa-xn(t),0):"

"sinalpha1(t):=piecewise((xa-xn(t))>0,(xa-xn(t))/(Lp(t)),0) :"

"cosalpha1(t):=piecewise((xa-xn(t))>0,((za-zn(t)))/(Lp(t)),1) :"

"alpha2(t):=arccos(cosalpha1(t))+phi(t) :"

NULL

"S1(t):=piecewise(zn(t)>=0 and xn(t)<=xa,fz*Lr(t),0):"

"S1z(t):=S1(t)*cos(phi(t)):"

"S1x(t):=S1(t)*sin(-phi(t)):"

"S2(t):=piecewise(xn(t)<=xa and zn(t)>0, (-1) S1(t)*exp(mu1*alpha2(t)),0) :"

"S2x(t):=S2(t)*sinalpha1(t):"

"S2z(t):=S2(t)*cosalpha1(t) :"

"Sx(t):=S2x(t)+S1x(t) :"

"Sz(t):=S1z(t)+S2z(t)+m1*g1:"

shape_factor := 10

"Fr(t):=S2(t)+S1(t)+shape_factor:"

NULL

NULL

"m(t):=Lr(t)q1*b1:"

NULL

"J(t):=1/(3)*m(t)*Lr(t)^(2) :"

"M(t):=g1*m(t)*(Lr(t))/(2)*sin(phi(t)) :"

k := 50*m1

NULL

NULL

``

eqx1 := m1*(diff(u(t), t)) = 0.; eqz1 := m1*(diff(v(t), t)) = m1*g1

900*(diff(u(t), t)) = 0.

900*(diff(v(t), t)) = -9900

eqx0 := diff(x(t), t) = u(t); eqz0 := diff(z(t), t) = v(t)

``

eqx1n := m1*(diff(un(t), t)) = Sx(t); eqz1n := m1*(diff(vn(t), t)) = m1*g1+S1z(t)+S2z(t)

eqx0n := diff(xn(t), t) = un(t); eqz0n := diff(zn(t), t) = vn(t)

eqr1 := TM1*(diff(omega(t), t)) = (S2(t)+S1(t)+shape_factor)*r1

NULL

u0n := u0*m1/(m1+mn)

15/26

NULL

phid0 := 2*u0n(t)/(l1-z0)

15/26

NULL

eqp := (diff(phi(t), t, t))*J(t) = M(t)-k*(diff(phi(t), t))

with(plots)

NULL

NULL

ini := x(0) = x0, u(0) = u0, z(0) = z0, v(0) = v0, xn(0) = x0, un(0) = u0, zn(0) = z0, vn(0) = v0, omega(0) = omega0, phi(0) = 0, (D(phi))(0) = phid0

sol1 := dsolve({eqp, eqr1, eqx0, eqx0n, eqx1, eqx1n, eqz0, eqz0n, eqz1, eqz1n, ini}, {omega(t), phi(t), u(t), un(t), v(t), vn(t), x(t), xn(t), z(t), zn(t)}, type = numeric, output = listprocedure)

odeplot(sol1, [t, 180*arccos(cosalpha1(t))/Pi], 0 .. 2, legend = ["alpha"], linestyle = [dashdot, solid], size = [300, 300])

temp := unapply(subs(xn = Xn, zn = Zn, cosalpha1(t)), t)

proc (t) options operator, arrow; piecewise(0 < -Xn(t), (25-Zn(t))/(Xn(t)^2+(Zn(t)-25)^2)^(1/2), 1) end proc

Xn := eval(xn(t), sol1)

Zn := eval(zn(t), sol1)

plot([t, 180*arccos(temp(t))/Pi, t = 0 .. 2], legend = ["alpha"], linestyle = [dashdot, solid], size = [300, 300])

Optimization:-Maximize(180*arccos(temp(t))/Pi, t = 0 .. 2)

[HFloat(22.00633969122702), [t = HFloat(0.6180259177597804)]]

 

Download evalv_max_ac.mw

There's nothing intrinsically wrong with returning NULL as an explicit statement. This is a common programming technique in Maple.

   return NULL;

One alternative is to have a statement that does nothing and happens to also return NULL, but that would be obfuscation for no benefit. The explicit return call is clear and understandable programming.

ps. Using assign brings the burden of having to remember that happens to return NULL. Since you didn't already think of that then using it would entail one more thing for you to remember. You already know what explicitly returning NULL means, and it's more likely that others reading your code would too.

Without a range supplied fsolve is trying to find roots from A=-infinity to A=infinity. That search is missing values very close to zero.

[edit] The situation is made more difficult due to the root lying close to a singularity, where it is steep, and with the expression nonreal on its other side.

restart:

N := 60: L := 10: a := 2*10^(-12):

eqn:=ns=1-((((N/A)+((A*L)^(L/(1-L))))^((1-L)/L))/(A*L))*(2+(a*exp((((N/A)+((A*L)^(L/(1-L)))))^(1/L)))/((A*L*(((N/A)+((A*L)^(L/(1-L))))^((L-1)/L)))+((a*exp((((N/A)+((A*L)^(L/(1-L)))))^(1/L)))))):

seq(fsolve(eqn,A=0..1e-6),ns=[0.9603,0.9647,0.9691]);

0.9300682773e-9, 0.3054144319e-8, 0.1172662805e-7

Download A_ac.mw

Here are two ways:
1) using plots:-odeplot directly with the sol1 returned by dsolve.
2) using plots:-spacecurve with procedures returned in sol1 by dsolve.

The second way requires some use of eval to extract the various results (after applying procedures at a numeric t value). There are a couple of ways to do that. I show one way that utilizes the output=listprocedure option. It can also be done without that option, using eval slightly differently. The first way looks simpler to me.

restart

q1 := 3

b1 := 20

l1 := 22

r1 := .5

m1 := 900

g1 := -11

u0 := -10

v0 := -10

 

x0 := 0

 

z0 := 20

 

xa := 0

 

za := z0+5

 

mu1 := .1

omega0 := -24

mn := q1*g1*b1*za

fz := mn/za

TM1 := (2/5)*m1*r1^2

3

20

22

.5

900

-11

-10

-10

0

20

0

25

.1

-24

-16500

-660

90.00000000

NULL

"Lp(t):=sqrt((xn(t)-xa)^(2)+(zn(t)-za)^(2)) "

proc (t) options operator, arrow, function_assign; sqrt((xn(t)-xa)^2+(zn(t)-za)^2) end proc

"Lr(t):=piecewise(sqrt(zn(t)^(2)+A1(t)^(2))>0,sqrt(zn(t)^(2)+A1(t)^(2)),0)  "

proc (t) options operator, arrow, function_assign; piecewise(0 < sqrt(zn(t)^2+A1(t)^2), sqrt(zn(t)^2+A1(t)^2), 0) end proc

"A1(t):=piecewise((xa-xn(t))>0,xa-xn(t),0)"

proc (t) options operator, arrow, function_assign; piecewise(0 < xa-xn(t), xa-xn(t), 0) end proc

"sinalpha1(t):=piecewise((xa-xn(t))>0,(xa-xn(t))/(Lp(t)),0) "

proc (t) options operator, arrow, function_assign; piecewise(0 < xa-xn(t), (xa-xn(t))/Lp(t), 0) end proc

"cosalpha1(t):=piecewise((xa-xn(t))>0,((za-zn(t)))/(Lp(t)),1) "

proc (t) options operator, arrow, function_assign; piecewise(0 < xa-xn(t), (za-zn(t))/Lp(t), 1) end proc

"alpha2(t):=arccos(cosalpha1(t))+phi(t) "

proc (t) options operator, arrow, function_assign; arccos(cosalpha1(t))+phi(t) end proc

NULL

"S1(t):=piecewise(zn(t)>=0 and xn(t)<=xa,fz*Lr(t),0)"

proc (t) options operator, arrow, function_assign; piecewise(0 <= zn(t) and xn(t) <= xa, fz*Lr(t), 0) end proc

"S1z(t):=S1(t)*cos(phi(t))"

proc (t) options operator, arrow, function_assign; S1(t)*cos(phi(t)) end proc

"S1x(t):=S1(t)*sin(-phi(t))"

proc (t) options operator, arrow, function_assign; S1(t)*sin(-phi(t)) end proc

"S2(t):=piecewise(xn(t)<=xa and zn(t)>0, (-1) S1(t)*exp(mu1*alpha2(t)),0) "

proc (t) options operator, arrow, function_assign; piecewise(xn(t) <= xa and 0 < zn(t), -S1(t)*exp(mu1*alpha2(t)), 0) end proc

"S2x(t):=S2(t)*sinalpha1(t)"

proc (t) options operator, arrow, function_assign; S2(t)*sinalpha1(t) end proc

"S2z(t):=S2(t)*cosalpha1(t) "

proc (t) options operator, arrow, function_assign; S2(t)*cosalpha1(t) end proc

"Sx(t):=S2x(t)+S1x(t) "

proc (t) options operator, arrow, function_assign; S2x(t)+S1x(t) end proc

"Sz(t):=S1z(t)+S2z(t)+m1*g1"

proc (t) options operator, arrow, function_assign; S1z(t)+S2z(t)+m1*g1 end proc

shape_factor := 10

10

"Fr(t):=S2(t)+S1(t)+shape_factor"

proc (t) options operator, arrow, function_assign; S2(t)+S1(t)+shape_factor end proc

NULL

``

"m(t):=Lr(t)q1*b1"

proc (t) options operator, arrow, function_assign; Lr(t)*q1*b1 end proc

``

"J(t):=1/(3)*m(t)*Lr(t)^(2) "

proc (t) options operator, arrow, function_assign; (1/3)*m(t)*Lr(t)^2 end proc

"M(t):=g1*m(t)*(Lr(t))/(2)*sin(phi(t)) "

proc (t) options operator, arrow, function_assign; (1/2)*g1*m(t)*Lr(t)*sin(phi(t)) end proc

k := 50*m1

45000

NULL

NULL

NULL

eqx1 := m1*(diff(u(t), t)) = 0.; eqz1 := m1*(diff(v(t), t)) = m1*g1

900*(diff(u(t), t)) = 0.

900*(diff(v(t), t)) = -9900

eqx0 := diff(x(t), t) = u(t); eqz0 := diff(z(t), t) = v(t)

diff(x(t), t) = u(t)

diff(z(t), t) = v(t)

NULL

eqx1n := m1*(diff(un(t), t)) = Sx(t); eqz1n := m1*(diff(vn(t), t)) = m1*g1+S1z(t)+S2z(t)

eqx0n := diff(xn(t), t) = un(t); eqz0n := diff(zn(t), t) = vn(t)

diff(xn(t), t) = un(t)

diff(zn(t), t) = vn(t)

eqr1 := TM1*(diff(omega(t), t)) = (S2(t)+S1(t)+shape_factor)*r1

NULL

u0n := u0*m1/(m1+mn)

15/26

``

phid0 := 2*u0n(t)/(l1-z0)

15/26

``

eqp := (diff(phi(t), t, t))*J(t) = M(t)-k*(diff(phi(t), t))

with(plots)

``

NULL

ini := x(0) = x0, u(0) = u0, z(0) = z0, v(0) = v0, xn(0) = x0, un(0) = u0, zn(0) = z0, vn(0) = v0, omega(0) = omega0, phi(0) = 0, (D(phi))(0) = phid0

x(0) = 0, u(0) = -10, z(0) = 20, v(0) = -10, xn(0) = 0, un(0) = -10, zn(0) = 20, vn(0) = -10, omega(0) = -24, phi(0) = 0, (D(phi))(0) = 15/26

sol1 := dsolve({eqp, eqr1, eqx0, eqx0n, eqx1, eqx1n, eqz0, eqz0n, eqz1, eqz1n, ini}, {omega(t), phi(t), u(t), un(t), v(t), vn(t), x(t), xn(t), z(t), zn(t)}, type = numeric, output = listprocedure)

odeplot(sol1, [xn(t), t, zn(t)], t = 0 .. 1, thickness = 3, color = red)

Xn := eval(xn(t), sol1); Zn := eval(zn(t), sol1)

spacecurve([Xn(t), t, Zn(t)], t = 0 .. 1, thickness = 3, color = red)

Download unable_to_evaluate_ac.mw

@Syed Asadullah Shah

Your code doesn't initialize F (or T) as an Array, so your assignments to indexed references cause a table to be assigned to F (and T). But your code also shows F(k+2) , which is nonsense. What do you intend by that?

Your attempted second call to pade (inside the solve call) is messed up 2D Input, and is actually being parsed as pade*(...) . You could get rid of that multiplication.

The n that appears in the expressions returned by your procedure gnrpoly is the local variable n of that same procedure.

That escaped local name n is not the same as the global name n, which you are trying to use for substitution at the top-level. That is why your calls to eval are not making a substitution.

There are several ways to fix your issue.

One approach is to utilize unapply (w.r.t that local name n) inside gnrpoly, and have gnrpoly return a procedure. Another way is to pass in the variable name (eg. n, from the higher level) as another parameter of gnrpoly. Below I show the former approach, with unapply.

restart

interface(rtablesize = 12)

[10, 10]

i) Recurrence relation

 

"Recurrence Relation for next column. . The matrix starts at 1,1 not 0,0 A(i,j)=A(i-1,j-1)*(j-2)+A(i,j-1)"
NULL

``NULL

Msize := 12

A := Matrix(Msize)

A[1, 1] := 1

1

for i from 2 to Msize do for j from i to Msize do A[i, j] := A[i, j-1]*(j-2)+A[i-1, j-1] end do end do

A

Matrix(%id = 36893628682748306964)

NULL

NULL

viia)  General formula for diagonals:-

Select diagonal  & Difference Table procedures

 

diag := proc (M::Matrix, dg::posint) local i, rd, c, dt; rd := LinearAlgebra:-RowDimension(M); dt := Matrix(rd, rd); for i from dg to rd do dt[1+i-dg, 1] := M[1+i-dg, i] end do; return dt end proc

diag(A, 5)

Matrix(%id = 36893628682748321172)

diftab := proc (M::Matrix) local i, j, cl, rd; rd := LinearAlgebra:-RowDimension(M); for i from rd by -1 to 1 do if M[i, 1] = 0 then rd := rd-1 else break end if end do; cl := rd; for i from 2 to cl do for j to rd-i do M[j, i] := M[j+1, i-1]-M[j, i-1] end do end do; print(M); return M end proc

diftab(diag(A, 4))

Matrix(%id = 36893628682599410612)

gnrpoly := proc (M::Matrix) local i, p, rd, n; rd := LinearAlgebra:-RowDimension(M); for i from rd by -1 to 1 do if M[1, i] = 0 then rd := rd-1 else break end if end do; p := 0; for i from 0 to rd-1 do p := p+M[1, i+1]*binomial(n, i) end do; return unapply(p, n) end proc

"for i from 0 to 3 do print("Diagonal  ",i);   eqn:=gnrpoly(diftab(diag(A,i+1)));    cat(poly,i):=unapply(factor(expand(eqn(n))),n);    print("--------------------------------------------------------------");  end do"

"--------------------------------------------------------------"

NULL

NULL

NULL

The equations do not evaluate. They are acting inert

poly1(n)

(1/2)*n*(1+n)

poly1(2)

3

poly2(n)

(1/24)*n*(3*n+5)*(n+2)*(1+n)

poly2(3)

35

poly3(n)

(1/48)*n*(1+n)*(n+3)^2*(n+2)^2

poly3(-1)

0

eval(eqn)

proc (n) options operator, arrow; 6*n+38*binomial(n, 2)+93*binomial(n, 3)+111*binomial(n, 4)+65*binomial(n, 5)+15*binomial(n, 6) end proc

eqn(4)

735

``

Download Q_10-10-22_gentrate_eqn_ac.mw

ps. You used the term "inert". This is not an issue of inertness.

I disagree with your claim that "foo() has z" used.

The only z that foo "has" (apart from its declaration as a local of foo) is as a parameter of an anonymous procedure defined and used within foo. And that is not the same z as the local z of foo.

The local z of foo is not used within foo.

An alternative is,

  plots:-textplot([2,2,InertForm:-Display(%floor(5.5),'inert'=false)])

First 60 61 62 63 64 65 66 Last Page 62 of 336