Internally, the anonymous evaluator in GiNaC is implemented by the methods
@example
-ex ex::eval(int level = 0) const;
-ex basic::eval(int level = 0) const;
+ex ex::eval() const;
+ex basic::eval() const;
@end example
but unless you are extending GiNaC with your own classes or functions, there
the same type to GiNaC methods such as @code{subs()} and some @code{matrix}
constructors, so you should have a basic understanding of them.
-Lists can be constructed by assigning a comma-separated sequence of
-expressions:
+Lists can be constructed from an initializer list of expressions:
@example
@{
symbol x("x"), y("y");
lst l;
- l = x, 2, y, x+y;
+ l = @{x, 2, y, x+y@};
// now, l is a list holding the expressions 'x', '2', 'y', and 'x+y',
// in that order
...
@end example
-There are also constructors that allow direct creation of lists of up to
-16 expressions, which is often more convenient but slightly less efficient:
-
-@example
- ...
- // This produces the same list 'l' as above:
- // lst l(x, 2, y, x+y);
- // lst l = lst(x, 2, y, x+y);
- ...
-@end example
-
Use the @code{nops()} method to determine the size (number of expressions) of
a list and the @code{op()} method or the @code{[]} operator to access
individual elements:
creates a matrix with @samp{r} rows and @samp{c} columns with all elements
set to zero.
-The fastest way to create a matrix with preinitialized elements is to assign
-a list of comma-separated expressions to an empty matrix (see below for an
-example). But you can also specify the elements as a (flat) list with
+The easiest way to create a matrix is using an initializer list of
+initializer lists, all of the same size:
+
+@example
+@{
+ matrix m = @{@{1, -a@},
+ @{a, 1@}@};
+@}
+@end example
+
+You can also specify the elements as a (flat) list with
@example
matrix::matrix(unsigned r, unsigned c, const lst & l);
@cindex @code{symbolic_matrix()}
@example
ex diag_matrix(const lst & l);
+ex diag_matrix(initializer_list<ex> l);
ex unit_matrix(unsigned x);
ex unit_matrix(unsigned r, unsigned c);
ex symbolic_matrix(unsigned r, unsigned c, const string & base_name);
const string & tex_base_name);
@end example
-@code{diag_matrix()} constructs a diagonal matrix given the list of diagonal
+@code{diag_matrix()} constructs a square diagonal matrix given the diagonal
elements. @code{unit_matrix()} creates an @samp{x} by @samp{x} (or @samp{r}
by @samp{c}) unit matrix. And finally, @code{symbolic_matrix} constructs a
matrix filled with newly generated symbols made of the specified base name
@example
@{
- matrix m(3,3);
- m = 11, 12, 13,
- 21, 22, 23,
- 31, 32, 33;
+ matrix m = @{@{11, 12, 13@},
+ @{21, 22, 23@},
+ @{31, 32, 33@}@};
cout << reduced_matrix(m, 1, 1) << endl;
// -> [[11,13],[31,33]]
cout << sub_matrix(m, 1, 2, 1, 2) << endl;
@{
symbol a("a"), b("b");
- matrix M(2, 2);
- M = a, 0,
- 0, b;
+ matrix M = @{@{a, 0@},
+ @{0, b@}@};
cout << M << endl;
// -> [[a,0],[0,b]]
cout << M2 << endl;
// -> [[a,0],[0,b]]
- cout << matrix(2, 2, lst(a, 0, 0, b)) << endl;
+ cout << matrix(2, 2, lst@{a, 0, 0, b@}) << endl;
// -> [[a,0],[0,b]]
- cout << lst_to_matrix(lst(lst(a, 0), lst(0, b))) << endl;
+ cout << lst_to_matrix(lst@{lst@{a, 0@}, lst@{0, b@}@}) << endl;
// -> [[a,0],[0,b]]
- cout << diag_matrix(lst(a, b)) << endl;
+ cout << diag_matrix(lst@{a, b@}) << endl;
// -> [[a,0],[0,b]]
cout << unit_matrix(3) << endl;
@example
@{
- matrix A(2, 2), B(2, 2), C(2, 2);
- A = 1, 2,
- 3, 4;
- B = -1, 0,
- 2, 1;
- C = 8, 4,
- 2, 1;
+ matrix A = @{@{ 1, 2@},
+ @{ 3, 4@}@};
+ matrix B = @{@{-1, 0@},
+ @{ 2, 1@}@};
+ matrix C = @{@{ 8, 4@},
+ @{ 2, 1@}@};
matrix result = A.mul(B).sub(C.mul_scalar(2));
cout << result << endl;
symbol x("x"), y("y");
// A is a 2x2 matrix, X is a 2x1 vector
- matrix A(2, 2), X(2, 1);
- A = 1, 2,
- 3, 4;
- X = x, y;
+ matrix A = @{@{1, 2@},
+ @{3, 4@}@};
+ matrix X = @{@{x, y@}@};
cout << indexed(A, i, i) << endl;
// -> 5
of the non-commutative classes. The drawback is that to work with other than
the built-in algebras you have to implement new classes yourself. Both
symbols and user-defined functions can be specified as being non-commutative.
+For symbols, this is done by subclassing class symbol; for functions,
+by explicitly setting the return type (@pxref{Symbolic functions}).
@cindex @code{return_type()}
@cindex @code{return_type_tinfo()}
dirac_gamma(mu.toggle_variance()) *
(dirac_slash(l, D) + m * dirac_ONE());
e = dirac_trace(e).simplify_indexed(sp);
- e = e.collect(lst(l, ldotq, m));
+ e = e.collect(lst@{l, ldotq, m@});
cout << e << endl;
// -> (8-4*D)*l^2+(8-4*D)*ldotq+4*D*m^2
@}
...
idx i(symbol("i"), 4);
realsymbol s("s");
- ex M = diag_matrix(lst(1, -1, 0, s));
+ ex M = diag_matrix(lst@{1, -1, 0, s@});
ex e = clifford_unit(i, M);
ex e0 = e.subs(i == 0);
ex e1 = e.subs(i == 1);
...
idx i(symbol("i"), 4);
realsymbol s("s");
- ex M = diag_matrix(lst(1, -1, 0, s));
- ex e0 = lst_to_clifford(lst(1, 0, 0, 0), i, M);
- ex e1 = lst_to_clifford(lst(0, 1, 0, 0), i, M);
- ex e2 = lst_to_clifford(lst(0, 0, 1, 0), i, M);
- ex e3 = lst_to_clifford(lst(0, 0, 0, 1), i, M);
+ ex M = diag_matrix(@{1, -1, 0, s@});
+ ex e0 = lst_to_clifford(lst@{1, 0, 0, 0@}, i, M);
+ ex e1 = lst_to_clifford(lst@{0, 1, 0, 0@}, i, M);
+ ex e2 = lst_to_clifford(lst@{0, 0, 1, 0@}, i, M);
+ ex e3 = lst_to_clifford(lst@{0, 0, 0, 1@}, i, M);
...
@}
@end example
@example
@{
symbol A("A"), B("B"), C("C");
- ex e = lst(lst(A, B), C);
+ ex e = lst@{lst@{A, B@}, C@};
std::copy(e.begin(), e.end(),
std::ostream_iterator<ex>(cout, "\n"));
// -> 73
ex e2 = x*y + x;
- cout << "e2(-2, 4) = " << e2.subs(lst(x == -2, y == 4)) << endl;
+ cout << "e2(-2, 4) = " << e2.subs(lst@{x == -2, y == 4@}) << endl;
// -> -10
@}
@end example
If you specify multiple substitutions, they are performed in parallel, so e.g.
-@code{subs(lst(x == y, y == x))} exchanges @samp{x} and @samp{y}.
+@code{subs(lst@{x == y, y == x@})} exchanges @samp{x} and @samp{y}.
The second form of @code{subs()} takes an @code{exmap} object which is a
pair associative container that maps expressions to expressions (currently
symbol x("x"), y("y");
ex e2 = x*y + x;
- cout << "e2(-2, 4) = " << e2.subs(lst(x, y), lst(-2, 4)) << endl;
+ cout << "e2(-2, 4) = " << e2.subs(lst@{x, y@}, lst@{-2, 4@}) << endl;
@}
@end example
@example
(x*y*sin(y)).is_polynomial(x) // Returns true.
-(x*y*sin(y)).is_polynomial(lst(x,y)) // Returns false.
+(x*y*sin(y)).is_polynomial(lst@{x,y@}) // Returns false.
@end example
@subsection Expanding and collecting
Square-free decomposition is available in GiNaC:
@example
-ex sqrfree(const ex & a, const lst & l = lst());
+ex sqrfree(const ex & a, const lst & l = lst@{@});
@end example
Here is an example that by the way illustrates how the exact form of the
result may slightly depend on the order of differentiation, calling for
symbol x("x"), y("y");
ex BiVarPol = expand(pow(2-2*y,3) * pow(1+x*y,2) * pow(x-2*y,2) * (x+y));
- cout << sqrfree(BiVarPol, lst(x,y)) << endl;
+ cout << sqrfree(BiVarPol, lst@{x,y@}) << endl;
// -> 8*(1-y)^3*(y*x^2-2*y+x*(1-2*y^2))^2*(y+x)
- cout << sqrfree(BiVarPol, lst(y,x)) << endl;
+ cout << sqrfree(BiVarPol, lst@{y,x@}) << endl;
// -> 8*(1-y)^3*(-y*x^2+2*y+x*(-1+2*y^2))^2*(y+x)
cout << sqrfree(BiVarPol) << endl;
cout << ex(indexed(A, i, j)).symmetrize() << endl;
// -> 1/2*A.j.i+1/2*A.i.j
- cout << ex(indexed(A, i, j, k)).antisymmetrize(lst(i, j)) << endl;
+ cout << ex(indexed(A, i, j, k)).antisymmetrize(lst@{i, j@}) << endl;
// -> -1/2*A.j.i.k+1/2*A.i.j.k
- cout << ex(lst(a, b, c)).symmetrize_cyclic(lst(a, b, c)) << endl;
+ cout << ex(lst@{a, b, c@}).symmetrize_cyclic(lst@{a, b, c@}) << endl;
// -> 1/3*@{a,b,c@}+1/3*@{b,c,a@}+1/3*@{c,a,b@}
@}
@end example
will be interpreted as the sequence of signs for the corresponding indices
@code{m} or the sign of the imaginary part for the
corresponding arguments @code{a}, it must contain 1 or -1, e.g.
-@code{zeta(lst(3,4), lst(-1,1))} means
+@code{zeta(lst@{3,4@}, lst@{-1,1@})} means
@tex
$\zeta(\overline{3},4)$
@end tex
@command{zeta(\overline@{3@},4)}
@end ifnottex
and
-@code{G(lst(a,b), lst(-1,1), c)} means
+@code{G(lst@{a,b@}, lst@{-1,1@}, c)} means
@tex
$G(a-0\epsilon,b+0\epsilon;c)$.
@end tex
@end ifnottex
The definition of @code{H} allows indices to be 0, 1 or -1 (in expanded notation) or equally to
be any integer (in compact notation). With GiNaC expanded and compact notation can be mixed,
-e.g. @code{lst(0,0,-1,0,1,0,0)}, @code{lst(0,0,-1,2,0,0)} and @code{lst(-3,2,0,0)} are equivalent as
+e.g. @code{lst@{0,0,-1,0,1,0,0@}}, @code{lst@{0,0,-1,2,0,0@}} and @code{lst@{-3,2,0,0@}} are equivalent as
indices. The anonymous evaluator @code{eval()} tries to reduce the functions, if possible, to
the least-generic multiple polylogarithm. If all arguments are unit, it returns @code{zeta}.
Arguments equal to zero get considered, too. Riemann's zeta function @code{zeta} (with depth one)
@{
...
public:
- ex eval(int level = 0) const;
+ ex eval() const override;
...
@};
-ex mystring::eval(int level) const
+ex mystring::eval() const
@{
string new_str;
for (size_t i=0; i<str.length(); i++) @{
if (new_str.length() == 0)
return 0;
- else
- return mystring(new_str).hold();
+
+ return mystring(new_str).hold();
@}
@end example
-The @code{level} argument is used to limit the recursion depth of the
-evaluation. We don't have any subexpressions in the @code{mystring}
-class so we are not concerned with this. If we had, we would call the
-@code{eval()} functions of the subexpressions with @code{level - 1} as
-the argument if @code{level != 1}. The @code{hold()} member function
-sets a flag in the object that prevents further evaluation. Otherwise
-we might end up in an endless loop. When you want to return the object
-unmodified, use @code{return this->hold();}.
+The @code{hold()} member function sets a flag in the object that prevents
+further evaluation. Otherwise we might end up in an endless loop. When you
+want to return the object unmodified, use @code{return this->hold();}.
+
+If our class had subobjects, we would have to evaluate them first (unless
+they are all of type @code{ex}, which are automatically evaluated). We don't
+have any subexpressions in the @code{mystring} class, so we are not concerned
+with this.
Let's confirm that it works:
@cindex @code{calchash()}
@cindex @code{is_equal_same_type()}
@example
-unsigned calchash() const;
-bool is_equal_same_type(const basic & other) const;
+unsigned calchash() const override;
+bool is_equal_same_type(const basic & other) const override;
@end example
The @code{calchash()} method returns an @code{unsigned} hash value for the
might want to provide:
@example
-bool info(unsigned inf) const;
-ex evalf(int level = 0) const;
-ex series(const relational & r, int order, unsigned options = 0) const;
-ex derivative(const symbol & s) const;
+bool info(unsigned inf) const override;
+ex evalf(int level = 0) const override;
+ex series(const relational & r, int order, unsigned options = 0) const override;
+ex derivative(const symbol & s) const override;
@end example
If your class stores sub-expressions (see the scalar product example in the
@cindex @code{let_op()}
@example
-size_t nops() cont;
-ex op(size_t i) const;
-ex & let_op(size_t i);
-ex subs(const lst & ls, const lst & lr, unsigned options = 0) const;
-ex map(map_function & f) const;
+size_t nops() const override;
+ex op(size_t i) const override;
+ex & let_op(size_t i) override;
+ex subs(const lst & ls, const lst & lr, unsigned options = 0) const override;
+ex map(map_function & f) const override;
@end example
@code{let_op()} is a variant of @code{op()} that allows write access. The