Questions tagged [sympy]
SymPy is an open source Python library for symbolic mathematics.
5,694
questions
0
votes
0
answers
32
views
Equations of motions for 3D pendulum in SymPy
does anybody have experience with obtaining equations of motions in sympy for 3D pendulum using sympy mechanics ? I tried to do it the easiest way as possible, but the resulting mass matrix has NaNs ...
2
votes
0
answers
70
views
Dfferentiating a sum in Sympy
I would like to differentiate the entropy H(T) of a discrete random variable T with respect to one of the masses q(t') using Sympy.
The following implements the expression for H(T):
import sympy as ...
0
votes
0
answers
58
views
I want MatrixFactor?: Matrix([[2*a, 3*a], [4*a, 5*a]]) → a*Matrix([[2, 3], [4, 5]])
? how to find common "a"
Matrix all element conver str? I want to find common str
from sympy import *
var('a',real=True)
def myMatrixFactor_Str(M):
# sum=M[0,0]+M[0,1]+M[1,0]+M[1,1]
...
1
vote
0
answers
23
views
Use sympy for calculation of nested sums invoking matrix entry
I try to implement a nested summation. For readability, I use sympy. Now I found a dead-end. The code sequence calculate the matrix entries of F_np. Currently, for sake of simplicity, f_np remains ...
0
votes
0
answers
45
views
Solving for and assigning the last remaining variable in a sympy expression
This is a simplification of a more complex problem, in which, depending on previous input, a sympy exression needs to be solved with one remaining variable, where it is not known which variable that ...
0
votes
1
answer
19
views
sympy solve() result cannot be used in np.isclose(): ufunc 'isfinite' not supported
My code is as following. The goal is to, given 1) a circle with radius r, 2) a point (x0, y0) on it, and 3) a line with slope through that point, find out the other point that the line intersects with ...
-3
votes
0
answers
34
views
How to use Sympy to simplify two equations and solve for the variables?
WHAT I WANT TO DO
In Sympy,
A is defined as (-K * erf(sqrt(2) * (2 * T * r - T * sigma2 + 2 * log(S/K)) / (4 * sqrt(T) * sigma)) - K + S * (erf(sqrt(2) * (2 * T * r + T * sigma2 + 2 * log(S/K)) / (4 * ...
0
votes
1
answer
25
views
sympy fails to find the volume of a sphere with a two fold integral
The following script computes the volume of a sphere
from sympy import sqrt, symbols, integrate
x,y = symbols('x y ')
R = symbols('R', constant=True) # radius
f=sqrt(R**2 - x**2 - y**2) # explicit ...
0
votes
0
answers
34
views
Sympy: simplify gives TypeError: cannot determine truth value of Relational
I have a complicated sympy expression and I want to simplify it. However, I get a TypeError. The following is a shortened version of my expression but reproduces the error:
from sympy import *
x = ...
0
votes
0
answers
32
views
How to resolve errors related to Cython compilation of the Kivy in the Buildozer packaging of the kivy app for android 13
I ran the following the buildozer code for the packaging a kivy app for an android environment
!buildozer -v android debug
I am getting the following error snippet showing a series of errors related ...
0
votes
1
answer
27
views
Customizing SymPy's LaTeX Representation of Positive Infinity to Display +\infty
I've noticed that in SymPy, the default LaTeX representation for positive infinity is \infty. I'm seeking a method to customize this representation to show +\infty instead.
I've explored here
https://...
0
votes
1
answer
26
views
How to put a numpy norm condition in loop?
I'm trying to implement a Gradient Descent method with optimal step in python, but i get this error :
AttributeError: 'Float' object has no attribute 'sqrt'
if (np.linalg.norm(np.array(dk)) < eps):...
0
votes
0
answers
40
views
how to generate multi dimensional grid from a system of multi-variate inequalites
I have a system of 6 inequalities in the form of (rational but not necessarily linear):1+x1x2>x3; x4>(1+x5)x6; x3/x4>1-x2;... and more.
Now I need to generate a grid of (x1,x2,x3,x4,x5,x6,x7) ...
0
votes
1
answer
22
views
Sympy - evalf vs subs
I don't get same results using evalf or using subs. Why?
from sympy import *
x = Symbol('x')
w = Symbol('w')
b = Symbol('b')
i = Symbol('I')
e = Symbol('E')
c3 = Symbol('c3')
m1 = w*b/2*x
yd1= ...
0
votes
1
answer
35
views
Why sympy cannot correctly determine identity of simple sqrt expressions?
Even if it correctly determine the inequalities of both sides, it cannot determine the identity.
from sympy import *
assert not 2 * sqrt(2 - sqrt(3)) == -sqrt(2) + sqrt(6)
assert 2 * sqrt(2 - sqrt(3)) ...
0
votes
1
answer
15
views
sympy could not solve two equations that come from two planes intersecting
The following piece of code should solve two equations (shown in the picture below)
which represent two planes intersecting in a line.
import sympy as sm
x,y,z = sm.symbols('x y z ')
p1 = sm.Plane(sm....
0
votes
2
answers
52
views
sympy not returning solution using solve
I'm attempting to calculate Lagrange multiplier using a question from this youtube video: https://www.youtube.com/watch?v=B0yzLgJ6wn8, however using sympy.solve does not return solutions
I've coded ...
0
votes
0
answers
40
views
Can sympy arrays be properly sliced after numerical substitutions?
Consider this example for sympy:
import numpy
from sympy import Array, IndexedBase, symbols
k = symbols('k', positive=True)
M = IndexedBase('M')
M_vals = numpy.array((
(0, 1, 2),
(3, 4, 5),
...
0
votes
1
answer
56
views
solve a mathematical expression about a specific unknown parameter with python
I have this equation
and want to solve it about t with Python, numpy. First of all I should say that in this equation:
<<h r ai hint T*=1000 tau=t/t*>>
is known. Before I ask the question ...
0
votes
1
answer
36
views
Is there any way to print the expressions of sympy pretty in pydroid3
Two questions, the first question, is it possible to print expressions of sympy In an attractive way in the terminal of pydroid3 Especially in pydroid3 terminal because most of the ways like below has ...
3
votes
1
answer
45
views
sympy will produce a zero derivative when it is not
Here is a code sample for the problem.
from sympy import Function, symbols, diff, cos,sin
x,y = symbols('x y')
f, g = symbols("f g", cls=Function)
g = g(x)
f= f(g) # function composition ...
1
vote
1
answer
51
views
substitute would not work when using chain rule in sympy
I have the following SymPy script
from sympy import Function, symbols, diff, cos
x, y = symbols('x y')
f, g = symbols("f g", cls=Function)
g = g(x)
f = f(x + g) # function composition here
...
0
votes
2
answers
60
views
How to check if sympy recognizes a function when it's parsed by sympify()?
Consider the following example where an arbitrary mathematical expression is given.
>>> import sympy as sp
>>> expr = sp.sympify("sin(x) + foo(y)")
Above, sin is the sine ...
0
votes
0
answers
22
views
Monomial comparison in SymPy with various monomial orderings
Division of polynomials of more than one variable is a lot more complicated than one variable which we learned in high school.
I am trying to implement in SymPy the division algorithm for multi-...
0
votes
1
answer
23
views
How do I get Sympy dsolve to produce all the eigenvectors for a ODE?
I am using dsolve on a basic ODE and I am getting f(x) = 0 as the only result. This is a valid result but there are infinitely many Eigenvector solutions to this equation. How do I encourage SYMPY to ...
0
votes
1
answer
51
views
Simplifying an arithmetic expression in Python
I'm trying to write a python code on simplifying a given expression (in order to solve a non-homogeneous linear diophantine equation.) For example, given the input:
7x+13y = 1
the code will return
1=...
0
votes
1
answer
52
views
How to minimize a multivariate sympy function by casting it to a lambda function?
I'm trying to write a function that implements the 'effective variance method' of least squares regression. In this formula the derivative of a model needs to be used in the least squares sum. I was ...
0
votes
0
answers
35
views
Simplifying equations created from a matrix
Good day! I'm trying to write a python script that can model the temperature of a space.
I've chosen an 11x11 matrix to represent this space, the edges are 20 degrees, and the centre is 3000 degrees.
...
1
vote
1
answer
48
views
The computation had not completed because of the undecidable set membership is found in every candidates
Using Sympy from IPython, I receive an error that I cannot understand.
The Y1 function below is a polynomial in x, that has a maximum in [0,1] (see the plot, please notice that the y axis is inverted!)...
1
vote
2
answers
128
views
How to change sympy plot properties in Jupyter with matplotlib methods
The following code in a script works as expected,
from sympy import *
x = symbols('x')
p = plot(x, x*(1-x), (x, 0, 1))
ax = p._backend.ax[0]
ax.set_yticks((0, .05, .25))
p._backend.fig.savefig('...
0
votes
1
answer
28
views
Manipulating an sympy expression with .func() and args
I have this expression: $expr = \frac{2 A B}{A m + m} + C^2 + D^2$
and I want to get: $expr = \frac{2 B}{m \left( 1 + \frac{1}{A} \right)} + C^2 + D^2$
I am trying to use expr.args to get the term ...
1
vote
1
answer
120
views
Find the inverse of a function
I am trying to find an inverse in Python.
import sympy as sp
import numpy as np
k = sp.Symbol("k")
x = sp.Symbol("x")
f = 1 - ((k * x**2 + 2 * k * x + 2) / 2) * sp.exp(-k * x) # ...
0
votes
0
answers
46
views
Implementing Diffie-Hellman in python. Shared secret keys aren't getting matched
hope everything is well. I am trying to implement the Diffie_Hellman in Python. I read that for better security, it is recommended that the prime 𝑝
should be at least 2048 bits long, and 𝑔 should ...
0
votes
0
answers
24
views
Fourier series of Piecewise -2pi to 2pi with sympy
I'm trying to find the fourier series of the following piecewise
f_t = sym.Piecewise(
(sym.sin(t), (t >= -2 * sym.pi) & (t <= 2 * sym.pi))
)
This is the code I already have, but I'm not ...
0
votes
1
answer
51
views
solve equation with abs using sympy
I'm trying to solve the following equation using sympy:
.
The code I am using to find the solution to the equation for Vr is the following:
import sympy as sp
Vr, Vs, gamma, l, Zc, P, Q = sp.symbols('...
0
votes
0
answers
22
views
AttributeError: 'list' object has no attribute 'free_symbols' when usins latex2sympy
I want to define a program that extracts the variables of an equation in latex, but sometimes I get an error and I don't know why this happens.
This the code I have done:
from sympy import sympify, ...
1
vote
1
answer
45
views
formula for nth derivative in sympy
I am playing with the sympy for a while. I want sympy to derive me a formula of nth derivative for specific function.
I tried:
import sympy as sp
x = sp.symbols('x')
n = sp.Symbol('n',integer=True)
f =...
0
votes
1
answer
29
views
Get the matrix-vector representation of a set of symbolic linear inequalities
With Sympy, it is possible to define symbolic linear inequalities, like:
This system is equivalent to:
And then it can be represented by a matrix A of coefficients and a vector b of upper bounds:
A =...
0
votes
2
answers
60
views
Store SymPy solve() results in a useful form
I wrote a for-loop to solve for a bunch of angles. I have all the results stored in a NumPy array of dictionaries. But I want all the answers in degrees (not radians) and rounded. It is basic but I am ...
0
votes
1
answer
40
views
SymPy hangs up when trying to solve system of equations
I am trying to solve a system of two equations for two unknowns. Jupyter ran indefinitely in the attempt to solve these equations.
My code:
import numpy as np
import sympy as smp
#Enable LaTex display
...
0
votes
1
answer
29
views
How to ouput SymPy solution as LaTex
Does anyone know an easy way to have SymPy display the solution with a LaTex format? The solution is going into a report, and I do not want to have to retype the whole thing. The equations are ...
1
vote
2
answers
106
views
solving Ax =b with constraints for a non-square matrix A, using python
I solved this problem in Mathcad, but I don't know how to transfer it to Python.
Mathcad:
I tried this code:
from sympy import symbols, nonlinsolve
Q = np.array([230.8084,119.1916,76.943,153.8654,196....
0
votes
2
answers
77
views
Sympy solve complex algebraic equation
I am trying to solve a transcendental algebraic equation
1 / sqrt(x) = -2 * log10( 0.0015 / (3.7 * 0.25) + 2.51 / (267471 * sqrt(x)))
In reality 0.0015, 0.25 and 267471 are input parameters with names ...
0
votes
1
answer
34
views
How to fill an empty Sympy Piecewise function?
I have a function called 'symbolic_a0(array, T)' and the objective of that function is to create a piecewise function from the array that is given. The way that the function works is the following:
...
0
votes
1
answer
27
views
How can I plot a sympy function with the dependant variable on log scale?
I want to plot a function in sympy with the dependent variable on log
scale. However, it don’t get it to work.
For the linear plot it works nicely:
import matplotlib.pyplot as plt
import sympy as sym
...
0
votes
1
answer
41
views
How can I manually specify text for legends in sympy.plotting.plot?
I am using sympy.plotting.plot for plotting different functions. I want
to add manual labels.
This is what I tried, however, I would like to have ‘Foo1’ in line 1 of
the legend and ‘Foo2’ in line 2 of ...
0
votes
1
answer
37
views
Using sympy to solve 2nd order DE. Trying to solve for constants. Having trouble getting sympy to recognize that a symbol ahs been solved for
def A1_A2slvr():
if(ωSquared > αSquared):
print("Underdamped")
ωD = math.sqrt(ωSquared - αSquared)
α = math.sqrt(αSquared)
e = math.e
t = sp....
0
votes
2
answers
53
views
Faster evaluation of function of multiple derivatives in sympy
I have a generic symbolic function x(t) [think of t as time], and a function of various derivatives of x, ie F(x(t),x'(t),x''(t),..., x^(n)(t)). I would like to evaluate F as fast as possible given ...
1
vote
1
answer
45
views
How to evaluate every element of a list with Sympy?
I have a list A below and I want to print the evaluated version where theta = 90°.
import sympy as sp
theta = sp.symbols('theta')
A = [sp.cos(theta), sp.sin(theta),0,0]
theta_val = sp.pi / 2
...
0
votes
1
answer
32
views
Pass an array to sympy expression in place of symbol
I'm new to SymPy and I'm currently trying to learn how to use this tool in conjunction with Jupyter to create adaptive notebooks for my university.
I'm currently facing an issue related to summations. ...