-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.py
More file actions
44 lines (34 loc) · 990 Bytes
/
functions.py
File metadata and controls
44 lines (34 loc) · 990 Bytes
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
import numpy as N
def ApproximateJacobian(f, x, dx=1e-6):
"""Return an approximation of the Jacobian Df(x) as a numpy matrix"""
try:
n = len(x)
except TypeError:
n = 1
fx = f(x)
Df_x = N.matrix(N.zeros((n,n)))
for i in range(n):
v = N.matrix(N.zeros((n,1)))
v[i,0] = dx
Df_x[:,i] = (f(x + v) - fx)/dx
return Df_x
def AnalyticJacobian(f,x):
Df_x= f(x)
return Df_x
class Polynomial(object):
"""Callable polynomial object.
Example usage: to construct the polynomial p(x) = x^2 + 2x + 3,
and evaluate p(5):
p = Polynomial([1, 2, 3])
p(5)"""
def __init__(self, coeffs):
self._coeffs = coeffs
def __repr__(self):
return "Polynomial(%s)" % (", ".join([str(x) for x in self._coeffs]))
def f(self,x):
ans = self._coeffs[0]
for c in self._coeffs[1:]:
ans = x*ans + c
return ans
def __call__(self, x):
return self.f(x)