Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 function matrix
#11
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))

Quote
#12
(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]]
Quote
#13
thanks, but the output issue still remains.
I keep getting 1.
Why is that?
Quote
#14
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.
Quote
#15
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))
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)