list_is_matrix()

home > kero > Documentation

Check that a list is in an appropriate matrix dimensions m x n.

kero.utils.utils.py

def list_is_matrix(x):
  return is_matrix, row_size, col_size

Arguments/Return

x List of list
return is_matrix, row_size, col_size is_matrix (bool). True if the list of list can be represented by a valid m x n matrix.

row_size (integer). Row size of the matrix. Returns -1 if the matrix is not valid, i.e. if is_matrix is False.

col_size (integer). Column size of the matrix. Returns -1 if the matrix is not valid

Example Usage

testUtils1.py

import kero.utils.utils as ut
import numpy as np

def report(x,xbool,row_size,col_size):
	print(x,"\n   ===>",xbool, " || row/col sizes = ", row_size, "/", col_size,"\n")
	return

M=[]
m=[[1,2],[3,4]]; M = M + [m]
m=[[1,2],[3,4],[7,7]]; M = M + [m]
m=[[1,2],[["abc"],4],[10,[]]]; M = M + [m]
m=[[1,2,3],[1]]; M = M + [m]
m=list(np.random.normal(size=(2,4))) ; M = M + [m]
m=list(np.random.normal(size=(12,5))) ; M = M + [m]

for x in M:
	xbool, row_size, col_size = ut.list_is_matrix(x)
	report(x,xbool,row_size,col_size)

The output is:

[[1, 2], [3, 4]]
   ===> True  || row/col sizes =  2 / 2

[[1, 2], [3, 4], [7, 7]]
   ===> True  || row/col sizes =  3 / 2

[[1, 2], [['abc'], 4], [10, []]]
   ===> True  || row/col sizes =  3 / 2

[[1, 2, 3], [1]]
   ===> False  || row/col sizes =  -1 / -1

[array([ 0.79751743,  0.27708435,  2.20502094, -0.56980892]), array([ 0.62829062,  0.31405476, -0.904752  , -0.78342527])]
   ===> True  || row/col sizes =  2 / 4

[array([-2.45412985,  0.50002652,  0.70909205,  1.2913311 , -0.455712  ]), array([ 0.56675073, -0.1715436 ,  1.32696228,  0.36380945, -1.15911077]), array([ 0.15478803,  1.44316791,  1.52492451,  0.12792644, -0.63141821]), array([-1.395655  ,  0.40833624,  1.76730068,  0.12728381,  0.97082398]), array([ 0.25001207,  0.7466505 , -0.71317166, -2.90068385, -0.45016763]), array([ 0.15222356, -0.27922956, -0.2978351 ,  0.23026661, -0.24148927]), array([-3.0036298 , -0.42740637,  0.14102307,  0.66340539,  0.35172813]), array([-0.18296741,  0.99643515, -0.48146279,  1.07686337,  0.32101536]), array([-1.07929927,  0.6994359 , -4.13820507, -0.19078732,  1.7644502 ]), array([ 1.67834366, -1.93677862,  0.81235027,  0.50377342,  0.3465469 ]), array([ 0.63897981,  1.22549464, -0.34187347, -0.39033893, -0.22186534]), array([-0.30978628,  1.5892679 , -0.86626641, -1.56080776, -0.53248118])]
   ===> True  || row/col sizes =  12 / 5

kero version: 0.6.0 and above