X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=doc%2Ftutorial%2Fginac.texi;h=ad48666c783e916c607579191c221c5d2b2fcb13;hp=92fb7d1463f31d004015366fac9a100235b83b9d;hb=5a8b8e3c4d882249db35b679ce3144a59a7012e8;hpb=568524ae4543f305035f4c5b2ecdb4cf78514c33 diff --git a/doc/tutorial/ginac.texi b/doc/tutorial/ginac.texi index 92fb7d14..ad48666c 100644 --- a/doc/tutorial/ginac.texi +++ b/doc/tutorial/ginac.texi @@ -4592,56 +4592,22 @@ The last example would be written in C++ in this way: @} @end example -@subsection Algebraic substitutions -Supplying the @code{subs_options::algebraic} option to @code{subs()} -enables smarter, algebraic substitutions in products and powers. If you want -to substitute some factors of a product, you only need to list these factors -in your pattern. Furthermore, if an (integer) power of some expression occurs -in your pattern and in the expression that you want the substitution to occur -in, it can be substituted as many times as possible, without getting negative -powers. - -An example clarifies it all (hopefully): - -@example -cout << (a*a*a*a+b*b*b*b+pow(x+y,4)).subs(wild()*wild()==pow(wild(),3), - subs_options::algebraic) << endl; -// --> (y+x)^6+b^6+a^6 - -cout << ((a+b+c)*(a+b+c)).subs(a+b==x,subs_options::algebraic) << endl; -// --> (c+b+a)^2 -// Powers and products are smart, but addition is just the same. - -cout << ((a+b+c)*(a+b+c)).subs(a+b+wild()==x+wild(), subs_options::algebraic) - << endl; -// --> (x+c)^2 -// As I said: addition is just the same. - -cout << (pow(a,5)*pow(b,7)+2*b).subs(b*b*a==x,subs_options::algebraic) << endl; -// --> x^3*b*a^2+2*b - -cout << (pow(a,-5)*pow(b,-7)+2*b).subs(1/(b*b*a)==x,subs_options::algebraic) - << endl; -// --> 2*b+x^3*b^(-1)*a^(-2) - -cout << (4*x*x*x-2*x*x+5*x-1).subs(x==a,subs_options::algebraic) << endl; -// --> -1-2*a^2+4*a^3+5*a - -cout << (4*x*x*x-2*x*x+5*x-1).subs(pow(x,wild())==pow(a,wild()), - subs_options::algebraic) << endl; -// --> -1+5*x+4*x^3-2*x^2 -// You should not really need this kind of patterns very often now. -// But perhaps this it's-not-a-bug-it's-a-feature (c/sh)ould still change. - -cout << ex(sin(1+sin(x))).subs(sin(wild())==cos(wild()), - subs_options::algebraic) << endl; -// --> cos(1+cos(x)) - -cout << expand((a*sin(x+y)*sin(x+y)+a*cos(x+y)*cos(x+y)+b) - .subs((pow(cos(wild()),2)==1-pow(sin(wild()),2)), - subs_options::algebraic)) << endl; -// --> b+a -@end example +@subsection The option algebraic +Both @code{has()} and @code{subs()} take an optional argument to pass them +extra options. This section describes what happens if you give the former +the option @code{has_options::algebraic} or the latter +@code{subs:options::algebraic}. In that case the matching condition for +powers and multiplications is changed in such a way that they become +more intuitive. Intuition says that @code{x*y} is a part of @code{x*y*z}. +If you use these options you will find that +@code{(x*y*z).has(x*y, has_options::algebraic)} indeed returns true. +Besides matching some of the factors of a product also powers match as +often as is possible without getting negative exponents. For example +@code{(x^5*y^2*z).subs(x^2*y^2==c, subs_options::algebraic)} will return +@code{x*c^2*z}. This also works with negative powers: +@code{(x^(-3)*y^(-2)*z).subs(1/(x*y)==c, subs_options::algebraic)} will +return @code{x^(-1)*c^2*z}. Note that this only works for multiplications +and not for locating @code{x+y} within @code{x+y+z}. @node Applying a Function on Subexpressions, Visitors and Tree Traversal, Pattern Matching and Advanced Substitutions, Methods and Functions