Skip to content

Commit

Permalink
When combinging k matrices, the right hand matrix now overwrites the …
Browse files Browse the repository at this point in the history
…left one. Solves glotaran#605
  • Loading branch information
joernweissenborn authored and jsnel committed Mar 28, 2021
1 parent dc0bdeb commit a7bdee3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions glotaran/builtin/models/kinetic_image/k_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def combine(self, k_matrix: KMatrix) -> KMatrix:
"""
if not isinstance(k_matrix, KMatrix):
raise TypeError("K-Matrices can only be combined with other K-Matrices.")
combined_matrix = {entry: k_matrix.matrix[entry] for entry in k_matrix.matrix}
for entry in self.matrix:
combined_matrix[entry] = self.matrix[entry]
combined_matrix = {entry: self.matrix[entry] for entry in self.matrix}
for entry in k_matrix.matrix:
combined_matrix[entry] = k_matrix.matrix[entry]
combined = KMatrix()
combined.label = f"{self.label}+{k_matrix.label}"
combined.matrix = combined_matrix
Expand Down
26 changes: 26 additions & 0 deletions glotaran/builtin/models/kinetic_image/test/test_k_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,29 @@ def test_unibranched():

print(mat.a_matrix_unibranch(con))
assert np.allclose(mat.a_matrix_unibranch(con), wanted_a_matrix)


def test_combine_matrices():

matrix1 = {
("s1", "s1"): "1",
("s2", "s2"): "2",
}
mat1 = KMatrix()
mat1.label = "A"
mat1.matrix = matrix1

matrix2 = {
("s2", "s2"): "3",
("s3", "s3"): "4",
}
mat2 = KMatrix()
mat2.label = "B"
mat2.matrix = matrix2

combined = mat1.combine(mat2)

assert combined.label == f"{mat1.label}+{mat2.label}"
assert combined.matrix[("s1", "s1")] == mat1.matrix[("s1", "s1")]
assert combined.matrix[("s2", "s2")] == mat2.matrix[("s2", "s2")]
assert combined.matrix[("s3", "s3")] == mat2.matrix[("s3", "s3")]

0 comments on commit a7bdee3

Please sign in to comment.