A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
96
Task/Formal-power-series/Python/formal-power-series-1.py
Normal file
96
Task/Formal-power-series/Python/formal-power-series-1.py
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
''' \
|
||||
For a discussion on pipe() and head() see
|
||||
http://paddy3118.blogspot.com/2009/05/pipe-fitting-with-python-generators.html
|
||||
'''
|
||||
|
||||
from itertools import islice
|
||||
from fractions import Fraction
|
||||
from functools import reduce
|
||||
try:
|
||||
from itertools import izip as zip # for 2.6
|
||||
except:
|
||||
pass
|
||||
|
||||
def head(n):
|
||||
''' return a generator that passes through at most n items
|
||||
'''
|
||||
return lambda seq: islice(seq, n)
|
||||
|
||||
def pipe(gen, *cmds):
|
||||
''' pipe(a,b,c,d, ...) -> yield from ...d(c(b(a)))
|
||||
'''
|
||||
return reduce(lambda gen, cmd: cmd(gen), cmds, gen)
|
||||
|
||||
def sinepower():
|
||||
n = 0
|
||||
fac = 1
|
||||
sign = +1
|
||||
zero = 0
|
||||
yield zero
|
||||
while True:
|
||||
n +=1
|
||||
fac *= n
|
||||
yield Fraction(1, fac*sign)
|
||||
sign = -sign
|
||||
n +=1
|
||||
fac *= n
|
||||
yield zero
|
||||
def cosinepower():
|
||||
n = 0
|
||||
fac = 1
|
||||
sign = +1
|
||||
yield Fraction(1,fac)
|
||||
zero = 0
|
||||
while True:
|
||||
n +=1
|
||||
fac *= n
|
||||
yield zero
|
||||
sign = -sign
|
||||
n +=1
|
||||
fac *= n
|
||||
yield Fraction(1, fac*sign)
|
||||
def pluspower(*powergenerators):
|
||||
for elements in zip(*powergenerators):
|
||||
yield sum(elements)
|
||||
def minuspower(*powergenerators):
|
||||
for elements in zip(*powergenerators):
|
||||
yield elements[0] - sum(elements[1:])
|
||||
def mulpower(fgen,ggen):
|
||||
'From: http://en.wikipedia.org/wiki/Power_series#Multiplication_and_division'
|
||||
a,b = [],[]
|
||||
for f,g in zip(fgen, ggen):
|
||||
a.append(f)
|
||||
b.append(g)
|
||||
yield sum(f*g for f,g in zip(a, reversed(b)))
|
||||
def constpower(n):
|
||||
yield n
|
||||
while True:
|
||||
yield 0
|
||||
def diffpower(gen):
|
||||
'differentiatiate power series'
|
||||
next(gen)
|
||||
for n, an in enumerate(gen, start=1):
|
||||
yield an*n
|
||||
def intgpower(k=0):
|
||||
'integrate power series with constant k'
|
||||
def _intgpower(gen):
|
||||
yield k
|
||||
for n, an in enumerate(gen, start=1):
|
||||
yield an * Fraction(1,n)
|
||||
return _intgpower
|
||||
|
||||
|
||||
print("cosine")
|
||||
c = list(pipe(cosinepower(), head(10)))
|
||||
print(c)
|
||||
print("sine")
|
||||
s = list(pipe(sinepower(), head(10)))
|
||||
print(s)
|
||||
# integrate cosine
|
||||
integc = list(pipe(cosinepower(),intgpower(0), head(10)))
|
||||
# 1 - (integrate sine)
|
||||
integs1 = list(minuspower(pipe(constpower(1), head(10)),
|
||||
pipe(sinepower(),intgpower(0), head(10))))
|
||||
|
||||
assert s == integc, "The integral of cos should be sin"
|
||||
assert c == integs1, "1 minus the integral of sin should be cos"
|
||||
58
Task/Formal-power-series/Python/formal-power-series-2.py
Normal file
58
Task/Formal-power-series/Python/formal-power-series-2.py
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
from itertools import islice, tee
|
||||
from fractions import Fraction
|
||||
try:
|
||||
from itertools import izip as zip # for 2.6
|
||||
except:
|
||||
pass
|
||||
|
||||
def pluspower(*powergenerators):
|
||||
for elements in zip(*powergenerators):
|
||||
yield sum(elements)
|
||||
def minuspower(*powergenerators):
|
||||
for elements in zip(*powergenerators):
|
||||
yield elements[0] - sum(elements[1:])
|
||||
def mulpower(fgen,ggen):
|
||||
'From: http://en.wikipedia.org/wiki/Power_series#Multiplication_and_division'
|
||||
a,b = [],[]
|
||||
for f,g in zip(fgen, ggen):
|
||||
a.append(f)
|
||||
b.append(g)
|
||||
yield sum(f*g for f,g in zip(a, reversed(b)))
|
||||
def constpower(n):
|
||||
yield n
|
||||
while True:
|
||||
yield 0
|
||||
def diffpower(gen):
|
||||
'differentiatiate power series'
|
||||
next(gen)
|
||||
for n, an in enumerate(gen, start=1):
|
||||
yield an*n
|
||||
def intgpower(gen):
|
||||
'integrate power series with bounds from 0 to x'
|
||||
yield 0
|
||||
for n, an in enumerate(gen, start=1):
|
||||
yield an * Fraction(1,n)
|
||||
|
||||
|
||||
def sine_cosine_series():
|
||||
def deferred_sin():
|
||||
for i in sinx_temp:
|
||||
yield i
|
||||
def deferred_cos():
|
||||
for i in cosx_temp:
|
||||
yield i
|
||||
|
||||
sinx_result, sinx_copy1 = tee(deferred_sin(), 2)
|
||||
cosx_result, cosx_copy1 = tee(deferred_cos(), 2)
|
||||
|
||||
sinx_temp = intgpower(cosx_copy1)
|
||||
cosx_temp = minuspower(constpower(1), intgpower(sinx_copy1))
|
||||
|
||||
return sinx_result, cosx_result
|
||||
|
||||
sinx, cosx = sine_cosine_series()
|
||||
|
||||
print("cosine")
|
||||
print(list(islice(sinx, 10)))
|
||||
print("sine")
|
||||
print(list(islice(cosx, 10)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue