-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionalPro.py
More file actions
135 lines (118 loc) · 3.19 KB
/
Copy pathFunctionalPro.py
File metadata and controls
135 lines (118 loc) · 3.19 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 5 10:47:32 2017
@author: root
"""
from itertools import repeat
def sign(x):
if x>0:
return 1
elif x<0:
return -1
else:
return 0
class StrangeNum:
def __init__(self,t):
self.t=t
def __lt__(self,x):
if isinstance(x,StrangeNum):
return self.t<x.t
return (2*self.t)<sign(x)
def __gt__(self,x):
if isinstance(x,StrangeNum):
return self.t>x.t
return (2*self.t)>sign(x)
def __neg__(self):
return StrangeNum(-1*self.t)
inf=StrangeNum(1)
nan=StrangeNum(0)
class Function:
def __init__(self,basefunc):
self.f=basefunc
def __call__(self,x):
if type(x)==int:
x=float(x)
if type(x)==list:
return [self.f(x_i) for x_i in x]
return self.f(x)
def __add__(self,b):
if type(b) != type(self):
def f(x):
return self.f(x)+b
return Function(f)
def f(x):
return self.f(x)+b(x)
return Function(f)
def __rsub__(self,b):
def f(x):
return self.f(x)-b
return Function(f)
return Function(f)
def __sub__(self,b):
if type(b) != type(self):
def f(x):
return self.f(x)-b
return Function(f)
def f(x):
return self.f(x)-b(x)
return Function(f)
def __div__(self,b):
if type(b) != type(self):
def f(x):
if b==0.0:
return inf
return self.f(x)/b
return Function(f)
def f(x):
if b(x)==0.0:
return inf
return self.f(x)/b(x)
return Function(f)
def __rtruediv__(self,b):
print 'tp'
if type(b) != type(self):
def f(x):
if b==0.0:
return inf
return self.f(x)/b
return Function(f)
def f(x):
if b(x)==0.0:
return inf
return self.f(x)/b(x)
return Function(f)
def __mul__(self,b):
if type(b) != type(self):
def f(x):
return self.f(x)*b
return Function(f)
def f(x):
return self.f(x)*b(x)
return Function(f)
def LagBase(Xvec,x0):
vec=[x_i for x_i in Xvec if x_i!=x0]
f=reduce(lambda x,y:x*y ,map(lambda x_i:Function(lambda x: float(x-x_i) ) , vec))
f=f/f(x0)
return f
def Lagrange_Interpolation(Xvec,Yvec):
f=reduce(lambda x,y:x+y,map(lambda x,y,Xvec:LagBase(Xvec,x)*y, Xvec,Yvec,repeat(Xvec,len(Yvec))))
def cf(x):
x=float(x)
return f(x)
return f
def Lins(Xrange):
b=Xrange[1]
a=Xrange[0]
N=Xrange[2]
interval=1.0*(b-a)/Xrange[2]
try:
Xvec=[a+i*interval for i in range(N+1)]
except:
print Xrange
return Xvec
def Function_Interpolation(f,Xrange=None,Xvec=None):
if not Xvec:
Xvec=Lins(Xrange)
Yvec=f(Xvec)
return Lagrange_Interpolation(Xvec,Yvec)