acer

32303 Reputation

29 Badges

19 years, 309 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

The unapply command is what you need here.

It has been available in every Maple version for decades.

Below, I use the unapply command to construct a re-usable operator (procedure) from the output expression than was assigned to fin.

restart

with(Statistics)

 

X := [0, 8, 10, 14, 25, 28]

Y := [50, 250, 300, 400, 700, 775]

 

fin := Fit(a*x+b, X, Y, x)

HFloat(26.09176748303333)*x+HFloat(42.86662732369449)

f := unapply(fin, x)

f(25)

HFloat(695.1608143995278)


Note that you don't have to assign to fin, since
you could also act directly on Fit call.

f2 := unapply(Fit(a*x+b, X, Y, x), x)

f2(25)

HFloat(695.1608143995278)


If you do assign the expression to fin, you could
also use the eval command to evaluate the
expression at values of x.

 

eval(fin, x = 25)

HFloat(695.1608143995278)

map(f, X)

[HFloat(42.86662732369449), HFloat(251.60076718796114), HFloat(303.7843021540278), HFloat(408.15137208616113), HFloat(695.1608143995278), HFloat(773.4361168486278)]

[seq(fin, x = X)]

[HFloat(42.86662732369449), HFloat(251.60076718796114), HFloat(303.7843021540278), HFloat(408.15137208616113), HFloat(695.1608143995278), HFloat(773.4361168486278)]

Download unapply_ex.mw

Instead of trying to reset that numbering you should improve your program so that it is robust with respect to the naming of such introduced variables.

Let's suppose that you have an expression assigned to expr, which might be a solution from solve (or a list of set of such, etc). Then you can issue,

    indets(expr, suffixed(_Z,posint))

and get a set of such names present in the expression.

You can then programatically substitute for the introduced name(s) into the expression, without having to hard-code the name.

For example,

expr := solve(cos(x*Pi),x,allsolutions);

        expr := 1/2 + _Z5~

nms := indets(expr, suffixed(_Z,posint));

          nms := {_Z5~}

eval(expr, nms[1]=6);;

              13/2

seq(eval(expr, nms[1]=v), v=1..6);;

      3/2, 5/2, 7/2, 9/2, 11/2, 13/2

Notes:

1.) The need for robust handling of indeterminate names is standard stuff.

2.) Resetting that counter isn't a great idea in general. Different solutions with potentially differently assumed introduced-names should have such names be distinct, regardless of whether the assumptions happen to be. (I'm deliberately going to not mention how to do it.)

Based on your description so far, I'd suggest using a PlotComponent within your document, to get that aspect of interactivity.

If you read the Help page for that embedded component you'll see that the Component Properties (clickx, clicky) can provide you with access to the position of the mouse cursor when you Click (or Click&Drag) on a 2D plot frame. Similarly for the properties endx and endy, when a Drag ends, or hoverx and hovery when the pointer hovers over.

That access is also conveniently programmatic, and fits in well with triggering/working with other interactivity. The x- and y-values can be easily used for other computations, in a manner straightforwardly consistent with other interactive purposes (and documented for such).

The plot `annotation` mechanism is more convenient (IMO) for displaying the x- and y-values in a formatted "hover-over" frame over the plot (though it's possible it might be used programmatically to trigger an embedded component action elsewhere). It has more functionality related to closeness of plotted features in the plotting frame.

I'm not able to tell from your Question and details so far which is closer to your situation and wider goals.

Using space " " is not reliable here, in my experience.

In the past, I've resorted to using one of:
           thin space
           en space
           em space


IIRC, I was not able to get those as named HTML entities, eg.        

Here are three ways. (There are others.)

restart

y := x+1

Explore(InertForm:-Display(%y, inert = false) = y, x = 0 .. 5, echoexpression = false)

Explore(`#mi("y");` = y, x = 0 .. 5, echoexpression = false)

F := proc (X) options operator, arrow; 'y' = eval(y, x = X) end proc; Explore(F(x), x = 0 .. 5, echoexpression = false)

Explore_with_Inert_Form_of_display_ac.mw

ps. I suspect that the uneval-quoting didn't work because Explore makes an effort (in order to overcome its special evaluation rules, sigh...) to resolve assigned names.

The m in the denominator of 200*Unit(kg)/m^3 was not entered as units.

It's just the name m. The visual cue of that is that it was rendered in italics in your output.

Below, I entered in as the unit m, instead.

restartNULL

Pi

Pi

"(->)"

3.1415926535897932385

 ``

Dyson[Inside] := 355016117130*Unit('ft')NULL

NULLDyson[Outside] := 355016117280*Unit('ft')

355016117280*Units:-Unit(ft)

NULLNULL

Vol[Dyson] := (4/3)*Pi*Dyson[Outside]^3-(4/3)*Pi*Dyson[Inside]^3 = 75621866085188578208340000*Pi*Units:-Unit(ft)^3"(->)"0.23757309894397957472e27*Units:-Unit(ft)^3

   

Total_Weight := 200*Unit('kg')*Vol[Dyson]/Unit('m')^3

15124373217037715641668000000*Units:-Unit(kg)*Pi*Units:-Unit(ft)^3/Units:-Unit(m)^3

"(=)"

(6691789940422039289112993734304/15625)*Pi*Units:-Unit(kg)

"(->)"

0.13454641994365413638e28*Units:-Unit(kg)NULL

Download Q_for_Dyson_Shell_Weight_ac.mw

Are you trying for one of these?

nprintf("#mrow(%a,mo(\"=\"),%a,mo(\"=\"),%a)",
        Typesetting:-Typeset(P(X <= 5)),
        Typesetting:-Typeset(P(-5 <= -X)),
        Typesetting:-Typeset(P(E(X)-5 <= E(X)-X)));
 

`#mrow(Typesetting:-mrow(Typesetting:-mi("P"),Typesetting:-mo("&ApplyFunction;"),Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi("X"),Typesetting:-mo("&le;"),Typesetting:-mn("5")))),mo("&equals;"),Typesetting:-mrow(Typesetting:-mi("P"),Typesetting:-mo("&ApplyFunction;"),Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn("&uminus0;5"),Typesetting:-mo("&le;"),Typesetting:-mrow(Typesetting:-mo("&uminus0;"),Typesetting:-mi("X"))))),mo("&equals;"),Typesetting:-mrow(Typesetting:-mi("P"),Typesetting:-mo("&ApplyFunction;"),Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mrow(Typesetting:-mrow(Typesetting:-mi("E"),Typesetting:-mo("&ApplyFunction;"),Typesetting:-mfenced(Typesetting:-mi("X"))),Typesetting:-mo("&minus;"),Typesetting:-mn("5")),Typesetting:-mo("&le;"),Typesetting:-mrow(Typesetting:-mrow(Typesetting:-mi("E"),Typesetting:-mo("&ApplyFunction;"),Typesetting:-mfenced(Typesetting:-mi("X"))),Typesetting:-mo("&minus;"),Typesetting:-mi("X"))))))`

`And`(P(X <= 5) = P(-5 <= -X),
      P(-5 <= -X) = P(E(X)-5 <= E(X)-X));
 

And(P(X <= 5) = P(-5 <= -X), P(-5 <= -X) = P(E(X)-5 <= E(X)-X))

`%and`(P(X <= 5) = P(-5 <= -X),
       P(-5 <= -X) = P(E(X)-5 <= E(X)-X));
  

%and(P(X <= 5) = P(-5 <= -X), P(-5 <= -X) = P(E(X)-5 <= E(X)-X))

Download ts_inert_ex.mw

Note that the latter two are inert, ie. not merely unevaluated by quotes (which might be subsequently ruined by inadvertant evaluation if passed around as arguments, etc).

Is this the kind of thing you're after?

It's not entirely clear what precisely you mean by, "...if R is not zero". Do you mean just when R is not a 4x4 Matrix with all zero as entries, or do you mean whenever R is some other 4x4 Matrix, or when R is any other size Matrix, or when R<>0 (scalar), or something else...?

restart;

 

p := proc(R)
  if R::'Matrix(4,4)' and LinearAlgebra:-Norm(R)=0 then
      A;
  else
      B;
  end if;
end proc:

 

p( Matrix(4,4) );

A

p( Matrix(4,4,12.3) );

B

# adjust code if you'd rather this threw error, or other
p( Matrix(2,2) );

B

Download Matrix_type_chk.mw

I believe that you have your target expression slightly wrong, missing one additive term in its denominator so that it's not actually equivalent to your starting R1/R2 expression.

But R1/R2 can be reformulated in a similar overall form as your cited target expression:

restart;

Hi := -Delta*S1^2 - J*S1*S2:

R1 := S1*exp(-beta*Hi):

R1 := add(R1, S1 = [-2, -1, 0, 1, 2]):

R2 := exp(-beta*Hi):

R2 := add(R2, S1 = [-2, -1, 0, 1, -2]):

R1/R2;

(-2*exp(-beta*(2*J*S2-4*Delta))-exp(-beta*(J*S2-Delta))+exp(-beta*(-J*S2-Delta))+2*exp(-beta*(-2*J*S2-4*Delta)))/(1+2*exp(-beta*(2*J*S2-4*Delta))+exp(-beta*(J*S2-Delta))+exp(-beta*(-J*S2-Delta)))

F1 := collect(convert(expand(R1*exp(-4*Delta*beta)),trig,{J*S2*beta}),
              exp,combine@simplify@expand@combine):

F2 := collect(convert(expand(R2*exp(-4*Delta*beta)),trig,{J*S2*beta}),
              exp,combine@simplify@expand@combine):

cand2 := combine(F1/F2,power);

(4*sinh(2*J*S2*beta)+2*sinh(J*S2*beta)*exp(-3*Delta*beta))/(2*cosh(2*J*S2*beta)-2*sinh(2*J*S2*beta)+2*cosh(J*S2*beta)*exp(-3*Delta*beta)+exp(-4*Delta*beta))

simplify(cand2 - R1/R2);

0

Download Barcellos_2a.mw

Simplifying the difference between cand2 and R1/R2 produces zero, ie. they are mathematically equivalent.

As mentioned earlier, this cited target result is not right,

(4*sinh(2*J*S2*beta) + 2*sinh(J*S2*beta)*exp(-3*Delta*beta))/(2*cosh(2*J*S2*beta) + 2*cosh(J*S2*beta)*exp(-3*Delta*beta) + exp(-4*Delta*beta))

It ought to have been something like the following (computed by the code in the attachment),

(4*sinh(2*J*S2*beta) + 2*sinh(J*S2*beta)*exp(-3*Delta*beta))/(2*cosh(2*J*S2*beta) - 2*sinh(2*J*S2*beta) +
2*cosh(J*S2*beta)*exp(-3*Delta*beta) + exp(-4*Delta*beta))

Numeric checking by substitution of random numeric values for the variables corroborates the equivalence of R1/R2 and cand2, but not of the cited target expression.

Here is one numeric way to show the plots of the space-curve intersection of 3D surfaces implied by the two equations, over the same (small, 0..1) y- and z-range. It runs in a reasonable amount of time, though I did not optimize.

Note that you didn't tell us what ranges you were interested in, for y and z.

Note that I set this up only to find one y-z solution for each x-value. It's not clear to me what you mean by "first" when you write, "I need to find the first 3 or 5 solutions".

You might also find that you can successfuly alter the ranges for x,y,z. I wasn't entirely sure what you wanted there.

restart;

expr1 := z*(1/150-2*y)*exp(-2*Pi*x*y)+1/5*((z^2-y^2)*sin(2*Pi*x*z)-2*y*z*cos(2*Pi*x*z));

z*(1/150-2*y)*exp(-2*Pi*x*y)+(1/5)*(-y^2+z^2)*sin(2*Pi*x*z)-(2/5)*y*z*cos(2*Pi*x*z)

expr2 := (z^2-y^2+1/150*y-1)*exp(-2*Pi*x*y)+1/5*((z^2-y^2)*cos(2*Pi*x*z)+2*y*z*sin(2*Pi*x*z));

(z^2-y^2+(1/150)*y-1)*exp(-2*Pi*x*y)+(1/5)*(-y^2+z^2)*cos(2*Pi*x*z)+(2/5)*y*z*sin(2*Pi*x*z)

G := proc(X) local s;
  s := fsolve(eval({expr1,expr2},x=X),{y=0..1,z=0..1});
  if s::set then eval([X,y,z],s);
  else [X,undefined,undefined]; end if;
end proc:

L := [seq(G(x), x=0..1, 0.01)]:

PC := plots:-pointplot3d(L, color="Red", symbol=solidcircle, symbolsize=10):

PI := plots:-implicitplot3d([expr1,expr2],x=0..1,y=0..1,z=0..1,
                            style=surface, color=[green,blue], transparency=0.8):

plots:-display(PC,PI,size=[400,400]);

plots:-display(PC,orientation=[-90,0,0],size=[300,300],labels=[x,y,""]);

plot(L[..,[1,2]], labels=[x,y], view=[0..1,0..1]);

plots:-display(PC,orientation=[-90,90,0],size=[300,300],labels=[x,"",z]);

plot(L[..,[1,3]], labels=[x,z], view=[0..1,0..1]);

 

Download impl_3d_inter.mw

[edit] I just noticed that your Question is tagged Maple 2019, in which the size option was not available for 3D plots. The following attachment is the same, but run and saved in Maple 2019.2 and without using that option. The rest looks the same.
impl_3d_inter_M2019.mw

There was an errant closing </Font> XML tag that didn't seem to have a corresponding opening tag.

Does the following attachment contain most of your document's original contents (or at least more than the Maple 2023 GUI itself recovers)?

Chemistry-Cheatsheet-Ramz_edition_Week_7-12_1_1_acc.mw

In your followup comment it seems as if your are trying to approximate an n-sided die by using (several, possibly n, possibly more?) 6-sided dice.

You tried unsuccessfully to do that (in a comment) by using mod. You could try mod (or irem) on a weighted sum.

restart;

randomize():

 

n := 20;

20

f:=rand(1..6):

 

G := [seq(1+irem(add(i*f(),i=1..n),n),j=1..50000)]:

 

Statistics:-Histogram(G,frequencyscale=absolute,discrete=true);

Statistics:-Tally(G);

[1 = 2427, 2 = 2446, 3 = 2528, 4 = 2513, 5 = 2524, 6 = 2509, 7 = 2447, 9 = 2484, 8 = 2554, 11 = 2577, 10 = 2458, 13 = 2476, 12 = 2557, 15 = 2473, 14 = 2448, 18 = 2545, 19 = 2469, 16 = 2464, 17 = 2508, 20 = 2593]

Download rand_die.mw


Your problem in your Question's original example is due to the sum of 3 calls to rand(1..6)() being automatically simplified to 3*rand(1..6)(), before evaluation of the rand(1..6)() calls happens.

In the code above I get around that premature evaluation by using add, which has special evaluation rules.

This is the kind of automatic simplification that you were seeing.

f := rand(1..6):

'f()+f()+f()';

3*f()

'rand(1..6)()+rand(1..6)()+rand(1..6)()';

3*(rand(1 .. 6))()

Download auto_simp_eh.mw

By the way, it's not efficient to invoke rand(1..6)() each and every time. And, as you saw originally, it doesn't prevent the automatic simplifcation problem. I assign f:=rand(1..6), then call f for each instance: for efficiency. But it's not the aspect that fixes your automatic simplification problem.

I also changed your inefficient repeated list-concatenation into a seq call, for efficiency.

restart;

V := exp(lambda*S) = S^4*a4 + S^3*a3 + S^2*a2 + S*a1 + a0:


By simplifying after solving (but before converting to trigh) we can
avoid requiring any fortuitous order of the specifed solve-variables.

raw := convert(combine(expand(solve({seq(V,S=[2,1,0,-1,-2])}))),trigh);

{a0 = 1, a1 = -(1/6)*sinh(2*lambda)+(4/3)*sinh(lambda), a2 = -(1/12)*cosh(2*lambda)+(4/3)*cosh(lambda)-5/4, a3 = (1/6)*sinh(2*lambda)-(1/3)*sinh(lambda), a4 = (1/12)*cosh(2*lambda)-(1/3)*cosh(lambda)+1/4, lambda = lambda}


Get rid of the trivial lambda=lambda result.

raw := remove(evalb, raw);

{a0 = 1, a1 = -(1/6)*sinh(2*lambda)+(4/3)*sinh(lambda), a2 = -(1/12)*cosh(2*lambda)+(4/3)*cosh(lambda)-5/4, a3 = (1/6)*sinh(2*lambda)-(1/3)*sinh(lambda), a4 = (1/12)*cosh(2*lambda)-(1/3)*cosh(lambda)+1/4}


The OP also queried about statements which make these assignments.

assign~(raw):

 

a0;

1

a1;

-(1/6)*sinh(2*lambda)+(4/3)*sinh(lambda)

a2;

-(1/12)*cosh(2*lambda)+(4/3)*cosh(lambda)-5/4

a3;

(1/6)*sinh(2*lambda)-(1/3)*sinh(lambda)

a4;

(1/12)*cosh(2*lambda)-(1/3)*cosh(lambda)+1/4

Automatic simplification avoidance. This is just for display purposes,
to show the numeric coefficients exactly as was asked.

(print@InertForm:-Display)~((u->`if`(rhs(u)::constant, rhs(u),
                                     content(rhs(u))%*(rhs(u)/content(rhs(u)))))~(raw),
                            'inert'=false):

1

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

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

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

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

Download solve_trigh.mw

You've probably also noticed that when you open your corrupted file in Maple 2022 you only get two Sections. It seems that the Maple 2022.2 GUI doesn't recover a large portion of your document.

The attachment below contains five Sections (Lecture 4 and Lecture 5 are expanded and fully visible).

Exercises_all_lectures_1_acc.mw

Unfortunately Section three (Lecture 3) appears to be quite short. But there does not seem to be very much in the file in that Section, after the text "...and we have...".

If you have it available to you, I suggest that you upgrade to Maple 2023, as it seems to have fewer reported incidents of these problems.

Here are two more ways.

The second uses a piece of manipulation that I've used before... (with different divisors, of course).

expr := 2*A*B/(A*m+m)+C^2+D^2;

2*A*B/(A*m+m)+C^2+D^2

First way:

collect(expr,[B,m],u->content(u)*1/expand(content(u)/u));

2*B/((1+1/A)*m)+C^2+D^2

Second way, using a little procedure I've needed many times before...

divid := (u,F) -> expand(numer(u)/F)/expand(denom(u)/F):

 

map(divid, expr, A*m);

2*B/((1+1/A)*m)+C^2+D^2

Download ut.mw

First 32 33 34 35 36 37 38 Last Page 34 of 336