1

So I am very unexperienced with Python, I know basically nothing, and our teacher gave us the task to write a code that makes a partial fraction decomposition with this function:

function

I don't really know how to start or even how to define that function. I tried this at first: `

def function(x):
    a = (x^4)-(3*x^2)+x+5
    b = (x^11)-(3*x^10)-(x^9)+(7*x^8)-(9*x^7)+(23*x^6)-(11*x^5)-(3*x^4)-(4*x^3)-(32*x^2)-16
    return a/b

But our maths script says that we need to split up the denominator and then make a system of equations out of it and solve it. how to split it up So I was thinking about defining each part of the function itself and then make a function somehow like a = 7*x and use it like f(x) = b/a^7 if this works but I don't really know. We are unfortunately not allowed to use "apart" which I think is a sympy-function?

Thank you so much in advance!

Sincerely, Phie

Addition: So after a few hours of trying I figured this. But I am very sure that this is not the way to do it. Also it tells me that variable l is not defined in z and I am sure that all the others aren't as well. I don't know what to do.

def function(x):
global a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v
a = (x^4)-(3*x^2)+x+5

b = 11
c = 10
d = 9
e = 8
f = 7
g = 6
h = 5
i = 4
j = 3
k = 2
l = x**b
m = 3*x**c
n = x**d
o = 7*x**e
p = 9*x**f
q = 23*x**g
r = 11*x**h
s = 3*x**i
t = 4*x**j
u = 32*x**k
v = 16

return a/(l-m-n+o-p+q-r-s-t-u-v)

print("We are starting the partial fraction decomposition with this 
function: (x^4)-(3*x^2)+x+5 / (x^11)-(3*x^10)-(x^9)+(7*x^8)-(9*x^7)+ 
(23*x^6)-(11*x^5)-(3*x^4)-(4*x^3)-(32*x^2)-16")

z = l-m-n+o-p+q-r-s-t-u-v
while c >= 0:
    c = c-1
    z = z-l

while d >= 0:
    d = d-1
    z = z-m

while e >= 0:
    e = e-1
    z = z-n 

while f >= 0:
    f = f-1
    z = z+o 

while g >= 0:
    g = g-1
    z = z-p

while h >= 0:
    h = h-1
    z = z+q

while i >= 0:
    i = i-1
    z = z-r

while j >= 0:
    j = j-1
    z = z-s

while k >= 0:
    k = k-1
    z = z-t

print(z)
Phie Phie
  • 11
  • 1
  • 3
  • 4
    Can you share the actual task (maybe with inputs and desired outputs)? I don't think your teacher wants you to write code that only works for one input (that function) but maybe something more generic... – AKX Dec 18 '20 at 09:01
  • 3
    Also, remember that `^` is the XOR operation in Python, not the power operation (which is `**`) – AKX Dec 18 '20 at 09:01
  • Well the actual task is in german but here is what it says: Split up this rational function (from the picture above) in partial fractions, without using the command "apart". Note: You can use the commands "roots" and "solve_undetermined_coeffs". So I think it's okay to make it only work with this function?? But I'm not quite sure. Also I just looked up what XOR is and I didn't mean that, I meant power (x²). Sorry for the confusion. – Phie Phie Dec 18 '20 at 09:30
  • okay it can be specific:) i just asked @AKX (forgot to tag you so you would see it) – Phie Phie Dec 18 '20 at 10:21

1 Answers1

0

Since I just solved this myself, here's some input:

Let poly = function() for your function, although be careful to replace ^ with **. Include both from sympy import * and from sympy.abc import a, b, c, d, e, f, g, h, i, j, k, x.

Using factor(exp) you can find all the roots of your function, use these to define the 11 terms term_1 = a/(x-2), term_2 = b/(x2-)**2, ... , term_6 = (f*x + g)/(x**2 +1), ..., term_8 = (j*x + k)/(x**2 + 1) (you get the gist). Define your_sum = term_1 + ... + term_8, eq = Eq(your_sum, poly)

Define the variable your_sum = sum(term_1, ..., term_8), and use solve_undetermined_coeffs(eq, [a,b, ..., k], x))) to get the result.