[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
5.4 Pattern matching and advanced substitutions
GiNaC allows the use of patterns for checking whether an expression is of a certain form or contains subexpressions of a certain form, and for substituting expressions in a more general way.
A pattern is an algebraic expression that optionally contains wildcards.
A wildcard is a special kind of object (of class wildcard
) that
represents an arbitrary expression. Every wildcard has a label which is
an unsigned integer number to allow having multiple different wildcards in a
pattern. Wildcards are printed as ‘$label’ (this is also the way they
are specified in ginsh
). In C++ code, wildcard objects are created
with the call
ex wild(unsigned label = 0); |
which is simply a wrapper for the wildcard()
constructor with a shorter
name.
Some examples for patterns:
Constructed as | Output as |
| ‘$0’ |
| ‘x^$0’ |
| ‘atan2($1,$2)’ |
| ‘A.$0’ |
Notes:
- Wildcards behave like symbols and are subject to the same algebraic rules. E.g., ‘$0+2*$0’ is automatically transformed to ‘3*$0’.
- As shown in the last example, to use wildcards for indices you have to
use them as the value of an
idx
object. This is because indices must always be of classidx
(or a subclass). - Wildcards only represent expressions or subexpressions. It is not possible to use them as placeholders for other properties like index dimension or variance, representation labels, symmetry of indexed objects etc.
- Because wildcards are commutative, it is not possible to use wildcards as part of noncommutative products.
- A pattern does not have to contain wildcards. ‘x’ and ‘x+y’ are also valid patterns.