acer

29759 Reputation

29 Badges

19 years, 317 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

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.

restart;

kernelopts(version);

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

de := diff(u(x),x$2) = Heaviside(x - a)*u(x);

diff(diff(u(x), x), x) = Heaviside(x-a)*u(x)

sol := dsolve(de, u(x), parametric);

sol := u(x) = piecewise(x < a, _C1*x+_C2, a <= x, ((1/2)*(a+1)*exp(x-a)+(1/2)*(a-1)*exp(-x+a))*_C1+((1/2)*exp(x-a)+(1/2)*exp(-x+a))*_C2)

map(collect, rhs(sol), exp, simplify);

piecewise(x < a, _C1*x+_C2, a <= x, ((1/2)*(a-1)*_C1+(1/2)*_C2)*exp(-x+a)+((1/2)*(a+1)*_C1+(1/2)*_C2)*exp(x-a))

Download de_h_exp.mw

(I had similar success in old Maple 16.02.)

The names you give the axes are your own choice, as is the orientation. You can adjust them however you want.

restart;

with(Student:-Calculus1):

VolumeOfRevolution(2*x^2 + 1, x = 0 .. 1, labels = [x,"",y],
                   output = plot, axis = vertical, axis=vertical,
                   orientation=[-90,90,0],
                   view=[-1.5..1.5,default,default]);

Download vorZ_ex.mw

Try passing the option explicit to the solve command.

1) The link for the form to make a Software Change Request is here.

(That also has a link item, in the drop menu "More" on the Mapleprimes tab menu at top.)

2) In Maple you can return to text entry mode with the shortcut Ctl-t . Does that also work in Maple Flow?

Here's an example of using sort to alter the form in which the (unique representation) of the expression is stored in the engine.

If you have some more involved answer that presents further problems then you ought to provide us with it. It often saves time for responders as well as questioners if a concrete example to reproduce the issue is provided up front.

restart;

expr := exp(U(a - b)) + a - b;

exp(U(a-b))+a-b

expr;

exp(U(a-b))+a-b

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

-b+a+exp(U(-b+a))

expr;

-b+a+exp(U(-b+a))

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

a-b+exp(U(a-b))

expr;

a-b+exp(U(a-b))

Download sort_term_ordering.mw

Here's a goal that someone might have:

   Take your Maple code snippet and programmatically translate it
   into another language such that the computations could be
   executed while relying only the stock runtime for that language
   (libc.so and libm.so or Windows equivalents, or a Python
   interpreter, etc), and without needing Maple in any runtime
   capability (including shared object library support, or its Library
   of interpretable commands, etc).

That's an interesting goal. A key aspect is the absence of any need for Maple proper, when running the translated code.

Note that your original Question doesn't make it clear whether you have these goals and restrictions.

There is currently no simple set of stock commands which can directly or easily accomplish the above stated goal (in italics above) for blocks of Maple code that generate its Graph structures, return its Matrix structures, or call almost all of the user-level commands of its GraphTheory or LinearAlgebra packages.

I'm guessing that by \cos^{i+2}(\theta) you intend cos(theta)^(i+2) in Maple syntax.

restart;

kernelopts(version);

`Maple 18.02, X86 64 LINUX, Oct 20 2014, Build ID 991181`

ee := cos(theta)^i-cos(theta)^(i+2);

cos(theta)^i-cos(theta)^(i+2)

R := 2*value(IntegrationTools:-Change(Int(ee,theta=0..Pi),cos(theta)=s,s));

(1/2)*Pi^(1/2)*((-1)^i+1)*GAMMA(1/2+(1/2)*i)/GAMMA(2+(1/2)*i)

seq(R, i=0..20);

Pi, 0, (1/4)*Pi, 0, (1/8)*Pi, 0, (5/64)*Pi, 0, (7/128)*Pi, 0, (21/512)*Pi, 0, (33/1024)*Pi, 0, (429/16384)*Pi, 0, (715/32768)*Pi, 0, (2431/131072)*Pi, 0, (4199/262144)*Pi

seq(int(ee,theta=0..2*Pi), i=0..20);

Pi, 0, (1/4)*Pi, 0, (1/8)*Pi, 0, (5/64)*Pi, 0, (7/128)*Pi, 0, (21/512)*Pi, 0, (33/1024)*Pi, 0, (429/16384)*Pi, 0, (715/32768)*Pi, 0, (2431/131072)*Pi, 0, (4199/262144)*Pi

Download int_cos_i.mw

Are you supposing that i is an integer?  A nonnegative integer, perhaps?

There are several ways to get such a result.

R := e -> coeff~(e,[indets(e,specfunc(T))[]]):

R( numapprox:-chebyshev(exp(x),x) );

[HFloat(1.2660658777520082), HFloat(1.1303182079849698), HFloat(0.2714953395340765), HFloat(0.044336849848663804), HFloat(0.005474240442093705), HFloat(5.429263119139931e-4), HFloat(4.497732295427603e-5), HFloat(3.1984364624457973e-6), HFloat(1.992124806415823e-7), HFloat(1.103677170949997e-8), HFloat(5.505896979790788e-10)]

R( numapprox:-chebyshev(exp(x),x,1e-4) );

[HFloat(1.2660658777520082), HFloat(1.1303182079849698), HFloat(0.2714953395340765), HFloat(0.044336849848663804), HFloat(0.005474240442093705), HFloat(5.429263119139931e-4)]

Download coeff_T.mw

Here's one way (which works whether your lprime is itself even, or odd).

restart;

lp:=5;

5

add(f(2*N), N=-floor(lp/2)..floor(lp/2));

f(-4)+f(-2)+f(0)+f(2)+f(4)

add(g(2*N), N=1..floor(lp/2));

g(2)+g(4)

restart;

lp:=6;

6

add(f(2*N), N=-floor(lp/2)..floor(lp/2));

f(-6)+f(-4)+f(-2)+f(0)+f(2)+f(4)+f(6)

add(g(2*N), N=1..floor(lp/2));

g(2)+g(4)+g(6)

 

Download sum_even_terms.mw

Here's another way, using inert multiplication.

The marked-up display of the result is done differently than working with the value of the result.

(In the actual Maple GUI the space between LongExpression and the x-dot is more apparent. You could also throw another %* in there too.)

restart;

interface(typesetting=extended):
with(Typesetting):
Settings(typesetdot=true):
Suppress(x(t)):

res := diff((1/2) %* LongExpression*x(t),t):

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

`%*`(1/2, LongExpression)*(diff(x(t), t))

value(res);

(1/2)*LongExpression*(diff(x(t), t))

 

Download inert_mult_examp.mw

It appears as if your goal might be simply to keep the coefficients of the Vectors from being distributed throughout their respective elements.

In that case you'd be ok with active operations within the Vector elements, in addition to active Vector calls.

restart;

Str :=  "1/3*Vector([3^(1/2), -3^(1/2), 3^(1/2), 0]) -1/3*Vector([3^(1/2), 3^(1/2), 3^(1/2), 0])+ Vector([2,1,1,0])":

Set := {op(InertForm:-Parse( Str ))}:

subsindets(Set,specfunc(%Vector),value);

{-`%*`(`%/`(1, 3), Vector(4, {(1) = sqrt(3), (2) = sqrt(3), (3) = sqrt(3), (4) = 0})), `%*`(`%/`(1, 3), Vector(4, {(1) = sqrt(3), (2) = -sqrt(3), (3) = sqrt(3), (4) = 0})), Vector(4, {(1) = 2, (2) = 1, (3) = 1, (4) = 0})}

 

Download inert_Vector.mw

I'm not sure why some parts of the following attached worksheet appear like blocked yellow when rendered on this site. In the actual Maple GUI they are highlighted, but their foreground math is visible. (This site uses an old MapleNet...)

restart

with(Student:-Calculus1)

 

ShowSolution(Diff(x^5, x))

"[[,,"Differentiation Steps"],[,,(&DifferentialD;)/(&DifferentialD;x) (x^5)],["&EmptyVerySmallSquare;",,"1. Apply the" "power" "rule to the term" (&DifferentialD;)/(&DifferentialD;x) (x^5)],[,"?","Recall the definition of the" "power" "rule"],[,,(&DifferentialD;)/(&DifferentialD;x) (x^[])=[] x^([]-1)],[,"?","This means:"],[,,(&DifferentialD;)/(&DifferentialD;x) (x^5)=[]],[,,"We can rewrite the derivative as:"],[,,5 x^4]]6""

ShowSolution(Diff(x^2*exp(x), x))

"[[,,"Differentiation Steps"],[,,(&DifferentialD;)/(&DifferentialD;x) (x^2 (e)^x)],["&EmptyVerySmallSquare;",,"1. Apply the" "product" "rule"],[,"?","Recall the definition of the" "product" "rule"],[,,(&DifferentialD;)/(&DifferentialD;x) (f(x) g(x))=((&DifferentialD;)/(&DifferentialD;x) f(x)) g(x)+f(x) ((&DifferentialD;)/(&DifferentialD;x) g(x))],[,,f(x)=x^2],[,,g(x)=(e)^x],[,,"This gives:"],[,,((&DifferentialD;)/(&DifferentialD;x) (x^2)) (e)^x+x^2 ((&DifferentialD;)/(&DifferentialD;x) (e)^x)],["&EmptyVerySmallSquare;",,"2. Apply the" "power" "rule to the term" (&DifferentialD;)/(&DifferentialD;x) (x^2)],[,"?","Recall the definition of the" "power" "rule"],[,,(&DifferentialD;)/(&DifferentialD;x) (x^[])=[] x^([]-1)],[,"?","This means:"],[,,(&DifferentialD;)/(&DifferentialD;x) (x^2)=[]],[,"?","So,"],[,,(&DifferentialD;)/(&DifferentialD;x) (x^2)=[]],[,,"We can rewrite the derivative as:"],[,,[]],["&EmptyVerySmallSquare;",,"3. Apply the" "exponential" "rule"],[,"?","Recall the definition of the" "exponential" "rule"],[,,(&DifferentialD;)/(&DifferentialD;x) (e^x)=e^x],[,,"This gives:"],[,,[]]]6""

ShowSolution(Diff(x*sin(x), x))

"[[,,"Differentiation Steps"],[,,(&DifferentialD;)/(&DifferentialD;x) (x sin(x)"

ShowSolution(Diff(exp(x)*tan(x), x))

"[[,,"Differentiation Steps"],[,,(&DifferentialD;)/(&DifferentialD;x) ((e)^x tan(x))],["&EmptyVerySmallSquare;",,"1. Apply the" "product" "rule"],[,"?","Recall the definition of the" "product" "rule"],[,,(&DifferentialD;)/(&DifferentialD;x) (f(x) g(x))=((&DifferentialD;)/(&DifferentialD;x) f(x)) g(x)+f(x) ((&DifferentialD;)/(&DifferentialD;x) g(x))],[,,f(x)=(e)^x],[,,g(x)=tan(x)],[,,"This gives:"],[,,((&DifferentialD;)/(&DifferentialD;x) (e)^x) tan(x)+(e)^x ((&DifferentialD;)/(&DifferentialD;x) tan(x))],["&EmptyVerySmallSquare;",,"2. Apply the" "exponential" "rule"],[,"?","Recall the definition of the" "exponential" "rule"],[,,(&DifferentialD;)/(&DifferentialD;x) (e^x)=e^x],[,,"This gives:"],[,,[]],["&EmptyVerySmallSquare;",,"3. Evaluate the derivative of" tan "("x")"],[,"?","Recall the definition of the" tan "rule"],[,,(&DifferentialD;)/(&DifferentialD;x) tan(x)=1+(tan(x))^2],[,,"This gives:"],[,,[]]]6""

ShowSolution(Diff(tan(x)*sec(x), x))

"[[,,"Differentiation Steps"],[,,(&DifferentialD;)/(&DifferentialD;x) (tan(x) sec(x))],["&EmptyVerySmallSquare;",,"1. Apply the" "product" "rule"],[,"?","Recall the definition of the" "product" "rule"],[,,(&DifferentialD;)/(&DifferentialD;x) (f(x) g(x))=((&DifferentialD;)/(&DifferentialD;x) f(x)) g(x)+f(x) ((&DifferentialD;)/(&DifferentialD;x) g(x))],[,,f(x)=tan(x)],[,,g(x)=sec(x)],[,,"This gives:"],[,,((&DifferentialD;)/(&DifferentialD;x) tan(x)) sec(x)+tan(x) ((&DifferentialD;)/(&DifferentialD;x) sec(x))],["&EmptyVerySmallSquare;",,"2. Evaluate the derivative of" tan "("x")"],[,"?","Recall the definition of the" tan "rule"],[,,(&DifferentialD;)/(&DifferentialD;x) tan(x)=1+(tan(x))^2],[,,"This gives:"],[,,[]],["&EmptyVerySmallSquare;",,"3. Evaluate the derivative of" sec "("x")"],[,"?","Recall the definition of the" sec "rule"],[,,(&DifferentialD;)/(&DifferentialD;x) sec(x)=tan(x) sec(x)],[,,"This gives:"],[,,[]]]6""


Integration Questions

ShowSolution(Int(5*x^4, x))

"[[,,"Integration Steps"],[,,&int;5 x^4 &DifferentialD;x],["&EmptyVerySmallSquare;",,"1. Apply the" "constant multiple" "rule to the term" &int;5 x^4 &DifferentialD;x],[,"?","Recall the definition of the" "constant multiple" "rule"],[,,&int;[] f(x) &DifferentialD;x=[] (&int;f(x) &DifferentialD;x)],[,"?","This means:"],[,,&int;5 x^4 &DifferentialD;x=5 (&int;x^4 &DifferentialD;x)],[,,"We can rewrite the integral as:"],[,,5 (&int;x^4 &DifferentialD;x)],["&EmptyVerySmallSquare;",,"2. Apply the" "power" "rule to the term" &int;x^4 &DifferentialD;x],[,"?","Recall the definition of the" "power" "rule, for n" "<>" "-1"],[,,&int;x^[] &DifferentialD;x=[]],[,"?","This means:"],[,,&int;x^4 &DifferentialD;x=[]],[,"?","So,"],[,,&int;x^4 &DifferentialD;x=(x^5)/5],[,,"We can rewrite the integral as:"],[,,x^5],["&bullet;",,"Add constant of integration"],[,,x^5+C]]6""

ShowSolution(Int(sin(x)+x*cos(x), x))

"[[,,"Integration Steps"],[,,&int;(sin(x)+x cos(x)) &DifferentialD;x],["&EmptyVerySmallSquare;",,"1. Apply the" "sum" "rule"],[,"?","Recall the definition of the" "sum" "rule"],[,,&int;(f(x)+g(x)) &DifferentialD;x=&int;f(x) &DifferentialD;x+&int;g(x) &DifferentialD;x],[,,f(x)=sin(x)],[,,g(x)=x cos(x)],[,,"This gives:"],[,,&int;sin(x) &DifferentialD;x+&int;x cos(x) &DifferentialD;x],["&EmptyVerySmallSquare;",,"2. Evaluate the integral of" sin"("x")"],[,"?","Recall the definition of the" sin "rule"],[,,&int;sin(x) &DifferentialD;x=-cos(x)],[,,"This gives:"],[,,[]],["&EmptyVerySmallSquare;",,"3. Apply integration by Parts"],[,"?","Recall the definition of the" "Parts" "rule"],[,,&int;u &DifferentialD;v=v u-(&int;v &DifferentialD;u)],[,"?","First part"],[,,u=x],[,"?","Second part"],[,,[]=cos(x)],[,"?","Differentiate first part"],[,,[]=(&DifferentialD;)/(&DifferentialD;x) x],[,,[]=1],[,"?","Integrate second part"],[,,v=&int;cos(x) &DifferentialD;x],[,,v=sin(x)],[,,&int;x cos(x) &DifferentialD;x=x sin(x)-(&int;sin(x) &DifferentialD;x)],[,,"This gives:"],[,,-cos(x)+x sin(x)-(&int;sin(x) &DifferentialD;x)],["&EmptyVerySmallSquare;",,"4. Evaluate the integral of" sin"("x")"],[,"?","Recall the definition of the" sin "rule"],[,,&int;sin(x) &DifferentialD;x=-cos(x)],[,,"This gives:"],[,,x sin(x)],["&bullet;",,"Add constant of integration"],[,,x sin(x)+C]]6""


Limit questions

ShowSolution(Limit(sin(x)/x, x = 0))

"[[,,"Limit Steps"],[,,(lim)(sin(x))/x],["&EmptyVerySmallSquare;",,"1. Apply" "L'Hôpital's Rule"],[,"?","Recall the definition of the" "L'Hôpital's Rule" "rule"],[,,(lim)(f(x))/(g(x))=(lim)((&DifferentialD;)/(&DifferentialD;x) f(x))/((&DifferentialD;)/(&DifferentialD;x) g(x))],[,"?","Rule applied"],[,,(lim)(sin(x))/x=(lim)cos(x)],[,,"This gives:"],[,,(lim)cos(x)],["&EmptyVerySmallSquare;",,"2. Evaluate the limit of" cos"("x")"],[,"?","Recall the definition of the" cos "rule"],[,,(lim)cos(f(x))=cos((lim)f(x))],[,,"This gives:"],[,,1]]6""

ShowSolution(Limit((x+2)/(x^2-4), x = -2))

"[[,,"Limit Steps"],[,,(lim)(x+2)/(x^2-4)],["&EmptyVerySmallSquare;",,"1. Factor"],[,"?","Factor a polynomial or rational function"],[,,(x+2)/(x^2-4)=1/(x-2)],[,,"This gives:"],[,,(lim)1/(x-2)],["&EmptyVerySmallSquare;",,"2. Apply the" "power" "rule"],[,"?","Recall the definition of the" "power" "rule"],[,,(lim)x^[]=((lim)x)^[]],[,,"This gives:"],[,,1/((lim)(x-2))],["&EmptyVerySmallSquare;",,"3. Apply the" "sum" "rule"],[,"?","Recall the definition of the" "sum" "rule"],[,,(lim)(f(x)+g(x))=((lim)f(x))+((lim)g(x))],[,,f(x)=x],[,,g(x)=-2],[,,"This gives:"],[,,1/(((lim)x)+(lim)(-2))],["&EmptyVerySmallSquare;",,"4. Apply the" "constant" "rule to the term" (lim)(-2)],[,"?","Recall the definition of the" "constant" "rule"],[,,(lim)C=C],[,"?","This means:"],[,,(lim)(-2)=-2],[,,"We can now rewrite the limit as:"],[,,1/(((lim)x)-2)],["&EmptyVerySmallSquare;",,"5. Apply the" "identity" "rule"],[,"?","Recall the definition of the" "identity" "rule"],[,,(lim)x=a],[,,"This gives:"],[,,-1/4]]6""

ShowSolution(Limit(x*sin(1/x), x = 0))

"[[,,"Limit Steps"],[,,(lim)x sin(1/x)],["&EmptyVerySmallSquare;",,"1. Apply the" "product" "rule"],[,"?","Recall the definition of the" "product" "rule"],[,,(lim)f(x) g(x)=((lim)f(x)) ((lim)g(x))],[,,f(x)=x],[,,g(x)=sin(1/x)],[,,"This gives:"],[,,((lim)x) ((lim)sin(1/x))],["&EmptyVerySmallSquare;",,"2. Apply the" "identity" "rule"],[,"?","Recall the definition of the" "identity" "rule"],[,,(lim)x=a],[,,"This gives:"],[,,0]]6""

 

Download Help_derivative_and_limit_ac.mw

First 38 39 40 41 42 43 44 Last Page 40 of 309