Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 function matrix mcgrim Splitter Posts: 157 Threads: 30 Joined: Mar 2019 Reputation: 0 Likes received: 1 #1 Mar-13-2019, 01:15 PM 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 ` scidam Posts: 509 Threads: 1 Joined: Mar 2018 Reputation: 69 Likes received: 69 #2 Mar-14-2019, 12:04 AM What errors did you get? Your function `a` is declared with argument `x`, why? it is not used in the function body. mcgrim Splitter Posts: 157 Threads: 30 Joined: Mar 2019 Reputation: 0 Likes received: 1 #3 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. scidam Posts: 509 Threads: 1 Joined: Mar 2018 Reputation: 69 Likes received: 69 #4 Mar-14-2019, 12:11 PM 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... mcgrim Splitter Posts: 157 Threads: 30 Joined: Mar 2019 Reputation: 0 Likes received: 1 #5 Mar-15-2019, 02:00 PM (This post was last modified: Mar-15-2019, 02:10 PM by mcgrim. Edited 1 time in total. Edit Reason: fixed tags ) 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)) ``` ichabod801 wrote Mar-15-2019, 02:02 PM:Use python tags (the python icon) for blocks of code. The icode tags (brackets icon) is for `inline code`. scidam Posts: 509 Threads: 1 Joined: Mar 2018 Reputation: 69 Likes received: 69 #6 Mar-16-2019, 12:12 PM (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`. mcgrim Splitter Posts: 157 Threads: 30 Joined: Mar 2019 Reputation: 0 Likes received: 1 #7 Mar-17-2019, 08:24 PM 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)) ``` scidam Posts: 509 Threads: 1 Joined: Mar 2018 Reputation: 69 Likes received: 69 #8 Mar-18-2019, 12:22 AM `.all` is a method, not a property, you forgot `()` at the end. mcgrim Splitter Posts: 157 Threads: 30 Joined: Mar 2019 Reputation: 0 Likes received: 1 #9 Mar-18-2019, 12:39 PM even after writing .all(), the outcome doesn't change. I keep getting the same outcome. scidam Posts: 509 Threads: 1 Joined: Mar 2018 Reputation: 69 Likes received: 69 #10 Mar-18-2019, 12:48 PM (This post was last modified: Mar-18-2019, 12:48 PM by scidam. Edited 1 time in total.) ```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`````` « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Alternative ways of plotting confusion matrix amjass12 0 94 Aug-14-2019, 07:21 PM Last Post: amjass12 Add column to numpy matrix Gigux 1 120 Aug-02-2019, 12:05 AM Last Post: scidam Python/C API Matrix to Numpy jibarra 0 83 Jul-29-2019, 03:25 PM Last Post: jibarra When dividing elements of matrix i would like the result 0 instead of inf? Koczkodan 4 220 Jul-22-2019, 11:40 AM Last Post: Koczkodan comparing each rows of two matrix PhysChem 10 999 May-17-2019, 06:50 AM Last Post: Gribouillis Inverse of singular matrix Divanova94 4 698 May-11-2019, 02:40 AM Last Post: scidam matrix by vector mcgrim 8 345 May-02-2019, 10:39 AM Last Post: ichabod801 Extract of matrix subpart using a deep copy paul18fr 2 227 May-02-2019, 06:49 AM Last Post: paul18fr Help in adding confusion matrix Aashish 5 580 Apr-15-2019, 11:45 PM Last Post: scidam counting the occurence of a specified number in a numpy-matrix PhysChem 1 269 Apr-03-2019, 01:37 PM Last Post: PhysChem

Forum Jump:

Users browsing this thread: 1 Guest(s)