0

I'm looking for a way to compare all elements in a matrix with another matrix of the same size in a while loop by using Sympy.

I know that singular elements for Sympy matrices can be compared via the following (for example the first element is compared):

import sympy as sp
from sympy.interactive import printing
from sympy import Eq, solve_linear_system, Matrix 

s = Matrix([2, 2])
e = Matrix([1, 1])


while s[0] > e[0]: #This works
    print('Working')
else:
    print('Not working')

But I'm looking to compare all matrix elements with eachother. I tried doing this code but I got an error:

import sympy as sp
from sympy.interactive import printing
from sympy import Eq, solve_linear_system, Matrix 

s = Matrix([2, 2])
e = Matrix([1, 1])


while s > e: #This does not work and gives an error
    print('Working')
else:
    print('Not working')

The error recieved is:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-203350991a18> in <module>
      7 
      8 
----> 9 while s > e: #Works
     10     print('Working')
     11 else:

TypeError: '>' not supported between instances of 'MutableDenseMatrix' and 'MutableDenseMatrix'

Can someone please help guide me in the right direction?

Hendrix13
  • 127
  • 8
  • You can use e.g. `s[0,0] > e[0,0]`. – Oscar Benjamin Jul 05 '21 at 13:27
  • Thank you for that! How does it actually work though? Is each of the zero entries refer to [row, column]? I want to make the code such that all the elements in 's' have to be bigger than the elements in 'e' to continue the while loop. Does that make sense? – Hendrix13 Jul 07 '21 at 07:38
  • Thanks again for that @OscarBenjamin sorry I forgot to @you I want to compare all elements in one matrix to another, I tried this from ur answer: ``` import sympy as sp from sympy.interactive import printing from sympy import Matrix s = Matrix([2, 0]) e = Matrix([1, 1]) while s[0, 0] > e[0, 0]: print('Working') else: print('Not working') ``` Which printed "Working". The 2nd element in matrix e is < the 2nd element in matrix s now and it's still printing "Working" when I want it to print "Not Working". Any advice or do I have to use many "and" stmnts for each element? – Hendrix13 Jul 13 '21 at 03:30
  • You can use a loop with and. – Oscar Benjamin Jul 13 '21 at 12:14
  • @OscarBenjamin thank you for your help! Is there a way I can credit you for helping me answer this question (sorry I'm new to stack overflow so I'm not sure how to give you the proper credit you deserve here). – Hendrix13 Jul 22 '21 at 09:12
  • You can upvote the comment. Perhaps you could post an answer to your own question if you've got this working now – Oscar Benjamin Jul 22 '21 at 11:33
  • @OscarBenjamin, I don't think I have enough reputation to upvote your comment but when I do I'll come back and upvote it. I've just posted the answer though, thank you for your help :) – Hendrix13 Jul 23 '21 at 06:43

1 Answers1

0

It seems that the only way to do this to compare all elements in one matrix to another in Sympy you have to use a while loop. Refer to the code below to see how this works, I've used two matrices with three elements each so that you can see it really works this way regardless of the amount of elements.

import sympy as sp
from sympy.interactive import printing
from sympy import Matrix 

s = Matrix([2, 2, 2])
e = Matrix([1, 1, 1])

while (s[0, 0] >  e[0, 0]) and (s[1,0] >  e[1, 0]) and (s[2,0] >  e[2, 0]): #This works
    print('Working')
    break
else:
    print('Not working')

Hope this helps. Big thanks to @OscarBenjamin for the help in finding this solution! :)

Hendrix13
  • 127
  • 8