Check equality using conditions sympy - python

I want to proove that (x/a)^2 + (y/b)^2 + (z/c)^2 == 1, if conditions x/a + y/b + z/c ==1 and a/x + b/y + c/z == 0 are given. I know that, for example, in Maple I can simply write
eq1 := x/a + y/b + z/c = 1;
eq2 := a/x + b/y + c/z = 0;
f := x^2/a^2 + y^2/b^2 + z^2/c^2 = 1;
simplify(lhs(f)-rhs(f), {eq1, eq2});
But I'm struggling to come up with solution using sympy.

Without loss of generality, let x <= x/a, etc...
>>> e1=Eq(x + y + z , 1)
>>> e2=Eq(1/x+1/y+1/z , 0)
>>> e3=Eq(x**2 + y**2 + z**2, 1)
Eq(x**2 + y**2 + z**2, 1)
>>> [e3.subs(i).expand() for i in solve((e1,e2))]
[True, True]
Thus, e3 is true for all values that satisfy e1 and e2

Related

How to multiply all terms of a fraction by something in sympy?

I have this equation:
u = -J/4 + (J*exp(-J*β) + bz*(-exp(bz*β) + exp(-bz*β)))/(exp(bz*β) + 1 + exp(-bz*β) + exp(-J*β))
The fraction part is this:
(J*exp(-J*β) + bz*(-exp(bz*β) + exp(-bz*β)))/(exp(bz*β) + 1 + exp(-bz*β) + exp(-J*β))
I'm trying to multiply the fraction by exp(-bz*β) in both the numerator and the denominator to get this exactly
u = -J/4 + (J*exp(-(J+bz)*β) + bz*(-1 + exp(-2*bz*β)))/(1 + exp(-bz*β) + exp(-2*bz*β) + exp(-(J+bz)*β))
How do I do it?
epath might be able to help with this but I would just take the expression apart and put it back together:
>>> eq.args
(-J/4, (J*exp(-J*ß) + bz*(-exp(bz*ß) + exp(-bz*ß)))/(exp(bz*ß) + 1 + exp(-bz*ß)
+ exp(-J*ß)))
>>> j, f = _
>>> c = exp(-bz*β)
>>> n, d = fraction(f)
>>> powsimp(expand(c*n))/(powsimp(expand(c*d)))
(J*exp(-ß*(J + bz)) - bz + bz*exp(-2*bz*ß))/(exp(-J*ß - bz*ß) + 1 + exp(-bz*ß) +
exp(-2*bz*ß))
>>> collect(_,bz)
(J*exp(-ß*(J + bz)) + bz*(-1 + exp(-2*bz*ß)))/(exp(-J*ß - bz*ß) + 1 + exp(-bz*ß)
+ exp(-2*bz*ß))
>>> factor_terms(_)
(J*exp(-ß*(J + bz)) + bz*(-1 + exp(-2*bz*ß)))/(1 + exp(-ß*(J + bz)) + exp(-bz*ß)
+ exp(-2*bz*ß))
>>> j + _
-J/4 + (J*exp(-ß*(J + bz)) + bz*(-1 + exp(-2*bz*ß)))/(1 + exp(-ß*(J + bz)) + exp
(-bz*ß) + exp(-2*bz*ß))
Note the use of expand(c*n)/expand(c*d) -- without expansion, the c will cancel out automatically in the ratio.

Large Kartusba multiplication is failing. What is possibly going wrong?

I have implemented Karatsuba multiplication in Python.
The code and pseudocode are as follow:
def karatsuba(x, y):
"""
Input: two n-digit positive integers x and y.
Output: the product x·y.
Assumption: n is a power of 2. (NOT assuming this)
if n =1 then // base case
compute x·y in one step and return the result
else
a,b := first and second halves of x
c,d := first and second halves of y
compute p := a + b and q := c + d using grade-school addition
recursively compute ac := a·c, bd := b·d, and pq := p·q
compute adbc := pqacbd using grade-school addition
compute 10n ·ac + 10n/2 ·adbc + bd using grade-school addition and return the result
"""
str_x = str(x)
str_y = str(y)
if len(str_x) == 1 or len(str_y) == 1:
return x*y
else:
n = max(len(str_x), len(str_y))
n_half = int(n / 2)
a, b = x // 10**n_half, x % 10**n_half
c, d = y // 10**n_half, y % 10**n_half
p = a + b
q = c + d
ac = karatsuba(a, c)
bd = karatsuba(b, c)
pq = karatsuba(p, q)
adbc = pq - ac - bd
return 10**(2*n_half) * ac + 10**n_half * adbc + bd
When I do the multiplication of very large numbers, my code is failing.
Here is an example:
>>> a = 3141592653589793238462643383279502884197169399375105820974944592
>>> b = 2718281828459045235360287471352662497757247093699959574966967627
>>> mul_karatsuba = karatsuba(a, b)
>>> mul_builtin = a * b
>>> mul_karatsuba == mul_builtin
False
>>> mul_karatsuba
8945653667798941823160787739573304887842903322205621858854691873536479127635014727457078833467629169552143732979528067839403974
>>> mul_builtin
8539734222673567065463550869546574495034888535765114961879601127067743044893204848617875072216249073013374895871952806582723184
I am not able to find what exactly is wrong with my code. I have coded everything according to mentioned pseudocode.
Can you please help me?

Custom math function in panda dataframe with two columns

I would like to get this custum function for a panda dataframe to work.
It is a simple function with two inputs
wordCount
imageCount
and supposed to calculate the reading time of a text in a panda dataframe.
c = ImageCount
x = WordCount
(5.717938 + (12.03401 - 5.717938)/(1 + (c /3.579499)^4.092419))* c) + x * 0.0037736111111111113
I tried it in a couple of ways, but could not get it to work properly.
def readingT(df, y="imageCount", x="wordCount"):
readingTimeImage = (5.717938 + (12.03401 - 5.717938)/(1 + (c/3.579499)^4.092419))* c
readingTimeWords = 0.0037736111111111113 * x
return readingTimeImage + readingTimeWords
def readingT2(c="imageCount", w="wordCount"):
return ((5.717938 + (12.03401 - 5.717938)/(1 + (c/3.579499)^4.092419))* c + 0.0037736111111111113 * w)
readingT2.apply(readingT, c="imageCount", w="wordCount")
#Try next
def readingT3(x, y):
(((5.717938 + (12.03401 - 5.717938)/(1 + ( x /3.579499)**4.092419)) * x) + 0.0037736111111111113 * y)
readingT3.apply(lambda x: rule(x["imageCount"], x["wordCount"]), axis = 1)
Every single one of them gives throws out an error.
Cheers in advance for any help.
def f(c, x):
return (5.717938 + (12.03401 - 5.717938)/(1 + (c /3.579499)^4.092419))* c) + x * 0.0037736111111111113
df['reading_time'] = df.apply(lambda x: f(x.imageCount, x.wordCount), axis=1)

Python script to generate gradients not working

I have this python script to generate x,y,z lists and u,v,w lists such that u[i],v[i],w[i] is the gradient vector for point x[i],y[i],z[i].
It doesn't seem to be getting the right values. Does anyone know whats wrong?
from math import *
def coordinates(lst, f, gradx, grady, gradz):
lst = lst[1:-1].split(",")
lst = [float(x.strip()) for x in lst]
xlst = []
ylst = []
zlst = []
ulst = []
vlst = []
wlst = []
for x in lst:
for y in lst:
xlst.append(str(x))
ylst.append(str(y))
zlst.append(str(f(x,y)))
ulst.append(str(gradx(x,y)))
vlst.append(str(grady(x,y)))
wlst.append(str(gradz(x,y)))
string = "xlst=[" + ",".join(xlst) + "]\n" + \
"ylst=[" + ",".join(ylst) + "]\n" + \
"zlst=[" + ",".join(zlst) + "]\n" + \
"ulst=[" + ",".join(ulst) + "]\n" + \
"vlst=[" + ",".join(vlst) + "]\n" + \
"wlst=[" + ",".join(wlst) + "]\n"
return string
lst = "{0, 2, 4, 6, 8, 10}"
# get function in the form f(x,y)=z or here its y^2 - x^2 - z = 0
f = lambda x,y: y**2 - x**2
# get the three gradient functions (df/dx, df/dy, df/dz)
gx = lambda x,y: -2*x
gy = lambda x,y: 2*y
gz = lambda x,y: -1
c = coordinates(lst, f, gx, gy, gz)
print c

generate polynomial in python

I am trying to make a function which can print a polynomial of order n of x,y
i.e. poly(x,y,1) will output c[0] + c[1]*x + c[2]*y
i.e. poly(x,y,2) will output c[0] + c[1]*x + c[2]*y + c[3]*x**2 + c[4]*y**2 + c[5]*x*y
Could you give me some ideas? Maybe itertools?
You could try to start from something like
def poly(x,y,n):
counter = 0
for nc in range(n+1):
for i in range(nc+1):
print "c[", counter, "]",
print " * ", x, "**", i,
print " * ", y, "**", nc-i,
print " + ",
counter += 1
For example
poly("x", "y", 2)
will produce
c[ 0 ] * x ** 0 * y ** 0 + c[ 1 ] * x ** 0 * y ** 1 + c[ 2 ] * x ** 1 * y ** 0 + c[ 3 ] * x ** 0 * y ** 2 + c[ 4 ] * x ** 1 * y ** 1 + c[ 5 ] * x ** 2 * y ** 0 +
Build in ifs, if you want to suppress undesired output.
Since you wanted a functional solution with itertools, here's a one-liner:
import itertools as itt
from collections import Counter
n = 3
xy = ("x", "y") # list of variables may be extended indefinitely
poly = '+'.join(itt.starmap(lambda u, t: u+"*"+t if t else u, zip(map(lambda v: "C["+str(v)+"]", itt.count()),map(lambda z: "*".join(z), map(lambda x: tuple(map(lambda y: "**".join(map(str, filter(lambda w: w!=1, y))), x)), map(dict.items, (map(Counter, itt.chain.from_iterable(itt.combinations_with_replacement(xy, i) for i in range(n+1))))))))))
That would give you
C[0]+C[1]*x+C[2]*y+C[3]*x**2+C[4]*y*x+C[5]*y**2+C[6]*x**3+C[7]*y*x**2+C[8]*y**2*x+C[9]*y**3
Note, the order of coefficients is slightly different. This will work not only for any n, but also for any number of variables (x, y, z, etc...)
Just for laughs
Slightly more generalized:
from itertools import product
def make_clause(c, vars, pows):
c = ['c[{}]'.format(c)]
vp = (['', '{}', '({}**{})'][min(p,2)].format(v,p) for v,p in zip(vars,pows))
return '*'.join(c + [s for s in vp if s])
def poly(vars, max_power):
res = (make_clause(c, vars, pows) for c,pows in enumerate(product(*(range(max_power+1) for v in vars))))
return ' + '.join(res)
then poly(['x', 'y'], 2) returns
"c[0] + c[1]*y + c[2]*(y**2) + c[3]*x + c[4]*x*y + c[5]*x*(y**2) + c[6]*(x**2) + c[7]*(x**2)*y + c[8]*(x**2)*(y**2)"

Categories

Resources