-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model1D.py
executable file
·85 lines (74 loc) · 1.52 KB
/
Model1D.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python
import random, math
import Dist1D
class Term:
Dist = 0
x = 0.0
s = 0.0
def __init__(self, Dist):
self.Dist = Dist
self.x, self.s = self.Dist.newPoint()
def update(self, scale):
self.s = self.s * scale
self.Dist = Dist1D.Dist1D(self.x, self.s)
self.x, self.s = self.Dist.newPoint()
def calc_coef(self, xin):
y = 0.0
try:
y = self.x * xin
except:
y = 0.0
return y
def calc_exp(self, xin):
y = 0.0
try:
y = self.x ** xin
except:
y = 0.0
return y
def __str__(self):
return str(self.x)
class Model1D:
terms = []
def __init__(self, mean = 0.0, stddev = 1.0, N = 10):
N = int(N)
self.terms = []
self.create_terms(mean, stddev, N)
def create_terms(self, mean, stddev, N):
terms = self.terms
for i in range(N):
Dist = Dist1D.Dist1D(mean,stddev)
T = Term(Dist)
terms.append(T)
def update_terms(self, s):
terms = self.terms
for t in terms:
t.update(s)
def solve(self, x_data):
terms = self.terms
num_terms = len(terms)
y_data = []
for x in x_data:
y = 0.0
b = 1.0
e = 1.0
if (num_terms % 2) == 0:
for i in range(0,num_terms,2):
try:
y = y + terms[i].x * (x ** terms[i+1].x)
except:
y = 0.0
else:
y = y + terms[0].x * x
for i in range(1,num_terms,2):
try:
y = y + terms[i].x * (x ** terms[i+1].x)
except:
y = 0.0
y_data.append(y)
return y_data
def show(self):
terms = self.terms
for i in range(len(terms)):
T = terms[i]
print "T" + str(i) + " :" + str(T)