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

You could declare gamma as local at the top-level, to avoid conflict with the predefined constant.

You could issue this command at the top of the worksheet.

_local(gamma)

sqrt(1+(cos(alpha(t))^2-1)*cos(gamma(t))^2)

(1+(cos(alpha(t))^2-1)*cos(gamma(t))^2)^(1/2)

simplify((1+(cos(alpha(t))^2-1)*cos(gamma(t))^2)^(1/2))

(cos(alpha(t))^2*cos(gamma(t))^2-cos(gamma(t))^2+1)^(1/2)

NULL

Download arg_removed_ac.mw

One objection that you might have to using normal here is that it will cause expansion of terms like (x+y)^3, etc, which can be unnecessarily expensive or even tricky to undo after the fact. (See first two examples below.) Using numer/denom has a similar issue.

It can be tricky to prevent this by using frontend. Instead, below I temporarily freeze the bases of exponentiated sums.

I do not claim that this will handle all kinds of example that you intend, since we have not been told the full class and the hardest kind of example you want handled.

restart;

F := e->thaw(normal(subsindets(e,'`^`'(`+`,anything),
                               u->`^`(freeze(op(1,u)),op(2,u))))):

 

expr := (x+y)^5/(B*C^3)+C^2;

(x+y)^5/(B*C^3)+C^2

normal(expr);

(B*C^5+x^5+5*x^4*y+10*x^3*y^2+10*x^2*y^3+5*x*y^4+y^5)/(B*C^3)

F(expr);
simplify(%-expr);

((x+y)^5+C^5*B)/(B*C^3)

0

expr2 := (x+y)^5/(B*(C+E)^3)+(C+E)^2;

(x+y)^5/(B*(C+E)^3)+(C+E)^2

normal(expr2);

(B*C^5+5*B*C^4*E+10*B*C^3*E^2+10*B*C^2*E^3+5*B*C*E^4+B*E^5+x^5+5*x^4*y+10*x^3*y^2+10*x^2*y^3+5*x*y^4+y^5)/(B*(C+E)^3)

F(expr2);
simplify(%-expr2);

((x+y)^5+(C+E)^5*B)/(B*(C+E)^3)

0

F(A/B+C);

(B*C+A)/B

Download normal_ex.mw

So, you want to compute antiderivatives, by integrating?

restart;

with(InertForm):

A := ee -> subsindets(int(convert(ee(__t),diff),__t),
                      anyfunc(identical(__t)), u->op(0,u)):

examp := D(y/x);
A(examp);
Display(%D(A(examp)), 'inert'=false);

D(y)/x-y*D(x)/x^2

y/x

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

examp := D(x*y);
A(examp);
Display(%D(A(examp)), 'inert'=false);

D(x)*y+x*D(y)

x*y

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

examp := D(x^2+y^2);
A(examp);
Display(%D(A(examp)), 'inert'=false);

2*D(x)*x+2*D(y)*y

x^2+y^2

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

Download umm.mw

The above applies the input expression to a dummy name, __t. (For some other, as yet unprovided examples it might serve better to initially substitute for application on that dummy.)

For some strange reason pointplot accepts a list of lists, or a list of column Vectors, but not a list of row Vectors.

I changed it below, to use column Vectors.

I used Maple 2021.2 for this.

restart;
with(LinearAlgebra):
with(plots):

EqBIS := proc(P, U, V) local a, eq1, M1, t, PU, PV, bissec1; a := (P - U)/LinearAlgebra:-Norm(P - U, 2) + (P - V)/LinearAlgebra:-Norm(P - V, 2); M1 := P + a*t; eq1 := op(eliminate({x = M1[1], y = M1[2]}, t)); return op(eq1[2]); end proc:

A := <4, 8>;
B := <4, 2>;
C := <1, 4>;
EqBIS(A, B, C);

A := Vector(2, {(1) = 4, (2) = 8})

B := Vector(2, {(1) = 4, (2) = 2})

C := Vector(2, {(1) = 1, (2) = 4})

-5*y-20+15*x

Cen := proc(M, N, R) local eq1, eq2, sol; eq1 := EqBIS(M, N, R) = 0; eq2 := EqBIS(N, M, R) = 0; sol := simplify(solve({eq1, eq2}, {x, y})); return [subs(sol, x), subs(sol, y)]; end proc:
Cen(A,B,C):

CircleParm := t -> [(-t^2 + 1)/(t^2 + 1), 2*t/(t^2 + 1)];

proc (t) options operator, arrow; [(-t^2+1)/(t^2+1), 2*t/(t^2+1)] end proc

P1 := Vector(CircleParm(1/4));

P2 := Vector(CircleParm(5));
P3 := Vector(CircleParm(-1/10));
P4 := Vector(CircleParm(-3/2));

P1 := Vector(2, {(1) = 15/17, (2) = 8/17})

P2 := Vector(2, {(1) = -12/13, (2) = 5/13})

P3 := Vector(2, {(1) = 99/101, (2) = -20/101})

P4 := Vector(2, {(1) = -5/13, (2) = -12/13})

C1 := Vector(Cen(P1, P2, P3));

C1 := Vector(2, {(1) = (1/44642)*(1066*17^(1/2)-255*26^(1/2))*101^(1/2)-(1/442)*26^(1/2)*17^(1/2), (2) = (1/44642)*(156*17^(1/2)-833*26^(1/2))*101^(1/2)+(21/442)*26^(1/2)*17^(1/2)})

Pts := [P1, P2, P3, P4, C1];

Pts := [Vector(2, {(1) = 15/17, (2) = 8/17}), Vector(2, {(1) = -12/13, (2) = 5/13}), Vector(2, {(1) = 99/101, (2) = -20/101}), Vector(2, {(1) = -5/13, (2) = -12/13}), Vector(2, {(1) = (1/44642)*(1066*17^(1/2)-255*26^(1/2))*101^(1/2)-(1/442)*26^(1/2)*17^(1/2), (2) = (1/44642)*(156*17^(1/2)-833*26^(1/2))*101^(1/2)+(21/442)*26^(1/2)*17^(1/2)})]

display(implicitplot([x^2 + y^2 - 1], x = -2 .. 2, y = -4 .. 2, colour = [blue], scaling = constrained), pointplot(Pts, symbolsize = 16));

 

Download jamet_ac.mw

restart;

Relabel := proc(g,s,k) uses GT=GraphTheory;
  GT:-RelabelVertices(g,subs(Equate(s,k),GT:-Vertices(g)));
end proc:

 

with(GraphTheory):

 

G := Graph({{1,2},{1,3},{1,4},{1,5},{3,4},{2,5}}):

 

S := [1,3,5];

[1, 3, 5]

K := [a,1,"[1,2,3,4]"];

[a, 1, "[1,2,3,4]"]

H := Relabel(G,S,K):

 

Vertices(G);

[1, 2, 3, 4, 5]

Vertices(H);

[a, 2, 1, 4, "[1,2,3,4]"]

Download relabel.mw

restart;

convert(U(xi)^2, list, `*`);

[U(xi)^2]

convert(U(xi), list, `*`);

[U(xi)]

convert(U(xi)^2*y(xi)^5, list, `*`);

[U(xi)^2, y(xi)^5]

convert(U(xi)^2*y(xi)^5*p(xi), list, `*`);

[U(xi)^2, y(xi)^5, p(xi)]

convert(U(xi)^2+y(xi)^5, list, `*`);

[U(xi)^2+y(xi)^5]

convert(U(xi)^2+y(xi)^5, list, `+`);

[U(xi)^2, y(xi)^5]

Download convert_list.mw

Here are three ways, that show the numerals in upright Roman.

I don't care so much for solutions in which any numerals appear in italic, eg.
   caption=typeset("Or like this ",10^`8`)

note 1: I have utilized special display mechanisms because the input 10^8 becomes 100000000 due to automatic simplification by the kernel/engine. It cannot be prevented by delaying evalution, ie. unevaluation quotes (single right-ticks).

restart;

with(plots):

logplot(10^(x),x=0..8,size=[300,300],legend=[y(x)=10^(x)],legendstyle=[location=top],
        caption=typeset("Like this ",InertForm:-Display(10%^8,inert=false)));

logplot(10^(x),x=0..8,size=[300,300],legend=[y(x)=10^(x)],legendstyle=[location=top],
        caption=typeset("Or like this ",`#msup(mn(10),mn(8))`));

Download inert_exponent.mw

note 2: An alternative is to enter 10^8 in 2D Input mode, right-click and in the popup menu choose "2D Math"->"Convert To"->"Atomic Variable". That produces a pure name that conveniently happens to pretty-print just like the input, and which can be re-used in the caption. You could even do that directly in your plot call.

restart

with(plots)


Below, I selected the 10^8 bit of the 2D Input, then right-clicked and converted it to "Atomic Variable". Then I executed the statement.

logplot(10^x, x = 0 .. 8, size = [300, 300], legend = [y(x) = 10^x], legendstyle = [location = top], caption = typeset("Or like this ", `#msup(mn("10"),mn("8"))`))

Download inert_atomic_variable.mw

The mechanism by which the GUI accomplishes the numeric formatting constructions is not generally documented. But quite a bit may be discovered through the debugger. (I used anames to figure out what to first start debugging...)

Below I use Maple 2022.1, with interface(typesetting) returning the default value extended, and interface(prettyprint) returning the default value 3.

Let's consider your image, showing "Scientific" with 2 "Decimal places" and 1 "Minimum Exponent Digits". I set that to be the numeric formatting for the input,
    5.123456789;
Something (perhaps originating in some streamcall from the GUI) makes the following Library call, which results in a bit of Typesetting function-call-structure which renders accordingly.

Typesetting:-Typeset:-Kernel(5.123456789,
                             numericformatting = ["0.0{2}E-0{1}",
                             true, true, false, false]);

Parse:-ConvertTo1D, "invalid input %1", 5.123456789

lprint(%);

Typesetting:-mcomplete(Typesetting:-mrow(Typesetting:-mn("5.12",family =
"Times New Roman",size = "12",bold = "false",italic = "false",underline =
"false",subscript = "false",superscript = "false",foreground = "[0,0,255]",
background = "[255,255,255]",opaque = "false",executable = "false",readonly =
"true",composed = "false",converted = "false",imselected = "false",placeholder
= "false",`selection-placeholder` = "false",font_style_name = "2D Output",
mathvariant = "normal"),Typesetting:-mo("&times;",family = "Times New Roman",
size = "12",bold = "false",italic = "false",underline = "false",subscript =
"false",superscript = "false",foreground = "[0,0,255]",background =
"[255,255,255]",opaque = "false",executable = "false",readonly = "true",
composed = "false",converted = "false",imselected = "false",placeholder =
"false",`selection-placeholder` = "false",font_style_name = "2D Output",
mathvariant = "normal",fence = "false",separator = "false",stretchy = "false",
symmetric = "false",largeop = "false",movablelimits = "false",accent = "false",
lspace = "0.2222222em",rspace = "0.2222222em"),Typesetting:-msup(Typesetting:-
mn("10",family = "Times New Roman",size = "12",bold = "false",italic = "false",
underline = "false",subscript = "false",superscript = "false",foreground =
"[0,0,255]",background = "[255,255,255]",opaque = "false",executable = "false",
readonly = "true",composed = "false",converted = "false",imselected = "false",
placeholder = "false",`selection-placeholder` = "false",font_style_name =
"2D Output",mathvariant = "normal"),Typesetting:-mn("0",family =
"Times New Roman",size = "12",bold = "false",italic = "false",underline =
"false",subscript = "false",superscript = "false",foreground = "[0,0,255]",
background = "[255,255,255]",opaque = "false",executable = "false",readonly =
"true",composed = "false",converted = "false",imselected = "false",placeholder
= "false",`selection-placeholder` = "false",font_style_name = "2D Output",
mathvariant = "normal"),superscriptshift = "0")),5.123456789)

Download numformatting_stuff.mw

So it looks as if the GUI may be forming that list as value for the numericformatting option, based on the stuff in the popup menu in your image. Figuring out all the rules it may use in doing so could be a chore.

I do not know what those four true/false arguments mean, and the names of the keyword options only help so much with my guessing:
    showstat(Typesetting:-Typeset:-Kernel);

So, I believe that this is how it works. But I don't think that it's intended for general user consumption.

And figuring it out completely might be time-consuming. I doubt many people would enjoy figuring these out:
  showstat(Typesetting:-Typeset:-Tdisplay);
  showstat((Typesetting::Typeset)::Tn);

 

The behaviour for this example changed between Maple 2018.2 and Maple 2019.0.

In Maple 2018.2 and earlier, the evaluation of the expression under evalhf mode would produce an error when overflow occurred for larger values of t. Here is the default behavior in Maple 2018.2,

restart;
kernelopts(version);f := t -> 140^t*exp(-140)/t!:

   Maple 2018.2, X86 64 LINUX, Nov 16 2018, Build ID 1362973

evalhf(f(200.0));
Error, powering may produce overflow

But in Maple 2020 (and 2022.1) the default is for that overflow to not throw an error under evalhf.

restart;
kernelopts(version);f := t -> 140^t*exp(-140)/t!:

   Maple 2020.2, X86 64 LINUX, Nov 11 2020, Build ID 1502365

evalhf(f(200.0));

     Float(undefined)

In consequence, in Maple 2018 and earlier the plotting routines will detect the overflow and fall back to so-called software-float evalf-mode, and successfully generate the curve at the higher values of t. But in Maple 2019 and later that overflow doesn't generate an error by default, and so the evalhf results are accepted (including undefined results), and so only a partial curve is attained.

I am going to submit a bug report. I don't know whether this change in behaviour was deliberate. To me it seems like an undesirable change; it'd be better if the plotting worked ok by default, and expert knowledge should not be necessary in order to make it work.

There are various workarounds, for Maple 2020:
   - set Digits > 15 (and leave UseHardwareFloats at its default of deduced), so that evalf-mode gets used.
   - set UseHardwareFloats to false, so that evalf-mode gets used.
   - set the numeric event handler to throw an error on overflow, so that the evalhf attempt throws a detected error and then evalf-mode gets used as fallback.

restart; kernelopts(version);

`Maple 2020.2, X86 64 LINUX, Nov 11 2020, Build ID 1502365`

Digits := 16:
plot(140^t*exp(-140)/t!, t = 0 .. 300, size=[300,150]);

restart; kernelopts(version);

`Maple 2020.2, X86 64 LINUX, Nov 11 2020, Build ID 1502365`

UseHardwareFloats := false:
plot(140^t*exp(-140)/t!, t = 0 .. 300, size=[300,150]);

restart; kernelopts(version);

`Maple 2020.2, X86 64 LINUX, Nov 11 2020, Build ID 1502365`

NumericEventHandler(overflow = exception):
plot(140^t*exp(-140)/t!, t = 0 .. 300, size=[300,150]);

Download plot_overflow.mw

Here's one relatively simple way.

restart;

e := a/b:

f := unapply('unapply'(e, b), a);

proc (a) options operator, arrow; unapply(a/b, b) end proc

f(A);

proc (b) options operator, arrow; A/b end proc

f(A)(B);

A/B

Download unapply_nest.mw

It could be done with a more efficient result, I suspect, but not so similar to your original. And, anyway, this kind of goal has some inherent inefficiency.

[edit] For example, this is a bit more efficient:

e := a/b:

f := unapply('subs'(__a=a,unapply(subs(a=__a,e), b)), a);

proc (a) options operator, arrow; subs(__a = a, proc (b) options operator, arrow; __a/b end proc) end proc

f(A);

proc (b) options operator, arrow; A/b end proc

f(A)(B);

A/B

Download unapply_nest2.mw

A terser variant on that slightly more efficient alternative is:
    f := unapply('subs'(a=aa, unapply(e, b)), aa);
although you may have wanted(?) the formal parameter of f to be named a. I wasn't sure.

That's a nice find. I'll submit a bug report.

Naturally, a central benefit of the 1-argument calling sequence of implicitplot is that one doesn't have to supply the ranges.

On line 21 of `plots/implicitplot` (Maple 2022.1) I see,

   21   names := indets(fcn,':-name');

which might be better as,

   21   names := indets(fcn,And(':-name',Not(constant)));

(Another fix for that first issue might be for depends(x-y-Pi,Pi) to return false instead of true. I don't know whether that's best.)

Then, on line 30 it calls Reasonable:-Domain:-Implicit. At various places under that it will balk at Pi which is not of type numeric. I don't know how robust/safe it'd be to change various type-checks under that to realcons. Perhaps one can get by by passing evalf(fcn_set) instead of fcn_set to the mapped argument of Reasonable:-Domain:-Implicit, although I don't know offhand whether that's really OK.

The following does a hot-edit of `plots/implicitplot`, changing lines 21 and 30 (as see under showstat). It's more a byproduct of curiosity than a recommendation...

restart;

kernelopts(version);

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

if convert(kernelopts(version),string)[7..12] = "2022.1" then
  #showstat(`plots/implicitplot`,21);
  #showstat(`plots/implicitplot`,30);
  __k:=kernelopts(opaquemodules=false):
  unprotect(`plots/implicitplot`):
  `plots/implicitplot`:=FromInert(
subsop([5,5,1,2,7,2,2,1,2,2]=_Inert_FUNCTION(_Inert_ASSIGNEDNAME("evalf", "PROC", _Inert_ATTRIBUTE(_Inert_NAME("protected", _Inert_ATTRIBUTE(_Inert_NAME("protected"))))), _Inert_EXPSEQ(_Inert_LOCAL(64))),
subsop([5,5,1,2,6,1,2,1]=_Inert_ASSIGN(_Inert_LOCAL(66), _Inert_FUNCTION(_Inert_ASSIGNEDNAME("indets", "PROC", _Inert_ATTRIBUTE(_Inert_NAME("protected", _Inert_ATTRIBUTE(_Inert_NAME("protected"))))), _Inert_EXPSEQ(_Inert_LOCAL(42), _Inert_FUNCTION(_Inert_NAME("And", _Inert_ATTRIBUTE(_Inert_NAME("protected", _Inert_ATTRIBUTE(_Inert_NAME("protected"))))), _Inert_EXPSEQ(_Inert_UNEVAL(_Inert_MEMBER(_Inert_EXPSEQ(), _Inert_NAME("name", _Inert_ATTRIBUTE(_Inert_NAME("protected", _Inert_ATTRIBUTE(_Inert_NAME("protected"))))))), _Inert_FUNCTION(_Inert_NAME("Not", _Inert_ATTRIBUTE(_Inert_NAME("protected", _Inert_ATTRIBUTE(_Inert_NAME("protected"))))), _Inert_EXPSEQ(_Inert_NAME("constant", _Inert_ATTRIBUTE(_Inert_NAME("protected", _Inert_ATTRIBUTE(_Inert_NAME("protected")))))))))))), ToInert(eval(`plots/implicitplot`))))):
  protect(`plots/implicitplot`):
  kernelopts(':-opaquemodules'=__k):
  #showstat(`plots/implicitplot`,21);
  #showstat(`plots/implicitplot`,30);
end if:

 

f := x - y - Pi;

x-y-Pi

plots:-implicitplot(f);

Download impl_plot_constPi.mw

The D command can "differentiate" an operator such as your g, with respect to its first parameter (ie. x).

That is one of the primary purposes of the D command. And so your example can be handled easily and efficiently.

g := (x, T) -> T*x + x^2;

proc (x, T) options operator, arrow; T*x+x^2 end proc

dgdx := D[1](g);

proc (x, T) options operator, arrow; T+2*x end proc

dgdx(1,2);

4

Download D_ex.mw

What do you want the result to be, a plain Vector?

df := DataFrame( < 1, 2, 3; 4, 5, 6 >, 'rows' = [ 'a', 'b' ], 'columns' = [ 'A', 'B', 'C' ] );

DataFrame(Matrix(2, 3, {(1, 1) = 1, (1, 2) = 2, (1, 3) = 3, (2, 1) = 4, (2, 2) = 5, (2, 3) = 6}), rows = [a, b], columns = [A, B, C])

Vector(map(u->convert(df[u],Vector),[A,B]));

Vector(4, {(1) = 1, (2) = 4, (3) = 2, (4) = 5})

Download dfvecstack.mw

Or, if you want the result as a DataFrame, then what would be its row labels?

You main problem is confusion between operators and expressions.

Notice that in your original worksheet what actually gets assigned to f is an operator which returns a call to statevalf (without parameter x being used). That's all muddled up.

Ideally you should be using the Statistics package, instead of the deprecated stats package. Below I show how you can use either.

In the case of using an operator then there are some idiosyncrasies with using int and performing purely numeric integration. I suggest using evalf(Int(...)) instead of using int for purely numeric integration (quadrature). However, you could use int for symbolic integration of an expression for the pdf (see below).

Look carefully at how the plot and evalf(Int(...)) commands get called. When their first argument is an operator then second argument is just a range like -20..20. When their first argument is an expression (in x) then the second argument is of the form name=range like x=-20..20.

Note that, depending on your approach and the working precision (Digits), you may see some small discrepencies from the floating-point computations (eg. artefacts due to roundoff error, loss of precision, etc).

restart

with(plots, display)

with(Statistics)

f1 := proc (x) options operator, arrow; PDF(Normal(0, 2*4.47), x, numeric) end proc

display(Array([plot(proc (x) options operator, arrow; x*f1(x) end proc, -20 .. 20), plot(f1, -20 .. 20)]), size = [default, 150])

 

 

 

 

 

evalf(Int(proc (x) options operator, arrow; x*f1(x) end proc, -20 .. 20))

0.2803841168e-14

evalf(Int(proc (x) options operator, arrow; f1(x) end proc, -20 .. 20)); evalf(Int(f1, -20 .. 20)); CDF(Normal(0, 2*4.47), 20)-CDF(Normal(0, 2*4.47), -20)

.9747225777

.9747225777

.9747225776

f1expr := PDF(Normal(0, 2*4.47), x)

0.3155422726e-1*2^(1/2)*exp(-0.6255974455e-2*x^2)

display(Array([plot(x*f1expr, x = -20 .. 20), plot(f1expr, x = -20 .. 20)]), size = [default, 150])

 

 

 

 

 


Floating-point (numeric) integration

evalf(Int(x*f1expr, x = -20 .. 20))

0.


Floating-point (numeric) integration

evalf(Int(f1expr, x = -20 .. 20))

.9747225775


Symbolic integration (of expression containing float coefficients)

int(x*f1expr, x = -20 .. 20)

0.


Symbolic integration (of expression containing float coefficients)

int(f1expr, x = -20 .. 20)
``

.9747225775

with(stats)

f2 := statevalf[pdf, normald[0, 2*4.47]]

display(Array([plot(proc (x) options operator, arrow; x*f2(x) end proc, -20 .. 20), plot(f2, -20 .. 20)]), size = [default, 150])

 

 

 

 

 

evalf(Int(proc (x) options operator, arrow; x*f2(x) end proc, -20 .. 20))

0.

evalf(Int(proc (x) options operator, arrow; f2(x) end proc, -20 .. 20)); evalf(Int(f2, -20 .. 20)); statevalf[cdf, normald[0, 2*4.47]](20)-statevalf[cdf, normald[0, 2*4.47]](-20)

.9747225777

.9747225777

.9747225776

Download pdf_ex.mw

The catch mechanism can recognize a match in the beginning of an error message.

So you can make all your own errors begin with some key string.

Then you only need a single catch, to catch all of your own.

You can optionally rethrow the error, or rethrow some other error of your devising, or print something, etc.

And you could optionally deal with all other errors similarly, or just let them error-out, as is.

restart;

 

f := proc(x)
  try
    if x > 2 then error "custom: error A";
    elif x > 1 then error "custom: error B";
    elif x > 0 then error "custom: error C";
    else x; end if;
  catch "custom:":
    print("caught one of my custom errors");
    error lasterror;
  catch:
    print("some other error");
    error lasterror;
  end try;
end proc:

 

f(0);

0

f(2.5);

"caught one of my custom errors"

Error, (in f) custom: error A

f(1.5);

"caught one of my custom errors"

Error, (in f) custom: error B

f(0.5);

"caught one of my custom errors"

Error, (in f) custom: error C

f(t);

"some other error"

Error, (in f) cannot determine if this expression is true or false: 2 < t

 

Download catch_examp.mw

Let us know if you have difficulty making do.

First 64 65 66 67 68 69 70 Last Page 66 of 336