Skip to content

Commit

Permalink
Update least squares
Browse files Browse the repository at this point in the history
  • Loading branch information
duembgen committed Jan 20, 2024
1 parent 9589b87 commit 7b7c68c
Showing 1 changed file with 1 addition and 22 deletions.
23 changes: 1 addition & 22 deletions poly_matrix/least_squares_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def get_B_matrix(self, variables, output_type="csc"):
def get_Q(self):
if self.Q is None:
self.Q = self.B.transpose().multiply(self.B)
self.Q.symmetric = True
return self.Q

def add_residual(self, res_dict: dict):
Expand All @@ -39,25 +40,3 @@ def add_residual(self, res_dict: dict):
self.B[self.m, key] += val
self.m += 1
return

# old implementation directly constructs Q.
for diag, val in res_dict.items():
# forbid 1-dimensional arrays cause they are ambiguous.
assert np.ndim(val) in [
0,
2,
]
if np.ndim(val) == 0:
self[diag, diag] += val**2
else:
self[diag, diag] += val @ val.T

for off_diag_pair in itertools.combinations(res_dict.items(), 2):
dict0, dict1 = off_diag_pair

if np.ndim(dict1[1]) > 0:
new_val = dict0[1] * dict1[1].T
else:
new_val = dict0[1] * dict1[1]
# new value is an array:
self[dict0[0], dict1[0]] += new_val

0 comments on commit 7b7c68c

Please sign in to comment.