Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binomial expansion with fractional powers in Python

Is there a quick method of expanding and solving binomials raised to fractional powers in Scypy/numpy?

For example, I wish to solve the following equation

y * (1 + x)^4.8 = x^4.5

where y is known (e.g. 1.03).

This requires the binomial expansion of (1 + x)^4.8.

I wish to do this for millions of y values and so I'm after a nice and quick method to solve this.

I've tried the sympy expand (and simplification) but it seems not to like the fractional exponent. I'm also struggling with the scipy fsolve module.

Any pointers in the right direction would be appreciated.

EDIT:

By far the simplest solution I found was to generate a truth table (https://en.wikipedia.org/wiki/Truth_table) for assumed values of x (and known y). This allows fast interpolation of the 'true' x values.

y_true = np.linspace(7,12, 1e6)
x = np.linspace(10,15, 1e6)

a = 4.5
b = 4.8
y = x**(a+b) / (1 + x)**b

x_true = np.interp(y_true, y, x)
like image 951
smashbro Avatar asked Dec 21 '25 21:12

smashbro


1 Answers

EDIT: Upon comparing output with that of Woldfram alpha for y=1.03, it looks like fsolve will not return complex roots. https://stackoverflow.com/a/15213699/3456127 is a similar question that may be of more help.

Rearrange your equation: y = x^4.5 / (1+x)^4.8. Scipy.optimize.fsolve() requires a function as its first argument.

Either:

from scipy.optimize import fsolve
import math
def theFunction(x):   
    return math.pow(x, 4.5) / math.pow( (1+x) , 4.8)

for y in millions_of_values:
    fsolve(theFunction, y)

Or using lambda (anonymous function construct):

from scipy.optimize import fsolve
import math
for y in millions_of_values:
    fsolve((lambda x: (math.pow(x, 4.5) / math.pow((1+x), 4.8))), y)
like image 103
Ray Avatar answered Dec 23 '25 15:12

Ray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!