nm

11353 Reputation

20 Badges

13 years, 19 days

MaplePrimes Activity


These are questions asked by nm

After doing 

A:=tan(3*x);
B:=expand(A)

How to get A back from B? I tried

combine(B);
simplify(B,size);
simplify(B,trig);
simplify(B);

They do not give A back. tried few conversions to exp() and back and forth. Can't get original expression back.  trigsubs() does not help here.

This is all need to be done in code, assuming one can not look at the expression and decide what to do on any A/B pair. But I am mainly now looking at trig expressions.

Any suggestions?

Maple 2020.2

Both exp(x)^2 and exp(2*x) are correct and the same. 

simplify(exp(x)^2 - exp(2*x))

                     0

But I can't figure how to make Maple always use exp(2*x) instead. This is for exponent  in integers. This covers 90% of all cases in the Latex I generate.

This is only for Latex purposes and for typesetting only as it looks much better and more standard to use exp(2*x) than exp(x)^2.

Here is an example

restart;
expr:=-(2*x+1)*exp(-x)/(exp(-2*x))+(2*x+1)/exp(-2*x);
expand(expr)

The latex for the above looks like 

The Latex take much larger vertical space on the line. Compare the above to the same expression, but written with exponent inside

expr:=-2*exp(x)*x - exp(x) + 2*exp(2*x)*x + exp(2*x)

Which generates Latex

Which is much more pleasent to read and has less height as well.

Is there a way to automatically make Maple do that? Could I add my own `Latex\function` ? As described in 

https://www.maplesoft.com/support/help/Maple/view.aspx?path=latex%2ffunctions

How? Also, I am using Physics:-Latex and not :-latex, so I do not know if the above page would even apply here.

it will be hard to write code to manually fix this, by may be string searching and fix it as the program is running.

Any suggestions? Note that Mathematica automatically does this when the exponent is integer

Update

Thanks to the hint by Axel Voget below, I tried to use combine, but only target it to exp(x)^n part of the whole expression, as I do not want to apply combine to each expression. This is what I came up with

restart;
expr := -2*exp(x)*x - exp(x) + 2*exp(x)^2*x + exp(x)^2 + sin(x)^3 + cos(x)^2;
subsindets(expr,'exp(anything)^(anything)',f->combine(f));

Which seems to do what I want. It only applies to exp(x)^n and no other part. Compare this to applying combine to the whole expression

combine(expr)

Which might not be what I wanted.

I still need to test the above more, as may be I did not check for other cases. But if this works, then this will solve my problem. I can add this in one place, where it does this just before emitting the Latex. All my Latex generation is done inside one function which is called from everywhere. So I could easily add this there without changing anything else in the program.

 

This is the context. I am doing reduction of order on an ODE. This sometimes converts the ode to form  where common factor containing only  x can be moved outside, making the ode looks like  f(x)*(y''(x)+....etc...) = 0 then now I can eliminate f(x)  and only solve (y''(x)+....etc...) = 0 which is simpler.

I found if I can call factor on the ode, it works. It does remove any common terms. The problem I am having is how to cleanly determine the factors obtained. In the above example, it will be f(x) and (y''(x)+....etc...) 

For an example, the ODE   x^2 y'' + x y' = 0 can be written (using factor) as  x ( x y'' + y') =0 and now canceling x<>0, the ode becomes simpler x y''+ y' = 0.

I am now trying to find the two factors, using using op() on the result of factor.

But it does not work for all cases. There should be a way more robust way to obtain the factors.  I give 2 examples to better explain.

Example 1

ode:=x^2*diff(y(x),x$2)-(x-3/16)*y(x)=0:
ode:=expand(algsubs( y(x)=v(x)*x^(1/4)*exp(2*sqrt(x)),ode));
ode:=factor(ode);

After factoring the original ODE, there is common term found, which is the one shown UP. I need to find this to cancel it and keep the rest.

Currently I do this, which does not work for call cases

#check it is factored OK. If the type is `*` then Maple
#found common factor.
if type(lhs(ode),`*`) then 
   #add code to extract the two parts
   LHS:=op([1..-2],lhs(ode));
   RHS:=op(-1,lhs(ode));
fi;

Even though this worked here. I can now check it is the LHS which needs to be canceled. But all what I have are the operands. I do not know how to reconstruct the LHS from the operands. They could be + or *.  If it was `*` between the oprands for LHS, then I can do LHS:=mul(LHS) and this gives 

But I got lucky here. I do not know if it will be `*` all the time for LHS operands.

example 2

ode:=x^2*diff(y(x),x$2)-x*(x+2)*diff(y(x),x)+(x+2)*y(x)=0;
ode:=algsubs( y(x)=v(x)*x,ode):
ode:=factor(ode);

For this, the code I have works, but this is only because the LHS was simple.

if type(lhs(ode),`*`) then #factored OK
   LHS:=op([1..-2],lhs(ode));
   RHS:=op(-1,lhs(ode));
fi;

My question is: I expect factor, if there is common factor, to generate 2 expressions with `*` between them. I am looking for a good way to find what these two factors are. Once I do, it is easy for me to find which is the ODE and which is not and cancel the one which is not out.

I tried collect, but this does not work in general. Since I do not know beforehand, what is the common term, if any, present.

If this needs more clarification please feel free to ask. I have many more examples. This is all done by coding. Non-interactive. So solution based on looking at the output then do something, will not work for me.

 

 

Currently to parse initial conditions of ODE entered bu user in the form y(x0)=y0 in order to determine x0 and y0 I use patmatch on lhs and rhs separatly which is not a big deal. I wanted to see if I can use an equation inside patmatch.

I could not figure how to use patmatch on both sides of equation on one call.  Help says expression to patmatch is algebraic expression. Does this means an equation can't be used as whole? Here is an example to make it more clear

restart;
ic:=y(0) = 1;
patmatch(ic,y(x0::anything)=y0::anything,'la')

Which gives false. i.e. no match found.

Is there a trick to make the above work if possible? This is just a basic example. I might want to apply this on more complicated equation where more things are on lhs and rhs of =  but patmatch does not like `=` in the middle there. 

 

 

 

After I just updated to Physics 871, my main worksheet which allready has for long time now:

Physics:-Latex:-UseColor := false;  
Physics:-Latex:-UseImaginaryUnit:=i;

Now I get 

The reason I use Physics:-Latex:-UseImaginaryUnit:=i; is to make the Latex use for complex number in Latex instead of I

What is now the correct way to use these settings with the new Physics update? if I do not use them, then compelx I  will still show as I whcih I do not want. I want lower case i

I could add interface(imaginaryunit=i); in place of Physics:-Latex:-UseImaginaryUnit:=i; and that work for the latex, making the Latex generate i. But this solution now makes the User interface no longer accept I  as complex number any more and must be typed as lower case, which is not what I want.

That is why I was using Physics:-Latex:-UseImaginaryUnit:=i; This keeps the user interface complex number as Uppercase, but let the latex generated be lower case.

I feel I am doing something wrong, but do not see it. Made sure I use Physics:-Latex on everything now.

Please see below 2 worksheets. One using the 871 version and one using 865 version.
 

restart;

Physics:-Version();

`The "Physics Updates" version in the MapleCloud is 871 and is the same as the version installed in this computer, created 2020, November 14, 14:16 hours Pacific Time.`

Physics:-Latex:-Settings()

[invisibletimes = ` `, powersoftrigonometricfunctions = mixed, usecolor = true, useimaginaryunit = I, useinputlineprompt = true, userestrictedinputtranslation = false, usespecialfunctionrules = true, usetypesettingcurrentsettings = false]

Physics:-Latex:-UseColor := false;  
Physics:-Latex:-UseImaginaryUnit:=i;
 

Error, attempting to assign to `Physics:-Latex:-UseColor` which is protected

Error, attempting to assign to `Physics:-Latex:-UseImaginaryUnit` which is protected

Physics:-Latex(I)

I

 

 

Download latex_871.mw
 

restart;

Physics:-Version();

`The "Physics Updates" version in the MapleCloud is 871. The version installed in this computer is 865 created 2020, November 11, 8:43 hours Pacific Time, found in the directory C:\Users\me\maple\toolbox\2020\Physics Updates\lib\`

Physics:-Latex:-Settings()

usecolor = true, useimaginaryunit = I, useinvisibletimes = ` `, useinputlineprompt = true, userestrictedinputtranslation = false, usespecialfunctionrules = true, usetypesettingcurrentsettings = false

Physics:-Latex:-UseColor := false;  
Physics:-Latex:-UseImaginaryUnit:=i;
Physics:-Latex(I);
 

false

i

i

 

 

Download latex_865.mw

First 105 106 107 108 109 110 111 Last Page 107 of 199