]> www.ginac.de Git - ginac.git/blobdiff - doc/tutorial/ginac.texi
degree(), ldegree(), coeff(), lcoeff(), tcoeff() and collect() can now
[ginac.git] / doc / tutorial / ginac.texi
index 7ee78cb9791d5764a137ac825d8615b9e467fc29..d3a4d992beb56c90b726a9185f896a39874c8271 100644 (file)
@@ -180,6 +180,7 @@ pointless) bivariate polynomial with some large coefficients:
 
 @example
 #include <ginac/ginac.h>
+using namespace std;
 using namespace GiNaC;
 
 int main()
@@ -213,6 +214,7 @@ generates Hermite polynomials in a specified free variable.
 
 @example
 #include <ginac/ginac.h>
+using namespace std;
 using namespace GiNaC;
 
 ex HermitePoly(const symbol & x, int n)
@@ -849,7 +851,7 @@ int main()
     // Trott's constant in scientific notation:
     numeric trott("1.0841015122311136151E-2");
     
-    cout << two*p << endl;  // floating point 6.283...
+    std::cout << two*p << std::endl;  // floating point 6.283...
 @}
 @end example
 
@@ -888,6 +890,7 @@ digits:
 
 @example
 #include <ginac/ginac.h>
+using namespace std;
 using namespace GiNaC;
 
 void foo()
@@ -938,6 +941,7 @@ some multiple of its denominator and test what comes out:
 
 @example
 #include <ginac/ginac.h>
+using namespace std;
 using namespace GiNaC;
 
 // some very important constants:
@@ -1042,21 +1046,16 @@ Simple polynomial expressions are written down in GiNaC pretty much like
 in other CAS or like expressions involving numerical variables in C.
 The necessary operators @code{+}, @code{-}, @code{*} and @code{/} have
 been overloaded to achieve this goal.  When you run the following
-program, the constructor for an object of type @code{mul} is
+code snippet, the constructor for an object of type @code{mul} is
 automatically called to hold the product of @code{a} and @code{b} and
 then the constructor for an object of type @code{add} is called to hold
 the sum of that @code{mul} object and the number one:
 
 @example
-#include <ginac/ginac.h>
-using namespace GiNaC;
-
-int main()
-@{
+    ...
     symbol a("a"), b("b");
     ex MyTerm = 1+a*b;
-    // ...
-@}
+    ...
 @end example
 
 @cindex @code{pow()}
@@ -1064,7 +1063,7 @@ For exponentiation, you have already seen the somewhat clumsy (though C-ish)
 statement @code{pow(x,2);} to represent @code{x} squared.  This direct
 construction is necessary since we cannot safely overload the constructor
 @code{^} in C++ to construct a @code{power} object.  If we did, it would
-have several counterintuitive effects:
+have several counterintuitive and undesired effects:
 
 @itemize @bullet
 @item
@@ -1173,37 +1172,27 @@ There are quite a number of useful functions hard-wired into GiNaC.  For
 instance, all trigonometric and hyperbolic functions are implemented
 (@xref{Built-in Functions}, for a complete list).
 
-These functions are all objects of class @code{function}.  They accept one
-or more expressions as arguments and return one expression.  If the arguments
-are not numerical, the evaluation of the function may be halted, as it
-does in the next example:
+These functions are all objects of class @code{function}.  They accept
+one or more expressions as arguments and return one expression.  If the
+arguments are not numerical, the evaluation of the function may be
+halted, as it does in the next example, showing how a function returns
+itself twice and finally an expression that may be really useful:
 
 @cindex Gamma function
 @cindex @code{subs()}
 @example
-#include <ginac/ginac.h>
-using namespace GiNaC;
-
-int main()
-@{
-    symbol x("x"), y("y");
-    
+    ...
+    symbol x("x"), y("y");    
     ex foo = x+y/2;
-    cout << "tgamma(" << foo << ") -> " << tgamma(foo) << endl;
+    cout << tgamma(foo) << endl;
+     // -> tgamma(x+(1/2)*y)
     ex bar = foo.subs(y==1);
-    cout << "tgamma(" << bar << ") -> " << tgamma(bar) << endl;
+    cout << tgamma(bar) << endl;
+     // -> tgamma(x+1/2)
     ex foobar = bar.subs(x==7);
-    cout << "tgamma(" << foobar << ") -> " << tgamma(foobar) << endl;
-@}
-@end example
-
-This program shows how the function returns itself twice and finally an
-expression that may be really useful:
-
-@example
-tgamma(x+(1/2)*y) -> tgamma(x+(1/2)*y)
-tgamma(x+1/2) -> tgamma(x+1/2)
-tgamma(15/2) -> (135135/128)*Pi^(1/2)
+    cout << tgamma(foobar) << endl;
+     // -> (135135/128)*Pi^(1/2)
+    ...
 @end example
 
 Besides evaluation most of these functions allow differentiation, series
@@ -1261,6 +1250,9 @@ Indexed expressions in GiNaC are constructed of two special types of objects,
 
 @itemize @bullet
 
+@cindex contravariant
+@cindex covariant
+@cindex variance
 @item Index objects are of class @code{idx} or a subclass. Every index has
 a @dfn{value} and a @dfn{dimension} (which is the dimension of the space
 the index lives in) which can both be arbitrary expressions but are usually
@@ -1283,6 +1275,7 @@ A simple example shall illustrate the concepts:
 
 @example
 #include <ginac/ginac.h>
+using namespace std;
 using namespace GiNaC;
 
 int main()
@@ -1491,6 +1484,7 @@ simplifications:
 @end example
 
 @cindex @code{get_free_indices()}
+@cindex Dummy index
 @subsection Dummy indices
 
 GiNaC treats certain symbolic index pairs as @dfn{dummy indices} meaning
@@ -1640,9 +1634,9 @@ representation @code{diag(1,1,1,...)}. It is constructed by the function
 @cindex @code{metric_tensor()}
 @subsubsection General metric tensor
 
-The function @code{metric_tensor()} creates a general metric tensor with
-two indices that can be used to raise/lower tensor indices. The metric
-tensor is denoted as @samp{g} in the output and if its indices are of
+The function @code{metric_tensor()} creates a general symmetric metric
+tensor with two indices that can be used to raise/lower tensor indices. The
+metric tensor is denoted as @samp{g} in the output and if its indices are of
 mixed variance it is automatically replaced by a delta tensor:
 
 @example
@@ -1724,17 +1718,15 @@ tensor).
 @subsection Linear algebra
 
 The @code{matrix} class can be used with indices to do some simple linear
-algebra (products of vectors and matrices, traces and scalar products):
+algebra (linear combinations and products of vectors and matrices, traces
+and scalar products):
 
 @example
 @{
     idx i(symbol("i"), 2), j(symbol("j"), 2);
     symbol x("x"), y("y");
 
-    matrix A(2, 2), X(2, 1);
-    A.set(0, 0, 1); A.set(0, 1, 2);
-    A.set(1, 0, 3); A.set(1, 1, 4);
-    X.set(0, 0, x); X.set(1, 0, y);
+    matrix A(2, 2, lst(1, 2, 3, 4)), X(2, 1, lst(x, y));
 
     cout << indexed(A, i, i) << endl;
      // -> 5
@@ -1743,20 +1735,20 @@ algebra (products of vectors and matrices, traces and scalar products):
     cout << e.simplify_indexed() << endl;
      // -> [[ [[2*y+x]], [[4*y+3*x]] ]].i
 
-    e = indexed(A, i, j) * indexed(X, i);
+    e = indexed(A, i, j) * indexed(X, i) + indexed(X, j) * 2;
     cout << e.simplify_indexed() << endl;
-     // -> [[ [[3*y+x,4*y+2*x]] ]].j
+     // -> [[ [[3*y+3*x,6*y+2*x]] ]].j
 @}
 @end example
 
-You can of course obtain the same results with the @code{matrix::mul()}
-and @code{matrix::trace()} methods but with indices you don't have to
-worry about transposing matrices.
+You can of course obtain the same results with the @code{matrix::add()},
+@code{matrix::mul()} and @code{matrix::trace()} methods but with indices you
+don't have to worry about transposing matrices.
 
 Matrix indices always start at 0 and their dimension must match the number
 of rows/columns of the matrix. Matrices with one row or one column are
 vectors and can have one or two indices (it doesn't matter whether it's a
-row or a columnt vector). Other matrices must have two indices.
+row or a column vector). Other matrices must have two indices.
 
 You should be careful when using indices with variance on matrices. GiNaC
 doesn't look at the variance and doesn't know that @samp{F~mu~nu} and
@@ -1778,16 +1770,10 @@ alternatively call it in a functional way as shown in the simple
 example:
 
 @example
-#include <ginac/ginac.h>
-using namespace GiNaC;
-
-int main()
-@{
-    ex x = numeric(1.0);
-    
-    cout << "As method:   " << sin(x).evalf() << endl;
-    cout << "As function: " << evalf(sin(x)) << endl;
-@}
+    ...
+    cout << "As method:   " << sin(1).evalf() << endl;
+    cout << "As function: " << evalf(sin(1)) << endl;
+    ...
 @end example
 
 @cindex @code{subs()}
@@ -2097,7 +2083,7 @@ being polynomials in the remaining variables.  The method
 @code{collect()} accomplishes this task:
 
 @example
-ex ex::collect(const symbol & s);
+ex ex::collect(const ex & s);
 @end example
 
 Note that the original polynomial needs to be in expanded form in order
@@ -2112,8 +2098,8 @@ The degree and low degree of a polynomial can be obtained using the two
 methods
 
 @example
-int ex::degree(const symbol & s);
-int ex::ldegree(const symbol & s);
+int ex::degree(const ex & s);
+int ex::ldegree(const ex & s);
 @end example
 
 which also work reliably on non-expanded input polynomials (they even work
@@ -2121,14 +2107,14 @@ on rational functions, returning the asymptotic degree). To extract
 a coefficient with a certain power from an expanded polynomial you use
 
 @example
-ex ex::coeff(const symbol & s, int n);
+ex ex::coeff(const ex & s, int n);
 @end example
 
 You can also obtain the leading and trailing coefficients with the methods
 
 @example
-ex ex::lcoeff(const symbol & s);
-ex ex::tcoeff(const symbol & s);
+ex ex::lcoeff(const ex & s);
+ex ex::tcoeff(const ex & s);
 @end example
 
 which are equivalent to @code{coeff(s, degree(s))} and @code{coeff(s, ldegree(s))},
@@ -2139,6 +2125,7 @@ polynomial is analyzed:
 
 @example
 #include <ginac/ginac.h>
+using namespace std;
 using namespace GiNaC;
 
 int main()
@@ -2271,6 +2258,39 @@ int main()
 @end example
 
 
+@subsection Square-free decomposition
+@cindex square-free decomposition
+@cindex factorization
+@cindex @code{sqrfree()}
+
+GiNaC still lacks proper factorization support.  Some form of
+factorization is, however, easily implemented by noting that factors
+appearing in a polynomial with power two or more also appear in the
+derivative and hence can easily be found by computing the GCD of the
+original polynomial and its derivatives.  Any system has an interface
+for this so called square-free factorization.  So we provide one, too:
+@example
+ex sqrfree(const ex & a, const lst & l = lst());
+@end example
+Here is an example that by the way illustrates how the result may depend
+on the order of differentiation:
+@example
+    ...
+    symbol x("x"), y("y");
+    ex BiVarPol = expand(pow(x-2*y*x,3) * pow(x+y,2) * (x-y));
+
+    cout << sqrfree(BiVarPol, lst(x,y)) << endl;
+     // -> (y+x)^2*(-1+6*y+8*y^3-12*y^2)*(y-x)*x^3
+
+    cout << sqrfree(BiVarPol, lst(y,x)) << endl;
+     // -> (1-2*y)^3*(y+x)^2*(-y+x)*x^3
+
+    cout << sqrfree(BiVarPol) << endl;
+     // -> depending on luck, any of the above
+    ...
+@end example
+
+
 @node Rational Expressions, Symbolic Differentiation, Polynomial Arithmetic, Methods and Functions
 @c    node-name, next, previous, up
 @section Rational expressions
@@ -2306,8 +2326,8 @@ int main()
     symbol x("x");
     ex t1 = (pow(x,2) + 2*x + 1)/(x + 1);
     ex t2 = (pow(sin(x),2) + 2*sin(x) + 1)/(sin(x) + 1);
-    cout << "t1 is " << t1.normal() << endl;
-    cout << "t2 is " << t2.normal() << endl;
+    std::cout << "t1 is " << t1.normal() << std::endl;
+    std::cout << "t2 is " << t2.normal() << std::endl;
 @}
 @end example
 
@@ -2426,7 +2446,7 @@ ex EulerNumber(unsigned n)
 int main()
 @{
     for (unsigned i=0; i<11; i+=2)
-        cout << EulerNumber(i) << endl;
+        std::cout << EulerNumber(i) << std::endl;
     return 0;
 @}
 @end example
@@ -2456,6 +2476,7 @@ term).  A sample application from special relativity could read:
 
 @example
 #include <ginac/ginac.h>
+using namespace std;
 using namespace GiNaC;
 
 int main()
@@ -2513,6 +2534,8 @@ ex mechain_pi(int degr)
 
 int main()
 @{
+    using std::cout;  // just for fun, another way of...
+    using std::endl;  // ...dealing with this namespace std.
     ex pi_frac;
     for (int i=2; i<12; i+=2) @{
         pi_frac = mechain_pi(i);
@@ -2745,6 +2768,7 @@ With this constructor, it's also easy to implement interactive GiNaC programs:
 #include <string>
 #include <stdexcept>
 #include <ginac/ginac.h>
+using namespace std;
 using namespace GiNaC;
 
 int main()
@@ -2776,8 +2800,9 @@ of class @code{archive} and archive expressions in it, giving each
 expression a unique name:
 
 @example
-#include <ginac/ginac.h>
 #include <fstream>
+using namespace std;
+#include <ginac/ginac.h>
 using namespace GiNaC;
 
 int main()
@@ -3664,6 +3689,7 @@ as you try to change the second.  Consider the simple sequence of code:
 
 @example
 #include <ginac/ginac.h>
+using namespace std;
 using namespace GiNaC;
 
 int main()
@@ -3695,6 +3721,7 @@ can be:
 
 @example
 #include <ginac/ginac.h>
+using namespace std;
 using namespace GiNaC;
 
 int main()
@@ -3988,13 +4015,13 @@ and the @samp{AM_PATH_GINAC} macro. The program used here is @file{simple.cpp}:
 
 @example
 #include <ginac/ginac.h>
-using namespace GiNaC;
 
 int main(void)
 @{
-    symbol x("x");
-    ex a = sin(x); 
-    cout << "Derivative of " << a << " is " << a.diff(x) << endl;
+    GiNaC::symbol x("x");
+    GiNaC::ex a = GiNaC::sin(x);
+    std::cout << "Derivative of " << a 
+              << " is " << a.diff(x) << std::endl;
     return 0;
 @}
 @end example