Setting Assumptions on Variables in Sympy Relative to Other Variables

后端 未结 2 1186
甜味超标
甜味超标 2020-12-16 01:20

I know that sympy in python can set assumptions on variables, such as x is positive, negative, real, complex, etc. I was wondering if sympy can set assumptions on variables

相关标签:
2条回答
  • 2020-12-16 02:01

    The assumptions are not coming into play here. That usually only matters if you have square roots, because sqrt(x**2) = x only if x >= 0.

    All you need to do for this is simplify the result. Matrix.eigenvects has a simplify flag, but it apparently doesn't simplify the results. I'll open an issue for that. In the meanwhile, you can do so manually. Note that Matrix.simplify acts in-place (if you don't like that, you can use Matrix.applyfunc(simplify)

    >>> A = M.eigenvects()
    >>> A[0][2][0].simplify()
    >>> A[1][2][0].simplify()
    >>> pprint(A)
    ⎡⎛1, 1, ⎡⎡1⎤⎤⎞, ⎛-4⋅b + 1, 1, ⎡⎡-1⎤⎤⎞, ⎛-2⋅a - 2⋅b + 1, 2, ⎡⎡-1⎤, ⎡0 ⎤⎤⎞⎤
    ⎢⎜      ⎢⎢ ⎥⎥⎟  ⎜             ⎢⎢  ⎥⎥⎟  ⎜                   ⎢⎢  ⎥  ⎢  ⎥⎥⎟⎥
    ⎢⎜      ⎢⎢1⎥⎥⎟  ⎜             ⎢⎢-1⎥⎥⎟  ⎜                   ⎢⎢1 ⎥  ⎢0 ⎥⎥⎟⎥
    ⎢⎜      ⎢⎢ ⎥⎥⎟  ⎜             ⎢⎢  ⎥⎥⎟  ⎜                   ⎢⎢  ⎥  ⎢  ⎥⎥⎟⎥
    ⎢⎜      ⎢⎢1⎥⎥⎟  ⎜             ⎢⎢1 ⎥⎥⎟  ⎜                   ⎢⎢0 ⎥  ⎢-1⎥⎥⎟⎥
    ⎢⎜      ⎢⎢ ⎥⎥⎟  ⎜             ⎢⎢  ⎥⎥⎟  ⎜                   ⎢⎢  ⎥  ⎢  ⎥⎥⎟⎥
    ⎣⎝      ⎣⎣1⎦⎦⎠  ⎝             ⎣⎣1 ⎦⎦⎠  ⎝                   ⎣⎣0 ⎦  ⎣1 ⎦⎦⎠⎦
    
    0 讨论(0)
  • 2020-12-16 02:14

    I was wondering whether to post this as a comment but it is too long:

    Short answer: not in a usable way.

    The assumption system of SymPy is kind of a mess right now (version 0.7.2, latest as of May 2013). There is a possibility that it will get better this summer due to a prospective GSoC project, but this is not certain yet.

    There are actually two assumption systems within SymPy. The old one, which adds the assumptions to the Symbols themselves (hence causes problems with rebuilding of expression trees) and is called in the constructor (e.g. Symbol(..., positive=True)), and there is the new one, which is based around global variables for the global assumptions and context managers (with assume(...):) for local ones.

    Many of the functions within SymPy do check the old assumptions (for instance Abs will check whether the keyword argument positive was set), but there still can be misses. The new assumption system can be more powerful but is almost unused at the moment (except in very recent submodules).

    In the old assumption system what you want is not possible. In the new one it is possible, but probably not implemented yet and not used in any parts of SymPy.

    So you have two options: help us with the assumption systems or help us with the matrix module. Both can use some more love.

    0 讨论(0)
提交回复
热议问题