Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 01:23
    sympy-bot commented #23511
  • 01:23
    eagleoflqj review_requested #23511
  • 01:23
    eagleoflqj opened #23511
  • 00:54
    agryman commented #23510
  • May 17 22:06
    whutabarat-sheffield starred sympy/sympy
  • May 17 21:14
    eagleoflqj commented #23495
  • May 17 20:42
    oscarbenjamin commented #23510
  • May 17 20:29
    oscarbenjamin commented #23499
  • May 17 19:45
    ThePauliPrinciple commented #23499
  • May 17 19:45
    FranzAtGithub commented #8871
  • May 17 19:36
    ThePauliPrinciple commented #23499
  • May 17 18:15
    agryman commented #23510
  • May 17 17:44
    oscarbenjamin commented #23510
  • May 17 15:54
    sohithkumar starred sympy/sympy
  • May 17 15:48
    agryman commented #23510
  • May 17 13:59
    oscarbenjamin commented #23495
  • May 17 12:23
    gxyd edited #23505
  • May 17 11:33
    saleiro starred sympy/sympy
  • May 17 11:13
    gxyd commented #23495
  • May 17 11:13
    gxyd commented #23495
Bjorn Madsen
@root-11

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((
    [52,23,35,15,7,-60],
    [1,0,0,0,0,0],
    [0,1,0,0,0,0],
    [0,0,1,0,0,0],
    [0,0,0,1,0,0],
    [0,0,0,0,1,0]

))

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

Bjorn Madsen
@root-11
Sorry if this is a dumb question. I'm just overwhelmed by the amount of documentation.
Bjorn Madsen
@root-11
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
@root-11
Ok. Nobody is burning my statements. PuLP seems to be what I need.
Sayandip Halder
@sayandip18
>>> 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
@sayandip18

Also,

>>> 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
@asmeurer
@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
@root-11
Thanks Aaron.
Benoit Martin
@metabsd
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
@isuruf
@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
@0sidharth
Currently sympy returns Order(oo) as O(1). Would a better alternative be returning oo (no order term)?
Aaron Meurer
@asmeurer
You can try nsimplify with the rational_conversion='base10' which tries to guess what short fraction a float came from.
Constantin Mateescu
@costica1234_twitter

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_")})
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
@asmeurer
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
>>> list(tokenize.tokenize(io.BytesIO(b"x'").readline))
[TokenInfo(type=57 (ENCODING), string='utf-8', start=(0, 0), end=(0, 0), line=''), TokenInfo(type=1 (NAME), string='x', start=(1, 0), end=(1, 1), line="x'"), TokenInfo(type=54 (ERRORTOKEN), string="'", start=(1, 1), end=(1, 2), line="x'"), TokenInfo(type=4 (NEWLINE), string='', start=(1, 2), end=(1, 3), line=''), TokenInfo(type=0 (ENDMARKER), string='', start=(2, 0), end=(2, 0), line='')]
Constantin Mateescu
@costica1234_twitter
Thanks a lot for the detailed response! Will take a closer look at those transformations and see if I can define one that suits my needs.
Aradhana Kund
@aradhana72
Hello, I am Aradhana Kund from ETC-2nd year IIIT Bhubaneswar, India. I am interested into contributing to this organization . It would be a great help if I am guided for the same. I am very efficient in data structure and algorithms in C,C++.Also I am efficient in Python and mathematics also and wish to collaborate with this community to contribute and resolve the issues.
Looking forward for further interaction.
Suryam Arnav Kalra
@suryam35
Hi @aradhana72 , you can start with https://github.com/sympy/sympy/wiki/Introduction-to-contributing for starters, and then you can refer to the docs https://docs.sympy.org/latest/index.html. For the later part, refer to https://github.com/sympy/sympy/wiki/Development-workflow.
Aradhana Kund
@aradhana72
Thank you so much @suryam35 for guiding me.Will definitely look into the resources given by you and get back. Want to contribute for GSoC'21 with sympy . Looking forward to collaborating with this organization and the mentors. Please do guide for the same.
stuzenz
@stuzenz

Hi, this is similar to a question of mine last week. I am using sympy as a peripheral tool to teach my daughters some math. I have noticed that with some expressions such as the below one that sympy will still simplify

sympify ("(sqrt(9))/(sqrt(16))", evaluate=False)

where the result is

3/4

Is it possible to have sympy do something more like this library? If not, I will just import the second library. Still, before I go there I thought I should understand if it is possible in sympy first.

https://github.com/connorferster/handcalcs

handcalcs is a library to render Python calculation code automatically in Latex, but in a manner that mimics how one might format their calculation if it were written with a pencil: write the symbolic formula, followed by numeric substitutions, and then the result.

Because handcalcs shows the numeric substitution, the calculations become significantly easier to check and verify by hand.

stuzenz
@stuzenz
as a side note, @ctr26 was helpful last week in showing me that in the example that I presented then (which was for fractions) was achievable with the above syntax. It just doesn't seem to work universally (square roots for a perfect square counterpart being one example)
Benoit Martin
@metabsd
Hi, There a way to get the negative representation of a matrix ? Example m1 = Matrix([[2, -1], [3, 12], [1.3, -2]]) applying a function that result to Matrix([[-2, 1], [-3, -12], [-1.3, 2]]) Thx in advance!
stuzenz
@stuzenz

Hi, There a way to get the negative representation of a matrix ? Example m1 = Matrix([[2, -1], [3, 12], [1.3, -2]]) applying a function that result to Matrix([[-2, 1], [-3, -12], [-1.3, 2]]) Thx in advance!

This will get the result

m = Matrix([[1, 2], [3, 4]])
m = -m
m
stuzenz
@stuzenz

Hi, There a way to get the negative representation of a matrix ? Example m1 = Matrix([[2, -1], [3, 12], [1.3, -2]]) applying a function that result to Matrix([[-2, 1], [-3, -12], [-1.3, 2]]) Thx in advance!

not sure if you need it as a function - but if you do - something like the below will get you there

def invert_function(matrix):
    result = -matrix
    print(str(result))
invert_function(Matrix([[1, 2], [3, 4]]))
# Returns Matrix([[-1, -2], [-3, -4]])
MiguelMonteiro
@MiguelMonteiro
Hello, I am trying to get the primitive of a very complex expression and I don't understand the meaning of the symbols c2 and c3 introduced in the result. Could someone explain please? Here is the code.
from sympy import IndexedBase, Sum, init_printing, integrate
from sympy.abc import I, N, j, t, x
from sympy.functions import conjugate, exp, sqrt

init_printing()
X = IndexedBase('X')

gamma = (1 / N) * Sum(exp(I * X[j] * t), (j, 0, N))
kernel = N / (2 * (N - 1)) * (1 + sqrt(1 - (4 * (N - 1)) / ((N ** 2) * gamma * conjugate(gamma))))
data = exp(-I * x * t)
kde = kernel * gamma * data
primitive = integrate(kde, t)
3 replies
Benoit Martin
@metabsd
@stuzenz Thx!
Benoit Martin
@metabsd
Hi, There is a way to see all step that use sympy to reduce a matrix ? .rref()
1 reply
stuzenz
@stuzenz

Hi, There is a way to see all step that use sympy to reduce a matrix ? .rref()
Hi, this is similar to a question of mine last week. I am using sympy as a peripheral tool to teach my daughters some math. I have noticed that with some expressions such as the below one that sympy will still simplify

sympify ("(sqrt(9))/(sqrt(16))", evaluate=False)

where the result is

3/4

Is it possible to have sympy do something more like this library? If not, I will just import the second library. Still, before I go there I thought I should understand if it is possible in sympy first.

https://github.com/connorferster/handcalcs

handcalcs is a library to render Python calculation code automatically in Latex, but in a manner that mimics how one might format their calculation if it were written with a pencil: write the symbolic formula, followed by numeric substitutions, and then the result.

Because handcalcs shows the numeric substitution, the calculations become significantly easier to check and verify by hand.

To answer my own question (in case anyone is interested in the same question). The python library, handcalcs, does what I need and already integrates well with sympy so that is fine - no real need for me to try and get the same result directly from sympy.

a,b,c = symbols("a b c")
d = a**2 + b*a
f = integrate(d, a)
# handcalcs integrates nicely with sympy
%%render sympy

a =5
b = 10
x = f
# I can get handcalcs to not evaluate/simplify
%%render symbolic

x_pos = sqrt(25) + sqrt(9)
# It seems I cannot with symy - but handcalcs (above example) works fine for me (with sympy) anyway
sympify(sqrt(25) +sqrt(9),evaluate=False)

So, I guess all is good. Thanks!

ref
https://github.com/connorferster/handcalcs#symbolic

You can view the above example results as an image here:
https://ibb.co/HDTvbH0

stuzenz
@stuzenz

Hi, There is a way to see all step that use sympy to reduce a matrix ? .rref()
Hi, this is similar to a question of mine last week. I am using sympy as a peripheral tool to teach my daughters some math. I have noticed that with some expressions such as the below one that sympy will still simplify

sympify ("(sqrt(9))/(sqrt(16))", evaluate=False)

where the result is

3/4

Is it possible to have sympy do something more like this library? If not, I will just import the second library. Still, before I go there I thought I should understand if it is possible in sympy first.

https://github.com/connorferster/handcalcs

handcalcs is a library to render Python calculation code automatically in Latex, but in a manner that mimics how one might format their calculation if it were written with a pencil: write the symbolic formula, followed by numeric substitutions, and then the result.

Because handcalcs shows the numeric substitution, the calculations become significantly easier to check and verify by hand.

To answer my own question (in case anyone is interested in the same question). The python library, handcalcs, does what I need and already integrates well with sympy so that is fine - no real need for me to try and get the same result directly from sympy.

a,b,c = symbols("a b c")
d = a**2 + b*a
f = integrate(d, a)
# handcalcs integrates nicely with sympy
%%render sympy

a =5
b = 10
x = f
# I can get handcalcs to not evaluate/simplify
%%render symbolic

x_pos = sqrt(25) + sqrt(9)
# It seems I cannot with symy - but handcalcs (above example) works fine for me (with sympy) anyway
sympify(sqrt(25) +sqrt(9),evaluate=False)

So, I guess all is good. Thanks!

ref
https://github.com/connorferster/handcalcs#symbolic

You can view the above example results as an image here:
https://ibb.co/HDTvbH0

just a quick side note to stop some frustration. I edited in the comments in the render cells after executing the cells. At the time, I didn't realise that the %%render has to be at the first line - so don't use comments in the same cell if you can help it.

Mirco
@mircodezorzi
Hello, any way to find the intersection between an parabolic cylinder and a cylinder? I have tried with [...] but didn't have any luck:
solve(z==y**2+4*x+6,x**2+y**2==9, z)
Francesco
@cescofran_gitlab
Are there any documentations I can look at for better understand
the Basic.__new__(cls, ..) pattern and use of related metaclasses in sympy?
Aaron Meurer
@asmeurer
I think it's not very well documented unfortunately, outside of the docstrings and comments in basic.py
Your best bet is to look at what a similar class to what you are trying to write is doing.
Francesco
@cescofran_gitlab
thanks @asmeurer is that pattern proper to sympy or is ispired from other sources I can look to?
Aaron Meurer
@asmeurer
I mean look at the sympy source code.
Paul Moore
@pfmoore
I'm playing with sympy as a possible way of doing some (relatively basic) probability calculations, using the sympy.Stats module. Doing something like X, Y = Die('X', 6), Die('Y', 6); density(X+Y) is neat, but if I wanted to get the distribution of the highest of the 2 rolls, how would I do that? density(max(X,Y)) doesn't work - I can see why, but I don't know where to look in the docs for something equivalent that would work. What's the best way for me to learn how to model calculations like this?
3 replies
Kuldeep Borkar Jr
@KuldeepBorkar
Hello developers,
I am Kuldeep Borkar from Maharashtra
in 1st year of B.tech Artificial Intelligence,
I was just looking for a great opportunity to make a great contribution to this organization and I found GSOC. It would be great help if I am guided for making contributions and yes join community to work on a project. I am comfortable with Python and C programming. I just now want to start making contributions to this organization, resolve issues and wish to collaborate with community.
Francesco
@cescofran_gitlab
@pfmoore have you tried with density(Max(X, Y))? Where Max is the sympy max operation vs max the python operation?
Paul Moore
@pfmoore
@cescofran_gitlab Ah, no, I hadn't spotted that there's a sympy max. (There's so much in sympy, it's hard to know you've missed something like that :-)) I'll give that a try, thanks.
Aaron Meurer
@asmeurer
If you have any suggestions on how we could improve the discoverability of such things in our documentation, let us know.
We are hoping to apply to Google Season of Docs this year with a project to improve the organization of our documentation.
2 replies
aplund
@aplund:matrix.org
[m]
If I have a Tuple() object, can I do .simplify() to simplify the elements in inplace?