Commits

German Larrain committed 0c16258

utils: new 'matrix' module, that includes many classes and functions related to matrices usage

Comments (0)

Files changed (1)

ars/utils/matrix.py

+import numpy as np
+import math
+
+class Vector:
+	pass
+
+
+class Matrix:
+	def __init__(self, elements=None, rows=None):
+		if elements is None and rows is None:
+			raise Exception()
+		if rows is not None:
+			pass
+		pass
+
+	###########################################################################
+	# calculations
+	###########################################################################
+
+	def transpose(self):
+		pass
+
+	###########################################################################
+	# properties
+	###########################################################################
+
+	def is_square(self):
+		pass
+
+	###########################################################################
+	# getters and index accessors
+	###########################################################################
+
+	def get_element(self):
+		"""zero-based index"""
+		pass
+
+	def flatten(self): # or get_flat
+		pass
+
+	def get_row(self):
+		"""zero-based index"""
+		pass
+
+	def get_rows(self):
+		pass
+
+	def get_column(self):
+		"""zero-based index"""
+		pass
+
+	def get_columns(self):
+		pass
+
+
+class SquareMatrix(Matrix):
+	"""Square-shaped matrix."""
+
+	def determinant(self):
+		pass
+
+	def inverse(self):
+		pass
+
+	def is_square(self):
+		return True
+
+
+class Matrix3(SquareMatrix):
+	"""3x3 square matrix."""
+	pass
+
+
+class Matrix4(SquareMatrix):
+	"""4x4 square matrix."""
+	pass
+
+
+def sequence_as_rows(seq, row_size):
+	"""Returns size ``row_size`` sequence slices as a list.
+
+	Examples:
+	sequence_as_rows((1,2,3,4,5,6,7,8,9,10), 2)
+		[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
+	sequence_as_rows((1,2,3,4,5,6,7,8,9,10), 5)
+		[(1, 2, 3, 4, 5), (6, 7, 8, 9, 10)]
+
+	"""
+	n_rows = len(seq) / row_size
+	rows = []
+
+	#if n_rows * row_size != len(seq):
+	if not _valid_matrix_params(len(seq), n_rows):
+		raise Exception('Invalid number of elements')
+
+	for i in range(0, n_rows):
+		start = i * row_size
+		stop = (i + 1) * row_size # seq[stop] not included in the slice
+		rows.append(seq[start:stop])
+
+	# tests
+	#	print(sequence_as_rows((1,2,3,4,5,6,7,8,9,10), 5))
+	#	print(sequence_as_rows((1,2,3,4,5,6,7,8,9,10), 2))
+	#	print(sequence_as_rows((1,2,3,4,5,6,7,8,9), 3))
+	#	print(sequence_as_rows((1,2,3,4,5,6,7,8,9,10), 3))
+
+	return rows
+
+
+###############################################################################
+# auxiliary functions
+###############################################################################
+
+
+def _valid_matrix_params(n_elements, n_rows=None, n_cols=None):
+	"""Check that the number of elements and the declared size of the matrix
+	(numbers of rows and columns) is coherent.
+
+	"""
+	if n_rows is None and n_cols is None:
+		# matrix assumed to be square
+		valid = n_elements == int(math.sqrt(n_elements)) ** 2
+	elif n_rows is not None:
+		valid = n_elements == int(n_elements / n_rows) * n_rows
+	elif n_cols is not None:
+		valid = n_elements == int(n_elements / n_cols) * n_cols
+	else:
+		# the number of rows and columns is given
+		valid = n_elements == n_rows * n_cols
+
+	return valid
+
+
+def _calc_matrix_size(n_elements, n_rows=None, n_cols=None):
+	"""Matrix parameters are assumed to be valid."""
+
+	size = (None, None)
+	if n_rows is None and n_cols is None:
+		# matrix assumed to be square
+		a = int(math.sqrt(n_elements))
+		size = (a,a)
+	elif n_rows is not None:
+		size = (n_rows,int(n_elements / n_rows))
+	elif n_cols is not None:
+		size = (int(n_elements / n_cols),n_cols)
+	else:
+		# the numbers of rows and columns are given
+		size = (n_rows, n_cols)
+
+	return size
+
+
+def _build_np_matrix_from_flat(elements, n_rows=None, n_cols=None):
+	"""Assumed to be square, except if ``n_rows`` and/or ``n_cols`` are
+	provided (integers).
+
+	"""
+	n_elements = len(elements)
+	if not _valid_matrix_params(n_elements, n_rows, n_cols):
+		raise Exception('Invalid number of elements')
+
+	mat_size = _calc_matrix_size(n_elements, n_rows, n_cols)
+	rows = sequence_as_rows(elements, mat_size[0])
+
+	return _build_np_matrix_from_rows(rows)
+
+
+def _build_np_matrix_from_rows(rows):
+	"""rows: (1,4,3.0)"""
+	return np.mat(rows)
+
+
+def _valid_matrix_structure():
+	"""Check all rows are of the same size (rectangular structure)."""
+	pass