1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import math
24 from libavg import Point2D
25
27 vec = p2 - p1
28 res = math.atan2(vec.y, vec.x)
29 if res < 0:
30 res += math.pi * 2
31 return res
32
34 return math.sqrt((p.x-q.x)**2 + (p.y-q.y)**2)
35
37 return (p.x-q.x)**2 + (p.y-q.y)**2
38
47
74
75
80
82 """Puts given matrix (2D array) into the Reduced Row Echelon Form.
83 Returns True if successful, False if 'm' is singular.
84 NOTE: make sure all the matrix items support fractions! Int matrix will NOT work!
85 Written by Jarno Elonen in April 2005, released into Public Domain
86 http://elonen.iki.fi/code/misc-notes/affine-fit/index.html"""
87 (h, w) = (len(m), len(m[0]))
88 for y in range(0,h):
89 maxrow = y
90 for y2 in range(y+1, h):
91 if abs(m[y2][y]) > abs(m[maxrow][y]):
92 maxrow = y2
93 (m[y], m[maxrow]) = (m[maxrow], m[y])
94 if abs(m[y][y]) <= eps:
95 raise EquationSingular
96 for y2 in range(y+1, h):
97 c = m[y2][y] / m[y][y]
98 for x in range(y, w):
99 m[y2][x] -= m[y][x] * c
100 for y in range(h-1, 0-1, -1):
101 c = m[y][y]
102 for y2 in range(0,y):
103 for x in range(w-1, y-1, -1):
104 m[y2][x] -= m[y][x] * m[y2][y] / c
105 m[y][y] /= c
106 for x in range(h, w):
107 m[y][x] /= c
108 return m
109
110
112 matrix=[]
113 for coefficients, res in _matrix:
114 newrow = map(float, coefficients + (res,))
115 matrix.append(newrow)
116 matrix = gauss_jordan (matrix)
117 res=[]
118 for col in xrange(len(matrix[0])-1):
119 rows = filter(lambda row: row[col] >= eps, matrix)
120 if len(rows)!=1:
121 raise EquationNotSolvable
122 res.append (rows[0][-1])
123
124 return res
125
126
131
133 return (not(x<=0) and not(x>=0))
134
136 if x<0:
137 return -1
138 elif x==0:
139 return 0
140 else:
141 return 1
142
144 """
145 Moving average implementation.
146 Example:
147 ma = MovingAverage(20)
148 print ma(2)
149 print ma(3)
150 print ma(10)
151 """
153 self.__points = points
154 self.__values = []
155
157 self.__values = (self.__values + [value])[-self.__points:]
158
160 sum = reduce(lambda a,b:a+b, self.__values)
161 return float(sum) / len(self.__values)
162
164 self.__appendValue(value)
165 return self.__getAverage()
166