Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
  • 00:05
    oscarbenjamin commented #21739
  • Jan 21 22:23
    asmeurer commented #21739
  • Jan 21 22:17
    asmeurer commented #22899
  • Jan 21 22:17
    asmeurer commented #22899
  • Jan 21 21:46
    GeoffCope opened #22899
  • Jan 21 21:28
    asmeurer commented #20678
  • Jan 21 21:27
    asmeurer commented #20678
  • Jan 21 20:58
    asmeurer commented #16504
  • Jan 21 20:54
    asmeurer commented #22445
  • Jan 21 20:53
    asmeurer commented #22445
  • Jan 21 20:40
    asmeurer commented #20015
  • Jan 21 18:46
    smichr commented #22866
  • Jan 21 18:34
    github-actions[bot] commented #22872
  • Jan 21 17:31
    me-t1me synchronize #22872
  • Jan 21 17:05
    oscarbenjamin commented #22889
  • Jan 21 16:20
    NikhilSDate commented #22866
  • Jan 21 16:18
    smichr commented #22866
  • Jan 21 16:11
    smichr commented #22889
  • Jan 21 16:09
    NikhilSDate commented #22866
  • Jan 21 16:05
    banana59 commented #22895
Aaron Stiff
Vishesh Mangla
class Data:
    def __init__(self, string,symbol, value):
        self.string = string
        self.symbol = sm.Symbol(symbol)
        self.value = value

    def __add__(self, other):
        return self.value + other.value if isinstance(other, Data) else other

    def __str__(self):
        return self.string
    def _repr_latex_(self):
        return display(Latex(' '.join([self.string, f'({self.symbol})' ,r'\equals', self.value] )))
TypeError                                 Traceback (most recent call last)
c:\users\dell\onedrive\desktop\courses sem8\venv\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

<ipython-input-49-83e3f780e89d> in _repr_latex_(self)
     11         return self.string
     12     def _repr_latex_(self):
---> 13         return display(Latex(' '.join([self.string, f'({self.symbol})' ,r'\equals', self.value] )))
     14 # area_each_plate = Data("Area of each plate", 15.8*15.8*SI.cm**2)
     15 # inlet_pressure = Data("Area of each plate", 15.8*15.8*SI.cm**2)

TypeError: sequence item 3: expected str instance, Quantity found
how to get the latex out of sympy's Symbol?
neither does f'({latex(self.symbol)})' work
Aaron Meurer
"TypeError: sequence item 3: expected str instance, Quantity found"
"item 3" is the item at index 3, which is self.value. join requires that every item is converted to a string, so use latex(self.value).
Also I'm not sure, but I think _repr_latex should return a string, not call display().
Ayush Bisht


With this command python3 -m doctest -v frame.py , I found these error , can anyone help me , why they are failing??

Sidharth Mundhra
@ayushbisht2001 It looks like you do not have the development version setup properly. I think it is something like you are using sympy 1.7.1 version to run sympy 1.8-dev files
Ayush Bisht
@0sidharth , how can I update sympy 1.7.1 ?
because, It's not updating further from 1.7.1
! pip install sympy --upgrade
Sidharth Mundhra
I think you have to go into sympy directory and do pip install -e . in the virtual environment
Ayush Bisht
Thanks @0sidharth , Now it's working fine ..
Harshit Yadav
Is there some way to return two equations in the same Piecewise condition, like Piecewise(((a, b), Eq(n, 0)), (nan, True)), though writing as Tuple doesn't work? Thanks.
Minor point I just noticed: I don't think you need to check if a<=2, because it is checked already that a<b. But I think this detail also depends on sympy style guides, so somebody else should comment on that
Benoit Martin
Hi, How can I evaluate this in sympy ?
b1, b2 = symbols('b1 b2')
f = (5b1 - 3b2)**2

Hi, I am using sympy to help my daughter go through some IGCSE math (while exposing her to some coding) and I am trying to understand, is there a way to have the below expression displayed versus simplified/calculated out.


Craig Russell
sympify ("2**5", evaluate=False)
Pow(2,5, evaluate=False) also works
Excellent - thank you. I had tried a few different things - just not the right combination.
Love the sympy library by the way - I am getting more familiar for the library - this group has some useful information which I have also been reading.

sympify ("2**5", evaluate=False)

Interesting, simplify("2**5",evaluate=False) didn't work for me, but the Pow function did work with evaluate set to False.

Craig Russell
Sympify vs simplify
Thanks Craig - that did work
while running some tests, my code is getting some infinte recursion errors, is there a way t o exactly find which statement/test leads to the infinte recursion error?
Sidharth Mundhra
The tests generally print the traceback, from where you can see the recurring statements
Bjorn Madsen

Hi - I'm learning and am looking for a hint for setting up an equation.
The problem is naturally expressed as:

# maximize:
#   z = 100*x_1 + 60*x_2 + 70*x_3 + 15*x_4 + 15*x_5

# subject to limits:
#   52*x_1 + 23*x_2 + 35*x_3 + 15*x_4 + 7*x_5 <= 60
#      X_1                                    in {0,1}
#               x_2                           in {0,1}
#                        x_3                  in {0,1}
#                                 x_4         in {0,1}
#                                         x_5 in {0,1}

But I can't find a way to express "x_1 in the set containing {0,1}

It's a knapsack problem.

My personal preference is to use the matrix representation

m = Matrix((


...but I'm not sure how to package the variables for the nonlinsolve function.

Bjorn Madsen
Sorry if this is a dumb question. I'm just overwhelmed by the amount of documentation.
Bjorn Madsen
Whilst reading on I'm drifting towards the conclusion that sympy isn't hot on Linear Programming / Simplex method type problems on its own.
Bjorn Madsen
Ok. Nobody is burning my statements. PuLP seems to be what I need.
Sayandip Halder
>>> solveset(tan(x), x)
Union(ImageSet(Lambda(_n, 2*_n*pi + pi), Integers), ImageSet(Lambda(_n, 2*_n*pi), Integers))
Shouldn't the answer be simply ImageSet(Lambda(_n, _n*pi))?
Sayandip Halder


>>> solveset(sin(cos(x)), x)
ConditionSet(x, Eq(sin(cos(x)), 0), Complexes)

The answer should be something like ImageSet(Lambda(_n, 2*(n-1)*pi))

I guess this is an issue of improving solveset
Aaron Meurer
@root-11 yeah, SymPy currently doesn't have algorithms to solve that sort of problem. Anyway, if you are looking for a numerical solution rather than an exact analytic one, then SymPy is generally not your best choice.
Bjorn Madsen
Thanks Aaron.
Benoit Martin
Hi, I'm trying to find a way to convert a float/decimal to a fraction. As an example if I decimal(-7/3) I got -2.333333333333333481363069950020872056484222412109375 I increased the decimal precision. Now I want to convert back to a fraction but each time I got something else that -7/3. Any advice ?
Isuru Fernando
@metabsd, once you do,-7/3 you've already lost precision. Decimal(-7)/3
can you share what you want to do exactly
your question is unclear
Sidharth Mundhra
Currently sympy returns Order(oo) as O(1). Would a better alternative be returning oo (no order term)?
Aaron Meurer
You can try nsimplify with the rational_conversion='base10' which tries to guess what short fraction a float came from.
Constantin Mateescu

Using x' as a variable inside parse_expr(...) (https://docs.sympy.org/latest/modules/parsing.html?highlight=transformations#sympy.parsing.sympy_parser.parse_expr) doesn't seem to work:

>>> from sympy import Symbol
>>> from sympy.parsing.sympy_parser import (parse_expr, standard_transformations, implicit_multiplication_application, convert_xor)
>>> def parse_plain_text(e, local_dict=None):
...     return parse_expr(
...         e,
...         local_dict=local_dict,
...         transformations=(standard_transformations + (implicit_multiplication_application, convert_xor)),
...         evaluate=False)
>>> parse_plain_text("x_", {"x_": Symbol("x_")})
>>> parse_plain_text("x'", {"x'": Symbol("x'")})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in parse_plain_text
  File "REDACTED/sympy/sympy/parsing/sympy_parser.py", line 1005, in parse_expr
    code = compile(evaluateFalse(code), '<string>', 'eval')
  File "REDACTED/sympy/sympy/parsing/sympy_parser.py", line 1014, in evaluateFalse
    node = ast.parse(s)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ast.py", line 47, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    Symbol ('x' )'
SyntaxError: EOL while scanning string literal

I could remap this variable to something like x_ (which doesn't throw an error), but is there a way to avoid this workaround?

Aaron Meurer
You would need to define a transformation that enables this and pass it to the transformations keyword
The transformers aren't really documented. We should fix that. You can look in sympy/parsing/sympy_parser.py to see how they are defined. Basically you need to mutate the tokens from tokenize
In this case, you probably want to take auto_symbol and modify it so that it allows NAME + ' tokens.
It looks like the extra quote is tokenized as an ERRORTOKEN