matrixsympyelementwise-operationsuncertainty

Writing {value} +- {uncertainty} in matrix. How does that work with sympy?


I have two 6x6 matrices. Here I'll just show the first two rows and columns:

import sympy as sp
values = sp.Matrix([[-0.403900000000000, 0.158730000000000], [-1.52350000000000, -1.87937000000000]])
uncertainties = sp.Matrix([[0.000600000000000000, 0.000280000000000000], [0.000270000000000000, 0.000170000000000000]])

I want to get a matrix where the [0][0] element would look like "-0.4039 +- 0.0006". And this for every element, so that i can export this matrix to latex. How do I do this?

I tried using

f"{values[i][j]} +- {uncertainties[i][j]}" 

for all i and j, but that just gave me value[i][j] - uncertainty[i][j]

I couldn't find this on the internet. Do I need to look into latex for loops? Or can I do that in sympy? I jsut really really don't want to manually put the uncertainty behind every matrix element in multiple matrices.

Thanks!


Solution

  • Matrix cannot hold strings but it can hold Symbols with arbitrary names. Does the following produce the right latex?

    >>> str(Float(.4309).round(4))+' +- 0.006'
    '0.4309 +- 0.006'
    >>> Matrix([[Symbol(_)]])
    Matrix([[0.4309 +- 0.006]])
    

    If so then it's just a matter of knowing what decimal to round to and then putting the Symbols in place. I would probably have a values matrix and an uncertainties matrix and a function that puts them together and outputs the special symbol matrix. The uncertainties package might be of use in creating the strings.