Commits

German Larrain committed 046a37a

utils.mathematical: added 'np_matrix_to_tuple'; changed 'matrix3_multiply' for matrices of any size.

Comments (0)

Files changed (3)

ars/model/simulator/__init__.py

 			angle))
 		# Matrix (of the rotation to apply)
 		# multiplies from the LEFT the actual one
-		rot_final = mu.matrix_as_tuple(mu.matrix3_multiply(rot_to_apply, rot_now))
+		rot_final = mu.matrix_as_tuple(mu.matrix_multiply(rot_to_apply, rot_now))
 		self.set_rotation(rot_final)
 
 	def offset_by_position(self, offset_pos):

ars/utils/mathematical.py

 	return tuple(result)
 
 
-def matrix3_multiply(matrix1, matrix2):
-	"""Return the matrix multiplication of ``matrix1`` and ``matrix2``."""
+def np_matrix_to_tuple(array_):
+	"""Convert Numpy 2D array (i.e. matrix) to a tuple of tuples.
+
+	source: http://stackoverflow.com/a/10016379/556413
+
+	Example:
+	
+	>>> arr = numpy.array(((2, 2), (2, -2)))
+	>>> np_matrix_to_tuple(arr)
+	((2, 2), (2, -2))
+
+	:param array_: Numpy 2D array (i.e. matrix)
+	:return: matrix as tuple of tuples
+
+	"""
+	return tuple(tuple(x) for x in array_)
+
+
+def matrix_multiply(matrix1, matrix2):
+	"""Return the matrix multiplication of ``matrix1`` and ``matrix2``.
+
+	:param matrix1: LxM matrix
+	:param matrix2: MxN matrix
+	:return: LxN matrix, product of ``matrix1`` and ``matrix2``
+	:rtype: tuple of tuples
+
+	"""
 	# TODO: check objects are valid, or use exceptions to catch errors raised
 	# by numpy
 
 	a2 = np.array(matrix2)
 	result = np.dot(a1, a2)
 
-	return matrix_as_3x3_tuples(tuple(result.flatten()))
+	return np_matrix_to_tuple(result)
 
 
 def matrix_as_tuple(matrix_):

tests/utils_mathematical.py

+import numpy
+
 import ars.utils.mathematical as mut
 
 
 def _vector_diff_length(v1, v2):
 	return mut.length3(mut.sub3(v1, v2))
 
+def test_np_matrix_to_tuple():
+	arr = numpy.array(((2, 2), (2, -2)))
+	print(mut.np_matrix_to_tuple(arr))
+
 
 def test_matrix_multiply():
-	print (mut.matrix3_multiply(((2,0,0),(0,1,0),(0,0,1)),
+	print (mut.matrix_multiply(((2,),(1,)), ((1,0,0,0,2,0,0,0,1), )))
+	print (mut.matrix_multiply(((2,0,0),(0,1,0),(0,0,1)),
 		((1,0,0),(0,2,0),(0,0,1))))
+	print (mut.matrix_multiply(((2,0,0,0),(0,1,0,0),(0,0,1,0),(0,0,0,1)),
+		((1,0,0,0),(0,2,0,0),(0,0,1,0),(0,0,0,1))))
 
 
 def test_calc_acceleration():
 
 if __name__ == "__main__":
 
+	test_np_matrix_to_tuple()
 	test_matrix_multiply()
 	test_XYZ_axes_for_orthogonality()
 	test_calc_acceleration()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.