Is there a way to do fraction in sympy - python

I am trying to do fraction using sympy, I know sympy is supports symbolic but can it be done with old fraction, please know that I want it to be display it as unevaluatedexpr here is my code.
from sympy import *
s = (3)/(2) + (4) / (6)
display(s)
init_printing()

>>> import sympy
>>> a = sympy.Rational(3, 2)
>>> b = sympy.Rational(4, 6)
>>> a
3/2
>>> b
2/3

Alhamdulilaah I found it how to do it this is how you do it
from sympy import *
r = Rational(3,2)+Rational(4,2)
display(r)

Related

How to factorize fraction by a given symbol?

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)

Error while trying to use Sympy for the Law of Supply and Demand in Python

I am trying to solve an exercise about the Law of Supply and Demand
I have the following equations:
Supply: S(q):(q**2) + β + 10
Demand: D(q):(q - 20)**2
I want to solve it with β values to be from 0 to 10.
I made the following code
import sympy as sy
def S(q, β):
return (q**2) + β + 10
def D(q):
return (q - 20)**2
for i in range(11):
β = i
q = sy.Symbol('q')
eq = sy.Eq(S(q,β), D(q))
q_sol = sy.solve(eq)
p_sol = S(q_sol[0], β)
print(q_sol, p_sol)
But I get the following results instead of a float:
[39/4] 1681/16
[389/40] 168921/1600
[97/10] 10609/100
[387/40] 170569/1600
[193/20] 42849/400
[77/8] 6889/64
[48/5] 2704/25
[383/40] 173889/1600
[191/20] 43681/400
[381/40] 175561/1600
[19/2] 441/4
You are seeing the exact Rational numbers that SymPy can work with. To convert them to Float, use the n or evalf method:
>>> from sympy import Rational
>>> Rational(22, 7)
22/7
>>> _.n()
3.14285714285714

Sympy `factor` and `simplify` not working properly?

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

Assuming that a symbol has an integer value in SymPy

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

Symbolic Integration in Python using Sympy

I want to integrate exp(-(x^2 + y^2)) in python using sympy library.
I could find the integral of exp(-(x^2))
>>> B1 = sympy.exp(-alpha1 * (r1_x**2))
>>> p = integrate(B1,r1_x)
>>> p
pi**(1/2)*erf(alpha1**(1/2)*r1_x)/(2*alpha1**(1/2))
But when I want to try integrate exp(-(x^2 + y^2))
>>> B1 = sympy.exp(-alpha1 * (r1_x**2 + r1_y**2))
>>> p = integrate(B1,r1_x)
>>> p
Integral(exp(-alpha1*(r1_x**2 + r1_y**2)), r1_x)
There is no output and python can't take the integral!
(I am the lead developer of SymPy)
DSM is correct that you can get this to work by calling expand, and that there is no general way to do this (because in general, integrals don't have closed forms).
I just wanted to point out that if SymPy cannot do an integral that does have a closed form, we consider this a bug, and you should feel free to report it at http://code.google.com/p/sympy/issues.
sympy doesn't always recognize every form, and so sometimes you have to give it a little help:
>>> import sympy
>>> alpha1, r1_x, r1_y = sympy.var("alpha1 r1_x r1_y")
>>> B1 = sympy.exp(-alpha1 * (r1_x**2 + r1_y**2))
>>> B1.integrate(r1_x)
Integral(exp(-alpha1*(r1_x**2 + r1_y**2)), r1_x)
>>> B1.expand(alpha1)
exp(-alpha1*r1_x**2)*exp(-alpha1*r1_y**2)
>>> B1.expand(alpha1).integrate(r1_x)
sqrt(pi)*exp(-alpha1*r1_y**2)*erf(sqrt(alpha1)*r1_x)/(2*sqrt(alpha1))

Categories

Resources