From 8b7742960f875040c7d52c4866b8d31b456c0355 Mon Sep 17 00:00:00 2001 From: Zahra Askarpour <Zahra.Askarpour@mathematik.uni-stuttgart.de> Date: Thu, 15 Feb 2024 12:02:03 +0100 Subject: [PATCH] test_fitting works --- gext/fitting.py | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/gext/fitting.py b/gext/fitting.py index 84bac2d..0301c05 100644 --- a/gext/fitting.py +++ b/gext/fitting.py @@ -61,37 +61,35 @@ class DiffFitting(AbstractFitting): def fit(self, vectors: List[np.ndarray], target: np.ndarray): """Given a set of vectors and a target return the fitting coefficients.""" + if len(vectors) == 1: + raise ValueError("DiffFit does not work for one vector") target=target-vectors[-1] VECTORS=[] - print("lenvector", len(vectors)) - if len(vectors)>1: - for i in range(2, len(vectors)+1): - print("lenvector", len(vectors)) - VECTORS.append(vectors[i-2]-vectors[-1]) - print(len(VECTORS)) - matrix = np.array(VECTORS).T - a = matrix.T @ matrix - b = matrix.T @ target - if self.options["regularization"] > 0.0: - a += np.identity(len(b))*self.options["regularization"] - coefficients = np.linalg.solve(a, b) - print("coefficients", coefficients) - return np.array(coefficients, dtype=np.float64) + for i in range(0, len(vectors)+1): + VECTORS.append(vectors[i]-vectors[-1]) + matrix = np.array(VECTORS).T + a = matrix.T @ matrix + b = matrix.T @ target + if self.options["regularization"] > 0.0: + a += np.identity(len(b))*self.options["regularization"] + coefficients = np.linalg.solve(a, b) + print("coefficients", coefficients) + return np.array(coefficients, dtype=np.float64) def linear_combination(self, vectors: List[np.ndarray], coefficients: np. ndarray) -> np.ndarray: """Given a set of vectors (or matrices) and the corresponding coefficients, build their linear combination.""" + if len(vectors) == 1: + raise ValueError("DiffFit does not work for one vector") result = np.zeros(vectors[0].shape, dtype=np.float64) VECTORS_DiffFitting=[] - if len(vectors)>1: - for i in range(2,len(vectors)+1): - VECTORS_DiffFitting.append(vectors[i-2]-vectors[-1]) - for coeff, vector in zip(coefficients, vectors): - result += vector*coeff - result=result+vectors[-1] - print(result.shape) - return result + for i in range(0,len(vectors)+1): + VECTORS_DiffFitting.append(vectors[i]-vectors[-1]) + for coeff, vector in zip(coefficients, VECTORS_DiffFitting): + result += vector*coeff + result=result+vectors[-1] + return result class LeastSquare(AbstractFitting): -- GitLab