Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function matrix
#1
how do you write a function that takes a matrix as a parameter
and for symmetric, skew and others returns respectively 1,-1 and 0?

here is what I have so far but I get errors.



import numpy as np
a=1
b=2
c=3
d=4
e=5
f=6
g=7
h=8
i=9

def a(x):
return np.array([a,b,c], [d,e,f], [g,h,i])
def b(y):
return np.array([a,d,e],[b,e,h],[c,f,i])
if a(x)==b(y):
return 1
if A==(-1)*B:
return -1.
else:
return 0
Reply
#2
What errors did you get? Your function a is declared with argument x, why? it is not used in the function body.
Reply
#3
x invalid syntax, that's the error.
The program is supposed to return 1 if the matrix is symmetric, -1 if is skew symmetric
and 0 in all other cases.

I have also changed a(x) to just x
and b(y) to y.
Reply
#4
Your code should be something like this:

import numpy as np

def is_symmetric(A):
    """Returns True if input matrix is symmetric, False - otherwise.
    
    Parameters
    ==========
    
        :param A: a matrix (2D array, Numpy array or list of lists) to be tested;
        :rtype: bool;
        :returns: True if A == A.T, otherwise - False.
    """
    
    return np.array(A) == np.array(A).T

def is_skew(A):
    """Returns True if input matrix is skew, False - otherwise.
   
    # TODO: Docs needed (You need to accomplish docstring here!)
    """
    
    return np.array(A) == -np.array(A).T

def test_matrix(A):
    """Test matrix for special form
    
    Returns 1 if input matrix is symmetric, -1 if 
    input matrix is skew, 0 - otherwise.
    
    Parameters
    ==========
    
        :param A: # TODO: Docs needed
    
    """
    
    if is_symmetric(A):
        return 1
    # TODO: Additional conditions should be added


if __name__ == '__main__':
    a, b, c, d, e, f, g, h, i = 1, 2, 3, 4, 5, 6, 7, 8, 9
    A = [[a, b, c],
         [d, e, f],
         [g, h, i]]
    print("Testing matrix A: ", test_matrix(A))
You need to complete the code snippet I wrote...
Reply
#5
thanks a lot for your hints.

I have kept your code and changed it a bit, however when I run it, there is a logical error in it
as I always get 1, no matter how the matrix looks.

import numpy as np

 
def is_symmetric(A):
     
    return np.array(A) == np.array(A).T
 
def is_skew(A):
  
     
    return np.array(A) == -np.array(A).T
 
def test_matrix(A):
   

    if is_symmetric(np.any(A)):
        return 1
    if is_skew(np.any(A)):
       return -1
    else:
       return 0
 
 
if __name__ == '__main__':
    a, b, c, d, e, f, g, h, i = 1,2,3,4,5,6,7,8,9
    A = [[a, b, c],
         [d, e, f],
         [g, h, i]]
    print("Testing matrix A: ", test_matrix(A))

import numpy as np
 
  
def is_symmetric(A):
      
    return np.array(A) == np.array(A).T
  
def is_skew(A):
   
      
    return np.array(A) == -np.array(A).T
  
def test_matrix(A):
    
 
    if is_symmetric(np.any(A)):
        return 1
    if is_skew(np.any(A)):
       return -1
    else:
       return 0
  
  
if __name__ == '__main__':
    a, b, c, d, e, f, g, h, i = 1,2,3,4,5,6,7,8,9
    A = [[a, b, c],
         [d, e, f],
         [g, h, i]]
    print("Testing matrix A: ", test_matrix(A))
Reply
#6
(Mar-15-2019, 02:00 PM)mcgrim Wrote: I have kept your code and changed it a bit, however when I run it, there is a logical error in it
as I always get 1, no matter how the matrix looks.

This is because you need to use .all(), e.g. return (np.array(A) == np.array(A).T).all(). Moreover, it would be better to
use floating-point comparison, e.g. np.allclose(np.array(A), np.array(A).T). This will allow correct handling of such cases as 0.99999999999 == 1.00000000000003, that, obviously, will return False, but we might expect (wish) that it should return True.
Reply
#7
Thanks for your help, but the issue unfortunately remains.
here is the changed code:
import numpy as np

 
def is_symmetric(A):
     
    return (np.array(A) == np.array(A.transpose())).all
 
def is_skew(A):
  
     
    return (np.array(A) == -np.array(A.transpose())).all
 
def test_matrix(A):
   

    if is_symmetric(np.any(A)):
        return 1
    if is_skew(np.any(A)):
       return -1
    else:
       return 0
 
 
if __name__ == '__main__':
    a, b, c, d, e, f, g, h, i = 0,1,2,3,4,5,6,7,8
    A = [[a, b, c],
         [d, e, f],
         [g, h, i]]
    print("Testing matrix A: ", test_matrix(A))
Reply
#8
.all is a method, not a property, you forgot () at the end.
Reply
#9
even after writing .all(), the outcome doesn't change.
I keep getting the same outcome.
Reply
#10
import numpy as np

def is_symmetric(A):
    return (np.array(A) == np.array(A).transpose()).all()
  
def is_skew(A):
    return (np.array(A) == -np.array(A).transpose()).all()
  
def test_matrix(A):
    if is_symmetric(A):
        return 1
    if is_skew(A):
       return -1
    else:
       return 0
  
if __name__ == '__main__':
    a, b, c, d, e, f, g, h, i = 0,1,2,3,4,5,6,7,8
    symmetric_A = [[a, b, c],
                   [b, c, a],
                   [c, a, b]]
    skew_A      = [[0, -b, -c],
                   [b, 0, -a],
                   [c, a, 0]]
    arbitrary_A = [[a, b, c],
                   [d, e, f],
                   [g, h, i]]
    print("Testing with symmetric matrix ", test_matrix(symmetric_A))
    print("Testing with arbitrary matrix ", test_matrix(arbitrary_A))
    print("Testing with skew matrix ", test_matrix(skew_A))
    
Output:
Testing with symmetric matrix 1 Testing with arbitrary matrix 0 Testing with skew matrix -1
Reply


Forum Jump:

User Panel Messages

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