Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
  • 22:10
    ThePauliPrinciple commented #22360
  • 22:05
    ThePauliPrinciple commented #22360
  • 22:03
    ThePauliPrinciple commented #22360
  • 21:55
    ThePauliPrinciple commented #22360
  • 20:15
    jkr26 starred sympy/sympy
  • 18:41
    ayushk7102 commented #22275
  • 18:40
    smichr closed #22342
  • 18:40
    smichr commented #22342
  • 18:33
    ThePauliPrinciple commented #22351
  • 16:32
    qiayuanliao starred sympy/sympy
  • 15:25
    Albuquerque-David starred sympy/sympy
  • 13:30
    yasserotiefy starred sympy/sympy
  • 11:33
    github-actions[bot] commented #22342
  • 10:39
    ThePauliPrinciple commented #17287
  • 10:03
    github-actions[bot] commented #22342
  • 10:02
    smichr synchronize #22342
  • 09:46
    github-actions[bot] commented #22342
  • 09:32
    smichr synchronize #22342
  • 09:22
    smichr commented #17287
  • 09:20
    github-actions[bot] commented #22286
Miguel Guthridge
Is there a way for me to include sympy as a module in another project so that I can import it inside an interpreter where pip installing modules isn't accessible?
2 replies
I'm trying to make an extension to a search program that uses python so that when you type eq [some equation] it'll solve it
But since I've never included a package without having it pip installed, I'm not entirely sure where to start haha
My test is returning this as the expected output Complement(Integers, ConditionSet(n, Eq((n - 1)*(n + 1), 0), Integers)). I need to convert this to 'sympy' form. Need help

Why does the following latex code of the function varies from the symbol?

import sympy as sp
r = sp.symbols("r")
print(sp.latex(sp.symbols("\delta\ u_t")))
print(sp.latex(sp.Function("\delta\ u_t")(r)))


\delta u_{t}
\delta\ u_{t}{\left(r \right)}

Notice that the space right after \delta is escaped for the function, but not for the symbol.

Aaron Meurer
The latex printer uses different code paths for Symbol and Function. They should probably be made uniform. By the way, you should always use raw strings for latex, like r"\delta\ u_t".

Okay, is there a way I can use the Symbol-like expressions for functions, or define the symbol without the space? The way it gets displayed, it appears as if \delta and u_t are two different variables. Ofcourse, sp.symbols("\deltau_t"), it won't work.

Also, why raw strings for latex?

Kalevi Suominen

why raw strings for latex?

Otherwise double backslash will be needed like \\delta.


why raw strings for latex?

Otherwise double backslash will be needed like \\delta.

But sp.symbols("\delta\ u_t") works.

Kalevi Suominen
It seems to work, apparently because \d is not an escape sequence in Python. However, \b is an escape sequence:
>>> "\beta\ u_t"
'\x08eta\\ u_t'
Aah okay. \theta won't work either then. Thanks a lot.

Okay, is there a way I can use the Symbol-like expressions for functions, or define the symbol without the space? The way it gets displayed, it appears as if \delta and u_t are two different variables. Ofcourse, sp.symbols("\deltau_t"), it won't work.

Also, why raw strings for latex?

Any solution for this?

Arun sanganal
Hello! I am a freshman from IIT Kharagpur, new to sympy. Hoping to get along with this awesome community. Cheers!😄

Maybe someone can explain the following to me:
Using sympy 1.7.1 i get the following:

from sympy import *

a = Symbol('a')

a0, = solveset(Eq(2*a**5,0.34),domain=S.Reals)
a0 # 0.70160032942779

print(a0.round(4)) # 0.7016
print(a0.round(4).n(10)) # 0.7015991211

Thank you for any hints.

Hi! 👋 I have a sympy expression and want to iterate over elements of its syntax tree. Can somebody please point me to the right direction?
Nevermind. I just found it myself: docs.sympy.org/latest/tutorial/manipulation.html#recursing-through-an-expression-tree
@flxai:matrix.org hi you can use the args method, for example if you expression is 'expr' you can iterate on its elements by '[arg for arg in expr.args]'.
moiseszeleny-5dd0d487d73408ce4fd13ecb: Thanks. That's what I'm doing. Just recursively
Hi hi, can someone help me with a little thing plis?
I'm having a very hard time trying to get the memory usage of sympy expressions, actually for example, sys.getsizeof always return 64 bytes, even if the var have 200 bytes in the name...., just the name...., how can I get the actual memoery usage of a expression?
latot: You could try something like this: ix.io/3q9z

Sorry, a very general question, I'm just looking around with sympy.

I was confused by sympy having two implementations of vectors, sympy.vector an sympy.physics.vector. They seem to not be sharing code and the documentation for either doesn't mention another one and the reason to have them separate. Why is that? Is there a way to make them interoperate?


1 reply
Miguel Guthridge
I've been making a maths interpreter using sympy, and I'm trying to implement simultaneous equations, but I'm getting a cannot determine truth value of Relational error when I try to solve them
It's parsing the equations as being semicolon seperated, then recursively generating a sympy expression for them
For the = I'm using sym.Eq(a, b)
And then once I've got everything simplified as much as possible, it uses sym.solve() with a list of the expressions
Which is where the error occurs
Miguel Guthridge
Ok nevermind it turns out that sympy doesn't work well with prettyprinting so I just had to stringify it first
Hey guys,
My name is Adarsh V. Desai. I am an aspiring software developer. I would like to be a part of sympy and take my skills to the next level by learning new technologies while being an active contributor. I have already installed sympy (developer version) on my local system. Could you guys please point me towards what I should do next to become an active contributor and also understand the code base?
Thank you.
You could help solving issues, prioritizing bugs: https://github.com/sympy/sympy/issues
Priyanshu Agarwal
When I try sympy.vector.operators.Divergence('x') it gives me AttributeError: module 'sympy' has no attribute 'vector' but when I try from sympy.vector.operators import Divergence and Divergence('x') it works, could you please help me
Aaron Meurer
The vector module is not imported by default so you have to import sympy.vector before you can access it.
I'm a bit confused with the Fr and Frstar outputs of Kane's method. Here I've printed them. Fr, shows Tx, which is the load/torque. Cool. Frstar has 3 terms. First one is Ixx ddeta; inertia x angular acceleration. ok. But the next 2 terms are Ixx Omega^2; inertia x angular velocity^2. why is it like that? Fr has the unit Nm, term 1 of Frstar has Nm.rad, others have Nm.rad^2. Is it because rad is an SI derived unit?
Gerardo Jose Suarez
Hi. why is xconjugate(x)==abs(x)*2 false?
Kalevi Suominen

They are not structurally equal:

In [11]: srepr(x*conjugate(x))                                                  
Out[11]: "Mul(Symbol('x'), conjugate(Symbol('x')))"

In [12]: srepr(abs(x)**2)                                                       
Out[12]: "Pow(Abs(Symbol('x')), Integer(2))"

== denotes structural equality.

Gerardo Jose Suarez
got it, is there other equality I can use for this kind of stuff?
Kalevi Suominen
You can use (abs(x)**2).rewrite(conjugate).
Sebastian Helm
Just a quick question|remark: Under The Power of Symbolic Computation, where output is spread over several lines, it misaligns with most fonts. Which font is recommended for viewing this nicely? I only found it to work with Unifont, which isn't very pretty, though. Anyway, maybe the page can be rewritten to suggest such a font?
4 replies
Pieter Barendrecht

Hi guys, question about root finding with symbolic coefficients. Is it possible to get symbolic results for something like the following:

import sympy as sp
t = sp.Symbol('t')
P = sp.Poly(t**2 + 2*t + sp.log(2))

This does not work, as I get sympy.polys.polyerrors.PolynomialError: only univariate polynomials are allowed

Replacing the 3rd line with P = sp.Poly(t**2 + 2*t + sp.log(2), t, domain='RR') works for numerical values. Looking at the docs, I thought something like P = sp.Poly(t**2 + 2*t + sp.log(2), t, domain='QQ<log(2)>') might work, but this triggers a sympy.polys.polyerrors.NotAlgebraic: log(2) doesn't seem to be an algebraic element
Kalevi Suominen
QQ<log(2)> does not work because log(2) is not algebraic. One must use something like QQ(log(2))instead. Or, simply give t as the sole generator; then log(2) will automatically become a constant.
In [26]: p = Poly(t**2 + 2*t + log(2), t)                                       

In [27]: roots(p)                                                               
⎧       ____________            ____________   ⎫
⎨-1 - ╲╱ 1 - log(2) : 1, -1 + ╲╱ 1 - log(2) : 1⎬
⎩                                              ⎭
Pieter Barendrecht
Thanks! Using P.all_roots() still produces an error when doing so (NotImplementedError: sorted roots not supported over ZZ[log(2)]), but roots(P) works just fine
Kunal Mahatha
Hey everyone,
I am Kunal. I want to contribute to sympy, but I am a beginner and I know basic to intermediate Python. Could someone please guide me on how the contribution procedure works here, and suggest to me some good beginner-friendly issues to start working on?
hi, i'm an amateur at logic, and was wondering if it was possible to generate latex semantic tableaux trees using sympy?
Joao Aguirre
Hello everyone, just a simple question (I hope), is it possible to do nested summations with dependent indexes using the Concrete class Sum, replicating in a "single command" things like
f(x)=i=03(j=i+14axiyj) f \left ( {x} \right ) = \sum_{i = 0}^{3} {\left ( {\sum_{j = i + 1}^{4} {\frac{a}{x_{i} - y_{j}}}} \right )}
1 reply
Brandon Feder
Hey guys. I was just wondering if anyone knows what algorithm is being used in the GeneralSumOfSquaresclass used here. Is there a paper I can reference/explanation, etc?
Aaron Meurer
@brandon-feder there are some links in the source, e.g., in the power_representation function. Otherwise you might find some references in some of the old 2013 posts in this blog https://thilinaatsympy.wordpress.com/
Jason Ross

I'm having trouble learning the diffgeom module. I'm trying to compute covariant derivatives in spherical coordinates and I feel like I'm missing something.

Here's what I'm working with:

from sympy import *
from sympy.abc import x, y, z, r, theta, phi
from sympy.diffgeom import CoordSystem, Manifold, Patch, TensorProduct as TP

R3 = Manifold("R3", 3)
S = Patch("S", R3)
relations = {
    ("Car3D", "Sph") : Lambda(
        (x, y, z),
            sqrt(x**2 + y**2 + z**2), 
            acos(z / sqrt(x**2 + y**2 + z**2)),
            atan2(y, x),
    ("Sph", "Car3D"): Lambda(
        (r, theta, phi),
        Matrix([r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta)])
Car3D = CoordSystem("Car3D", S, [x, y, z], relations)
Sph = CoordSystem("Sph", S, [r, theta, phi], relations)

I can compute the metric tensor in spherical coordinates by doing the following:

g = (Car3D.jacobian(Sph) @ Car3D.jacobian(Sph).T).simplify()

However, this computes the metric tensor with respect to $x, y, z$, while I'd like to write the metric tensor with respect to $r, \theta, \phi$:
[10001x2+y2+z20001x2+y2]\left[\begin{matrix}1 & 0 & 0\\0 & \frac{1}{x^{2} + y^{2} + z^{2}} & 0\\0 & 0 & \frac{1}{x^{2} + y^{2}}\end{matrix}\right]
I can do a substitution, but it's kind of awkward:

x, y, z = Car3D.symbols
r, theta, phi = Sph.symbols
forward_subs = {
    x: r * sin(theta) * cos(phi),
    y: r * sin(theta) * sin(phi),
    z: r * cos(theta)
g_sph = g.subs(forward_subs).simplify()

[10001r20001r2sin2(θ)]\left[\begin{matrix}1 & 0 & 0\\0 & \frac{1}{r^{2}} & 0\\0 & 0 & \frac{1}{r^{2} \sin^{2}{\left(\theta \right)}}\end{matrix}\right]
This gives me the expected metric tensor as a matrix but there's a lot of extra effort:

  • I have to define the forward and backward transformations BEFORE I declare forward_subs since the CoordinateSymbol "x" does not exist until I create Car3D, and I have to provide the forward and backward transformations to create Car3D.
  • Converting from one coordinate system to another uses a dictionary that duplicates all the information already defined in the coordinate systems
  • Computing a metric tensor by taking the Jacobian times its transpose seems like extra effort
  • I still haven't got a tensor expression for the metric tensor

I can convert the metric tensor Matrix to a tensor expression as follows:

g_sph_tens = 0
for i, e_i in enumerate(Car3D.base_oneforms()):
    for j, e_j in enumerate(Car3D.base_oneforms()):
        g_sph_tens += g_sph[i, j] * TP(e_i, e_j)

However, computing the Christoffel symbol for g_sph_tens using metric_to_Christoffel_2nd gives all zeros, which is incorrect.

Can somebody help me out with this? It seems like I'm missing some fundamentals.

1 reply