Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#2537 sqrt in subst does not give expected results

None
open
nobody
None
5
2013-01-24
2013-01-21
Valery Lovchikov
No

The samples below demonstrate that function subst substitute the operations to f:
(%i1) subst(f,"+",a+b+c);
(%o1) f(a,b,c)
(%i2) subst(f,"^",exp(%ia));
(%o2) f(%e,%i
a)
(%i3) subst(f,sin,sin(a+b));
(%o3) f(b+a)

and so on.

But if sqrt function is used then subst gives not expected results:

(%i10) subst(f,sqrt,sqrt(a+b));
(%o10) sqrt(b+a)

So, the substitution of the expression does not happened.
(Should be: f(b+a) )

This seems not quite correct.

Discussion

  • Raymond Toy
    Raymond Toy
    2013-01-24

    This probably happens because sqrt(x) is represented internally as x^(1/2). Same issue with a/b, which is represented internally as a*(b^(-1)).

    subst(f,"^", sqrt(a+b)) -> f(a+b,1/2), which is probably not what you want.

     
  • if sqrt(x) would be always represented as x^(1/2) , than this question
    would no arise.

    Please, pay attention that name of sqrt(x) is sqrt:
    part(sqrt(x),0) -> sqrt

    And sqrt(x) has only one argument:
    args(sqrt(x)) -> [x]

    but "^" has 2 arguments:
    args(a^b) -> [a,b]

    So,
    subst(f,part(func(x),0),func(x))
    will not give the replacement of func to f only when func=sqrt.
    That seems not logical.

    In my opinion, EITHER
    subst should be changed that way that it acts accordingly with
    part(sqrt(x),0) и args(sqrt(x)),
    OR
    change part(sqrt(x),0) - > "^" and args(sqrt(x)) - > [x,1/2]
    so they worked accordingly to subst

     
    • Hmm, well Maxima has inpart, used as follows:

      Maxima 5.28.0_268_gedc4ea0 http://maxima.sourceforge.net
      using Lisp SBCL 1.0.57.0.debian
      Distributed under the GNU Public License. See the file COPYING.
      Dedicated to the memory of William Schelter.
      The function bug_report() provides bug reporting information.
      (%i1) part(sqrt(x), 0);
      (%o1)                                sqrt
      (%i2) inpart(sqrt(x), 0);
      (%o2)                                  ^
      (%i3) inpart(sqrt(x), 1);
      (%o3)                                  x
      (%i4) inpart(sqrt(x), 2);
                                             1
      (%o4)                                  -
                                             2
      

      I don't think that there is an "inargs" function, which would presumably return [x, 1/2] for sqrt(x). But maybe inpart is what you want to use.

      Since this is a bug report, rather than a mailing list thread, I suppose the remaining question is: What specific problem or missing feature do you think remains?

       
  • Thank you very much for the problem discussion.

    Let me explain you where from this question is originated:
    I need a function that changes in an expression one operation
    to another operation. For example, for cancelation (reducing)
    of positive common divisor in arguments of atan2

    atan2_gcd(a,b):=(abs(gcd(%%)), atan2(factor(a/%%),factor(b/%%)))$

    Than 2 more actions:

    subst(atan2_gcd, atan2, expr)$ ev(expr,atan2_gcd);

    This will make the cancelation (reducing) of the greatest common
    divisor
    in all atan2 of expression.

    The attempt to use the same scheme for work with arguments
    of the square roots was successful not for all operations.

    Now, after discussing of this subject, I use my own function,
    subst_op instead of subst

    subst_op(a,b,c):=
    if atom(c) then c
    else apply((op(c), if b=%% then a else %%),
    makelist(subst_op(a,b,c_),c_,args(c)))$

    The subst_op changes all operations "a" in expression "c".

    May be it make sense to add subst_op in the general list of functions
    of maxima?

     
  • atan2_gcd(a,b):=(abs(gcd(a,b)), atan2(factor(a/%%),factor(b/%%)))$