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
#11
(Jan-20-2022, 12:15 PM)ThiefOfTime Wrote: 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 :)

hmm, it seems that subs method is not working. I double checked and printed. m matrix is coming ok. but when we write

rho_matrix = rho_matrix.subs(cur_symbol, m)
rho_matrix is still rho00 and not the [1,0,0,0....]
Reply
#12
Puh, yeah I get your Problem. it works for one dimensional input, so like single values, because it doesn't effect the dimension of the matrix. But adding a vector changes that of course and since one would be changed into 3 dimensional and the others (at that point) are not, it will not put the changes into affect.
In that case we need a bit more tricky way. The good thing is that Symbol from sympy have a useful equals function and also numpy can work with sympy.
also numpy has a function called where.
so if we convert your lists to numpy arrays instead of Matrices we will be able to search for objects in the other array. after that i would suggest you replace the value in the list objects since arrays in numpy are vectors/matrices and therefore have fix dimensions.
so:
import numpy as np
count = 0
rho_final_arr = np.array(rho_final)
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')
            # i would leave this part as it is, as it is the fastest way to savely do what you are trying to do. also values that are succesfully found in both lists can be overwritten
            rho[row][column]=m
            rho_final[row][column]=m
        else:
            # searching for the symbol in the other list (will be a different object, but the name is the same)
            # also we are avoiding creating lookup tables or iterating through it more than necessary
            i, j = np.where(rho_final_arr == rho[row][column])
            rho[row][column]=m
            rho_final[int(i)][int(j)] = m
            
            
print('rho_final',rho_final[0][6])
i tested it with a small array, that should do the trick :)
quest likes this post
Reply
#13
I think you are way down the rabbit hole of "How do I do this" when I have serious doubts that this is something you need to do. Why do you have 64 rho variables instead of a rho list? If you had a rho list you could reshape it to fit your needs.

Of course I had to give it a try since it looks like a fun problem.
class RefMatrix():
    def __init__(self, ref):
        self.ref = ref
        self.indices = []
        self.rows = len(ref)
        self.columns = len(ref[0])
        self._values = []
        for r in range(self.rows):
            self.indices.append([(r, c) for c in range(self.columns)])
            self._values.append([0]*self.columns)

    def values(self):
        for r, row in enumerate(self.indices):
            for c, index in enumerate(row):
                self._values[r][c] = self.ref[index[0]][index[1]]
        return self._values

    def swapRows(self, a, b):
        self.indices[a], self.indices[b] = self.indices[b], self.indices[a]
        return self

    def swapColumns(self, a, b):
        for row in self.indices:
            row[a], row[b] = row[b], row[a]
        return self

data = [[r*4+c for c in range(4)] for r in range(4)]
ref = RefMatrix(data).swapRows(2, 3).swapColumns(2, 3)
print(data)
print(ref.values())
Output:
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]] [[0, 1, 3, 2], [4, 5, 7, 6], [12, 13, 15, 14], [8, 9, 11, 10]]
quest likes this post
Reply
#14
(Jan-20-2022, 02:34 PM)ThiefOfTime Wrote: Puh, yeah I get your Problem. it works for one dimensional input, so like single values, because it doesn't effect the dimension of the matrix. But adding a vector changes that of course and since one would be changed into 3 dimensional and the others (at that point) are not, it will not put the changes into affect.
In that case we need a bit more tricky way. The good thing is that Symbol from sympy have a useful equals function and also numpy can work with sympy.
also numpy has a function called where.
so if we convert your lists to numpy arrays instead of Matrices we will be able to search for objects in the other array. after that i would suggest you replace the value in the list objects since arrays in numpy are vectors/matrices and therefore have fix dimensions.
so:
import numpy as np
count = 0
rho_final_arr = np.array(rho_final)
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')
            # i would leave this part as it is, as it is the fastest way to savely do what you are trying to do. also values that are succesfully found in both lists can be overwritten
            rho[row][column]=m
            rho_final[row][column]=m
        else:
            # searching for the symbol in the other list (will be a different object, but the name is the same)
            # also we are avoiding creating lookup tables or iterating through it more than necessary
            i, j = np.where(rho_final_arr == rho[row][column])
            rho[row][column]=m
            rho_final[int(i)][int(j)] = m
            
            
print('rho_final',rho_final[0][6])
i tested it with a small array, that should do the trick :)

Thanks for the answer!
I now have this error:
Error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
will find why I am having this and then come back to you. It is working for 7 times. after entering else loop, works one more run and then it stops and gives this error...
Reply
#15
(Jan-20-2022, 06:24 PM)deanhystad Wrote: I think you are way down the rabbit hole of "How do I do this" when I have serious doubts that this is something you need to do. Why do you have 64 rho variables instead of a rho list? If you had a rho list you could reshape it to fit your needs.

Of course I had to give it a try since it looks like a fun problem.
class RefMatrix():
    def __init__(self, ref):
        self.ref = ref
        self.indices = []
        self.rows = len(ref)
        self.columns = len(ref[0])
        self._values = []
        for r in range(self.rows):
            self.indices.append([(r, c) for c in range(self.columns)])
            self._values.append([0]*self.columns)

    def values(self):
        for r, row in enumerate(self.indices):
            for c, index in enumerate(row):
                self._values[r][c] = self.ref[index[0]][index[1]]
        return self._values

    def swapRows(self, a, b):
        self.indices[a], self.indices[b] = self.indices[b], self.indices[a]
        return self

    def swapColumns(self, a, b):
        for row in self.indices:
            row[a], row[b] = row[b], row[a]
        return self

data = [[r*4+c for c in range(4)] for r in range(4)]
ref = RefMatrix(data).swapRows(2, 3).swapColumns(2, 3)
print(data)
print(ref.values())
Output:
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]] [[0, 1, 3, 2], [4, 5, 7, 6], [12, 13, 15, 14], [8, 9, 11, 10]]

thanks for answer,
will also try this way after make other way run correctly. but the main point I am multiplying this rho symbolic matrices with other real matrices. This is why I had to use sympy. Otherwise, it is not possible to multiply a real matrix with a symbolic matrix
Reply
#16
ah ok, i see. the problem is that you are iterating over lists that you alread modified. If I would guess I would say it is this part:
if (rho_final[row][column]==rho[row][column]):
            print('hey')
            # i would leave this part as it is, as it is the fastest way to savely do what you are trying to do. also values that are succesfully found in both lists can be overwritten
            rho[row][column]=m
            rho_final[row][column]=m
well to be more specific the if case. You could try changing it to something like this:
if (not type(rho_final[row][column]) == list and rho_final[row][column]==rho[row][column]):
            print('hey')
            # i would leave this part as it is, as it is the fastest way to savely do what you are trying to do. also values that are succesfully found in both lists can be overwritten
            rho[row][column]=m
            rho_final[row][column]=m
try playing around with it. the problem you are currently having is definately in that par. The
i, j = np.where(rho_final_arr == rho[row][column])
worked for me even when there were lists already present, so I am guessing that this line is the problem
quest likes this post
Reply
#17
(Jan-21-2022, 10:02 AM)ThiefOfTime Wrote: ah ok, i see. the problem is that you are iterating over lists that you alread modified. If I would guess I would say it is this part:
if (rho_final[row][column]==rho[row][column]):
            print('hey')
            # i would leave this part as it is, as it is the fastest way to savely do what you are trying to do. also values that are succesfully found in both lists can be overwritten
            rho[row][column]=m
            rho_final[row][column]=m
well to be more specific the if case. You could try changing it to something like this:
if (not type(rho_final[row][column]) == list and rho_final[row][column]==rho[row][column]):
            print('hey')
            # i would leave this part as it is, as it is the fastest way to savely do what you are trying to do. also values that are succesfully found in both lists can be overwritten
            rho[row][column]=m
            rho_final[row][column]=m
try playing around with it. the problem you are currently having is definately in that par. The
i, j = np.where(rho_final_arr == rho[row][column])
worked for me even when there were lists already present, so I am guessing that this line is the problem

Hello,
I am trying to all possible combination of if and still struggling with the same problem ://. I will try a few more tricks and will tell you
Reply
#18
unfortunately, I still have the same error and I do not understand the reason....

here is my new code:
count = 0
rho_final_arr = np.array(rho_final)
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 (not type(rho_final[row][column]) == list and rho_final[row][column]==rho_i[row][column]):  
        #if (rho_final[row][column]==rho_i[row][column]):
        print(rho_final[row][column],rho_i[row][column])
        if not isinstance(rho_final[row][column], list):
          if (rho_final[row][column]==rho_i[row][column]):
        #if (np.isclose(rho_final[row][column],rho_i[row][column]))==True:
            #print('hey')
            # i would leave this part as it is, as it is the fastest way to savely do what you are trying to do. also values that are succesfully found in both lists can be overwritten
            rho_i[row][column]=m
            rho_final[row][column]=m
          else:
            print('heyo')
            # searching for the symbol in the other list (will be a different object, but the name is the same)
            # also we are avoiding creating lookup tables or iterating through it more than necessary
            i, j = np.where(rho_final_arr == rho_i[row][column])
            rho_i[row][column]=m
            rho_final[int(i)][int(j)] = m
        else:
            print('heyo 2')
            # searching for the symbol in the other list (will be a different object, but the name is the same)
            # also we are avoiding creating lookup tables or iterating through it more than necessary
            i, j = np.where(rho_final_arr == rho_i[row][column])
            rho_i[row][column]=m
            rho_final[int(i)][int(j)] = m    
#print(rho_final_arr)
Reply
#19
Helloooo :)

I solved the problem. It is quite simple:

count = 0
rho_final_arr = np.array(rho_final)
rho_i_arr=np.array(rho_i)
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_arr[row][column]==rho_i_arr[row][column]):
                rho_i[row][column]=m
                rho_final[row][column]=m
        else:    
                print('heyo')
                i, j = np.where(rho_final_arr == rho_i[row][column])
                rho_i[row][column]=m
                rho_final[int(i)][int(j)] = m
ThiefOfTime 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