symengine / symengine

SymEngine is a fast symbolic manipulation library, written in C++
https://symengine.org
Other
1.17k stars 281 forks source link

Supporting more query method through the cwrapper #2015

Open anutosh491 opened 6 months ago

anutosh491 commented 6 months ago

This is inspired by sympy where usually every assumption maps to a query method

>>> x = Symbol('x',positive=True)
>>> assumptions(x + I)
{'commutative': True, 'complex': True, 'composite': False, 'even':
False, 'extended_negative': False, 'extended_nonnegative': False,
'extended_nonpositive': False, 'extended_nonzero': False,
'extended_positive': False, 'extended_real': False, 'finite': True,
'imaginary': False, 'infinite': False, 'integer': False, 'irrational':
False, 'negative': False, 'noninteger': False, 'nonnegative': False,
'nonpositive': False, 'nonzero': False, 'odd': False, 'positive':
False, 'prime': False, 'rational': False, 'real': False, 'zero':
False}

Some core ones which we encounter in all certainty would be 1) is_positive 2) is_negative 3) is_zero 4) is_finite 5) is_infinite

I see we have something like number_is_positive etc in the cwrapper https://github.com/symengine/symengine/blob/85d58bb34a6ec6c9396b027cb89d49baf9751d4f/symengine/cwrapper.h#L163-L169

Can this be extended to a general form ?

anutosh491 commented 6 months ago

Thinking of would a general implementation of is_positive look like Can a if-else ladder, where we check the class of the basic variable and then use the respective is_positive function work ?

rikardn commented 6 months ago

number.h has definitions of these types of tests (queries) that allows an optional Assumptions object to be used. Various assumptions can be set on symbols in such an object to be passed to the test functions. The implementation of the actual machinery can be found in the test_visitors.cpp. The main implementation strategy is to use the visitor pattern to traverse the expression tree to figure out if the test is either true, false or indeterminate. Indeterminate means that it is either impossible to know given the assumptions or that the algorithm is currently unable to know.

So for the cwrapper we need to be able to create Assumptions objects and the rest is simply wrapping the functions.