acer

32313 Reputation

29 Badges

19 years, 310 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

I'm not quite sure what you're after. Is it something like this?

restart:

 

alias(X = X(t)):

alias(phi=k*X(t)):

alias(Phi=Phi(X(t))):

 

kin := 1/2*M*Diff(X, t)^2 + 1/2*m*Diff(Phi, t)^2;

(1/2)*M*(Diff(X, t))^2+(1/2)*m*(Diff(Phi, t))^2

eval(kin, Phi=phi);

(1/2)*M*(Diff(X, t))^2+(1/2)*m*(Diff(phi, t))^2

value(%);

(1/2)*M*(diff(X, t))^2+(1/2)*m*k^2*(diff(X, t))^2

convert(%, D);

(1/2)*M*(D(X))(t)^2+(1/2)*m*k^2*(D(X))(t)^2

Download alias_diff_ex.mw

If your goal is simply to print the procedure in way that is formatted suitably as 1D plaintext Maple notation source code then you could try the new %P format descriptor of the printf command.

Eg,

interface(verboseproc=3):
printf("%P",eval(RonanRoutines:-SignedArea));

If you want that exported to a text file then you could use fprintf instead of printf.

Note that showstat does not show "\" line-continuation characters, which may have been part of your problem. But (as used above) printf will include them. That can allow the 1D parser to properly deal with such split lines.

ps. In general you should not try to paste plaintext Maple notation code in as 2D Input. (That doesn't seem to have been your original problem. But I suggest you note it anyway.)

Your given example can also be handled as follows. (But without the bigger picture it's unnecessarily difficult to guess whether this will help with your broader goal.)

convert(a*x+6*x^2-10, 'horner', x);

           -10 + (a + 6 x) x

or perhaps,

sort(convert(a*x+6*x^2-10, 'horner', x), x);

            (6 x + a) x - 10

Your followup comments on other Answers indicate that you have some more involved example (and goal) which you have not yet shown.

A common scenario is that someone concocts their own approach for a larger goal, then gets bogged down in implementing one problematic step of that, and then asks only about how to resolve that particular step. It's often more useful to provide the more complete motivating example.

It did actually substitute, in your first example. It just didn't happen to simplify (in the way you apparently expected).  That can be done in several ways. Eg,

algsubs(n2+n1=n+6,a+(n1-b)*k+a+(n2-b)*k);

    k (-n2 + n + 6 - b) + (n2 - b) k + 2 a

simplify(%);

           k (-2 b + n + 6) + 2 a

If you don't like how algsubs works then you could also use simplify (with side-relations) here. Eg,

simplify(a+(n1-b)*k+a+(n2-b)*k, {n2+n1=n+6});

           k (-2 b + n + 6) + 2 a

By the way, you also mentioned a particular rearrangement of the simplified form. That too can be attained in several straightforward ways. Eg.

ans1 := algsubs(n2+n1=n+6,a+(n1-b)*k+a+(n2-b)*k):

collect(ans1,[a,b],simplify);

        2 a - 2 k b + k (n + 6)

collect(simplify(ans1),[a,b]);

        2 a - 2 k b + k (n + 6)

ans2 := simplify(a+(n1-b)*k+a+(n2-b)*k, {n2+n1=n+6}):

collect(ans2,[a,b]);

        2 a - 2 k b + k (n + 6)

One way is to utilize the showstat command, but using :: instead of :- as separator of the name, eg.

   showstat((Student:-Calculus1:-Roots)::RootsNumeric);

You may need to introduce extra round-brackets there, to resolve ambiguity.

Another way is to set,

   kernelopts(opaquemodules=false):

and then use showstat or print or the debugger as usual.

If you do not know the names of the local members of the module then you may utilize op for inspection, eg.

   op(eval(Student:-Calculus1:-Roots));
   op(3,eval(Student:-Calculus1:-Roots));

 

Another possibility is to use a parameter for the ratio x/r.

That sidesteps having to deal with the case that the x-Slider might have a value greater than the r-Slider.

restart;

A := (x,r) -> 2*sqrt(-x^2 + r^2)*x:

Explore(plots:-display(plot(p -> sqrt(r^2 - p^2), -r..r, color=blue),
                       plottools:-rectangle([-X*r,0], [X*r,sqrt(-(X*r)^2 + r^2)],
                                            style=line, color=red, thickness=4),
                       title=typeset("r = %1\nx = %2, Area = %3\n(Optimal: x=%4, Area=%5)",
                                     r, X*r, A(X*r,r), r/sqrt(2.), r^2 ),
                       axis[1]=[tickmarks=[`if`(X*r>r*0.05 and r-X*r>0.05, X*r="x",
                                                ceil(r)=ceil(r)),
                                           seq(u=u,u=floor(-r)..ceil(r))]],
                       scaling=constrained),
        parameters=[[r = 0.0 .. 4.0, label="r"],
                    [X = 0.0 .. 1.0, label="x/r"]],
        initialvalues=[X=1/sqrt(2.), r=2.0]);

 

Download rect_semicricle_explored.mw

Here are a few ways, involving purely numeric rootfinding (since you asked about that specifically).

r1 := fsolve(x^(1/x)=1.2);

         r1 := 1.257734541

r2 := fsolve(x^(1/x)=1.2, x, avoid={x=r1});

         r2 := 14.76745838

[fsolve(x^(1/x)=1.2, x=0..20, maxsols=2)];

      [1.257734541, 14.76745838]

Student:-Calculus1:-Roots(x^(1/x)=1.2, x=0..20);

      [1.257734541, 14.76745838]

Here's one way to get Maple to cough them up exactly. Note that the _BXXX coming out of solve is a boolean-valued parameter, ie. it has the assumption of having a value of 0 or 1. So we can evaluate using either of those values.

T := solve(x^(1/x)=12/10, real, allsolutions);

-LambertW(-_B6, -ln(6/5))/ln(6/5)

b := indets(T,And(name,Non(constant)));

{_B6}

eval(T, b[1]=1);

-LambertW(-1, -ln(6/5))/ln(6/5)

evalf(%);

14.76745838

eval(T, b[1]=0);

-LambertW(-ln(6/5))/ln(6/5)

evalf(%);

1.257734541

Download rfex.mw

This can be tidied up, but I've deliberately kept some steps separate in order to give you a better idea of what's going on.

You didn't make it clear whether N was a multiple of n, so I'll show a way that chops to length N, and another that pads the Matrix.

restart;

with(StringTools):

raw := "How. doIt   urnate**xtintoaM?\"atrixofle{{12345ttersinth   emostconvenient";

"How. doIt   urnate**xtintoaM?"atrixofle{{12345ttersinth   emostconvenient"

str := Select(IsAlpha,raw);

"HowdoIturnatextintoaMatrixoflettersinthemostconvenient"

n := 7;

7

L := Explode(str);

["H", "o", "w", "d", "o", "I", "t", "u", "r", "n", "a", "t", "e", "x", "t", "i", "n", "t", "o", "a", "M", "a", "t", "r", "i", "x", "o", "f", "l", "e", "t", "t", "e", "r", "s", "i", "n", "t", "h", "e", "m", "o", "s", "t", "c", "o", "n", "v", "e", "n", "i", "e", "n", "t"]

Matrix(iquo(nops(L),n),n,L[1..n*iquo(nops(L),n)]);

Matrix(7, 7, {(1, 1) = "H", (1, 2) = "o", (1, 3) = "w", (1, 4) = "d", (1, 5) = "o", (1, 6) = "I", (1, 7) = "t", (2, 1) = "u", (2, 2) = "r", (2, 3) = "n", (2, 4) = "a", (2, 5) = "t", (2, 6) = "e", (2, 7) = "x", (3, 1) = "t", (3, 2) = "i", (3, 3) = "n", (3, 4) = "t", (3, 5) = "o", (3, 6) = "a", (3, 7) = "M", (4, 1) = "a", (4, 2) = "t", (4, 3) = "r", (4, 4) = "i", (4, 5) = "x", (4, 6) = "o", (4, 7) = "f", (5, 1) = "l", (5, 2) = "e", (5, 3) = "t", (5, 4) = "t", (5, 5) = "e", (5, 6) = "r", (5, 7) = "s", (6, 1) = "i", (6, 2) = "n", (6, 3) = "t", (6, 4) = "h", (6, 5) = "e", (6, 6) = "m", (6, 7) = "o", (7, 1) = "s", (7, 2) = "t", (7, 3) = "c", (7, 4) = "o", (7, 5) = "n", (7, 6) = "v", (7, 7) = "e"})

Matrix(`if`(irem(nops(L),n)=0,nops(L)/n, iquo(nops(L),n)+1), n, L);

Matrix(8, 7, {(1, 1) = "H", (1, 2) = "o", (1, 3) = "w", (1, 4) = "d", (1, 5) = "o", (1, 6) = "I", (1, 7) = "t", (2, 1) = "u", (2, 2) = "r", (2, 3) = "n", (2, 4) = "a", (2, 5) = "t", (2, 6) = "e", (2, 7) = "x", (3, 1) = "t", (3, 2) = "i", (3, 3) = "n", (3, 4) = "t", (3, 5) = "o", (3, 6) = "a", (3, 7) = "M", (4, 1) = "a", (4, 2) = "t", (4, 3) = "r", (4, 4) = "i", (4, 5) = "x", (4, 6) = "o", (4, 7) = "f", (5, 1) = "l", (5, 2) = "e", (5, 3) = "t", (5, 4) = "t", (5, 5) = "e", (5, 6) = "r", (5, 7) = "s", (6, 1) = "i", (6, 2) = "n", (6, 3) = "t", (6, 4) = "h", (6, 5) = "e", (6, 6) = "m", (6, 7) = "o", (7, 1) = "s", (7, 2) = "t", (7, 3) = "c", (7, 4) = "o", (7, 5) = "n", (7, 6) = "v", (7, 7) = "e", (8, 1) = "n", (8, 2) = "i", (8, 3) = "e", (8, 4) = "n", (8, 5) = "t", (8, 6) = 0, (8, 7) = 0})

 

Download str2mat.mw

You didn't mention what you wanted to happen to capital letters. You can easily add a step to turn such into lowercase, utilizing the LowerCase command. Just replace the obvious line above with,

    str := LowerCase(Select(IsAlpha,raw));

str2mat_lower.mw

Naturally you could also throw away any capital letters (instead of converting them to lower-case), by utilized the IsLower command as part of the predicate passed to Select. It isn't clear which you want.

For fun, here's another way to get a result in terms of Psi (although, fwiw, it goes funny if assuming n<0).

You can also get a Psi form by directly converting that hypergeom call. I don't know how to do the reverse conversion.

restart;

kernelopts(version);

`Maple 2021.1, X86 64 LINUX, May 19 2021, Build ID 1539851`

F := int( 1/(1+x^n), x=0..1);

int(1/(1+x^n), x = 0 .. 1)

ans1 := IntegrationTools:-Change(F,x^(-n)=r) assuming n>0;

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

targ := hypergeom( [1,1/n], [1+1/n], -1 );

hypergeom([1, 1/n], [1+1/n], -1)

convert(targ,Psi);

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

Download int_examp.mw

Heavy-handedly,

f := int( 1/(1+x^n), x);

x*LerchPhi(-x^n, 1, 1/n)/n

# limit at zero done under an assumption
raw := limit(f,x=1,left) - limit(f,x=0,right) assuming n>0;

LerchPhi(-1, 1, 1/n)/n

convert(raw, hypergeom);

hypergeom([1, 1/n], [1+1/n], -1)

simplify(convert(raw, Psi), size);

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

(I'll ignore that eval(f,x=0) produces 0 directly.)

Supplying the view option does not specify the range for the independent variable.

Either of these, where the input range for x is specified, work fine.

plot((x^3 - 4*x^2 - 9*x + 36)/(x^3 - 9*x), x = -3 .. 3,
     color = red, view = [-3 .. 3, -5 .. 5]);
plot((x^3 - 4*x^2 - 9*x + 36)/(x^3 - 9*x), x = -3 .. 3,
     color = red, view = -5 .. 5);

If you omit the x-range argument then plot will try to figure something out based on the expression -- but that doesn't utilize the supplied view option.

The view option only specifies how to restrict the display of the computed plotting data. It does not specify how that data should be taken.

Below I use printf, but it's just as easy write to a file by using fprintf.

You can interject any extra lines of text (or newlines) that you want.

Do you really need to create and store all of Fs at once, as opposed to merely checking that each entry of Es does not meet the criteria of being in Fs!? Are you using all Fs for something else? That's unclear, from your wording.

[edit] I keep the way you create the sets, below, since I cannot tell whether you use them for anything else later on. My main point is that the fprintf command is a convenient tool for writing your results to a text file, which was the question asked

restart;

with(GraphTheory):

n:=4:

L:=NonIsomorphicGraphs(n,output=iterator,outputform=graph):

Es:=Array([seq(Edges(L()),j=1..NonIsomorphicGraphs(n,output=count))]):

M:=NonIsomorphicGraphs(n-1,output=iterator,outputform=graph):

FS:={seq(Edges(M()),j=1..NonIsomorphicGraphs(n-1,output=count))}:

for i from 1 to numelems(Es) do
  if not member(Es[i],FS) then
    printf("%a\n",'G'=Es[i]);
    printf("text1\ntext2\ntext3\ntext4\ntext5\n\n");
  end if;
od;

G = {{1, 2}, {3, 4}}
text1
text2
text3
text4
text5

G = {{1, 2}, {1, 3}, {2, 4}}
text1
text2
text3
text4
text5

G = {{1, 2}, {1, 3}, {2, 4}, {3, 4}}
text1
text2
text3
text4
text5

G = {{1, 2}, {1, 3}, {1, 4}}
text1
text2
text3
text4
text5

G = {{1, 2}, {1, 3}, {1, 4}, {2, 3}}
text1
text2
text3
text4
text5

G = {{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}}
text1
text2
text3
text4
text5

G = {{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}}
text1
text2
text3
text4
text5
 

 

Download gprintf.mw

Those two piecewise are different from each other when t<0.

You'd have seen that if you'd plotted a range that included negative values for t.

my_sol_ver_1:=y(t) = piecewise(t < 1,-3+4*exp(-t)-exp(-2*t)+2*t,
                               t >= 1 ,4+4*exp(-t)-exp(-2*t)+3*exp(-2*t+2)-8*exp(-t+1),
                               t < 0, 0):

my_sol_ver_2:= y(t) = piecewise(t < 0, 0,
                                t < 1,-3+4*exp(-t)-exp(-2*t)+2*t,
                                t >= 1 ,4+4*exp(-t)-exp(-2*t)+3*exp(-2*t+2)-8*exp(-t+1)):

eval(my_sol_ver_1, t=-1);

y(-1) = -5+4*exp(1)-exp(2)

eval(my_sol_ver_2, t=-1);

y(-1) = 0

Download pw_compar.mw

A heisenbug is no fun.

As an example that arose while poking at your problem, the following produces FAIL for me,

restart;
IntegrationTools:-Indefinite:-RationalFunction((-3450*exp(2)*_z+210*exp(4)*_z-4*exp(6)*_z-24250*exp(2)+2085*exp(4)-76*exp(6)+exp(8)+17500*_z+100000)/(-10*exp(2)*_z+25*_z^2+exp(4)-50*exp(2)+250*_z+625), _z);

                       FAIL

But when I attempt to trace or debug it (via stopat), it goes into a recursive loop.

restart;
trace(IntegrationTools:-Indefinite:-RationalFunction):
IntegrationTools:-Indefinite:-RationalFunction((-3450*exp(2)*_z+210*exp(4)*_z-4*exp(6)*_z-24250*exp(2)+2085*exp(4)-76*exp(6)+exp(8)+17500*_z+100000)/(-10*exp(2)*_z+25*_z^2+exp(4)-50*exp(2)+250*_z+625), _z);

Naturally that makes it tricky to figure how how it proceeded when it produced FAIL, which would be useful here.

I notice that this procedure can do this call, when in that recursive loop,

showstat(IntegrationTools:-Indefinite:-RationalFunction,28);

IntegrationTools:-Indefinite:-RationalFunction := proc(f, x, $)
local n, d, q, b, e, const, answer, tmp, z;
       ...
  28       answer := Main(tmp,x)
       ...
end proc

A technique used within some routines under int, is, etc, consists of storing (up front) a FAIL result for a call to a procedure that might eventually induce an identical call to itself. If this is performed prior to the first call, and if a suitable check for that value is made upon re-entry, then infinite recursion errors might be prevented. That can be important because they can be un-trappable. This is also a way in which a second attempt by the user can side-step the problem and proceed to another internal method which might be successful.

Some kinds of memory-related session dependent results have been tamed by the "new" lexicographic set ordering (which can help when, say, using indets to pick off names.) But some kinds are still lurking, due to ordering of terms in sums (or products).

I saw some mapping of action over sums in this int example, but I don't know for sure whether that is a problematic source of memory/session dependent behaviour here.

It goes amok when trying to compute this,

is(1+1/(-exp(exp(exp(3)))-2*I)*x, real) assuming x>0, x<infinity;

Error, (in type/complex) too many levels of recursion

The recursion is something like this:

(AndProp:-IntersectLines, 16): return thisproc(res minus map2(op,4,changed) union map(lp -> ...,changed))
	[{real, LinearProp((exp(exp(exp(3)))-2*I)/(exp(exp(exp(3)))^2+4)^(1/2),RealRange(-infinity,Open(0)),1)}]
I will submit a bug report.

Here are results for those two sets of parameter values, as obtained via the NAG library routine d01amc (included in Maple and accessed below).

Basically, I am wrapping your integrand in Maple's Re and Im, and invoking evalf(Int(...)) on those separately. I stuffed that together using functional programming syntax, in the attached below.

This is about as small an error tolerance as it'd allow me. Raising working precision of the integrand's evaluations (only, using a custom proc wrapper) didn't seem to alter the result.

If you are unable to attain these same results in your Maple version then try relaxing the error tolerance (ie. increase the epsilon option tolerance).

These results are computed quickly, in less than 1/5 of a second on my machine. They agree with your exact formula more closely than do the 3rd-party quadrature results you provided.

restart;

kernelopts(version);

`Maple 2021.1, X86 64 LINUX, May 19 2021, Build ID 1539851`

exact := eval(exp(-I*k0*R)/R, R=sqrt(r^2+z^2));

exp(-I*k0*(r^2+z^2)^(1/2))/(r^2+z^2)^(1/2)

Digits := 15:

P := exp(-sqrt(k^2-k0^2)*z)*BesselJ(0, k*r)*k/sqrt(k^2-k0^2):

# unnamed 3rd-party quadrature result
0.418312271120895 + 0.269775590079537*I;

.418312271120895+.269775590079537*I

CodeTools:-Usage(
  (evalf@Int)~((Re+Im*I)(eval(P,[k0=18.47,r=2,z=0.19])),
             k=0..infinity,epsilon=0.3e-8,method=_d01amc)
                );

memory used=11.42MiB, alloc change=33.00MiB, cpu time=170.00ms, real time=171.00ms, gc time=6.82ms

.412832601383964+.278132933720904*I

evalf[15](evalf[100](eval(exact,[k0=18.47,r=2,z=0.19])));

.412832618966820+.278088401850905*I

# unnamed 3rd-party quadrature result
-0.418783805098286 + 0.269041872900897*I;

-.418783805098286+.269041872900897*I

CodeTools:-Usage(
  (evalf@Int)~((Re+Im*I)(eval(P,[k0=1.84799,r=2,z=0.19])),
             k=0..infinity,epsilon=0.15e-5,method=_d01amc)
                );

memory used=6.76MiB, alloc change=2.00MiB, cpu time=64.00ms, real time=64.00ms, gc time=0ns

-.418786667730937+.269038429807927*I

evalf[15](evalf[100](eval(exact,[k0=1.84799,r=2,z=0.19])));

-.418787362379814+.269037312724374*I

Download d01amc_ex.mw

The code I used is a short way to do the following, which might appear more legible to you:

evalf(Int(Re(eval(P,[k0=18.47,r=2,z=0.19])),
          k=0..infinity,epsilon=0.3e-8,method=_d01amc))
+ I*evalf(Int(Im(eval(P,[k0=18.47,r=2,z=0.19])),
          k=0..infinity,epsilon=0.3e-8,method=_d01amc));
First 77 78 79 80 81 82 83 Last Page 79 of 336