Posts: 203
Threads: 41
Joined: Mar 2019
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
Posts: 817
Threads: 1
Joined: Mar 2018
What errors did you get? Your function a is declared with argument x , why? it is not used in the function body.
Posts: 203
Threads: 41
Joined: Mar 2019
Mar-14-2019, 08:42 AM
(This post was last modified: Mar-14-2019, 08:57 AM by mcgrim.)
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.
Posts: 817
Threads: 1
Joined: Mar 2018
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...
Posts: 203
Threads: 41
Joined: Mar 2019
Mar-15-2019, 02:00 PM
(This post was last modified: Mar-15-2019, 02:10 PM by mcgrim.)
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))
Posts: 817
Threads: 1
Joined: Mar 2018
(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 .
Posts: 203
Threads: 41
Joined: Mar 2019
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))
Posts: 817
Threads: 1
Joined: Mar 2018
.all is a method, not a property, you forgot () at the end.
Posts: 203
Threads: 41
Joined: Mar 2019
even after writing .all(), the outcome doesn't change.
I keep getting the same outcome.
Posts: 817
Threads: 1
Joined: Mar 2018
Mar-18-2019, 12:48 PM
(This post was last modified: Mar-18-2019, 12:48 PM by scidam.)
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
|