schapplm

40 Reputation

5 Badges

4 years, 177 days

MaplePrimes Activity


These are replies submitted by schapplm

@acer thanks for point out the functions. Some of the commands are very hard to extract from the Maple documentation. At least for someone like me who only worked for six years with Maple at a low level of mathematical and syntactical complexity. Perhaps writing them down here helps someone else searching for this problem:

userinfo / infolevel # for logging
# for determining computational cost more efficiently:
weighting := [':-functions'=1,':-additions'=1, ':-multiplications'=1,':-divisions'=1];
simplify_tmpvar_c2 := codegen:-cost(Term2):
simplify_tmpvar_c2sum := eval(simplify_tmpvar_c2, weighting);
MmaTranslator:-Mma:-LeafCount(T) # number of indivisible expressions
`simplify/size/size`(T); # for getting the metric for the simplify command

the whole construction was necessary in the first place to avoid the simplify function un-simplifying the expression. But this might be a different topic...

@acer you are right. It does not work. It only works when doing a temporary assignment like:

v := Matrix(<l1*cos(q1s)+l2*cos(q1s)+l1*sin(q1s);l1*sin(q2)+l2;0>,3,1);
save v, "v_before_assignment":
l1 := length(v);
save v, "v_after_assignment":
tmp:=v(1,1);
v11simpl1 := simplify2(tmp); # works. Gives l1*cos(q1s)+l2*cos(q1s)+l1*sin(q1s)
v11simpl2 := simplify3(v(1,1)); # does not work. Gives "127*cos(q1s)+l2*cos(q1s)+127*sin(q1s)"
vsimpl := simplify2(v); # Does not work. Gives "Matrix(3, 1, [[127*sin(q1s)+(l2+127)*cos(q1s)],[127*sin(q2)+l2],[0]])"
save v11simpl1,v11simpl2,vsimpl, "v_after_simplify":

As I encountered other problems even after the correct simplification, I will have to let this approach go.

I just came to the conclusion that the whole approach of trying to make the procedure unaware of the global variables may not be useful. Every further calculation in the workspace suffers from the same problem: if an expression contains a symbol (say "l1") and the symbol is used as a temporary variable (say l1:=200), then the value seems to get inserted immediately in the next computation of the expression. I will probably redefine all temporary variables so they do not conflict with the list of allowed/expected variables for the expressions.

Otherwise I would have to add some kind of eval(x,2) command in all my formulas which will render them unreadable.

@tomleslie The output in the Maple worksheet does not seem to be what is "inside" the variable. The output seems to be the evaluation of the expression with symbols substituted. What I do is to use the read-statement to check the variable like

T := m2*(l1+l2)*(qD2)^2:
save T, "T_before_assign": String(T);
l1 := length(T);
save T, "T_after_assign": String(T);

This gives different outputs:

  • workspace: "m2*(l1+l2)*qD2^2" and "m2*(26+l2)*qD2^2"
  • files: "T := m2*(l1+l2)*qD2^2;" in both cases.

Since I work with files to transfer variables between different worksheets, the saved files are the "ground truth" for me and make the whole toolbox work. Using

printf("input to proc.: %s\n", String(Term)):

showed that the fully evaluated expression was already given to the procedure. But I think this was addressed by the next answer already. Thanks for pointing this out.

@Preben Alsholm thanks for this efficient solution. It works even without the for loop for finding the symbols and declaring them local variables.
One remaining issue is the following: If I use the same function for indexed matrices or arrays, it only returns the name of the array:

v := Matrix(<a+f;0;1>,3,1);
simplify2(v(1));

The function now returns

v(1)

instead of

a+f

I did a workaround to handle indexed variables:

simplify3 := proc (Term0)
  local Term1:
  Term1 := simplify2(Term0):
  return Term1:
end proc:

I could of course simplify all indexed variables with the (old) simplify2 and all non-indexed-variables with (the new) simplify3.
But this seems complicated and not very elegant. Do you know of a possible solution that covers both cases?
I am still wondering if this workaround is really necessary, since I "only" want to simplify some expressions...

@tomleslie Thanks for your answer. I did not think about sqrt(x)=x^(1/2) before. Since I want to use the function count in a scientific publication, I will verify them manually in the Matlab code. From what I read, sqrt is implemented with a dedicated algorithm in hardware in the CPU, so I will create a weighted sum of the different function calls (sqrt,sin,cos,atan) and count them separately.

Writing the code myself is not an option here, since it describes the dynamics of multibody systems (robots) and some special expressions are only available via symbolic computing and are way too complicated for manual derivation.

@vv 

Using the inert form makes this a lot faster and helps a lot with this kind of geometric problem.

Normally, I only need cos(alpha) and sin(alpha) from alpha which I can get directly from exp_x, exp_y of my example above (for rotation matrizes in kinematics). The angle alpha itself is only needed as a time derivative.

The time derivative of the inert form is calculated as fast as the time derivative of the full expression, so I guess the inert arctan expression does not have to be evaluated any more for time differentiation:

> alpha1:=%arctan(exp_y, exp_x): # fast
> alphaD:=diff(alpha1, t): # fast
> alpha2:=arctan(exp_y, exp_x): # slow
> alphaD:=diff(alpha2, t): # fast
 

 

 

 

@vv 

do you mean by inert variant to use a temporary variable which is substituted after the arctan expression is assigned?

> alpha := arctan(tmp1, tmp2):
> alpha := subs({tmp1=exp_y, tmp2=exp_x}, alpha):

my problem is, that I need the full expression with arctan for later calculations, for example time derivative of the whole expression for energy calculations.

 

@Preben Alsholm 

This is a good point.

Unfortunately, the read function of ".mpl" files takes a long time for complicated nested expressions so the "end user" is forced to use the ".m" format.

@Preben Alsholm 

The last part with string conversion did the trick for converting all variables. Thank you.

@Preben Alsholm 

From my understanding, Maple has two different file formats to save expressions: .m and plain text.

Saving as .mpl or with any other ending than .m just saves the expression in text form. The "~"-characters are gone in this expression, which is good, but the expression has to be re-evaluated when reading the file in maple.

I have a complex non-simplified expression taking 2MB when saving as text. Reading this expression again just freezes Maple...

Page 1 of 1