nrc-cnrc / MetroloPy

Tools for uncertainty propagation and measurement unit conversion — Outils pour la propagation des incertitudes et la conversion d'unités de mesure
https://nrc-cnrc.github.io/MetroloPy
GNU General Public License v3.0
34 stars 6 forks source link

gummy.create() with covariance matrix mutates input matrix #11

Closed nluetts closed 4 years ago

nluetts commented 4 years ago

Hi,

I noticed that gummy.create() mutates its input covariance matrix. This leads to unexpected behavior:

from metrolopy import gummy
import numpy as np

val = [1., 2.]
cov = np.array([[0.3, 0.1], [0.1, 0.2]])

print("cov before gummy creation:\n", cov)
print()

a, b = gummy.create(val, covariance_matrix=cov)

print("cov after gummy creation:\n", cov)
print()
print("a + b after first gummy creation ", a + b)

a, b = gummy.create(val, covariance_matrix=cov)

print("a + b after second gummy creation ", a + b)
cov before gummy creation:
 [[0.3 0.1]
 [0.1 0.2]]

cov after gummy creation:
 [[1.         0.40824829]
 [0.40824829 1.        ]]

a + b after first gummy creation  3.00(84)
a + b after second gummy creation  3.0(17)

I think this line is the problem:

https://github.com/nrc-cnrc/MetroloPy/blob/2230edc95b88bd61b2f66935555cc4669a37af9d/metrolopy/ummy.py#L381

Is the mutation necessary? A workaround is to pass a deepcopy of the covariance matrix.

hvparks commented 4 years ago

Good catch, the matrix should not be mutated. I will change this line to create a new matrix instead.

hvparks commented 4 years ago

Version 0.5.6 has now been released which fixes this bug.