Symbolic manipulation of non-numeric types

I'm interested in the python library, which allows symbolic manipulation, where the characters and can be unknown of arbitrary type.

This is the code I want to write:

>>> myexpression = symbol("foo") == "bar" >>> print myexpression foo == "bar" >>> print myexpression(foo="quux") False >>> myexpression.or_(True) True 

Or some approximate approximation of this. Actually it doesn’t even need to be smart, I would be happy enough to name a lot of additional introspection methods to get something like the above (for example, even if the logical tautology is not directly simplified)

My first instinct was to look sympy , but the library seems to make a strong assumption that symbolic variables should be numbers; and I would like to at least work with sequences and sets:

 >>> myexpression = sympy.Eq(sympy.Symbol("foo"), 5) >>> myexpression foo == 5 >>> myexpression = sympy.Eq(sympy.Symbol("foo"), "bar") Traceback (most recent call last): ... sympy.core.sympify.SympifyError: SympifyError: 'bar' 

Is there a way to make sympy understand non-numeric variables or another library that can do such things?

+8
python sympy
source share
3 answers

Not sure how good it is for the use you have in mind, but nltk (Natural Language Toolkit) has modules for symbolic manipulation, including first-order logic, a typed lambda calculus, and a proof of the theorem. Check out this method .

+1
source share

Could you just display all the sympy characters? For example, in the last expression: sympy.Eq (sympy.Symbol ("foo"), sympy.Symbol ("bar")). Or do you want to say that you really want to write logical statements about setting up relationships?

0
source share

Logical logic is in SymPy, although it is not as easily expressed as it should be. Of course have.

 In [1]: x,y,z = symbols('xy z') In [2]: A = Eq(x,y) In [3]: A Out[3]: x = y In [4]: B = Gt(y,z) In [5]: And(A,B) Out[5]: x = y ∧ y > z In [6]: C = Gt(x,z) In [7]: And(A, Or(B,C)) Out[7]: x = y ∧ (y > z ∨ x > z) 

I do not know many methods to simplify these expressions. This is something that would be easy to do if there was interest.

0
source share

All Articles