Python Forum
Write the Matrix2 by using Matrix1
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write the Matrix2 by using Matrix1
#1
I have this matrix1 which has free values(means that not have numbers but have variables):
[[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]]
here, we totally have 64 elements in the matrix since it is 8*8.
And first element is rho00=[1,0,0,0,0...0] #1*64 matrix
second element is rho01=[0,1,0,0,0...0] #1*64 matrix
last element is rho77=[0,0,0.......1] #1*64 matrix
my matrix2 is here:

[[rho00 rho01 rho02 rho03 rho04 rho05 rho07 rho06]
 [rho10 rho11 rho12 rho13 rho14 rho15 rho17 rho16]
 [rho20 rho21 rho22 rho23 rho24 rho25 rho27 rho26]
 [rho30 rho31 rho32 rho33 rho34 rho35 rho37 rho36]
 [rho40 rho41 rho42 rho43 rho44 rho45 rho47 rho46]
 [rho50 rho51 rho52 rho53 rho54 rho55 rho57 rho56]
 [rho70 rho71 rho72 rho73 rho74 rho75 rho77 rho76]
 [rho60 rho61 rho62 rho63 rho64 rho65 rho67 rho66]]
in the second matrix some values are changed.
If first matrix and second matrix has same element in the same index, then I can directly write the same row matrix to matrix2
For instance, matrix1[0][0]=rho00 and matrix2[0][0]=rho00, so these element of matrix2 will be the same of matrix1 and it will be [1,0,0,0........]
However matrix1[0][6]=rho06 and matrix2[0][6]=rho07. Here matrix2[0][6]=rho07 and I should put value of rho07 of matrix1 to matrix2

If the matrix elements are same, I could write the code but if they are not same I could not complete the algorithm
Here is my code
count = 0
for row in range(8):
    for column in range(8):
        if row==0 and column==0: count = 0
        else:    
            count+=1
        if count == 64: count = 63
        m = np.zeros((1,64))                         
        m[0][count]=1 
        
        if (rho_final[row][column]==rho[row][column]):
            #print('hey')
            rho[row][column]=m
            rho_final[row][column]=m
        else:
           #######here is the problem
            print('*',rho[row][column])
            print('**',rho_final[row][column])
            m = np.zeros((1,64))
            m[0][count]=1 
            rho_final[row][column]=rho[row][column]
           
print('rho_final',rho_final[0][6])
And I created this part with this code block
for row in range(8):
    for column in range(8):
        m = np.zeros((1,64))                         
        if (rho_final[row][column]==rho[row][column]):
            #print('hey')
            rho[row][column]=m
            rho_final[row][column]=m
Reply
#2
Let me get this straight. You got two matrices using the same sympy variables and you want to fill the tables now and assigning every variable a 1x64 matrix. so if matrix_a[0][0] == matrix_b[0][0] then matrix_a[0][0] = [1,0,.....,0] and matrix_b[0][0] = [1,0,......,0] and if not matrix_a[0][0] == matrix_b[0][0] then matrix_a[0][0] = [1,0,.....,0] and matrix_b[0][0] = [0, 1,0,......,0] since matrix_a would recieve the value of rho00 and matrix_b the value of (for example) rho10, right?
if that is the case the Matrix objects (well for example and those are what you want to use) can be handy as they have the subs method where you can assign a variable a value. so you can do that for both matrices with the same values for the same variables. Since it can also accept dictionaries you will have quite the easy job and do not have to iterate through everything :)
take a short look here:
https://stackoverflow.com/questions/3293...n-in-sympy
Reply
#3
exactly you got it right !!!!
I will check the example and will try to do something. Will let you know !
ah but there is a small problem

my matrix1 =[[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]]
and I am multiplying this matrix with a real matrix and then I am actually getting this:
[[1.0*rho00 1.0*rho01 1.0*rho02 1.0*rho03 1.0*rho04 1.0*rho05 1.0*rho07
  1.0*rho06]
 [1.0*rho10 1.0*rho11 1.0*rho12 1.0*rho13 1.0*rho14 1.0*rho15 1.0*rho17
  1.0*rho16]
 [1.0*rho20 1.0*rho21 1.0*rho22 1.0*rho23 1.0*rho24 1.0*rho25 1.0*rho27
  1.0*rho26]
 [1.0*rho30 1.0*rho31 1.0*rho32 1.0*rho33 1.0*rho34 1.0*rho35 1.0*rho37
  1.0*rho36]
 [1.0*rho40 1.0*rho41 1.0*rho42 1.0*rho43 1.0*rho44 1.0*rho45 1.0*rho47
  1.0*rho46]
 [1.0*rho50 1.0*rho51 1.0*rho52 1.0*rho53 1.0*rho54 1.0*rho55 1.0*rho57
  1.0*rho56]
 [1.0*rho70 1.0*rho71 1.0*rho72 1.0*rho73 1.0*rho74 1.0*rho75 1.0*rho77
  1.0*rho76]
 [1.0*rho60 1.0*rho61 1.0*rho62 1.0*rho63 1.0*rho64 1.0*rho65 1.0*rho67
  1.0*rho66]]
instead of getting directly this:

[[rho00 rho01 rho02 rho03 rho04 rho05 rho07 rho06]
 [rho10 rho11 rho12 rho13 rho14 rho15 rho17 rho16]
 [rho20 rho21 rho22 rho23 rho24 rho25 rho27 rho26]
 [rho30 rho31 rho32 rho33 rho34 rho35 rho37 rho36]
 [rho40 rho41 rho42 rho43 rho44 rho45 rho47 rho46]
 [rho50 rho51 rho52 rho53 rho54 rho55 rho57 rho56]
 [rho70 rho71 rho72 rho73 rho74 rho75 rho77 rho76]
 [rho60 rho61 rho62 rho63 rho64 rho65 rho67 rho66]]
SO now it is a bit more complicated since 1*rho00 is not equal rho00 in terms of walking the matrixes by for loop
idk what can I do now.. I am checking your link but still I am confused
Reply
#4
Well it is quite easy, you actually even answered it yourself in the other thread you opened :D
https://python-forum.io/thread-36127.html
sympy.nsimplify should remove the 1.0 from each symbol since it is redundant.
quest likes this post
Reply
#5
yess thanks!

and I am now at my starting point and thinking for the other part of the code.. I don't understand subs method - I mean I do not think it is really helpful, it is the same effort for me- and I did not like its structure too

anyway 2 problem has been solved already thanks to you
And I need to write this else structure..

count = 0
for row in range(8):
    for column in range(8):
        if row==0 and column==0: count = 0
        else:    
            count+=1
        if count == 64: count = 63
        m = np.zeros((1,64))                         
        m[0][count]=1 
        
        if (rho_final[row][column]==rho[row][column]):
            print('hey')
            rho[row][column]=m
            rho_final[row][column]=m
        else:
            print(''it is a big question mark')
           
print('rho_final',rho_final[0][6])
Reply
#6
If assigning the 3 Dimension of the Matrix is all you want to do then look at my first post in this thread again. You have matrices with symbols, so now you can assign values to those symbols. I would suggest you do following (assuming that both matrices have the same symbols, just maybe in different places:

import itertools
import numpy as np

all_symbols = list(itertools.chain(*rho)) #creating a 1 dimensional list with all your symbols 
# i know you had a list comprehension in the other thread where you created all symbols, maybe use this to create this new list, so you have the right order
count = 0
rho_matrix = sympy.Matrix(rho)
rho_final_matrix = sympy.Matrix(rho_final)
for current_symbol in all_symbols:
    new_dimension_entry = np.zeros((1, 64))
    new_dimension_entry[0][count] = 1
    rho_matrix.subs(current_symbol, new_dimension_entry)
    rho_final_matrix.subs(current_symbol, new_dimension_entry)
    count += 1
if you really want to stick to the way you already went, then this would be my suggestion for your else part:
#somewhere before your loops
rho_matrix = sympy.Matrix(rho)
rho_final_matrix = sympy.Matrix(rho_final)
for row in range(8):
    ...
    # this would be the save way for your if case, though as you can see the if and else are identical, so you may as well don't do an if-case
    # you surely can search the other matrix for the same variable, but that would be worse in regards of runtime. Especially mixing numpy.array and normal loops is really not good and you really need to avoid that
    if (rho_final[row][column]==rho[row][column]):
        print('hey')
        cur_symbol = rho[row][column]
        rho_matrix.sub(cur_symbol, m)
        rho_final_matrix.sub(cur_symbol, m)
    else:
        cur_symbol = rho[row][column]
        rho_matrix.sub(cur_symbol, m)
        rho_final_matrix.sub(cur_symbol, m)
# if you need it as list type, do this after your loop
rho = list(rho_matrix)
rho_final = list(rho_final_matrix)
quest likes this post
Reply
#7
Hello
Thanks a lot for helps
I am now trying to code and got this error:

Error:
'MutableDenseMatrix' object has no attribute 'sub'
WIll solve this problem and then will inform you
Reply
#8
So sorry, I mistyped it, the method is not called sub but is called subs:
rho_matrix.subs(cur_symbol, m)
for example
quest likes this post
Reply
#9
ah okay :)

I am trying to print elements of matrices but they are still co;ins as r00, r01.(I was expecting to see the real values like [1,0,0,0,0,0.....] instead of rho00,rho01) Is there any specific script to print them?
Reply
#10
Also there fail on my part, so sorry. I forgot to mention that subs is returning a new matrix. so in order to have it saved in your old variable you would need to do
rho_matrix = rho_matrix.subs(cur_symbol, m)
There is no special print function for that, the normal one will do fine :)
quest likes this post
Reply


Forum Jump:

User Panel Messages

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