login  home  contents  what's new  discussion  bug reports     help  links  subscribe  changes  refresh  edit

Edit detail for Manipulating Expressions revision 4 of 6

1 2 3 4 5 6
Editor: Bill Page
Time: 2008/08/21 07:44:13 GMT-7
Note: rewrite rules

changed:
-sincosAngles:= rule ( _
-   cos((n | integer?(n)) * x) == _
-      cos((n - 1)*x) * cos(x) - sin((n - 1)*x) * sin(x); _
-   sin((n | integer?(n)) * x) == _
-      sin((n - 1)*x) * cos(x) + cos((n - 1)*x) * sin(x) )
sincosAngles:= rule
   cos((n | integer?(n)) * x) ==
      cos((n - 1)*x) * cos(x) - sin((n - 1)*x) * sin(x)
   sin((n | integer?(n)) * x) ==
      sin((n - 1)*x) * cos(x) + cos((n - 1)*x) * sin(x)

The domain InputForm can be quite useful for manipulating parts
of expressions. For example

fricas
ex1:=integrate(log(x)+x, x)
\begin{equation} \label{eq1}{{2 \ x \ {\log \left({x}\right)}}+{{x}^{2}}-{2 \ x}}\over 2\end{equation}
Type: Union(Expression(Integer),...)
fricas
%::InputForm
\begin{equation} \label{eq2}\left(/ \ {\left(+ \ {\left( \ {\left( \ 2 \ x \right)}\ {\left(\log \ x \right)}\right)}\ {\left(+ \ {\left(^\ x \ 2 \right)}\ {\left( \ - 2 \ x \right)}\right)}\right)}\ 2 \right)\end{equation*}
Type: InputForm?
fricas
ex2:=interpret((%::InputForm).2.2)
\begin{equation} \label{eq3}2 \ x \ {\log \left({x}\right)}\end{equation}
Type: Expression(Integer)

If you would like to do this with a more common type of expression and hide the details, you can define

fricas
op(n,x) == interpret((x::InputForm).(n+1))
Type: Void

Then manipulating expressions looks like this:

fricas
op(1,ex1)
fricas
Compiling function op with type (PositiveInteger,Expression(Integer)
      ) -> Any
\begin{equation} \label{eq4}{2 \ x \ {\log \left({x}\right)}}+{{x}^{2}}-{2 \ x}\end{equation}
Type: Expression(Integer)
fricas
op(1,%)
fricas
Compiling function op with type (PositiveInteger,Any) -> Any
\begin{equation} \label{eq5}2 \ x \ {\log \left({x}\right)}\end{equation}
Type: Expression(Integer)
fricas
(op(1,op(1,ex1))-op(2,op(1,ex1)))/op(2,ex1)
\begin{equation} \label{eq6}{{2 \ x \ {\log \left({x}\right)}}-{{x}^{2}}+{2 \ x}}\over 2\end{equation}
Type: Expression(Integer)

Rules and Pattern Matching (from WesterProblemSet?)

Trigonometric manipulations---these are typically difficult for students

fricas
r:= cos(3*x)/cos(x)
\begin{equation} \label{eq7}{\cos \left({3 \ x}\right)}\over{\cos \left({x}\right)}\end{equation}
Type: Expression(Integer)

=> cos(x)^2 - 3 sin(x)^2 or similar

fricas
real(complexNormalize(r))
\begin{equation} \label{eq8}-{2 \ {{\sin \left({x}\right)}^{2}}}+{2 \ {{\cos \left({x}\right)}^{2}}}- 1\end{equation}
Type: Expression(Integer)

=> 2 cos(2 x) - 1

fricas
real(normalize(simplify(complexNormalize(r))))
\begin{equation} \label{eq9}{2 \ {\cos \left({2 \ x}\right)}}- 1\end{equation}
Type: Expression(Integer)

Use rewrite rules => cos(x)^2 - 3 sin(x)^2

fricas
sincosAngles:= rule
   cos((n | integer?(n)) * x) ==
      cos((n - 1)*x) * cos(x) - sin((n - 1)*x) * sin(x)
   sin((n | integer?(n)) * x) ==
      sin((n - 1)*x) * cos(x) + cos((n - 1)*x) * sin(x)
\begin{equation*} \label{eq10}\begin{array}{@{}l} \displaystyle \left\{{{\cos \left({n \ x}\right)}\mbox{\rm = =}{-{{\sin \left({x}\right)}\ {\sin \left({{\left(n - 1 \right)}\ x}\right)}}+{{\cos \left({x}\right)}\ {\cos \left({{\left(n - 1 \right)}\ x}\right)}}}}, \right. \ \ \displaystyle \left.\:{{\sin \left({n \ x}\right)}\mbox{\rm = =}{{{\cos \left({x}\right)}\ {\sin \left({{\left(n - 1 \right)}\ x}\right)}}+{{\cos \left({{\left(n - 1 \right)}\ x}\right)}\ {\sin \left({x}\right)}}}}\right\} \end{array} \end{equation*}
Type: Ruleset(Integer,Integer,Expression(Integer))

fricas
sincosAngles r
\begin{equation} \label{eq11}-{3 \ {{\sin \left({x}\right)}^{2}}}+{{\cos \left({x}\right)}^{2}}\end{equation}
Type: Expression(Integer)

Other Operations

The domain FunctionSpace? includes the following operations:

    isExpt(p,f:Symbol) returns [x, n] if p = x**n and n <> 0 and x = f(a)
    isExpt(p,op:BasicOperator) returns [x, n] if p = x**n and n <> 0 and x = op(a)
    isExpt(p) returns [x, n] if p = x**n and n <> 0
    isMult(p) returns [n, x] if p = n * x and n <> 0
    isPlus(p) returns [m1,...,mn] if p = m1 +...+ mn and n > 1
    isPower(p) returns [x, n] if p = x**n and n <> 0
    isTimes(p) returns [a1,...,an] if p = a1*...*an and n > 1

If these conditions are not met, then the above operations return "failed".

For example,

fricas
isMult(3*x)
\begin{equation*} \label{eq12}\left[{coef = 3}, \:{var = x}\right]?\end{equation*}
Type: Union(Record(coef: Integer,var: Kernel(Expression(Integer))),...)

but

fricas
isMult(x*y)
\begin{equation} \label{eq13}\mbox{\tt "failed"}\end{equation}
Type: Union("failed",...)

In the context of Expression Integer, or Polynomial Integer the parameter n must be an Integer. The Symbol y is not an Integer.

Not exactly analogously

fricas
isPower(x**y)
There are no library operations named ** Use HyperDoc Browse or issue )what op ** to learn if there is any operation containing " ** " in its name.
Cannot find a definition or applicable library operation named ** with argument type(s) Variable(x) Variable(y)
Perhaps you should use "@" to indicate the required return type, or "$" to specify which version of the function you need.

whereas

fricas
isPower(x**10)
There are no library operations named ** Use HyperDoc Browse or issue )what op ** to learn if there is any operation containing " ** " in its name.
Cannot find a definition or applicable library operation named ** with argument type(s) Variable(x) PositiveInteger
Perhaps you should use "@" to indicate the required return type, or "$" to specify which version of the function you need.

In the first case the Integer is assume to be 1.

We have:

fricas
isTimes(x*y*z)
\begin{equation*} \label{eq14}\left[ z , \: y , \: x \right]?\end{equation*}
Type: Union(List(Polynomial(Integer)),...)
fricas
isPlus(x+y+z*y)
\begin{equation*} \label{eq15}\left[{y \ z}, \: y , \: x \right]?\end{equation*}
Type: Union(List(Polynomial(Integer)),...)

Whereas

fricas
isTimes((x+y)*z)
\begin{equation} \label{eq16}\mbox{\tt "failed"}\end{equation}
Type: Union("failed",...)

That is because the expression is internally treated as a MultivariatePolynomial like this:

fricas
((x+y)*z)::MPOLY([x,y,z],INT)
\begin{equation} \label{eq17}{z \ x}+{z \ y}\end{equation}
Type: MultivariatePolynomial?([x,y,z],Integer)

If you say:

fricas
isPlus((x+y)*z)
\begin{equation*} \label{eq18}\left[{y \ z}, \:{x \ z}\right]?\end{equation*}
Type: Union(List(Polynomial(Integer)),...)

perhaps the result makes sense?

For some of the details of these operations I consulted the actual algebra code at:

http://axiom-wiki.newsynthesis.org/axiom--test--1/src/algebra/FspaceSpad

Click on pdf or dvi to see the documentation.

You can also enter expressions like isTimes in the search box on the upper right and see all the places in the algebra where this operation is defined and used.


Some or all expressions may not have rendered properly, because Latex returned the following error:
This is pdfTeXk, Version 3.141592-1.40.3 (Web2C 7.5.6)
 \write18 enabled.
 %&-line parsing enabled.
entering extended mode
(./7326715108243216002-16.0px.tex
LaTeX2e <2005/12/01>
Babel <v3.8h> and hyphenation patterns for english, usenglishmax, dumylang, noh
yphenation, arabic, farsi, croatian, ukrainian, russian, bulgarian, czech, slov
ak, danish, dutch, finnish, basque, french, german, ngerman, ibycus, greek, mon
ogreek, ancientgreek, hungarian, italian, latin, mongolian, norsk, icelandic, i
nterlingua, turkish, coptic, romanian, welsh, serbian, slovenian, estonian, esp
eranto, uppersorbian, indonesian, polish, portuguese, spanish, catalan, galicia
n, swedish, ukenglish, pinyin, loaded.
(/usr/share/texmf-texlive/tex/latex/base/article.cls
Document Class: article 2005/09/16 v1.4f Standard LaTeX document class
(/usr/share/texmf-texlive/tex/latex/base/size12.clo))
(/usr/share/texmf-texlive/tex/latex/ucs/ucs.sty
(/usr/share/texmf-texlive/tex/latex/ucs/data/uni-global.def))
(/usr/share/texmf-texlive/tex/latex/base/inputenc.sty
(/usr/share/texmf-texlive/tex/latex/ucs/utf8x.def))
(/usr/share/texmf-texlive/tex/latex/bbm/bbm.sty)
(/usr/share/texmf-texlive/tex/latex/jknapltx/mathrsfs.sty)
(/usr/share/texmf-texlive/tex/latex/base/fontenc.sty
(/usr/share/texmf-texlive/tex/latex/base/t1enc.def))
(/usr/share/texmf-texlive/tex/latex/pstricks/pstricks.sty
(/usr/share/texmf-texlive/tex/generic/pstricks/pstricks.tex
`PSTricks' v1.15  <2006/12/22> (tvz)
(/usr/share/texmf-texlive/tex/generic/pstricks/pstricks.con))
(/usr/share/texmf/tex/latex/xcolor/xcolor.sty
(/etc/texmf/tex/latex/config/color.cfg)
(/usr/share/texmf-texlive/tex/latex/graphics/dvips.def)
(/usr/share/texmf-texlive/tex/latex/graphics/dvipsnam.def)))
(/usr/share/texmf-texlive/tex/latex/graphics/epsfig.sty
(/usr/share/texmf-texlive/tex/latex/graphics/graphicx.sty
(/usr/share/texmf-texlive/tex/latex/graphics/keyval.sty)
(/usr/share/texmf-texlive/tex/latex/graphics/graphics.sty
(/usr/share/texmf-texlive/tex/latex/graphics/trig.sty)
(/etc/texmf/tex/latex/config/graphics.cfg))))
(/usr/share/texmf-texlive/tex/latex/pst-grad/pst-grad.sty
(/usr/share/texmf-texlive/tex/generic/pst-grad/pst-grad.tex
(/usr/share/texmf-texlive/tex/latex/xkeyval/pst-xkey.tex
(/usr/share/texmf-texlive/tex/latex/xkeyval/xkeyval.sty
(/usr/share/texmf-texlive/tex/latex/xkeyval/xkeyval.tex)))
`pst-plot' v1.05, 2006/11/04 (tvz,dg,hv)))
(/usr/share/texmf-texlive/tex/latex/pstricks/pst-plot.sty
(/usr/share/texmf-texlive/tex/generic/pstricks/pst-plot.tex
 v97 patch 2, 1999/12/12
(/usr/share/texmf-texlive/tex/generic/multido/multido.tex
 v1.41, 2004/05/18 <tvz>)))
(/usr/share/texmf-texlive/tex/latex/geometry/geometry.sty
(/usr/share/texmf-texlive/tex/xelatex/xetexconfig/geometry.cfg)

Package geometry Warning: `lmargin' and `rmargin' result in NEGATIVE (-108.405p t). `width' should be shortened in length.

) (/usr/share/texmf-texlive/tex/latex/amsmath/amsmath.sty For additional information on amsmath, use the `? option. (/usr/share/texmf-texlive/tex/latex/amsmath/amstext.sty (/usr/share/texmf-texlive/tex/latex/amsmath/amsgen.sty)) (/usr/share/texmf-texlive/tex/latex/amsmath/amsbsy.sty) (/usr/share/texmf-texlive/tex/latex/amsmath/amsopn.sty)) (/usr/share/texmf-texlive/tex/latex/amsfonts/amsfonts.sty) (/usr/share/texmf-texlive/tex/latex/amsfonts/amssymb.sty) (/usr/share/texmf-texlive/tex/latex/amscls/amsthm.sty) (/usr/share/texmf-texlive/tex/latex/setspace/setspace.sty Package: `setspace 6.7 <2000/12/01> ) (/usr/share/texmf-texlive/tex/generic/xypic/xy.sty (/usr/share/texmf-texlive/tex/generic/xypic/xy.tex Bootstrap'ing: catcodes, docmode, (/usr/share/texmf-texlive/tex/generic/xypic/xyrecat.tex) (/usr/share/texmf-texlive/tex/generic/xypic/xyidioms.tex)

Xy-pic version 3.7 <1999/02/16> Copyright (c) 1991-1998 by Kristoffer H. Rose <krisrose@ens-lyon.fr> Xy-pic is free software: see the User's Guide for details.

Loading kernel: messages; fonts; allocations: state, direction, utility macros; pictures: \xy, positions, objects, decorations; kernel objects: directionals, circles, text; options; algorithms: directions, edges, connections; Xy-pic loaded)) (/usr/share/texmf-texlive/tex/generic/xypic/xyall.tex Xy-pic option: All features v.3.3 (/usr/share/texmf-texlive/tex/generic/xypic/xycurve.tex Xy-pic option: Curve and Spline extension v.3.7 curve, circles, loaded) (/usr/share/texmf-texlive/tex/generic/xypic/xyframe.tex Xy-pic option: Frame and Bracket extension v.3.7 loaded) (/usr/share/texmf-texlive/tex/generic/xypic/xycmtip.tex Xy-pic option: Computer Modern tip extension v.3.3 (/usr/share/texmf-texlive/tex/generic/xypic/xytips.tex Xy-pic option: More Tips extension v.3.3 loaded) loaded) (/usr/share/texmf-texlive/tex/generic/xypic/xyline.tex Xy-pic option: Line styles extension v.3.6 loaded) (/usr/share/texmf-texlive/tex/generic/xypic/xyrotate.tex Xy-pic option: Rotate and Scale extension v.3.3 loaded) (/usr/share/texmf-texlive/tex/generic/xypic/xycolor.tex Xy-pic option: Colour extension v.3.3 loaded) (/usr/share/texmf-texlive/tex/generic/xypic/xymatrix.tex Xy-pic option: Matrix feature v.3.4 loaded) (/usr/share/texmf-texlive/tex/generic/xypic/xyarrow.tex Xy-pic option: Arrow and Path feature v.3.5 path, \ar, loaded) (/usr/share/texmf-texlive/tex/generic/xypic/xygraph.tex Xy-pic option: Graph feature v.3.7 loaded) loaded) (/usr/share/texmf-texlive/tex/latex/tools/verbatim.sty) (/usr/share/texmf/tex/latex/graphviz/graphviz.sty (/usr/share/texmf-texlive/tex/latex/psfrag/psfrag.sty)) (/usr/share/texmf/tex/latex/sagetex.sty Writing sage input file 7326715108243216002-16.0px.sage ) (/usr/share/texmf-texlive/tex/latex/gnuplottex/gnuplottex.sty (/usr/share/texmf-texlive/tex/latex/base/latexsym.sty) (/usr/share/texmf-texlive/tex/latex/moreverb/moreverb.sty) (/usr/share/texmf-texlive/tex/latex/base/ifthen.sty)) (./7326715108243216002-16.0px.aux) (/usr/share/texmf-texlive/tex/latex/ucs/ucsencs.def) (/usr/share/texmf-texlive/tex/latex/jknapltx/ursfs.fd) (/usr/share/texmf-texlive/tex/latex/amsfonts/umsa.fd) (/usr/share/texmf-texlive/tex/latex/amsfonts/umsb.fd) (/usr/share/texmf-texlive/tex/latex/base/ulasy.fd)

Package amsmath Warning: Foreign command \over; (amsmath) \frac or \genfrac should be used instead (amsmath) on input line 122.

[1] Missing { inserted. <to be read again> \ l.124 ...x \right)}\right)}\ {\left(+ \ {\left(^\ x \ 2 \right)}\ {\left(*...

Missing } inserted. <inserted text> } l.124 ...ht)}\ {\left(+ \ {\left(^\ x \ 2 \right )}\ {\left(* \ - 2 \ x \...

[2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] (/usr/share/texmf-texlive/tex/latex/base/t1cmtt.fd) [13] [14] [15] [16] [17] [18] (./7326715108243216002-16.0px.aux) ) (see the transcript file for additional information) Output written on 7326715108243216002-16.0px.dvi (18 pages, 5436 bytes). Transcript written on 7326715108243216002-16.0px.log.