A computer algebra system written in pure Python http://sympy.org/ . To get started to with contributing https://github.com/sympy/sympy/wiki/Introduction-to-contributing
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 ?
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?
tokenize
auto_symbol
and modify it so that it allows NAME + '
tokens.
>>> 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='')]
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.
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 toMatrix([[-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]])
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)
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 simplifysympify ("(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
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 simplifysympify ("(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#symbolicYou 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.
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?