[Python] How to create a Sympy IndexedBase using a custom subclass of Symbol?

Discussão em 'Python' iniciado por Stack, Janeiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I want to create a 2-D matrix of binary variables x. For now I do it like this: x = IndexedBase('x', shape=(imax,jmax), integer=True)

    For a binary variable, the following identity holds: x_i**n == x_i for n>0, and I want to make use of this identity for simplifying my expressions later on. Therefore, the 'integer' assumption should be replaced with a "stronger" 'binary' assumption.

    Creating a single binary variable works (Ref: https://stackoverflow.com/a/73953040/7740977):

    from sympy import *

    class Binary(Symbol):
    def _eval_power(self, other):
    return self

    x0 = Binary('x0')
    x0**2 == x0


    Output: True

    What I'm struggling with right now is creating an IndexedBase object where its entries are instances of my Binary class rather than of Symbol. Looking at https://docs.sympy.org/latest/modules/tensor/indexed.html#sympy.tensor.indexed.IndexedBase I saw that assumptions can be inherited if a Symbol is used to initialize the IndexedBase, which led me to unsuccessfully try the following:

    x = symbols('x', cls=Binary)
    x = IndexedBase(x)

    x[0,0]**2 == x[0,0]


    Output: False (expected True)

    Is there another way to get IndexedBase to use my custom Binary class?

    Many thanks in advance!

    Continue reading...

Compartilhe esta Página