Python Forum
nsimplify, make zero the really small numbers in the matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nsimplify, make zero the really small numbers in the matrix
#1
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?
Reply
#2
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
Reply
#3
(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
Reply
#4
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)
Reply
#5
my numpy version is: 1.20.3 ://
no idea what causes that....
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 790 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,305 May-03-2021, 06:30 AM
Last Post: Gribouillis
  How do I make a symmetric matrix from a column vector? leocsmith 3 3,621 Mar-30-2021, 10:17 AM
Last Post: leocsmith
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,333 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,669 May-09-2019, 12:19 PM
Last Post: Pleiades
  How to extract specific numbers from a matrix? Alberto 0 3,322 Mar-06-2018, 02:52 PM
Last Post: Alberto
  How to make a subtraction within a range of numbers? Alberto 3 10,050 May-08-2017, 09:13 PM
Last Post: ichabod801
  matrix from matrix python numpy array shei7141 1 3,643 Jan-16-2017, 06:10 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020