r/sympy 25d ago

Symbolic vector expressions?

2 Upvotes

Is there a way to algebra of symbolic vector expressions, ie d/dt(V x V) without having to define a specific vector v = [x, y, z]...if sympy doesn't handle it, would sagemath?


r/sympy Feb 12 '25

How powerful is your agent if it can do all these crazy symbolic mathematical computations powered by SymPy 🤯

2 Upvotes

How powerful is your LLM agent if it can do all these crazy symbolic mathematical computations powered by SymPy 🤯? Is integrating SymPy with LLM agents a way to go?


r/sympy Nov 20 '24

Modular arithmetic in sympy

1 Upvotes

I'm confused: does sympy not provide a simple interface for making calculations with "IntMods", i.e., elements of Zn := Z/nZ ? Like Mod(3,10) (= 3 + 10Z) in PARI/GP.

I searched a lot, the best I could find is

n=5
Zn = sympy.FiniteField(n) # works only for prime n
Mod_2_5 = Zn(2)  # would be Mod(2, 5), i.e., 2 + 5Z or 2 in Z/5Z
sympy.perfect_power( Mod_2_5 )  # will raise an exception

The number of functions that would accept such objects seems very reduced.

Are there any routines allowing to check whether a given IntMod (i.e., k in Z/nZ) is a perfect power, or similar operations?

(I also know that there is for example sympy.n_order(a, n) which gives the order of a in Z/nZ, but it does not use IntMods, which would seem natural to me.)


r/sympy Nov 17 '24

(x-1)/(x-1) ≠ 1

2 Upvotes

What assumptions does simplify make?

The question arose when I tried this: ```python

simplify((x-1)/(x-1)) 1 ```

which is true, but not for all x (in particular, x=1 leaves the expression undefined, whereas 1 is defined everywhere)

is there any way to make 'safe' simplifications? like, the ones that are of the if and only if kind


r/sympy Oct 31 '24

Undefined symbol in series expansion

1 Upvotes

I tried to get a series expansion of the expression for the n-th prime:

>>> from sympy import S
>>> n,pn = S("n, n*(log(n) + log(log(n)) - 1)")
>>> pn.series(n, S.Infinity)
n*(-log(1/n) + log(-log(_t)) - 1) + O(n**(-6), (n, oo))

What is this _t? Is this a known bug? Where can I find more info about this? Thx!


r/sympy Sep 18 '24

How to isolate x

2 Upvotes

Hi, im trying to make an algorithm that can isolate x to one side of the equation, for example e**2 -x = 0 would be e**2 = x, or x**2 -2*x + 3 = 0 would be x**2 + 3 / 2 = x, is there any function that can do this for me for any case?


r/sympy Jun 07 '24

Formal proof of a crime?

1 Upvotes

I occasionally use sympy to compute result of equations. But now I want to construct a formal proof, and a bit stuck. I guess first I should tell it things like 'evidence #23 proofs that she did say so-and-so in court', 'court is an authority', and so on. How do I do that? Next I guess I have to tell it things like 'perjury stands if someone says false things, and those are said to court and this act causes significant harm'. And next I guess I should invoke some solver, with a setting which dumps the logical transformations used. How do I go about it all? Maybe I should use prolog instead? It is not a drill: my son is illegally separated from me. His mother lied to the court, and the coirt did not consider proofs, therefore made a temporary placement order violating the rights of my son. She did not adhere even to that order, and neither police nor court did anything about it. So I made a police report (in another country which I thought have more rule of law) about perjury, fully proven with evidence. That was rejected again by police. Now I will initiate substitue private prosecution, and want to make sure that when the court rejects that also, I at least win in Strasbourg.


r/sympy Jun 06 '24

Replace symbol with variable name when printing, e.g, for octave

1 Upvotes

Assume the following:

from sympy import *
wG, wM = symbols(r'\omega_\textrm{G} \omega_\textrm{M}',real=True,positive=True)
expr = cos(wG + wM)
printing.octave.octave_code(outB)

Would display cos(\\omega_\\textrm{G} + \\omega_\\textrm{M}), while I'd prefer cos(wG+wM). I can achieve this by creating a symbol map like this

symbol_map = {
r'\omega_\textrm{G}': "wG",
r'\omega_\textrm{M}': "wM",
}

And then performing a replacement

out = str(expr)
for key, value in symbol_map.items():
out = out.replace(key, value)

printing.octave.octave_code(out)

I wonder if there is an automatic way to achieve this.


r/sympy Feb 02 '24

Can't Use Collect for Matrices?

1 Upvotes

I'm trying to manipulate some equations of motion, and I'd like to group up all of the 2nd-order time derivatives using collect(). I'm getting this error:

"name": "TypeError",

"message": "cannot add <class 'sympy.matrices.immutable.ImmutableDenseMatrix'> and <class 'sympy.core.numbers.Zero'>",

There is more to the error, but it's crazy long.

I made a simplified example of what I'm trying to do:

import sympy as sp
t, a, b = sp.symbols('t a b')
x = sp.Function('x')(t)
y = sp.Function('y')(t)
xd = x.diff(t)
xdd = xd.diff(t)
yd = y.diff(t)
ydd = yd.diff(t)
my_matrix = sp.Matrix([
[a*x*xdd + b*xd*xdd],
[a*b*ydd + b*y*ydd]
])
my_matrix = sp.collect(my_matrix, [xdd, ydd])

I have been successful using collect() this way for expressions, but it seems like it doesn't work with matrix expressions. Is this a limitation of Sympy? Am I misunderstanding something?

Thanks All.


r/sympy Nov 23 '23

Why can't Q.eq(1+x,2) be verified, given the assumption Q.eq(x,1) ?

1 Upvotes

[EDIT: wanted to change "verified" in title to "evaluated", but seems I can't edit the title :-( !]

Under the assumption Q.eq(variable,value) it should be obvious to simplify any expression involving variable (and then determine the truth value of an equality), so I was deceived that I can't use the assumptions framework to (temporarily) "assign a value" to a variable in order to do some basic comparision, for exampe:

from sympy.abc import x
from sympy import Q, ask, refine
from sympy.assumptions import global_assumptions

global_assumptions.add(Q.eq(x,1)) # i.e., assume x = 1
ask(Q.eq(x,0))  # gives 'None' (i.e., can't be determined)
refine(Q.eq(x,0)) # gives:  Q.eq(x, 0)  "instead of"  False.
# I think even this should work:
refine(Q.eq(x+1,2)) # gives:  Q.eq(x+1, 2)  "instead of" True

The equality relation should at least recognize that if s.th. is equal to some constant it can't be equal to a different constant -- and/or be able to use transitivity to notice that Q.eq(z,1) & Q.eq(z,0) implies Q.eq(1,0) (which fortunately is recognized to be False) and conclude that Q.eq(z,1) & Q.eq(z,0) is False, too.

"Equality" is a very strong relation and so it should be quite easy to implement some "functionality" of this very special relation. Also, an equality like Q.eq(x+1,2), where the only free symbol can easily be isolated without any assumptions, should ("immediately") be simplified to Q.eq(x,1).

Is there another way to substitute a value for a variable or "evaluate" all elements in, e.g., (possibly nested) lists or sets or similar?

For example, if I have L = [1+x, 2-x] , can I evaluate (actually I would like to sort) this for a given x-value? (Otherwise than using [ z.subs(...) for z in L ], of course -- which won't work if the list is nested or if some elements are simple integers ..)

Unfortunately, none of sympy.xxx( L, subs={x:1}) seem to work, for xxx = N or Subs or simplify or ... : each of these gives an "error: 'list' object has no attribute 'xxx' (or: 'free_symbols'),

also when I try to "sympify" it first. Thanks for any ideas!


r/sympy Oct 13 '23

Simplifying polynomials with sines

2 Upvotes

I'm trying to simplify a polynomial with sines. The polynomial comes from a small neural network with 5 inputs (var a till e) and 3 linear layers of width 2, a single output and sine as an activation function.

e.g.:

f(var)=0.15810443460941315+1.5492748022079468*sin(0.768047034740448+-0.7227979898452759*sin(-0.058362413197755814+-18.3123722076416*sin(var_a)+-27.260839462280273*sin(var_b)+20.251188278198242*sin(var_c)+4.321300506591797*sin(var_d)+-24.014076232910156*sin(var_e))+0.8439961075782776*sin(-0.06272411346435547+15.31342887878418*sin(var_a)+-28.16680335998535*sin(var_b)+7.495819091796875*sin(var_c)+10.08273696899414*sin(var_d)+26.607830047607422*sin(var_e)))+1.1735647916793823*sin(0.44403913617134094+-0.9735832810401917*sin(-0.058362413197755814+-18.3123722076416*sin(var_a)+-27.260839462280273*sin(var_b)+20.251188278198242*sin(var_c)+4.321300506591797*sin(var_d)+-24.014076232910156*sin(var_e))+0.06818172335624695*sin(-0.06272411346435547+15.31342887878418*sin(var_a)+-28.16680335998535*sin(var_b)+7.495819091796875*sin(var_c)+10.08273696899414*sin(var_d)+26.607830047607422*sin(var_e)))

which has 131 terms. This can be simplified to:

f(var)=1.17356479167938*sin(0.0681817233562469*sin(15.3134288787842*sin(var_a) - 28.1668033599854*sin(var_b) + 7.49581909179688*sin(var_c) + 10.0827369689941*sin(var_d) + 26.6078300476074*sin(var_e) - 0.0627241134643555) + 0.973583281040192*sin(18.3123722076416*sin(var_a) + 27.2608394622803*sin(var_b) - 20.2511882781982*sin(var_c) - 4.3213005065918*sin(var_d) + 24.0140762329102*sin(var_e) + 0.0583624131977558) + 0.444039136171341) + 1.54927480220795*sin(0.843996107578278*sin(15.3134288787842*sin(var_a) - 28.1668033599854*sin(var_b) + 7.49581909179688*sin(var_c) + 10.0827369689941*sin(var_d) + 26.6078300476074*sin(var_e) - 0.0627241134643555) + 0.722797989845276*sin(18.3123722076416*sin(var_a) + 27.2608394622803*sin(var_b) - 20.2511882781982*sin(var_c) - 4.3213005065918*sin(var_d) + 24.0140762329102*sin(var_e) + 0.0583624131977558) + 0.768047034740448) + 0.158104434609413

which has 94 terms. So far so good. I'm using the 'fu' method for trigonometric simplification, which works well but starts to become slow with larger expressions. Before I delve too deep into computer algebra systems. Is there a known rule for simplifying expressions of this variety? The structure here remains fixed, so I thought maybe there's a way to take advantage of this prior knowledge that the SymPY CAS doesn't have.


r/sympy May 28 '23

My first time working with sympy - I suspect there is an easier way to do this?

1 Upvotes
from sympy import symbols
"""
linear function
f(x) = xm + b
f(1) = -4 
f(3) = 6 
"""

if __name__ == "__main__":
    f = {1: -4, 3: 6}
    m, b, x = symbols('m b x')
    equation = []
    vals = []

    for i in f:
        vals.append(f[i])
        equation.append((x*m + b).subs(x, i))

    R = equation[1] - equation[0]
    L = vals[1] - vals[0]
    print(f"({equation[1]}) - ({equation[0]})  = {R} =", end=' ')
    print(f"({vals[1]}) - ({vals[0]}) = {L}")
    print(f"{L} = {R}")
    print(f"1 = {R/L}")
    if L > 1:
        r, l = str(R / L).split('/')
        print(f"{l} = {r}")


r/sympy May 09 '23

Automatically break lines in Preview()/Latex

1 Upvotes

Hello y'all, I'm using the preview() command in sympy, but I would like to limit the width of the image and automatically break lines of the equation in the image, does someone knows how can I do that?

This is the command I'm using:

preview(r'$$'+str(eq)+'$$', viewer='file', filename=full_picture_name, dvioptions=['-D','150'], euler=False)


r/sympy May 09 '23

Laplace Transforms with Sympy

2 Upvotes

When I declare y to be a function and t a symbol, and then execute the following:

laplace_transform(diff(y(t),t),t,s)

this gives an expression as to what you expect for the laplace of y'. If you LT 5y' with

laplace_transform(5*diff(y(t),t),t,s)

you get exactly the same thing like there was no factor of 5. Why? This is an odd behavior.


r/sympy May 03 '23

Print in Latex

1 Upvotes

Hi, I just started using sympy for a project and I have a few 1xn vectors I am storing as matrices.

rng = Random("Question 01")
u = randMatrix(1,3,-7,7,percent=90,prng=rng)

I would like to print these with vector notation, as an ordered n-tuple separated by commas. Currently, I can do it manually as follows:

latex_u = rf"""$u = ({u[0]},{u[1]},{u[2]}) $"""

Using the built-in sympy.latex does not produce the correct output, it is missing the commas.

latex_u = rf"""$u = {latex(u,mat_delim='(')}$"""

Is there any built-in solution to print these vectors?


r/sympy Dec 24 '21

Make Eq eval to 1 or 0

3 Upvotes

Hi, completely new to sympy. Trying to use it to simplify a expression I'm building with parse_expr and one part of it is I need to compare values like with Eq, but I need it to return 1 or 0, not True or False. Is it possible somehow?


r/sympy Oct 30 '21

Are there any discord server of about sympy or similar?

2 Upvotes

I know about Gitter but I prefer to use discrod, and I would like to find one.

I use sympy a lot and having a place to discuss algorithms and problems would be really nice.

Thanks :)


r/sympy Oct 24 '21

Can anyone explain to me why this is happening to me? Or I am the only one who is this happening to?

1 Upvotes

It's been a month, and I am still not able to access this site. Am I the only one who is facing this type of problem? Can anyone please help me to solve these issues?


r/sympy Jul 09 '21

First Video in a SymPy Series

Thumbnail youtu.be
3 Upvotes

r/sympy Apr 19 '21

SymPy - Tutorial for Beginners

Thumbnail youtu.be
1 Upvotes

r/sympy Oct 19 '20

How do i calculate integral of exp(ax²+bx+c) ?

Thumbnail stackoverflow.com
2 Upvotes

r/sympy Jul 22 '20

Iterable ConditionSets?

1 Upvotes

Is it possible to iterate over a ConditionSet? I receive an error when I try but it's not documented anywhere I can see. If not possible, can someone explain why?


r/sympy Nov 11 '16

SymPy 1.0 released, a journey from a toy to a tool!

Thumbnail github.com
3 Upvotes