diff --git a/gext/fitting.py b/gext/fitting.py index 84bac2d37945e47be00f016d34634f6f1d4ad11c..0301c05c3ce3cfd311e7f1b58c23954a543352c3 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):