Bottom Page

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

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
        return 0;

(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],
thanks, but the output issue still remains.
I keep getting 1.
Why is that?
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.
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
        return 0;
# Driver code 
mat = [ [3,3,3 ], [ 4,4,4 ], [ 5,5,5 ] ] 

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Alternative ways of plotting confusion matrix amjass12 0 262 Aug-14-2019, 07:21 PM
Last Post: amjass12
  Add column to numpy matrix Gigux 1 128 Aug-02-2019, 12:05 AM
Last Post: scidam
  Python/C API Matrix to Numpy jibarra 0 93 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 230 Jul-22-2019, 11:40 AM
Last Post: Koczkodan
  comparing each rows of two matrix PhysChem 10 1,026 May-17-2019, 06:50 AM
Last Post: Gribouillis
  Inverse of singular matrix Divanova94 4 709 May-11-2019, 02:40 AM
Last Post: scidam
  matrix by vector mcgrim 8 360 May-02-2019, 10:39 AM
Last Post: ichabod801
  Extract of matrix subpart using a deep copy paul18fr 2 238 May-02-2019, 06:49 AM
Last Post: paul18fr
  Help in adding confusion matrix Aashish 5 601 Apr-15-2019, 11:45 PM
Last Post: scidam
  counting the occurence of a specified number in a numpy-matrix PhysChem 1 277 Apr-03-2019, 01:37 PM
Last Post: PhysChem

Forum Jump:

Users browsing this thread: 1 Guest(s)