function matrix - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: function matrix (/thread-16755.html) Pages:
1
2
|
function matrix - mcgrim - Mar-13-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.
RE: function matrix - scidam - Mar-14-2019 What errors did you get? Your function a is declared with argument x , why? it is not used in the function body.
RE: function matrix - mcgrim - Mar-14-2019 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. RE: function matrix - scidam - Mar-14-2019 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... RE: function matrix - mcgrim - Mar-15-2019 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)) RE: function matrix - scidam - Mar-16-2019 (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 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 .
RE: function matrix - mcgrim - Mar-17-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)) RE: function matrix - scidam - Mar-18-2019 .all is a method, not a property, you forgot () at the end.
RE: function matrix - mcgrim - Mar-18-2019 even after writing .all(), the outcome doesn't change. I keep getting the same outcome. RE: function matrix - scidam - Mar-18-2019 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))
|