Very strange behaviour of solve command in SymPy, when I try to solve quadratic equation with parameter a:
from sympy import *
x, a = symbols("x a")
eq = Eq(0, (4 - 4*x + x**2)/(4*a**2))
print(solve(eq, x))
print(solve(simplify(eq), x))
Output:
[2 - sqrt(a**2 - 1)/a, 2 + sqrt(a**2 - 1)/a]
[2]
Just solve gives two (!) solutions, which depend on a. After symplifying it gives only solution x=2, which is correct. What happens? Command solveset works correctly, but I am interested in using solve command. My SymPy version is 1.9.
This is a bug in as_numer_denom which get an expression that starts as an Add but is essentially a Mul. It is being corrected with this PR.
Related
I'm trying to compose two functions and I get a bizzare result
'''
#!/usr/bin/python
from sympy import *
init_printing(use_unicode=True)
x= symbols('x')
f = x/(x+1);
g = x/(x+2);
print(compose(f,g))
This shows : x/((x + 1)*(x + 2))
Should be x/(2x+2)
I don't get it. Does anyone has an idea?
Thanks
Despite being available in the top-level sympy namespace under the plain name compose, sympy.compose doesn't actually do general function composition.
sympy.compose is actually sympy.polys.polytools.compose. It's actually a function for polynomial composition. When you try to compose x/(x+1) and x/(x+2), it ends up interpreting these inputs as multivariate polynomials in 3 variables, x, 1/(x+1), and 1/(x+2), and the results are total nonsense.
I'm trying to integrate functions in Python. scipy.integrate.quad seems to work ok; but just be sure I'd like to check the results against other integration code. It was suggested that I try sympy.integrate. Now the code for the functions I want to integrate contains int(), which I use to convert floats into ints. This is ok for quad, but not for sympy.integrate.
Here's a simple example that reproduces the error:
import sympy
def f(x):
return sympy.exp(int(x))
sympy.symbols('x')
print(sympy.integrate(f(x),(x,0,2)))
This yields the error: TypeError: can't convert symbols to int
So is there a way to integrate functions that involve int() with scipy.integrate?
Thanks
To use integrate f must be a SymPy symbolic function which disallows your particular use of int. int(x) where x is a Symbol will always yield a type error however you could represent this symbolically using the floor function:
def f(x):
return sympy.exp(sympy.floor(x))
However, using floor may defeat some of the purpose of using SymPy in the first place because it will probably prevent discovery of an analytic solution as the following python session demonstrates:
>>> from sympy import *
>>> x = symbols("x")
>>> integrate(exp(floor(x)), (x, 0, 2)) # use of floor prevents evaluated result
Integral(exp(floor(x)), (x, 0, 2))
Though you can use the evalf method to compute a numeric result (which is ultimately performed by mpmath):
>>> integrate(exp(floor(x)), (x, 0, 2)).evalf()
3.7
(perhaps this result suggests sympy could better handle this integral? Wolfram Alpha computes this as 1 + e = 3.71828... so I suppose there is at least a floating point precision bug here too - see comment re ceiling)
In any case, I don't know if you consider that an appropriate result considering the version of f without floor:
>>> integrate(exp(x), (x, 0, 2))
-1 + exp(2)
>>> _.evalf()
6.38905609893065
I am trying to automatically solve $x**-d - sympy.exp(100)$ with sympy.
import sympy
from sympy import solve, Symbol
x = Symbol('x')
d = Symbol('d', integer=True)
print(solve(x**-d - sympy.exp(100), x))
returns no solutions [], but the solution should be $x=e^{-100/d}$ (see e.g., wolframalpha)
What did I do wrong? Same result when removing integer=True.
However,
print(solve(x**-d - sympy.exp(100), x))
returns [exp(100/d)].
The solution appears when solve(check=False) is given.
see Solving Equations Involving Radicals of
http://www.cfm.brown.edu/people/dobrush/am33/SymPy/solvers.html
I have an expression which includes an IndexedBase. I'm taking a partial derivative, but in the output, this is retained:
In [105]: sympy.IndexedBase(b)[a].diff(b)
Out[105]:
∂
──(b[a])
∂b
My b[a] is simply an array b_0, b_1, ..., b_n. Those are all constants, so ∂b[a]/∂b is equal to 1. How do I rephrase my problem such that it evaluates to one?
First of all, install the latest master branch of SymPy (the latest SymPy version doesn't support these features). Otherwise wait for the next SymPy version release.
Anyway, you can get a Kronecker delta function:
In [27]: b = IndexedBase("b")
In [28]: b[a].diff(b[c])
Out[28]:
δ
a,c
If you derive the indexed object by the same index:
In [29]: b[a].diff(b[a])
Out[29]: 1
The operation b[a].diff(b) isn't clearly defined though, and will raise an error.
I would personally interpret b[a].diff(b) as an array of derivatives ∂b[a]/∂b[0], ∂b[a]/∂b[1], ...
I want to simplify an expression using the simplify function in sympy, and want output in special format
simplify("2*((2**n)+1)**2").expand(Basic=True)
However, sympy is returning me the following result:
2*2**(2*n) + 4*2**n + 2
But I want the result in the following format:
2**(2*n+1) + 2**(n+2) + 2
Is there a way to do that?
SymPy's simplify module offers several methods of simplification: the one you need here is powsimp.
from sympy import *
var('n')
a = simplify(2*((2**n)+1)**2).expand(Basic=True)
powsimp(a)
output: 2**(n + 2) + 2**(2*n + 1) + 2