Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 function matrix mcgrim Spam, spam, eggs, and spam Posts: 203 Threads: 35 Joined: Mar 2019 Reputation: 0 Likes received: 1 #11 Mar-18-2019, 09:53 PM unfortunately even with your new code, the outcome is still 1, no matter what. Also, I am really not understanding these input choices: 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]] shouldn't the symmetric case be [[a,d,g],[b,e,h],[c,f,i]] ? and the skew be [[-a,-d,-g],[-b,-e,-h],[-c,-f,-i]] ? anyway, I have rewritten my code this way but I still get 1. Is that a way to slightly change the code underneath to make it work properly? Why do I keep getting 1? ```import numpy as np from numpy import array 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]] 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.all(A)): return 1 if is_skew(np.all(A)): return -1 else: return 0; print(test_matrix(A)) ``` scidam Posts: 614 Threads: 1 Joined: Mar 2018 Reputation: 81 Likes received: 95 #12 Mar-18-2019, 11:34 PM (Mar-18-2019, 09:53 PM)mcgrim Wrote: shouldn't the symmetric case be [[a,d,g],[b,e,h],[c,f,i]] ? and the skew be [[-a,-d,-g],[-b,-e,-h],[-c,-f,-i]] ? Everything depends on values of variables `a, b, ..., i`. I think, it would be better to define symmetric, skew and arbitrary matrices directly, without using variables, e.g.: ```symmetric_A = [[1, 2, 3], [2, 10, 15], [3, 15, 1000]] skew_A = [[0, 2, 3], [-2, 0, 15], [-3, -15, 0]] #Skewness means that A = -A.T, so main diagonal of a skew matrix always consist of zero values. arbitrary_A [[1,2,3], [4,5,6], [7,8,9]] ``` mcgrim Spam, spam, eggs, and spam Posts: 203 Threads: 35 Joined: Mar 2019 Reputation: 0 Likes received: 1 #13 Mar-19-2019, 08:54 AM thanks, but the output issue still remains. I keep getting 1. Why is that? scidam Posts: 614 Threads: 1 Joined: Mar 2018 Reputation: 81 Likes received: 95 #14 Mar-19-2019, 10:42 AM Unfortunately, I don't understand what is wrong. Sorry... On my computer everything (the code defined in #10 post) works fine. I got `1` for symmetric matrix, `-1` for skew matrix and `0` for the matrix of arbitrary form. mcgrim Spam, spam, eggs, and spam Posts: 203 Threads: 35 Joined: Mar 2019 Reputation: 0 Likes received: 1 #15 Mar-19-2019, 09:28 PM since this issue is persisting, I have tried to write this code in a different way, however, the outcome remains the same (1 no matter the values in the matrix). Can you please tell me if you think this code has mistakes or not? Thank you. ```def isSymmetric(mat,N): for i in range(N): for j in range(N): return (mat[i][j] == mat[j][i]) def isSkew(mat,N): for i in range(N): for j in range(N): return (mat[i][j]==-mat[j][i]) def test(mat,N): if isSymmetric(mat,N): return 1 if isSkew(mat,N): return -1 else: return 0; # Driver code mat = [ [3,3,3 ], [ 4,4,4 ], [ 5,5,5 ] ] print(test(mat,2)) ``` « Next Oldest | Next Newest »

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)