acer

30782 Reputation

29 Badges

19 years, 14 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

You could instead use,

   patmatch(stat,`^`(0,a::anything))

This is to avoid your  0^(a::anything) becoming zero during your call.

restart;

stat:=0^n:

foo:=proc(stat)
  local a;
  if  patmatch(stat,`^`(0,a::anything)) then
     0;
  else
     stat;
  fi;
end proc:

foo(stat);

0


Download nm_zero_power_proc.mw

For your original, I see these arguments being passed to patmatch. Note that the second  argument received is just 0.

restart;

stat:=0^n:

foo:=proc(stat)
  local a;
  if  patmatch(stat,0^a::anything) then
     0;
  else
     stat;
  fi;
end proc:

trace(patmatch):

foo(stat);

{--> enter patmatch, args = 0^n, 0

false

<-- exit patmatch (now in foo) = false}

0^n


Download nm_zero_power_orig.mw

With the prefix form given above, trace shows that patmatch receives,
    0^n, 0^a::anything

 

There appear to be some inconsistencies (not affecting your given example, however) which I'll report.
hm_zero_power.mw

Here's one way.

restart;

e1:=-sqrt(-(exp(-2 + 2*x) - 2)*exp(-2 + 2*x))/(exp(-2 + 2*x) - 2);

-(-(exp(-2+2*x)-2)*exp(-2+2*x))^(1/2)/(exp(-2+2*x)-2)

convert(simplify(combine(simplify(convert(e1,trig)))),exp) assuming real;

1/(2*exp(-2*x+2)-1)^(1/2)

sort(%);

1/(2*exp(-2*x+2)-1)^(1/2)


Download nm_simp_exp0.mw

The sort call is there only because, depending on what's gone on earlier in the session, the subexpresssion in the final result can appear as,
      -1 + 2*exp(-2*x + 2)
instead of,
       2*exp(-2*x + 2) - 1
and you may have an aesthetic preference. This happens for me if I merely define e2 as you have it, before proceeding.

I presume you have some way to elect whether you accept this or not for other examples where it doesn't improve things.

Your given example can be handled by either of the following two ways,

C:={a=1/sqrt(2+A),b=6*sqrt(4+N) ,c=sqrt(7),d=5*H,e=-12,f=-96}:

selectremove(u->not type(rhs(u),constant), C);

   {a = 1/(2+A)^(1/2), b = 6*(4+N)^(1/2), d = 5*H},
   {c = 7^(1/2), e = -12, f = -96}

selectremove(u->indets(rhs(u),name)<>{}, C);

   {a = 1/(2+A)^(1/2), b = 6*(4+N)^(1/2), d = 5*H},
   {c = 7^(1/2), e = -12, f = -96

That first way can also be entered with a terser syntax for the type-check,

    selectremove(u->not rhs(u)::constant, C);

And if you just need the first of that returned pair of sets,

    remove(u->rhs(u)::constant, C);

For that second way (in the gray box) I just used a custom operator as the first argument, in a two argument call to selectremove. The first argument doesn't need to be the name of some stock command, and you don't always need to pass in extra aguments to it.

However, for that second way, in general you might want to guard against some special cases, and be more picky about which kinds of name matter.

For example, Pi is of type name but you might want to exclude it from the names that matter.

And if you have Int(4,t=0..1) and that name t acting as the variable of integration doesn't appear anywhere else then you might not want it to count. (I know, it's a contrived example.) The point is that you might want to consider only names upon which the rhs depends.

These aspects might not matter to you here, but in practice they crop up quite often when trying to extract variable names from expressions. 

C:={a=1/sqrt(2+A),b=6*sqrt(4+N) ,c=sqrt(7),d=5*H,e=-12,f=-96}

{a = 1/(2+A)^(1/2), b = 6*(4+N)^(1/2), c = 7^(1/2), d = 5*H, e = -12, f = -96}

selectremove(u->indets(rhs(u),
                       And(name,Not(constant),
                           satisfies(nm->depends(rhs(u),nm))))<>{}, C);

{a = 1/(2+A)^(1/2), b = 6*(4+N)^(1/2), d = 5*H}, {c = 7^(1/2), e = -12, f = -96}


Download 2024-06-05_Q_Select_Remove_indet_elements_ac.mw

note: You might well find shorter syntax. Look for code that you could understand later on your own (provided it's reasonably efficient), over code that is less natural to you (no matter how terse).

You could try using

    max( lowvalue, min( maxvalue,  expr ) )

around the expression, and then using,

   view = [ lowvalue+eps .. maxvalue+eps ]

for some relatively small eps.

Those view particulars prevent any extra flat portions induced by this approach from appearing. But the max/min allow the colorscheme to use your specified boundaries for the extremes of the zgradient. Hence the color gradient is created and applied only for the targeted range.

An alternative is to use valuesplit instead of zgradient in the colorscheme. That allows to you to target your desired shading for just a range that matches your specified view. (Ie. you write the splitting values to make them match.) This seems to be the approach that you mentioned avoiding.

I prefer the first method because sometimes when there is a huge variation the GUI has weird flashing as the 3d plot is manually rotated. Getting rid of the huge (pos/neg) extremes fixes that for me. Also, the max/min approach can work better with a colorbar. It gets rid of the extreme values altogether. The valuesplit is not so helpful for these aspects.

You'd normally need an inert representation in some way, or else the result will evaluate right back to your complex number.

For example, (sorry, the site's not rendering the worksheet quite properly),

restart;

 

P := z -> abs(z)*%exp(I*argument(z)):

 

P(-1);

%exp(I*Pi)

value(%);

-1

ans := P(1-I);

2^(1/2)*%exp(-((1/4)*I)*Pi)

simplify(value(ans));

1-I

InertForm:-Display(ans,inert=false);

sqrt(2)*%exp(-(1/4*I)*Pi)


Download inert_exp.mw

The Kr value is the subscript, in the legend items.

This uses the same color for a given Kr value, and the same linestyle for a given function (from f,Theta,Phi).

animation_of_ode_ac.mw

For your second example, here is one way,

restart;

kernelopts(version);

`Maple 2024.0, X86 64 LINUX, Mar 01 2024, Build ID 1794891`

e := -1/2*(-2*x*sqrt(4*x^2 + 1) + (16*x^3*sinh(3/2*arcsinh(2*x)) - 8*x^2*cosh(3/2*arcsinh(2*x))*sqrt(4*x^2 + 1) + 4*sinh(3/2*arcsinh(2*x))*x - cosh(3/2*arcsinh(2*x))*sqrt(4*x^2 + 1))*sqrt(-2 + 2*sqrt(4*x^2 + 1)))/sqrt(4*x^2 + 1);

-(1/2)*(-2*x*(4*x^2+1)^(1/2)+(16*x^3*sinh((3/2)*arcsinh(2*x))-8*x^2*cosh((3/2)*arcsinh(2*x))*(4*x^2+1)^(1/2)+4*sinh((3/2)*arcsinh(2*x))*x-cosh((3/2)*arcsinh(2*x))*(4*x^2+1)^(1/2))*(-2+2*(4*x^2+1)^(1/2))^(1/2))/(4*x^2+1)^(1/2)

simplify(evala(convert(e,arctan))) assuming x<0;

0


Download nm_simp_exh.mw
 

Also, for x>0,

combine(evala(simplify(convert(e,arctan)))) assuming x>0;

2*x


Of course, there may well be shorter alternatives.

You have,

   xM2 = rhs(sol2[1]);
   yM2 = rhs(sol2[2]);
   xM3 = rhs(sol3[1]);
   yM3 = rhs(sol3[2]);

which are not assignments. Those are just equations.

Change = to := and it works.

You don't need the assume call.

JAMET_2.mw

I've deliberately done this in stages, for a little more clarity.

The idea is to replace a subexpression of the form Int(Sum(...)) with Sum(Int(...)), i.e. bring the integration inside the sum.

restart:

with(IntegrationTools):

NULL

eq1:=int(1-(sum(p[i]*(1-exp(-((t-xi)/tau[i]))),i=1..n)),xi=0..t);

int(1-(sum(p[i]*(1-exp(-(t-xi)/tau[i])), i = 1 .. n)), xi = 0 .. t)

temp := IntegrationTools:-Expand(eq1);

t-(sum(p[i], i = 1 .. n))*t+int(sum(p[i]*exp(xi/tau[i])/exp(t/tau[i]), i = 1 .. n), xi = 0 .. t)

temp := subsindets(temp, specfunc({specfunc(sum),name=range},int),
                   u->Sum(Int(op([1,1],u),op(2,u)),op([1,2],u)));

t-(sum(p[i], i = 1 .. n))*t+Sum(Int(p[i]*exp(xi/tau[i])/exp(t/tau[i]), xi = 0 .. t), i = 1 .. n)

simplify(temp);

t-(sum(p[i], i = 1 .. n))*t+Sum(p[i]*(Int(exp((-t+xi)/tau[i]), xi = 0 .. t)), i = 1 .. n)

value(%);

t-(sum(p[i], i = 1 .. n))*t+sum(p[i]*(-tau[i]*exp(-t/tau[i])+tau[i]), i = 1 .. n)

simplify(combine(%));

-(sum(p[i]*(tau[i]*exp(-t/tau[i])+t-tau[i]), i = 1 .. n))+t


Download 1111_ac.mw

@Dkunb You could examine the results from, say,

   collect(combine(convert(eq13,trig)),[sin,cos],simplify)

which could also be done with, say, assuming n::even,m::odd etc.

In such a case I don't know whether you'd prefer the collecting of terms by real/imaginary (ie. factor of I) or by sin/cos arguments, or by denominator factors.

The command log (with no index supplied, ie. no base specified) is the same in Maple as the command ln, i.e. the logarithm with respect to the default base e=exp(1), ie. the natural logarithm.

log(0.5/1.65)
                          -1.193922469

ln(0.5/1.65)
                          -1.193922469

log[exp(1)](0.5/1.65)
                          -1.193922469

log10(0.5/1.65)
                         -0.5185139399

log[10](0.5/1.65)
                         -0.5185139399

See also the second paragraph here.

This is also described on the Maple Help-page viewed by executing  ?log .

If you're going to use eval[recurse] then why not utilize the functionality to recurse, with,

   eval['recurse'](Pt[1],[Pt[2][],valsh[]])

instead of,

    eval['recurse'](eval['recurse'](Pt,valsh)[])

 

To address your observation: simplify(S1) and S2 differ as starting forms for simplification, and the starting form can make a significant difference to the heuristics of simplify.  You're asking for a "simpler" form, which is not the same as asking for some kind of canonical form.


By the way, there are a variety of metrics readily available, including length , MmaTranslator:-Mma:-LeafCount , `simplify/size/size` and even codegen:-cost .

My least favorite is length, not least because results can depends on choices of names(!). Switch a pair of names for each other and the measure can change.

Also, for fun, and all using Maple 2024.0,

simplify(combine(rationalize(simplify(S1))));
length(%);  # length 509

simplify(combine(rationalize(S2)));
length(%);  # length 467

Moving on, the following both get the same (even smaller) length.

simplify(combine(expand(radnormal(S2)))):
length(%);  # length 466

I beseeched my oracle, and the sybil whispered to me commands for an even more compact form,

collect(S2,[t],u->simplify(evala(u)));
length(%);   # length 398

note: You're measuring a list of two expressions. The size of the list accounts for both expressions. All these mentioned approaches are each applied to both expressions. So their individual sizes may play off each other in the goal of minimizing the total measure the the list containing both. You may well be able to find a separate approach for each, that makes the combined total size smaller still.

If you are going to pass multiple geometry objects to draw then they should be in a list. 

You made a similar mistake with textplot: the multiple lists of items need to be grouped within a parent list.

That's on top of all the errant `*` and/or bracketing issues.

Suggestions:
1) Stop using 2D Input since it's likely related to your recurrent problems with errant `*` and implicit mutiplication versus function calls.
2) Indent your code, with separate lines, so that you can more easily see the structure of long statements, nested calls, and syntax mistakes.
3) When you have problems with a long complicated display call, try each of its parts separately.
4) Read the text of error messages; sometimes they're confusing but often they give a hint
5) When having a problem with a specific command, read all of its Help page carefully.

restart;

with(plots):
with(geometry):

_EnvHorizontalName := 'x':
_EnvVerticalName := 'y':

x0 := 10: y0 := 9: a := 7: b := 5: c := sqrt(a^2 - b^2):

ellipse(el, x^2/a^2 + y^2/b^2 - 1):
point(F1, -c, 0): point(F2, c, 0): point(P, x0, y0):

eq := simplify((a^2 - x0^2)*(y - y0)^2 + (b^2 - y0^2)*(x - x0)^2 + 2*x0*y0*(x - x0)*(y - y0)) = 0;
eq := (a^2 - x0^2)*m^2 + 2*x0*y0*m + b^2 - y0^2 = 0;
sol := solve(%, m);

-56*x^2+(180*y-500)*x-51*y^2-882*y+6469 = 0

-51*m^2+180*m-56 = 0

30/17-(2/51)*1311^(1/2), 30/17+(2/51)*1311^(1/2)

m1 := sol[1]: m2 := sol[2]:

line(tang1, (y - y0 - m1) + (-x + x0)):
line(tang2, (y - y0 - m2) + (-x + x0)):

display(textplot([[-c, 0, "F1"], [c, 0, "F2"], [coordinates(P)[], "P"]],
                 align = {"above", 'right'}),
        draw([el(color = red), P(color = black, symbol = solidcircle, symbolsize = 16),
              tang1(color = green), tang2(color = green),
              F1(color = blue, symbol = solidcircle, symbolsize = 16),
              F2(color = red, symbol = solidcircle, symbolsize = 16)]),
        axes = none);

Download JAMET_x.mw

One way is to replicate the plot, but with a singleton point (defined, and within range) and a lower thickness.

It's not perfect, but at least the legend entries are more distinguishable than in the original.

The limit at which it works on my system is thickness=2 .
 

restart;

 

P := plot([sin(x)/x,sin(x),cos(x)], x=0..2*Pi, color="Burgundy",
          linestyle= [solid, longdash, dash],
          thickness = [4, 4, 4], legend=[a,b,c]);

plots:-display(
  plot([sin(x)/x,sin(x),cos(x)], x=0..2*Pi, color="Burgundy",
       linestyle= [solid, longdash, dash],
       thickness = [4, 4, 4]),
  plot([sin(x)/x,sin(x),cos(x)], x=1..1, color="Burgundy",
       linestyle= [solid, longdash, dash],
       thickness = 2, legend=[a,b,c])
);

Download legend_thick_hack.mw

It varies by example of course, but doesn't save much typing, say,

with(plots):
display(display(P,legend=""),
  plottools:-transform((x,y)->[Pi,0])(display(P,overrideoption,thickness=2)))

Here's a quick attempt at something flexible.

restart;


First, a procedure to do the work.

V := proc(expr, cutoffin::nonnegint:=0, Q::symbol:=':-Q',
          {special::list(name):=[]})
  local TT, W, new, cutoff;
  if cutoffin=0 then
    cutoff := ceil(min(length~(op~(1,indets(eqn,radical))))/2);
  else cutoff := cutoffin; end if;
  TT := table([]);
  map(proc(u) TT; local A,B;
        A,B := selectremove(hastype,u,{radical,identical(special[])});
        if length(B) > cutoff then
          TT[B] := LargeExpressions:-Veil[Q](B);
        end if; NULL; end proc,
      [indets(expr,And({`+`,`*`}))[]]):
  W:=ListTools:-Reverse(sort([indices(TT,nolist)[]],key=length)):
  new := eqn:
  while W <> [] do
    new := subsindets(new,And({`+`,`*`}),
                      proc(u) local A,B;
                      A,B := selectremove(hastype,u,{radical,identical(special[])});
                      B := subs(W[1]=TT[W[1]], B);
                      op(0,u)(A,B);
                      end proc);
  W := W[2..]; end do;
  return new;
end proc:


Now, that example,

vars[1]:=x: vars[2]:=y:

eqn:= 8*(2*((a^2-2*a*c+b^2+c^2)*(4*a*c*f-a*e^2-b^2*f+b*d*e-c*d^2)^2)^(1
                /2)+(-8*a*f+2*d^2)*c^2+(8*a^2*f-2*a*d^2+2*a*e^2+2*b^2*f-2*b*d*e)*c-2*a^2*e^2-2*
                a*b^2*f+2*a*b*d*e)^(1/2)*(a*c-1/4*b^2)/(4*a*c-b^2)^2*(vars[1]-(1/4*b*e-1/2*c*d)/(a*c-\
                1/4*b^2))-8*csgn((4*a*c*f-a*e^2-b^2*f+b*d*e-c*d^2)*(Complex(1)*a+Complex(-1)*c-
                b))*(a*c-1/4*b^2)*(2*((a^2-2*a*c+b^2+c^2)*(4*a*c*f-a*e^2-b^2*f+b*d*e-c*d^2)^2)^
                (1/2)+(8*a*f-2*d^2)*c^2+(-8*a^2*f+2*a*d^2-2*a*e^2-2*b^2*f+2*b*d*e)*c+2*a^2*e^2+
                2*a*b^2*f-2*a*b*d*e)^(1/2)/(4*a*c-b^2)^2*(vars[2]-(-1/2*a*e+1/4*b*d)/(a*c-1/4*b^2)):

 

eqn;

8*(2*((a^2-2*a*c+b^2+c^2)*(4*a*c*f-a*e^2-b^2*f+b*d*e-c*d^2)^2)^(1/2)+(-8*a*f+2*d^2)*c^2+(8*a^2*f-2*a*d^2+2*a*e^2+2*b^2*f-2*b*d*e)*c-2*a^2*e^2-2*a*b^2*f+2*a*b*d*e)^(1/2)*(a*c-(1/4)*b^2)*(x-((1/4)*b*e-(1/2)*c*d)/(a*c-(1/4)*b^2))/(4*a*c-b^2)^2-8*csgn((4*a*c*f-a*e^2-b^2*f+b*d*e-c*d^2)*(I*a-I*c-b))*(a*c-(1/4)*b^2)*(2*((a^2-2*a*c+b^2+c^2)*(4*a*c*f-a*e^2-b^2*f+b*d*e-c*d^2)^2)^(1/2)+(8*a*f-2*d^2)*c^2+(-8*a^2*f+2*a*d^2-2*a*e^2-2*b^2*f+2*b*d*e)*c+2*a^2*e^2+2*a*b^2*f-2*a*b*d*e)^(1/2)*(y-(-(1/2)*a*e+(1/4)*b*d)/(a*c-(1/4)*b^2))/(4*a*c-b^2)^2

 

Now, a compact form, using default settings,

 

ans := V(eqn);

2*(2*Q[1]^(1/2)+2*Q[10])^(1/2)*Q[6]-2*(2*Q[1]^(1/2)-2*Q[10])^(1/2)*Q[7]

 

indets(ans,specindex(posint,Q));

{Q[1], Q[6], Q[7], Q[10]}

map(q->print(q=LargeExpressions:-Unveil[Q](q)),%):

Q[1] = (a^2-2*a*c+b^2+c^2)*(4*a*c*f-a*e^2-b^2*f+b*d*e-c*d^2)^2

Q[6] = (4*a*c*x-b^2*x-b*e+2*c*d)/(4*a*c-b^2)^2

Q[7] = csgn((4*a*c*f-a*e^2-b^2*f+b*d*e-c*d^2)*(I*a-I*c-b))*(4*a*c*y-b^2*y+2*a*e-b*d)/(4*a*c-b^2)^2

Q[10] = 4*a^2*c*f-a^2*e^2-a*b^2*f+a*b*d*e-4*a*c^2*f-a*c*d^2+a*c*e^2+b^2*c*f-b*c*d*e+c^2*d^2

 

simplify(eqn - LargeExpressions:-Unveil[Q](ans));

0

 

And now also leaving `x` and `y` alone,

 

V(eqn, special=[x,y]);
simplify(eqn - LargeExpressions:-Unveil[Q](%));

8*(2*Q[1]^(1/2)+2*Q[10])^(1/2)*(a*c-(1/4)*b^2)*(x-Q[5])/(4*a*c-b^2)^2-2*(2*Q[1]^(1/2)-2*Q[10])^(1/2)*(y+Q[4])*Q[13]

0

 

And now with a few different expression length sizes, as cutoff.

Also, a different symbol for the replacement.

 

V(eqn, 80, P, special=[x,y]);
simplify(eqn - LargeExpressions:-Unveil[P](%));

8*(2*P[1]^(1/2)+2*P[4])^(1/2)*(a*c-(1/4)*b^2)*(x-((1/4)*b*e-(1/2)*c*d)/(a*c-(1/4)*b^2))/(4*a*c-b^2)^2-2*(2*P[1]^(1/2)-2*P[4])^(1/2)*(y-(-(1/2)*a*e+(1/4)*b*d)/(a*c-(1/4)*b^2))*P[3]

0

V(eqn, 120, special=[x,y]);
simplify(eqn - LargeExpressions:-Unveil[Q](%));

8*(2*((a^2-2*a*c+b^2+c^2)*(4*a*c*f-a*e^2-b^2*f+b*d*e-c*d^2)^2)^(1/2)+2*Q[10])^(1/2)*(a*c-(1/4)*b^2)*(x-((1/4)*b*e-(1/2)*c*d)/(a*c-(1/4)*b^2))/(4*a*c-b^2)^2-2*(2*((a^2-2*a*c+b^2+c^2)*(4*a*c*f-a*e^2-b^2*f+b*d*e-c*d^2)^2)^(1/2)-2*Q[10])^(1/2)*(y-(-(1/2)*a*e+(1/4)*b*d)/(a*c-(1/4)*b^2))*Q[13]

0

 

Download subs_recur_ac.mw

There are a few ways to finesse and improve it in terms of more involved functionality.

E.g. doing nested veiling using shortest first, and preserving "inner" results, instead of replacing longer-to-shorter.

Replacing instead by ascending length needs a careful cutoff, or else risks an unhelpful jumble of too many micro-substitutions, like from codegen:-optimize(eqn).

5 6 7 8 9 10 11 Last Page 7 of 321