I have the following fraction:
import sympy as sp
a = sp.Symbol("a")
b = sp.Symbol("b")
a/(a+b)
And would like to print it as
1/(1+b/a)
I saw sympy had a factor function but I couldn't obtain the expected behaviour.
I thought I could maybe do something like:
sp.factor((a/(a+b)), a)
I would call this "distributing the numerator in the denominator":
>>> a/(a + b)
>>> 1/expand(1/_)
1/(1 + b/a)
You can use expand and collect
import sympy as sp
a = sp.Symbol("a")
b = sp.Symbol("b")
expanded = sp.expand(a/(a+b))
collected = sp.collect(expanded, a)
print(collected)
Since I am working on a project involving square roots, I need square roots to be simplified to the max. However, some square roots expressions do not produce the disired result. Please consider checking this example:
>>> from sympy import * # just an example don't tell me that import * is obsolete
>>> x1 = simplify(factor(sqrt(3 + 2*sqrt(2))))
>>> x1 # notice that factoring doesn't work
sqrt(2*sqrt(2) + 3)
>>> x2 = sqrt(2) + 1
>>> x2
sqrt(2) + 1
>>> x1 == x2
False
>>> N(x1)
2.41421356237309
>>> N(x2)
2.41421356237309
>>> N(x1) == N(x2)
True
As you can see, the numbers are actually equal, but numpy can't recognize that because it can't factorize and simplify x1. So how do I get the simplified form of x1 so that the equality would be correct without having to convert them to float ?
Thanks in advance.
When you are working with nested sqrt expressions, sqrtdenest is a good option to try. But a great fallback to use is nsimplify which can be more useful in some situations. Since this can give an answer that is not exactly the same as the input, I like to use this "safe" function to do the simplification:
def safe_nsimplify(x):
from sympy import nsimplify
if x.is_number:
ns = nsimplify(x)
if ns != x and x.equals(ns):
return ns
return x
>>> from sympy import sqrt, sqrtdenest
>>> eq = (-sqrt(2) + sqrt(10))/(2*sqrt(sqrt(5) + 5))
>>> simplify(eq)
(-sqrt(2) + sqrt(10))/(2*sqrt(sqrt(5) + 5)) <-- no change
>>> sqrtdenest(eq)
-sqrt(2)/(2*sqrt(sqrt(5) + 5)) + sqrt(10)/(2*sqrt(sqrt(5) + 5)) <-- worse
>>> safe_nsimplify(eq)
sqrt(1 - 2*sqrt(5)/5) <-- better
On your expression
>>> safe_nsimplify(sqrt(2 * sqrt(2) + 3))
1 + sqrt(2)
And if you want to seek out such expressions wherever they occur in a larger expression you can use
>>> from sympy import bottom_up, tan
>>> bottom_up(tan(eq), safe_nsimplify)
tan(sqrt(1 - 2*sqrt(5)/5))
It might be advantageous to accept the result of sqrtdenest instead of using nsimplify as in
def safe_nsimplify(x):
from sympy import nsimplify, sqrtdenest, Pow, S
if x.is_number:
if isinstance(x, Pow) and x.exp is S.Half:
ns = sqrtdenest(x)
if ns != x:
return ns
ns = nsimplify(x)
if ns != x and x.equals(ns):
return ns
return x
Thanks to Oscar Benjamin, the function I was looking for was sqrtdenest:
>>> from sympy import *
>>> sqrtdenest(sqrt(2 * sqrt(2) + 3))
1 + sqrt(2)
I hope this answer would help other people
I believe that the following should evaluate to 1, but it doesn't. Any hints on how to make it work?
n = Symbol('n')
with assuming(Q.integer(n)):
print(cos(2*pi*n))
>>> from sympy import *
>>> n = Symbol('n', integer=True)
>>> cos(2*pi*n)
1
>>> sin(2*pi*n)
0
I want to add some assumptions in limit.
Suppose 0<x<1, then $$limit_{n \to \infty} x^n = 0$$
from sympy import *
x = var('x, n')
limit(x**n, n, oo)
But I get an error NotImplementedError: Result depends on the sign of sign(log(x)).
Is there some way in sympy to handle this?
EDIT: As pointed out in the comments the solution below fails with the same NotImplementedError as in the question (as of November 2019) saying that the answer depends on sign(log(x)). It seems that this sign problem can not be solved with assuming but only with the positive parameter of Symbol. So one way around the issue is to describe 0 < x < 1 as exp(-y) for y > 0:
from sympy import *
y = Symbol("y", positive=True)
n = Symbol("n")
print(limit(exp(-y)**n, n, oo)) # outputs 0
To assume something you can say:
from sympy.assumptions import assuming, Q
with assuming(...):
see here: http://docs.sympy.org/latest/modules/assumptions/assume.html
In your case:
from sympy import *
from sympy.assumptions import assuming, Q
x, n = symbols("x n")
with assuming(Q.is_true(0 < x), Q.is_true(x <1)):
print(limit(x**n, n, oo))
Can someone help me to find a solution on how to calculate a cubic root of the negative number using python?
>>> math.pow(-3, float(1)/3)
nan
it does not work. Cubic root of the negative number is negative number. Any solutions?
A simple use of De Moivre's formula, is sufficient to show that the cube root of a value, regardless of sign, is a multi-valued function. That means, for any input value, there will be three solutions. Most of the solutions presented to far only return the principle root. A solution that returns all valid roots, and explicitly tests for non-complex special cases, is shown below.
import numpy
import math
def cuberoot( z ):
z = complex(z)
x = z.real
y = z.imag
mag = abs(z)
arg = math.atan2(y,x)
return [ mag**(1./3) * numpy.exp( 1j*(arg+2*n*math.pi)/3 ) for n in range(1,4) ]
Edit: As requested, in cases where it is inappropriate to have dependency on numpy, the following code does the same thing.
def cuberoot( z ):
z = complex(z)
x = z.real
y = z.imag
mag = abs(z)
arg = math.atan2(y,x)
resMag = mag**(1./3)
resArg = [ (arg+2*math.pi*n)/3. for n in range(1,4) ]
return [ resMag*(math.cos(a) + math.sin(a)*1j) for a in resArg ]
You could use:
-math.pow(3, float(1)/3)
Or more generally:
if x > 0:
return math.pow(x, float(1)/3)
elif x < 0:
return -math.pow(abs(x), float(1)/3)
else:
return 0
math.pow(abs(x),float(1)/3) * (1,-1)[x<0]
You can get the complete (all n roots) and more general (any sign, any power) solution using:
import cmath
x, t = -3., 3 # x**(1/t)
a = cmath.exp((1./t)*cmath.log(x))
p = cmath.exp(1j*2*cmath.pi*(1./t))
r = [a*(p**i) for i in range(t)]
Explanation:
a is using the equation xu = exp(u*log(x)). This solution will then be one of the roots, and to get the others, rotate it in the complex plane by a (full rotation)/t.
Taking the earlier answers and making it into a one-liner:
import math
def cubic_root(x):
return math.copysign(math.pow(abs(x), 1.0/3.0), x)
The cubic root of a negative number is just the negative of the cubic root of the absolute value of that number.
i.e. x^(1/3) for x < 0 is the same as (-1)*(|x|)^(1/3)
Just make your number positive, and then perform cubic root.
You can also wrap the libm library that offers a cbrt (cube root) function:
from ctypes import *
libm = cdll.LoadLibrary('libm.so.6')
libm.cbrt.restype = c_double
libm.cbrt.argtypes = [c_double]
libm.cbrt(-8.0)
gives the expected
-2.0
numpy has an inbuilt cube root function cbrt that handles negative numbers fine:
>>> import numpy as np
>>> np.cbrt(-8)
-2.0
This was added in version 1.10.0 (released 2015-10-06).
Also works for numpy array / list inputs:
>>> np.cbrt([-8, 27])
array([-2., 3.])
You can use cbrt from scipy.special:
>>> from scipy.special import cbrt
>>> cbrt(-3)
-1.4422495703074083
This also works for arrays.
this works with numpy array as well:
cbrt = lambda n: n/abs(n)*abs(n)**(1./3)
Primitive solution:
def cubic_root(nr):
if nr<0:
return -math.pow(-nr, float(1)/3)
else:
return math.pow(nr, float(1)/3)
Probably massively non-pythonic, but it should work.
I just had a very similar problem and found the NumPy solution from this forum post.
In a nushell, we can use of the NumPy sign and absolute methods to help us out. Here is an example that has worked for me:
import numpy as np
x = np.array([-81,25])
print x
#>>> [-81 25]
xRoot5 = np.sign(x) * np.absolute(x)**(1.0/5.0)
print xRoot5
#>>> [-2.40822469 1.90365394]
print xRoot5**5
#>>> [-81. 25.]
So going back to the original cube root problem:
import numpy as np
y = -3.
np.sign(y) * np.absolute(y)**(1./3.)
#>>> -1.4422495703074083
I hope this helps.
For an arithmetic, calculator-like answer in Python 3:
>>> -3.0**(1/3)
-1.4422495703074083
or -3.0**(1./3) in Python 2.
For the algebraic solution of x**3 + (0*x**2 + 0*x) + 3 = 0 use numpy:
>>> p = [1,0,0,3]
>>> numpy.roots(p)
[-3.0+0.j 1.5+2.59807621j 1.5-2.59807621j]
New in Python 3.11
There is now math.cbrt which handles negative roots seamlessly:
>>> import math
>>> math.cbrt(-3)
-1.4422495703074083