Python Forum

Full Version: nsimplify, make zero the really small numbers in the matrix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I created a symbolic matrix with sympy:

states = [1,2,3,4,5,6,7,8]
rho = np.array([[sympy.Symbol("rho{}{}".format(i,j)) for j in range(len(states))] for i in range(len(states))])
Here is my matrix
[[rho00 rho01 rho02 rho03 rho04 rho05 rho06 rho07] [rho10 rho11 rho12 rho13 rho14 rho15 rho16 rho17] [rho20 rho21 rho22 rho23 rho24 rho25 rho26 rho27] [rho30 rho31 rho32 rho33 rho34 rho35 rho36 rho37] [rho40 rho41 rho42 rho43 rho44 rho45 rho46 rho47] [rho50 rho51 rho52 rho53 rho54 rho55 rho56 rho57] [rho60 rho61 rho62 rho63 rho64 rho65 rho66 rho67] [rho70 rho71 rho72 rho73 rho74 rho75 rho76 rho77]]
Now, I am multiplying my symbolic matrix and real matrix like that;

p_final=np.matmul(realmat,rho)
#print(p_final)
sympy.nsimplify(p_final,tolerance=1e-10,rational=True)
ALthough, I use nsimplyfy, I am getting this result which is quite annoying

[1.0*rho00 - 1.79380389039135e-16*rho01 - 1.79380389039135e-16*rho10 + 3.21773239718314e-32*rho11
-1.79380389039135e-16*rho00 + 1.0*rho01 + 3.21773239718314e-32*rho10 - 1.79380389039135e-16*rho11
1.0*rho02 - 3.25176795283269e-17*rho03 - 1.79380389039135e-16*rho12 + 5.83303400444119e-33*rho13
-3.25176795283269e-17*rho02 + 1.0*rho03 + 5.83303400444119e-33*rho12 - 1.79380389039135e-16*rho13
1.0*rho04 + 1.01465363575695e-17*rho05 - 1.79380389039135e-16*rho14 - 1.82008963922055e-33*rho15
1.01465363575695e-17*rho04 + 1.0*rho05 - 1.82008963922055e-33*rho14 - 1.79380389039135e-16*rho15
-3.25176795283269e-17*rho06 + 1.0*rho07 + 5.83303400444119e-33*rho16 - 1.79380389039135e-16*rho17
1.0*rho06 - 3.25176795283269e-17*rho07 - 1.79380389039135e-16*rho16 + 5.83303400444119e-33*rho17]
.....

I did not paste whole result but I do not want to see e-17*rho because it is already small or really small. I want to make them 0
Ideally nsimplfy should do that but it did not change the result

btw, my real matrix is here:

[[ 1. -0. 0. 0. 0. 0. 0. 0.]
[-0. 1. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 1. -0. 0. 0. 0. 0.]
[ 0. 0. -0. 1. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 1. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 1. 0. 0.]
[ 0. 0. 0. 0. 0. 0. -0. 1.]
[ 0. 0. 0. 0. 0. 0. 1. -0.]]
How to do that?
hey :)
you are definitely on the right track. That is exactly how it is supposed to be done with nsimplify. Unfortunately I was not able to reproduce the problem in Python 3.10
Which version are you using?
cheers
(Jan-19-2022, 10:51 AM)ThiefOfTime Wrote: [ -> ]hey :)
you are definitely on the right track. That is exactly how it is supposed to be done with nsimplify. Unfortunately I was not able to reproduce the problem in Python 3.10
Which version are you using?
cheers

Hello :)
My python version is 3.9.7
my sympy version is 1.9
That is strange, I tried your versions, with numpy arrays as well as sympy Matrices. Also python in general is converting -0 to 0 except it is a value that is not exactly zero, so like 1.5e-16, but I tried even that. I surely did get what you got for p_final but nsimplify converted everything nicely so my final result was always this Matrix:
Output:
[[rho00, rho01, rho02, rho03, rho04, rho05, rho06, rho07], [rho10, rho11, rho12, rho13, rho14, rho15, rho16, rho17], [rho20, rho21, rho22, rho23, rho24, rho25, rho26, rho27], [rho30, rho31, rho32, rho33, rho34, rho35, rho36, rho37], [rho40, rho41, rho42, rho43, rho44, rho45, rho46, rho47], [rho50, rho51, rho52, rho53, rho54, rho55, rho56, rho57], [rho70, rho71, rho72, rho73, rho74, rho75, rho76, rho77], [rho60, rho61, rho62, rho63, rho64, rho65, rho66, rho67]]
So in my opinion you did everything correctly. The only way I see how you got that ouput is from the print function you have in the line above. but since you have it commented out it should interfere. Which numpy version are you using? (probably the same as I am, just making sure)
my numpy version is: 1.20.3 ://
no idea what causes that....
Ok, I found the reason :)))

The thing is that my real matrix is coming from another mathematical process. And when I print my real matrix, I used this line:

np.set_printoptions(precision=3) #"rounding"
so real matrix came as normal
However I should have used this code line
realmat=realmat.round(3)
Because with the previous script, I was printing in a normal shape but I wasn't actually changing the my real matrix. so when I multiply my real matrix and the symbolic matrix, I had this issue

Now it is ok :)
Thanks