manpagez: man pages & more
info ginac
Home | html | info | man
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.1.1 Checking expression types

Sometimes it's useful to check whether a given expression is a plain number, a sum, a polynomial with integer coefficients, or of some other specific type. GiNaC provides a couple of functions for this:

 
bool is_a<T>(const ex & e);
bool is_exactly_a<T>(const ex & e);
bool ex::info(unsigned flag);
unsigned ex::return_type() const;
unsigned ex::return_type_tinfo() const;

When the test made by is_a<T>() returns true, it is safe to call one of the functions ex_to<T>(), where T is one of the class names (See section The class hierarchy, for a list of all classes). For example, assuming e is an ex:

 
{
    …
    if (is_a<numeric>(e))
        numeric n = ex_to<numeric>(e);
    …
}

is_a<T>(e) allows you to check whether the top-level object of an expression ‘e’ is an instance of the GiNaC class ‘T’ (See section The class hierarchy, for a list of all classes). This is most useful, e.g., for checking whether an expression is a number, a sum, or a product:

 
{
    symbol x("x");
    ex e1 = 42;
    ex e2 = 4*x - 3;
    is_a<numeric>(e1);  // true
    is_a<numeric>(e2);  // false
    is_a<add>(e1);      // false
    is_a<add>(e2);      // true
    is_a<mul>(e1);      // false
    is_a<mul>(e2);      // false
}

In contrast, is_exactly_a<T>(e) allows you to check whether the top-level object of an expression ‘e’ is an instance of the GiNaC class ‘T’, not including parent classes.

The info() method is used for checking certain attributes of expressions. The possible values for the flag argument are defined in ‘ginac/flags.h’, the most important being explained in the following table:

Flag

Returns true if the object is…

numeric

…a number (same as is_a<numeric>(...))

real

…a real number, symbol or constant (i.e. is not complex)

rational

…an exact rational number (integers are rational, too)

integer

…a (non-complex) integer

crational

…an exact (complex) rational number (such as 2/3+7/2*I)

cinteger

…a (complex) integer (such as 2-3*I)

positive

…not complex and greater than 0

negative

…not complex and less than 0

nonnegative

…not complex and greater than or equal to 0

posint

…an integer greater than 0

negint

…an integer less than 0

nonnegint

…an integer greater than or equal to 0

even

…an even integer

odd

…an odd integer

prime

…a prime integer (probabilistic primality test)

relation

…a relation (same as is_a<relational>(...))

relation_equal

…a == relation

relation_not_equal

…a != relation

relation_less

…a < relation

relation_less_or_equal

…a <= relation

relation_greater

…a > relation

relation_greater_or_equal

…a >= relation

symbol

…a symbol (same as is_a<symbol>(...))

list

…a list (same as is_a<lst>(...))

polynomial

…a polynomial (i.e. only consists of sums and products of numbers and symbols with positive integer powers)

integer_polynomial

…a polynomial with (non-complex) integer coefficients

cinteger_polynomial

…a polynomial with (possibly complex) integer coefficients (such as 2-3*I)

rational_polynomial

…a polynomial with (non-complex) rational coefficients

crational_polynomial

…a polynomial with (possibly complex) rational coefficients (such as 2/3+7/2*I)

rational_function

…a rational function (x+y, z/(x+y))

algebraic

…an algebraic object (sqrt(2), sqrt(x)-1)

To determine whether an expression is commutative or non-commutative and if so, with which other expressions it would commutate, you use the methods return_type() and return_type_tinfo(). See section Non-commutative objects, for an explanation of these.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]
© manpagez.com 2000-2024
Individual documents may contain additional copyright information.