Search code examples
pythonlistnumpynumpy-ndarrayuncertainty

Taking the square root of multidimensional uncertainties uarray


How do I take the square root of a 2D uarray?

For example,

import numpy as np
from uncertainties import unumpy,umath

array=np.array([[2,3],[6,7],[5,3]])
uncertainties=np.array([[2,2],[4,4],[5,5]])
Uarray=unumpy.uarray(array,uncertainties)
print(Uarray)

Output:

[[2.0+/-2.0 3.0+/-2.0]
 [6.0+/-4.0 7.0+/-4.0]
 [5.0+/-5.0 3.0+/-5.0]]

Now when I try to take the square root,

umath.sqrt(Uarray)

I get this error:

"only length-1 arrays can be converted to Python scalars"

What I really am trying to do is take an elementwise square root similar to taking the square root of a 2D NumPy array. I imagine a loop would be used but I am unsure of how to implement it.


Solution

  • Use unumpy.sqrt instead of umath.sqrt.

    >>> unumpy.sqrt(Uarray) 
    array([[1.4142135623730951+/-0.7071067811865475,
            1.7320508075688772+/-0.5773502691896258],
           [2.449489742783178+/-0.8164965809277261,
            2.6457513110645907+/-0.7559289460184544],
           [2.23606797749979+/-1.118033988749895,
            1.7320508075688772+/-1.4433756729740645]], dtype=object)