-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.py
More file actions
238 lines (201 loc) · 9.61 KB
/
Function.py
File metadata and controls
238 lines (201 loc) · 9.61 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from abc import ABC, abstractmethod
from Set import ISet, AffineSpace, MultidimensionalInterval
import numpy as np
from typing import Callable, Union
from multimethod import multimethod
class IFunction(object):
"""This interface models differentiable Functions from ISet to R^n."""
def __init__(self, name: str, domain: ISet):
super().__init__()
self._name = name
self._domain = domain
@abstractmethod
def evaluate(self, point: np.ndarray) -> np.ndarray:
"""Evaluates the function at point.
The parameter "point" is a vector in Double^(Domain.ambient_dimension) such that Domain.is_contained(point)=true."""
pass
@property
def domain(self) -> ISet:
"""The domain of the function, i.e., the set of points at which the function can be evaluated."""
return self._domain
@property
def name(self) -> str:
"""The name of the function, might be used for debugging"""
return self._name
@multimethod
def __add__(self, other: Union[int, float]) -> 'IFunction':
"""Adds the two functions value wise, where the second function is a constant"""
return Function(
name="(" + self.name + ") + " + str(other),
domain=self.domain,
evaluate=lambda v: self.evaluate(v) + other
)
@multimethod
def __add__(self, other: 'IFunction') -> 'IFunction':
"""Adds the two functions value wise"""
return Function(
name="(" + self.name + ") + (" + other.name + ")",
domain=self.domain.intersect(other.domain),
evaluate=lambda v: self.evaluate(v) + other.evaluate(v)
)
@multimethod
def __mul__(self, other: Union[int, float]) -> 'IFunction':
"""Multiplies the function by a scalar"""
return Function(
name=str(other) + " * (" + self.name + ")",
domain=self.domain,
evaluate=lambda v: other * self.evaluate(v)
)
@multimethod
def __mul__(self, other: 'IFunction') -> 'IFunction':
"""Multiplies the two functions value wise"""
# Überprüfen, ob die Eingabedimensionen übereinstimmen
assert self.domain._ambient_dimension == other.domain._ambient_dimension, "The input dimensions of the functions do not match"
# Überprüfen, ob beide Funktionen die Shape (1,) haben
if self.evaluate(self.domain.point()).shape == (1,) and other.evaluate(other.domain.point()).shape == (1,):
return Function(
name="(" + self.name + ") * (" + other.name + ")",
domain=self.domain.intersect(other.domain),
evaluate=lambda v: self.evaluate(v) * other.evaluate(v)
)
# Überprüfen, ob die Spaltenanzahl der ersten Ausgabe mit der Zeilenanzahl der zweiten Ausgabe übereinstimmt
assert self.evaluate(self.domain.point()).shape[1] == other.evaluate(self.domain.point()).shape[0], "The output dimensions of the functions do not match"
return Function(
name="(" + self.name + ") * (" + other.name + ")",
domain=self.domain.intersect(other.domain),
evaluate=lambda v: np.matmul(self.evaluate(v), other.evaluate(v))
)
def __pow__(self, power: int) -> 'IFunction':
"""Take integer exponents of a function"""
return Function(
name="(" + self.name + ")^" + str(power),
domain=self.domain,
evaluate=lambda v: self.evaluate(v)**power
)
def __rmul__(self, other: Union[int, float]):
"""Multiplies the function by a scalar"""
return self.__mul__(other)
def __sub__(self, other: 'IFunction') -> 'IFunction':
"""Subtracts two functions value wise"""
return self + (-1) * other
def Pairing(self, other: 'IFunction') -> 'IFunction':
"""Returns the pairing of two functions"""
return Function(
name="Pair(" + self.name + "," + other.name + ")",
domain=self.domain.intersect(other.domain),
evaluate=lambda v: np.concatenate(
(self.evaluate(v), other.evaluate(v)))
)
def CartesianProduct(self, other: 'IFunction') -> 'IFunction':
"""Returns the cartesian product of two functions"""
proj_domain = AffineSpace(
self.domain._ambient_dimension+other.domain._ambient_dimension)
proj1 = Function.Projection(
domain=proj_domain, l=range(0, self.domain._ambient_dimension))
proj2 = Function.Projection(
domain=proj_domain, l=range(self.domain._ambient_dimension, self.domain._ambient_dimension+other.domain._ambient_dimension))
f1 = Function.FromComposition(
self, proj1)
f2 = Function.FromComposition(
other, proj2)
return Function(
name="CartesianProduct(" + self.name + "," + other.name + ")",
domain=self.domain.cartesian_product(other.domain),
evaluate=lambda v: np.concatenate(
(f1.evaluate(v), f2.evaluate(v)))
)
class Function(IFunction):
"""This class models differentiable Functions from ISet to R^n, where the function and the Jacobian are given by lambdas."""
def __init__(self, name: str, domain: ISet, evaluate: Callable[[np.ndarray], np.ndarray]):
""" Construct a function from lambdas, for the function itself and for its derivatives
Sadly, the type system is not strict enough to check sizes of tensors"""
super().__init__(name=name, domain=domain)
self._evaluate = evaluate
def evaluate(self, point: np.ndarray) -> np.ndarray:
return self._evaluate(point)
@ classmethod
def FromComposition(cls, f: IFunction, g: IFunction) -> IFunction:
"""Constructs f ° g"""
return cls(
name="(" + f.name + ") ° (" + g.name + ")",
domain=g.domain,
evaluate=lambda v: f.evaluate(g.evaluate(v))
)
@ classmethod
def LinearMapFromMatrix(cls, A: np.array) -> IFunction:
"""Constructs x -> A*x"""
return cls(
name="linear",
domain=AffineSpace(A.shape[1]),
evaluate=lambda x: np.matmul(A, x)
)
@classmethod
def TranslationByVector(cls, v: np.array) -> IFunction:
"""Constructs x -> x+v"""
n = v.shape[0]
return cls(name="translation", domain=AffineSpace(n), evaluate=lambda x: x+v)
@classmethod
def Projection(cls, domain: ISet, l: list[int]) -> IFunction:
return cls(
name="projection("+str(l)+")",
domain=domain,
evaluate=lambda x: x[l]
)
@classmethod
def Identity(cls, domain: ISet) -> IFunction:
n = domain._ambient_dimension
return cls(
name="Id(n)",
domain=domain,
evaluate=lambda x: x
)
@classmethod
def Debug(cls, f: IFunction) -> IFunction:
"""Returns a modified function that prints its inputs and outputs"""
def modified_evaluate(x):
result = f.evaluate(x)
print(f"{f.name} - Input: {x}, Output: {result}")
return result
return cls(name=f.name, domain=f.domain, evaluate=modified_evaluate)
### Aufgabe 4.2: Implemetiere 10 weitere Funktionen: sin, cos, tan, exp, log, sqrt, sigmoid, heaviside, square, cube
@classmethod
def sin(cls, dimension: int) -> IFunction:
"""Returns a sinus function"""
return cls(name="sinus", domain=AffineSpace(dimension), evaluate=lambda x: np.sin(x))
@classmethod
def cos(cls, dimension: int) -> IFunction:
"""Returns a cosinus function"""
return cls(name="cosinus", domain=AffineSpace(dimension), evaluate=lambda x: np.cos(x))
@classmethod
def tan(cls, dimension: int) -> IFunction:
"""Returns a tan function"""
return cls(name="tan", domain=AffineSpace(dimension), evaluate=lambda x: np.tan(x))
@classmethod
def exp(cls, dimension: int) -> IFunction:
"""Returns a exponential function"""
return cls(name="exp", domain=AffineSpace(dimension), evaluate=lambda x: np.exp(x))
@classmethod
def log(cls, dimension: int) -> IFunction:
"""Returns a logarithm function"""
return cls(name="log", domain=AffineSpace(dimension), evaluate=lambda x: np.log(x))
@classmethod
def sqrt(cls, dimension: int) -> IFunction:
"""Returns a square root function"""
return cls(name="sqrt", domain=AffineSpace(dimension), evaluate=lambda x: np.sqrt(x))
@classmethod
def sigmoid(cls, dimension: int) -> IFunction:
"""Returns a sigmoid function"""
return cls(name="sigmoid", domain=AffineSpace(dimension), evaluate=lambda x: 1/(1+np.exp(-x)))
@classmethod
def square(cls, dimension: int) -> IFunction:
"""Returns a square function"""
return cls(name="square", domain=AffineSpace(dimension), evaluate=lambda x: x**2)
@classmethod
def cube(cls, dimension: int) -> IFunction:
"""Returns a cube function"""
return cls(name="cube", domain=AffineSpace(dimension), evaluate=lambda x: x**3)
@classmethod
def arccos(cls, dimension: int) -> IFunction:
"""Returns a arccos function"""
return cls(name="arccos", domain=AffineSpace(dimension), evaluate=lambda x: np.arccos(x))
### Ende Aufgabe 4.2