nm

8552 Reputation

19 Badges

12 years, 354 days

MaplePrimes Activity


These are questions asked by nm

I need to simplify terms such as (cos(x)^2+sin(x)^2) to 1 if present in input, but I do not Maple to also do any other simplification rewriting polynomials that might be present in the expression.

And example will make it clear. Given this

expr:= (cos(x)^2+sin(x)^2)+5+(1+x+x^2+x^3)*(cos(x)^2+sin(x)^2)*exp(x);

I want expr to become  6+(1+x+x^2+x^3)*exp(x).   i.e. only simplify trig terms

But simplify(expr,trig); gives

                 6 + (x + 1)*(x^2 + 1)*exp(x)

Which is not what I want. Then I tried the trick of thaw and freeze to tell Maple to freeze polynomial type, like this

restart;
expr:= (cos(x)^2+sin(x)^2)+5+(1+x+x^2+x^3)*(cos(x)^2+sin(x)^2)*exp(x);
thaw(simplify(subsindets[flat](expr,satisfies(Z->type(Z,polynom(integer, x))),(freeze))));

And it actually worked, giving

           6 + (x^3 + x^2 + x + 1)*exp(x)

Question is: Why did simplify(expr,trig) not do what expected, which is to only simplify trig terms in expression and not mess around with the polynomial there? 

Is the above method of thaw/freeze to control which parts of expression gets simplify a recommended way to work around this, or is there a better way?

 

I can't figure how to make my own type, which is rational and greater than one.

There are buildin types for postive and posint, and so on. But what if I want to make for rational and greater than some value, say 1?

This is easy to do using patmatch, using the conditional. But do not know how to do it to make my own type.

Here is a simple example. I want to check for sin(x)^n, where n is rational and must be >1.  Using pathmatch

restart;
patmatch(sin(x)^(1/2),conditional(sin(x)^a::rational,a>1),'la');la;

does it. Using structured type, the best I could do is this

restart;

TypeTools:-AddType('my_sin',specfunc(sin)^And(rational,positive));
type(sin(x)^(1/2),'my_sin');

But this does not check for >1, only positive.

Any suggestions? I know I could do this using other means, by direct parsing, using op, and so on. But I'd like to learn how to do it using structured type, just to learn the syntax if there is one.

is it possible to use conditional with structured types? But need  name to do that, like with patmatch, but there is no such syntax in structured types. 

ps. I think conditional does not make much sense with structured type. But I need to figure how to make my own type, which is rational and say >1, or integer and say >2 and so on. I just do not know how to do that yet. But I am sure there is a way. Will try to figure it out.

 

 

I can't get pdsolve to solve this pde. Here are my tries below. One of them works, but the analytical solution Maple gives is wrong. So I am not sure if it needs some additional hints or some other help to make it give the correct solution.

This is Laplace pde but with non-zero on RHS. On disk centered at origin and with radius 1, with given BC on edge of disk.

restart;
the_rhs := r^2*cos(theta)*sin(theta);
pde := VectorCalculus:-Laplacian(u(r,theta),'polar'[r,theta])=the_rhs;
bc := u(1, theta) = cos(theta)*sin(theta);
sol:=pdsolve([pde, bc], u(r, theta));

Maple gives

But this does not look right. There is a complex exponential on its own there. The solution should be real. 

I tried to solve it using pdsolve numerically, but could not make it work. It kepts complaining about missing BC. I am not good with numerical solvers in Maple. May be someone could try to do it.

evalf(eval(rhs(sol),[r= sqrt(1/4 + 1/4),theta=Pi/4]))

gives 0.5951891582 which is wrong,. It should be  0.23958

It tried to give it the symmetry conditions on theta, but now it did not solve it

restart;
the_rhs := r^2*cos(theta)*sin(theta);
pde := VectorCalculus:-Laplacian(u(r,theta),'polar'[r,theta])=the_rhs;
bc := u(1, theta) = cos(theta)*sin(theta),u(r,0) = u(r,2*Pi),(D[2](u))(r, 0) = (D[2](u))(r, 2*Pi);
sol:=pdsolve([pde, bc], u(r, theta));

No solution. I tried adding HINT = boundedseries(r=0) but that also did not help. No solution.

Could someone solve this PDE numerically in Maple and check the solution at the above location? It should be 0.23958 which shows the analytical solution given is not correct. Can Maple solve this numerically?

Maple 2021.1

ps. Analytical solution was obtained by Mathematica. I did not solve this by hand.

I was expanding an expressions, and found that Maple expands trig also. Then found there is a way to turn that off. Which is great. I tested it in my worksheet and it works. But when I put the same code inside my module, I get an error.  It seems like some scoping issue which I do not understand.

Here is a MWE

restart;
expr:=(1+x)*sin(3*x+k);
forget(expand);
expand(expandoff());
expandoff(cos,sin,exp);
F:=expand(expr);
forget(expand);

which gives 

Which does what I want. But inside a proc, the code fails

foo:=proc(expr)
local F;

forget(expand);
expand(expandoff());
expandoff(cos,sin,exp);
F:=expand(expr);
forget(expand);

end proc;

And now when calling it as 

expr:=x*sin(3*x+k);
foo(expr)

What Am I doing wrong? And how to make it work? I need to prevent expand() from expanding these functions. Help says that expand has memory table, and I want to clear that before and after doing this, so it does not affect later code.

Maple 2021.1

Update

I just found out, if I add a restart before defining my function, then the error goes away

restart;
expr:=(1+x)*sin(3*x+k);
forget(expand);
expand(expandoff());
expandoff(cos,sin,exp);
F:=expand(expr);
forget(expand);

restart;  # had to add this. But why??
foo:=proc(expr)
local F;

forget(expand);
expand(expandoff());
expandoff(cos,sin,exp);
F:=expand(expr);
forget(expand);
end proc;

#
expr:=(1+x)*sin(3*x+k);
foo(expr)

#no error. now it works

I need this to work repeatedly inside a module, so I can't do restart each time ofcourse like the above.

Even if I do restart before defining the function, next time I call it, it will fail:

 

restart; 
foo:=proc(expr)
local F;

forget(expand);
expand(expandoff());
expandoff(cos,sin,exp);
F:=expand(expr);
forget(expand);
end proc;

#
expr:=(1+x)*sin(3*x+k);
foo(expr); #OK
foo(expr); #FAILED

Very strange. 

I can't find how to use Maple structured type, to check for say sin(x) and say sin(x)^2 without having to duplicate the code and make a structured type for each of the two cases. An example will explain.

I need to check if expression is of this form   m*sin(anything)^n*cos(anything)^r  Where in this example below, m,n,r can be integer or rational.     

The problem is that 'specfunc'(sin)^Or(integer,rational) will not match sin(x) but will only match if sin(x) is raised to an actual power different from default 1.

So I have to duplicate the check below, by writing Or('specfunc'(sin),'specfunc'(sin)^Or(integer,rational) and the same for cos(x). I am trying to avoid all this duplication, because as the structured type becomes more involved, it will hard to read and work with like this.

Is there a way to eliminate this? In Mathematica for example, this is done using the pattern   x_. where the little dot at the end there, allows for zero of more. So for the above, it will be  Sin[x_]^n_. and this will match Sin[anything] and also match Sin[anything]^2 

I know it is possible to use patmatch in Maple to do this, but is it possible without using pathmatch and just using structured types? as I am trying to do everything without having to use patmatch now.

restart;
my_type:=''`*`'( { Or('specfunc'(sin),'specfunc'(sin)^Or(integer,rational)),
                   Or('specfunc'(cos),'specfunc'(cos)^Or(integer,rational)),
                   Or(integer,rational)})';
type(3*sin(x)^2*cos(x)^3,my_type);
type(sin(x)^2*99*cos(x),my_type);
type(sin(x)*cos(x),my_type);
type(cos(x)*sin(x)^(1/2),my_type);

gives

btw, the help page for structured type mentiones zero or more occurances of but do not know how to do this for the above example

 

First 46 47 48 49 50 51 52 Last Page 48 of 164