-
Notifications
You must be signed in to change notification settings - Fork 1
/
PoissonBracketCalc.py
61 lines (52 loc) · 2.01 KB
/
PoissonBracketCalc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
import scipy
import numpy as np
from scipy import linalg, matrix
degFreedom = 3
numInputs = 2*degFreedom
degree=2
dataPoint=np.zeros(numInputs)
dataPoint=dataPoint.reshape(1,numInputs)
poly= PolynomialFeatures(degree)
monomials=poly.fit_transform(dataPoint)
lengthSummands=monomials.size
equation1 = np.random.randn(lengthSummands)
equation1=equation1.reshape(1,lengthSummands)
print(equation1)
equation2 = np.random.randn(lengthSummands)
equation2=equation2.reshape(1,lengthSummands)
print(equation2)
class Memoize:
def __init__(self, fn):
self.fn = fn
self.memo = {}
def __call__(self, *args):
if args not in self.memo:
self.memo[args] = self.fn(*args)
return self.memo[args]
# takes two monomials ( without the c-number coefficient ) and returns the Poisson bracket thereof
# for example gets x^2 p^3 and x^1 p^1
# should return a list with the coefficients []
# to say the Poisson bracket is ..
#@Memoize
def PoisHelper(monomial1,monomial2,numInputs,lengthSummands):
returnFunction=np.zeros(lengthSummands)
totalDegree1=np.sum(monomial1)
totalDegree2=np.sum(monomial2)
if (totalDegree1 == 0 || totalDegree2 == 0):
return returnFunction
elif (totalDegree1 == 1 && totalDegree2 == 1):
# two linear functions so check if they are canonically conjugate or not
# take two expressions given as a list of coefficients and compute the Poisson bracket
def PoisB(equation1,equation2,numInputs,poly):
lengthSummands=equation1.size
returnFunction=np.zeros_like(equation1)
for i in range(lengthSummands):
eq1Coeff = equation1[:,i][0]
eq1PowerList = poly.powers_[i,range(numInputs)]
for j in range(lengthSummands):
eq2Coeff = equation1[:,i][0]
eq2PowerList = poly.powers_[i,range(numInputs)]
returnFunction=returnFunction+eq1Coeff*eq2Coeff*PoisHelper(eq1PowerList,eq2PowerList,numInputs,lengthSummands)
return -1