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
  PyCM 1.8 released: Machine learning library for confusion matrix statistical analysis sepandhaghighi 0 159 Jan-05-2019, 12:36 PM
Last Post: sepandhaghighi
  Creating matrix counting words in list of strings jazmad 2 275 Dec-23-2018, 05:47 PM
Last Post: jazmad
  Clustering based on a variable and on a distance matrix flucoe 2 256 Dec-16-2018, 09:57 PM
Last Post: flucoe
  The twelfth root of a matrix Arduinol 9 480 Nov-28-2018, 11:36 AM
Last Post: Gribouillis
  fill an empty matrix with random floats sofiapuvogel 2 343 Oct-29-2018, 12:33 PM
Last Post: ichabod801
  python 3 and unique matrix frame 4 360 Sep-20-2018, 11:31 PM
Last Post: micseydel
  Triangular matrix paul 5 647 Jun-15-2018, 07:22 AM
Last Post: Skaperen
  How to do matrix subtraction SriRajesh 0 469 Jun-01-2018, 02:00 PM
Last Post: SriRajesh
  Compressing Matrix RamyMaher 3 569 May-29-2018, 08:41 PM
Last Post: killerrex
  Creating a matrix of rolling variances vvvcvvcv 1 426 May-26-2018, 12:51 PM
Last Post: killerrex

Forum Jump:

Users browsing this thread: 1 Guest(s)