def kepler_trig_expand(s): """ Transform trigonometric part of series s [sin(nM) and cos(nM)] to sum of items like cos(M)^k1 * sin(M)^k2, where n=k1+k2. - s is series of e and M, - n is number of terms. """ from math import ceil from pyranha.math import binomial, cos, sin, degree temp, e, M, ecosM, esinM = 0, epst('e'), epst('M'), epst('ecosM'), epst( 'esinM') n = degree(s).numerator s_list = s.list for i in range(len(s_list)): for j in range(n + 1): trig_cos, trig_sin = 0, 0 # calculate cos(nM): if s_list[i][1] == cos(j * M): for k in range(int(ceil(j / 2)) + 1): trig_cos = trig_cos + (-1)**k * binomial( j, 2 * k) * ecosM**(j - 2 * k) * esinM**(2 * k) * e**-j temp = temp + s_list[i][0] * trig_cos # calculate sin(nM): if s_list[i][1] == sin(j * M): for k in range(int(ceil((j - 1) / 2)) + 1): trig_sin = trig_sin + (-1)**k * binomial( j, 2 * k + 1) * ecosM**(j - 2 * k - 1) * esinM**(2 * k + 1) * e**-j temp = temp + s_list[i][0] * trig_sin if type(temp) != type(1): temp = temp.trim() return temp
def binomial_exp(x, y, r, order): """ Binomial exponentiations. """ from pyranha.math import binomial temp = 0 for k in range(0, order + 1): temp = temp + binomial(r, k) * x**(r - k) * y**k return temp
# Import the pyranha.math submodule. from pyranha import math # Various ways of constructing an int. print(42) print(int("42")) print(int(42.123)) # Arbitrarily large ints are supported directly, # no need to go through a string conversion. print(12345678987654321234567898765432) # Interoperability with float. print(43. - 1) # Truncated division in Python 2.x, floating-point division in Python 3.x. print(85 / 2) # Exponentiation via the '**' operator. print(42**2) # Calculate (42 choose 21) using the binomial() function from the math # submodule. print(math.binomial(42, 21))
# Import the pyranha.math submodule. from pyranha import math # Import the standard Fraction class. from fractions import Fraction as F # Various ways of constructing a rational. print(F(42)) print(F("42")) print(F(1.5)) print(F(42, 12)) print(F("42/12")) # Arithmetics and interoperability with float and int. print(F(42, 13) * 2) print(1 + F(42, 13)) print(43. - F(1, 2)) print(F(85) / 13) print(F(84, 2) == 42) print(42 >= F(84, 3)) # Exponentiation is provided by the standard '**' operator. print(F(42, 13)**2) print(F(42, 13)**-3) # Conversion to int yields the truncated value. print(int(F(10, 3))) # Calculate (42/13 choose 21) via the math::binomial() function. print(math.binomial(F(42, 13), 21))