| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2004-09-18 18:47:03 +00:00
										 |  |  |  * $Id$ | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. The Blender | 
					
						
							|  |  |  |  * Foundation also sells licenses for use in proprietary software under | 
					
						
							|  |  |  |  * the Blender License.  See http://www.blender.org/BL/ for information
 | 
					
						
							|  |  |  |  * about this. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  |  * Contributor(s): Michel Selten & Joseph Gilbert | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL/BL DUAL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | #include "matrix.h"
 | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | //doc strings
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | char Matrix_Zero_doc[] = "() - set all values in the matrix to 0"; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | char Matrix_Identity_doc[] = | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	"() - set the square matrix to it's identity matrix"; | 
					
						
							|  |  |  | char Matrix_Transpose_doc[] = "() - set the matrix to it's transpose"; | 
					
						
							|  |  |  | char Matrix_Determinant_doc[] = "() - return the determinant of the matrix"; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | char Matrix_Invert_doc[] = | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	"() - set the matrix to it's inverse if an inverse is possible"; | 
					
						
							|  |  |  | char Matrix_TranslationPart_doc[] = | 
					
						
							|  |  |  | 	"() - return a vector encompassing the translation of the matrix"; | 
					
						
							|  |  |  | char Matrix_RotationPart_doc[] = | 
					
						
							|  |  |  | 	"() - return a vector encompassing the rotation of the matrix"; | 
					
						
							|  |  |  | char Matrix_Resize4x4_doc[] = "() - resize the matrix to a 4x4 square matrix"; | 
					
						
							|  |  |  | char Matrix_toEuler_doc[] = "() - convert matrix to a euler angle rotation"; | 
					
						
							|  |  |  | char Matrix_toQuat_doc[] = "() - convert matrix to a quaternion rotation"; | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | //methods table
 | 
					
						
							|  |  |  | struct PyMethodDef Matrix_methods[] = { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	{"zero", ( PyCFunction ) Matrix_Zero, METH_NOARGS, | 
					
						
							|  |  |  | 	 Matrix_Zero_doc}, | 
					
						
							|  |  |  | 	{"identity", ( PyCFunction ) Matrix_Identity, METH_NOARGS, | 
					
						
							|  |  |  | 	 Matrix_Identity_doc}, | 
					
						
							|  |  |  | 	{"transpose", ( PyCFunction ) Matrix_Transpose, METH_NOARGS, | 
					
						
							|  |  |  | 	 Matrix_Transpose_doc}, | 
					
						
							|  |  |  | 	{"determinant", ( PyCFunction ) Matrix_Determinant, METH_NOARGS, | 
					
						
							|  |  |  | 	 Matrix_Determinant_doc}, | 
					
						
							|  |  |  | 	{"invert", ( PyCFunction ) Matrix_Invert, METH_NOARGS, | 
					
						
							|  |  |  | 	 Matrix_Invert_doc}, | 
					
						
							|  |  |  | 	{"translationPart", ( PyCFunction ) Matrix_TranslationPart, | 
					
						
							|  |  |  | 	 METH_NOARGS, | 
					
						
							|  |  |  | 	 Matrix_TranslationPart_doc}, | 
					
						
							|  |  |  | 	{"rotationPart", ( PyCFunction ) Matrix_RotationPart, METH_NOARGS, | 
					
						
							|  |  |  | 	 Matrix_RotationPart_doc}, | 
					
						
							|  |  |  | 	{"resize4x4", ( PyCFunction ) Matrix_Resize4x4, METH_NOARGS, | 
					
						
							|  |  |  | 	 Matrix_Resize4x4_doc}, | 
					
						
							|  |  |  | 	{"toEuler", ( PyCFunction ) Matrix_toEuler, METH_NOARGS, | 
					
						
							|  |  |  | 	 Matrix_toEuler_doc}, | 
					
						
							|  |  |  | 	{"toQuat", ( PyCFunction ) Matrix_toQuat, METH_NOARGS, | 
					
						
							|  |  |  | 	 Matrix_toQuat_doc}, | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	{NULL, NULL, 0, NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*****************************/ | 
					
						
							|  |  |  | //    Matrix Python Object   
 | 
					
						
							|  |  |  | /*****************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *Matrix_toQuat( MatrixObject * self ) | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	float *quat, *mat; | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( self->colSize < 3 ) { | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 					      "inappropriate matrix size\n" ); | 
					
						
							|  |  |  | 	} else if( self->colSize > 2 ) {	//3 or 4 col
 | 
					
						
							|  |  |  | 		if( self->rowSize < 3 )	//3 or 4 row
 | 
					
						
							|  |  |  | 			return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 						      "inappropriate matrix size\n" ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		mat = PyMem_Malloc( 3 * 3 * sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 		if( mat == NULL ) { | 
					
						
							|  |  |  | 			return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 							"problem allocating matrix\n\n" ) ); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		mat[0] = self->matrix[0][0]; | 
					
						
							|  |  |  | 		mat[1] = self->matrix[0][1]; | 
					
						
							|  |  |  | 		mat[2] = self->matrix[0][2]; | 
					
						
							|  |  |  | 		mat[3] = self->matrix[1][0]; | 
					
						
							|  |  |  | 		mat[4] = self->matrix[1][1]; | 
					
						
							|  |  |  | 		mat[5] = self->matrix[1][2]; | 
					
						
							|  |  |  | 		mat[6] = self->matrix[2][0]; | 
					
						
							|  |  |  | 		mat[7] = self->matrix[2][1]; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		mat[8] = self->matrix[2][2]; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	quat = PyMem_Malloc( 4 * sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 	if( quat == NULL ) { | 
					
						
							|  |  |  | 		PyMem_Free( mat ); | 
					
						
							|  |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 						"problem allocating quat\n\n" ) ); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	Mat3ToQuat( ( float ( * )[3] ) mat, quat ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return ( PyObject * ) newQuaternionObject( quat ); | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *Matrix_toEuler( MatrixObject * self ) | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	float *eul, *mat; | 
					
						
							|  |  |  | 	int x; | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( self->colSize < 3 ) { | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 					      "inappropriate matrix size\n" ); | 
					
						
							|  |  |  | 	} else if( self->colSize > 2 ) {	//3 or 4 col
 | 
					
						
							|  |  |  | 		if( self->rowSize < 3 )	//3 or 4 row
 | 
					
						
							|  |  |  | 			return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 						      "inappropriate matrix size\n" ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		mat = PyMem_Malloc( 3 * 3 * sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 		if( mat == NULL ) { | 
					
						
							|  |  |  | 			return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 							"problem allocating mat\n\n" ) ); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		mat[0] = self->matrix[0][0]; | 
					
						
							|  |  |  | 		mat[1] = self->matrix[0][1]; | 
					
						
							|  |  |  | 		mat[2] = self->matrix[0][2]; | 
					
						
							|  |  |  | 		mat[3] = self->matrix[1][0]; | 
					
						
							|  |  |  | 		mat[4] = self->matrix[1][1]; | 
					
						
							|  |  |  | 		mat[5] = self->matrix[1][2]; | 
					
						
							|  |  |  | 		mat[6] = self->matrix[2][0]; | 
					
						
							|  |  |  | 		mat[7] = self->matrix[2][1]; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		mat[8] = self->matrix[2][2]; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	eul = PyMem_Malloc( 3 * sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 	if( eul == NULL ) { | 
					
						
							|  |  |  | 		PyMem_Free( mat ); | 
					
						
							|  |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 						"problem allocating eul\n\n" ) ); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	Mat3ToEul( ( float ( * )[3] ) mat, eul ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	for( x = 0; x < 3; x++ ) { | 
					
						
							|  |  |  | 		eul[x] *= ( float ) ( 180 / Py_PI ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return ( PyObject * ) newEulerObject( eul ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *Matrix_Resize4x4( MatrixObject * self ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	float *mat; | 
					
						
							|  |  |  | 	int x, row, col; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( self->colSize == 4 && self->rowSize == 4 ) | 
					
						
							|  |  |  | 		return EXPP_incr_ret( Py_None ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	mat = PyMem_Malloc( 4 * 4 * sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 	if( mat == NULL ) { | 
					
						
							|  |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 						"problem allocating mat\n\n" ) ); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	for( x = 0; x < 16; x++ ) { | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		mat[x] = 0.0f; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( self->colSize == 2 ) {	//2x2, 2x3, 2x4
 | 
					
						
							|  |  |  | 		mat[0] = self->matrix[0][0]; | 
					
						
							|  |  |  | 		mat[1] = self->matrix[0][1]; | 
					
						
							|  |  |  | 		mat[4] = self->matrix[1][0]; | 
					
						
							|  |  |  | 		mat[5] = self->matrix[1][1]; | 
					
						
							|  |  |  | 		if( self->rowSize > 2 ) { | 
					
						
							|  |  |  | 			mat[8] = self->matrix[2][0]; | 
					
						
							|  |  |  | 			mat[9] = self->matrix[2][1]; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		if( self->rowSize > 3 ) { | 
					
						
							|  |  |  | 			mat[12] = self->matrix[3][0]; | 
					
						
							|  |  |  | 			mat[13] = self->matrix[3][1]; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		mat[10] = 1.0f; | 
					
						
							|  |  |  | 		mat[15] = 1.0f; | 
					
						
							|  |  |  | 	} else if( self->colSize == 3 ) {	//3x2, 3x3, 3x4
 | 
					
						
							|  |  |  | 		mat[0] = self->matrix[0][0]; | 
					
						
							|  |  |  | 		mat[1] = self->matrix[0][1]; | 
					
						
							|  |  |  | 		mat[2] = self->matrix[0][2]; | 
					
						
							|  |  |  | 		mat[4] = self->matrix[1][0]; | 
					
						
							|  |  |  | 		mat[5] = self->matrix[1][1]; | 
					
						
							|  |  |  | 		mat[6] = self->matrix[1][2]; | 
					
						
							|  |  |  | 		if( self->rowSize > 2 ) { | 
					
						
							|  |  |  | 			mat[8] = self->matrix[2][0]; | 
					
						
							|  |  |  | 			mat[9] = self->matrix[2][1]; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 			mat[10] = self->matrix[2][2]; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		if( self->rowSize > 3 ) { | 
					
						
							|  |  |  | 			mat[12] = self->matrix[3][0]; | 
					
						
							|  |  |  | 			mat[13] = self->matrix[3][1]; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 			mat[14] = self->matrix[3][2]; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		if( self->rowSize == 2 ) | 
					
						
							|  |  |  | 			mat[10] = 1.0f; | 
					
						
							|  |  |  | 		mat[15] = 1.0f; | 
					
						
							|  |  |  | 	} else if( self->colSize == 4 ) {	//2x4, 3x4
 | 
					
						
							|  |  |  | 		mat[0] = self->matrix[0][0]; | 
					
						
							|  |  |  | 		mat[1] = self->matrix[0][1]; | 
					
						
							|  |  |  | 		mat[2] = self->matrix[0][2]; | 
					
						
							|  |  |  | 		mat[3] = self->matrix[0][3]; | 
					
						
							|  |  |  | 		mat[4] = self->matrix[1][0]; | 
					
						
							|  |  |  | 		mat[5] = self->matrix[1][1]; | 
					
						
							|  |  |  | 		mat[6] = self->matrix[1][2]; | 
					
						
							|  |  |  | 		mat[7] = self->matrix[1][3]; | 
					
						
							|  |  |  | 		if( self->rowSize > 2 ) { | 
					
						
							|  |  |  | 			mat[8] = self->matrix[2][0]; | 
					
						
							|  |  |  | 			mat[9] = self->matrix[2][1]; | 
					
						
							|  |  |  | 			mat[10] = self->matrix[2][2]; | 
					
						
							|  |  |  | 			mat[11] = self->matrix[2][3]; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		if( self->rowSize == 2 ) | 
					
						
							|  |  |  | 			mat[10] = 1.0f; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		mat[15] = 1.0f; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 	PyMem_Free( self->matrix ); | 
					
						
							|  |  |  | 	PyMem_Free( self->contigPtr ); | 
					
						
							|  |  |  | 	self->contigPtr = PyMem_Malloc( 4 * 4 * sizeof( float ) ); | 
					
						
							|  |  |  | 	if( self->contigPtr == NULL ) { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 						"problem allocating array space\n\n" ) ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	self->matrix = PyMem_Malloc( 4 * sizeof( float * ) ); | 
					
						
							|  |  |  | 	if( self->matrix == NULL ) { | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 		PyMem_Free( self->contigPtr ); | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 						"problem allocating pointer space\n\n" ) ); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for( x = 0; x < 4; x++ ) { | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 		self->matrix[x] = self->contigPtr + ( x * 4 ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	for( row = 0; row < 4; row++ ) { | 
					
						
							|  |  |  | 		for( col = 0; col < 4; col++ ) { | 
					
						
							|  |  |  | 			self->matrix[row][col] = mat[( row * 4 ) + col]; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	PyMem_Free( mat ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	self->colSize = 4; | 
					
						
							|  |  |  | 	self->rowSize = 4; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return EXPP_incr_ret( Py_None ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *Matrix_TranslationPart( MatrixObject * self ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	float *vec; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( self->colSize < 3 ) { | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 					      "inappropriate matrix size\n" ); | 
					
						
							|  |  |  | 	} else if( self->colSize > 2 ) {	//3 or 4 columns
 | 
					
						
							|  |  |  | 		if( self->rowSize < 4 )	//all 4 rows
 | 
					
						
							|  |  |  | 			return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 						      "inappropriate matrix size\n" ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		vec = PyMem_Malloc( 3 * sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 		if( vec == NULL ) { | 
					
						
							|  |  |  | 			return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 							"problem allocating vec\n\n" ) ); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		vec[0] = self->matrix[3][0]; | 
					
						
							|  |  |  | 		vec[1] = self->matrix[3][1]; | 
					
						
							|  |  |  | 		vec[2] = self->matrix[3][2]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return ( PyObject * ) newVectorObject( vec, 3 ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *Matrix_RotationPart( MatrixObject * self ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	float *mat; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( self->colSize < 3 ) { | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 					      "inappropriate matrix size\n" ); | 
					
						
							|  |  |  | 	} else if( self->colSize > 2 ) {	//3 or 4 col
 | 
					
						
							|  |  |  | 		if( self->rowSize < 3 )	//3 or 4 row
 | 
					
						
							|  |  |  | 			return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 						      "inappropriate matrix size\n" ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		mat = PyMem_Malloc( 3 * 3 * sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 		if( mat == NULL ) { | 
					
						
							|  |  |  | 			return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 							"problem allocating mat\n\n" ) ); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		mat[0] = self->matrix[0][0]; | 
					
						
							|  |  |  | 		mat[1] = self->matrix[0][1]; | 
					
						
							|  |  |  | 		mat[2] = self->matrix[0][2]; | 
					
						
							|  |  |  | 		mat[3] = self->matrix[1][0]; | 
					
						
							|  |  |  | 		mat[4] = self->matrix[1][1]; | 
					
						
							|  |  |  | 		mat[5] = self->matrix[1][2]; | 
					
						
							|  |  |  | 		mat[6] = self->matrix[2][0]; | 
					
						
							|  |  |  | 		mat[7] = self->matrix[2][1]; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		mat[8] = self->matrix[2][2]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return ( PyObject * ) newMatrixObject( mat, 3, 3 ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *Matrix_Invert( MatrixObject * self ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	float det; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	int x, y, z; | 
					
						
							| 
									
										
										
										
											2004-04-23 13:11:48 +00:00
										 |  |  | 	float *mat = NULL; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	float t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( self->rowSize != self->colSize ) | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 					      "only square matrices are supported\n" ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//calculate the determinant
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( self->rowSize == 2 ) { | 
					
						
							|  |  |  | 		det = Det2x2( self->matrix[0][0], self->matrix[0][1], | 
					
						
							|  |  |  | 			      self->matrix[1][0], self->matrix[1][1] ); | 
					
						
							|  |  |  | 	} else if( self->rowSize == 3 ) { | 
					
						
							|  |  |  | 		det = Det3x3( self->matrix[0][0], self->matrix[0][1], | 
					
						
							|  |  |  | 			      self->matrix[0][2], self->matrix[1][0], | 
					
						
							|  |  |  | 			      self->matrix[1][1], self->matrix[1][2], | 
					
						
							|  |  |  | 			      self->matrix[2][0], self->matrix[2][1], | 
					
						
							|  |  |  | 			      self->matrix[2][2] ); | 
					
						
							|  |  |  | 	} else if( self->rowSize == 4 ) { | 
					
						
							|  |  |  | 		det = Det4x4( *self->matrix ); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_StandardError, | 
					
						
							|  |  |  | 					      "error calculating determinant for inverse()\n" ); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( det != 0 ) { | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		//calculate the classical adjoint
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		if( self->rowSize == 2 ) { | 
					
						
							|  |  |  | 			mat = PyMem_Malloc( self->rowSize * self->colSize * | 
					
						
							|  |  |  | 					    sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 			if( mat == NULL ) { | 
					
						
							|  |  |  | 				return ( EXPP_ReturnPyObjError | 
					
						
							|  |  |  | 					 ( PyExc_MemoryError, | 
					
						
							|  |  |  | 					   "problem allocating mat\n\n" ) ); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 			mat[0] = self->matrix[1][1]; | 
					
						
							|  |  |  | 			mat[1] = -self->matrix[1][0]; | 
					
						
							|  |  |  | 			mat[2] = -self->matrix[0][1]; | 
					
						
							|  |  |  | 			mat[3] = self->matrix[0][0]; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		} else if( self->rowSize == 3 ) { | 
					
						
							|  |  |  | 			mat = PyMem_Malloc( self->rowSize * self->colSize * | 
					
						
							|  |  |  | 					    sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 			if( mat == NULL ) { | 
					
						
							|  |  |  | 				return ( EXPP_ReturnPyObjError | 
					
						
							|  |  |  | 					 ( PyExc_MemoryError, | 
					
						
							|  |  |  | 					   "problem allocating mat\n\n" ) ); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 			Mat3Adj( ( float ( * )[3] ) mat, *self->matrix ); | 
					
						
							|  |  |  | 		} else if( self->rowSize == 4 ) { | 
					
						
							|  |  |  | 			mat = PyMem_Malloc( self->rowSize * self->colSize * | 
					
						
							|  |  |  | 					    sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 			if( mat == NULL ) { | 
					
						
							|  |  |  | 				return ( EXPP_ReturnPyObjError | 
					
						
							|  |  |  | 					 ( PyExc_MemoryError, | 
					
						
							|  |  |  | 					   "problem allocating mat\n\n" ) ); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 			Mat4Adj( ( float ( * )[4] ) mat, *self->matrix ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		//divide by determinate
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		for( x = 0; x < ( self->rowSize * self->colSize ); x++ ) { | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 			mat[x] /= det; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		//set values
 | 
					
						
							|  |  |  | 		z = 0; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		for( x = 0; x < self->rowSize; x++ ) { | 
					
						
							|  |  |  | 			for( y = 0; y < self->colSize; y++ ) { | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 				self->matrix[x][y] = mat[z]; | 
					
						
							|  |  |  | 				z++; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		//transpose
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		if( self->rowSize == 2 ) { | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 			t = self->matrix[1][0]; | 
					
						
							|  |  |  | 			self->matrix[1][0] = self->matrix[0][1]; | 
					
						
							|  |  |  | 			self->matrix[0][1] = t; | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 			 | 
					
						
							|  |  |  | /* 
 | 
					
						
							|  |  |  |    Note: is the code below correct?   | 
					
						
							|  |  |  |    transposing mat and not copying into self->matrix?  | 
					
						
							|  |  |  |    s. swaney 11-oct-2004 | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		} else if( self->rowSize == 3 ) { | 
					
						
							|  |  |  | 			Mat3Transp( ( float ( * )[3] ) mat ); | 
					
						
							|  |  |  | 		} else if( self->rowSize == 4 ) { | 
					
						
							|  |  |  | 			Mat4Transp( ( float ( * )[4] ) mat ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		printf( "matrix does not have an inverse - none attempted\n" ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 	PyMem_Free( mat ); | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return EXPP_incr_ret( Py_None ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *Matrix_Determinant( MatrixObject * self ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	float det; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( self->rowSize != self->colSize ) | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 					      "only square matrices are supported\n" ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( self->rowSize == 2 ) { | 
					
						
							|  |  |  | 		det = Det2x2( self->matrix[0][0], self->matrix[0][1], | 
					
						
							|  |  |  | 			      self->matrix[1][0], self->matrix[1][1] ); | 
					
						
							|  |  |  | 	} else if( self->rowSize == 3 ) { | 
					
						
							|  |  |  | 		det = Det3x3( self->matrix[0][0], self->matrix[0][1], | 
					
						
							|  |  |  | 			      self->matrix[0][2], self->matrix[1][0], | 
					
						
							|  |  |  | 			      self->matrix[1][1], self->matrix[1][2], | 
					
						
							|  |  |  | 			      self->matrix[2][0], self->matrix[2][1], | 
					
						
							|  |  |  | 			      self->matrix[2][2] ); | 
					
						
							|  |  |  | 	} else if( self->rowSize == 4 ) { | 
					
						
							|  |  |  | 		det = Det4x4( *self->matrix ); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_StandardError, | 
					
						
							|  |  |  | 					      "error in determinant()\n" ); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return PyFloat_FromDouble( det ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *Matrix_Transpose( MatrixObject * self ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	float t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( self->rowSize != self->colSize ) | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 					      "only square matrices are supported\n" ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( self->rowSize == 2 ) { | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		t = self->matrix[1][0]; | 
					
						
							|  |  |  | 		self->matrix[1][0] = self->matrix[0][1]; | 
					
						
							|  |  |  | 		self->matrix[0][1] = t; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	} else if( self->rowSize == 3 ) { | 
					
						
							|  |  |  | 		Mat3Transp( *self->matrix ); | 
					
						
							|  |  |  | 	} else if( self->rowSize == 4 ) { | 
					
						
							|  |  |  | 		Mat4Transp( *self->matrix ); | 
					
						
							|  |  |  | 	} else | 
					
						
							|  |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_TypeError, | 
					
						
							|  |  |  | 						"unable to transpose matrix\n" ) ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return EXPP_incr_ret( Py_None ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *Matrix_Zero( MatrixObject * self ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int row, col; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	for( row = 0; row < self->rowSize; row++ ) { | 
					
						
							|  |  |  | 		for( col = 0; col < self->colSize; col++ ) { | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 			self->matrix[row][col] = 0.0f; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return EXPP_incr_ret( Py_None ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *Matrix_Identity( MatrixObject * self ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( self->rowSize != self->colSize ) | 
					
						
							|  |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 						"only square matrices supported\n" ) ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( self->rowSize == 2 ) { | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		self->matrix[0][0] = 1.0f; | 
					
						
							|  |  |  | 		self->matrix[0][1] = 0.0f; | 
					
						
							|  |  |  | 		self->matrix[1][0] = 0.0f; | 
					
						
							|  |  |  | 		self->matrix[1][1] = 1.0f; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	} else if( self->rowSize == 3 ) { | 
					
						
							|  |  |  | 		Mat3One( *self->matrix ); | 
					
						
							|  |  |  | 	} else if( self->rowSize == 4 ) { | 
					
						
							|  |  |  | 		Mat4One( *self->matrix ); | 
					
						
							|  |  |  | 	} else | 
					
						
							|  |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_TypeError, | 
					
						
							|  |  |  | 						"unable to create identity matrix\n" ) ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return EXPP_incr_ret( Py_None ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static void Matrix_dealloc( MatrixObject * self ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 	PyMem_Free( self->contigPtr ); | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	PyMem_Free( self->matrix ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	PyObject_DEL( self ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyObject *Matrix_getattr( MatrixObject * self, char *name ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( strcmp( name, "rowSize" ) == 0 ) { | 
					
						
							|  |  |  | 		return PyInt_FromLong( ( long ) self->rowSize ); | 
					
						
							|  |  |  | 	} else if( strcmp( name, "colSize" ) == 0 ) { | 
					
						
							|  |  |  | 		return PyInt_FromLong( ( long ) self->colSize ); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Py_FindMethod( Matrix_methods, ( PyObject * ) self, name ); | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static int Matrix_setattr( MatrixObject * self, char *name, PyObject * v ) | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	/* This is not supported. */ | 
					
						
							|  |  |  | 	return ( -1 ); | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyObject *Matrix_repr( MatrixObject * self ) | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	PyObject *repr, *str; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	int x, y; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	char ftoa[24]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	repr = PyString_FromString( "" ); | 
					
						
							|  |  |  | 	if( !repr ) | 
					
						
							|  |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 						"Attribute error in PyMatrix (repr)\n" ) ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	for( x = 0; x < self->rowSize; x++ ) { | 
					
						
							|  |  |  | 		str = PyString_FromString( "[" ); | 
					
						
							|  |  |  | 		PyString_ConcatAndDel( &repr, str ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		for( y = 0; y < ( self->colSize - 1 ); y++ ) { | 
					
						
							|  |  |  | 			sprintf( ftoa, "%.4f, ", self->matrix[x][y] ); | 
					
						
							|  |  |  | 			str = PyString_FromString( ftoa ); | 
					
						
							|  |  |  | 			PyString_ConcatAndDel( &repr, str ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		sprintf( ftoa, "%.4f]\n", self->matrix[x][y] ); | 
					
						
							|  |  |  | 		str = PyString_FromString( ftoa ); | 
					
						
							|  |  |  | 		PyString_ConcatAndDel( &repr, str ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return repr; | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | //no support for matrix[x][y] so have to return by sequence index
 | 
					
						
							| 
									
										
										
										
											2004-03-15 00:43:38 +00:00
										 |  |  | //will return a row from the matrix to support previous API
 | 
					
						
							|  |  |  | //compatability
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyObject *Matrix_item( MatrixObject * self, int i ) | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-03-15 00:43:38 +00:00
										 |  |  | 	float *vec; | 
					
						
							|  |  |  | 	int x; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( i < 0 || i >= self->rowSize ) | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_IndexError, | 
					
						
							|  |  |  | 					      "matrix row index out of range\n" ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	vec = PyMem_Malloc( self->colSize * sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 	if( vec == NULL ) { | 
					
						
							|  |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 						"problem allocating vec\n\n" ) ); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	for( x = 0; x < self->colSize; x++ ) { | 
					
						
							| 
									
										
										
										
											2004-03-15 00:43:38 +00:00
										 |  |  | 		vec[x] = self->matrix[i][x]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return ( PyObject * ) newVectorObject( vec, self->colSize ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyObject *Matrix_slice( MatrixObject * self, int begin, int end ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	PyObject *list; | 
					
						
							|  |  |  | 	int count, maxsize, x, y; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	maxsize = self->colSize * self->rowSize; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( begin < 0 ) | 
					
						
							|  |  |  | 		begin = 0; | 
					
						
							|  |  |  | 	if( end > maxsize ) | 
					
						
							|  |  |  | 		end = maxsize; | 
					
						
							|  |  |  | 	if( begin > end ) | 
					
						
							|  |  |  | 		begin = end; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	list = PyList_New( end - begin ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	for( count = begin; count < end; count++ ) { | 
					
						
							|  |  |  | 		x = ( int ) floor( ( double ) ( count / self->colSize ) ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		y = count % self->colSize; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		PyList_SetItem( list, count - begin, | 
					
						
							|  |  |  | 				PyFloat_FromDouble( self->matrix[x][y] ) ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return list; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static int Matrix_ass_item( MatrixObject * self, int i, PyObject * ob ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int maxsize, x, y; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	maxsize = self->colSize * self->rowSize; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( i < 0 || i >= maxsize ) | 
					
						
							|  |  |  | 		return EXPP_ReturnIntError( PyExc_IndexError, | 
					
						
							|  |  |  | 					    "array assignment index out of range\n" ); | 
					
						
							|  |  |  | 	if( !PyInt_Check( ob ) && !PyFloat_Check( ob ) ) | 
					
						
							|  |  |  | 		return EXPP_ReturnIntError( PyExc_IndexError, | 
					
						
							|  |  |  | 					    "matrix member must be a number\n" ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	x = ( int ) floor( ( double ) ( i / self->colSize ) ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	y = i % self->colSize; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	self->matrix[x][y] = ( float ) PyFloat_AsDouble( ob ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static int Matrix_ass_slice( MatrixObject * self, int begin, int end, | 
					
						
							|  |  |  | 			     PyObject * seq ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int count, maxsize, x, y, z; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	maxsize = self->colSize * self->rowSize; | 
					
						
							|  |  |  | 	if( begin < 0 ) | 
					
						
							|  |  |  | 		begin = 0; | 
					
						
							|  |  |  | 	if( end > maxsize ) | 
					
						
							|  |  |  | 		end = maxsize; | 
					
						
							|  |  |  | 	if( begin > end ) | 
					
						
							|  |  |  | 		begin = end; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( !PySequence_Check( seq ) ) | 
					
						
							|  |  |  | 		return EXPP_ReturnIntError( PyExc_TypeError, | 
					
						
							|  |  |  | 					    "illegal argument type for built-in operation\n" ); | 
					
						
							|  |  |  | 	if( PySequence_Length( seq ) != ( end - begin ) ) | 
					
						
							|  |  |  | 		return EXPP_ReturnIntError( PyExc_TypeError, | 
					
						
							|  |  |  | 					    "size mismatch in slice assignment\n" ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	z = 0; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	for( count = begin; count < end; count++ ) { | 
					
						
							|  |  |  | 		PyObject *ob = PySequence_GetItem( seq, z ); | 
					
						
							|  |  |  | 		z++; | 
					
						
							|  |  |  | 		if( !PyInt_Check( ob ) && !PyFloat_Check( ob ) ) | 
					
						
							|  |  |  | 			return EXPP_ReturnIntError( PyExc_IndexError, | 
					
						
							|  |  |  | 						    "list member must be a number\n" ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		x = ( int ) floor( ( double ) ( count / self->colSize ) ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		y = count % self->colSize; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		if( !PyArg_Parse( ob, "f", &self->matrix[x][y] ) ) { | 
					
						
							|  |  |  | 			Py_DECREF( ob ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static int Matrix_len( MatrixObject * self ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return ( self->colSize * self->rowSize ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *Matrix_add( PyObject * m1, PyObject * m2 ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	float *mat; | 
					
						
							|  |  |  | 	int matSize, rowSize, colSize, x, y; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( ( !Matrix_CheckPyObject( m1 ) ) | 
					
						
							|  |  |  | 	    || ( !Matrix_CheckPyObject( m2 ) ) ) | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_TypeError, | 
					
						
							|  |  |  | 					      "unsupported type for this operation\n" ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( ( ( MatrixObject * ) m1 )->flag > 0 | 
					
						
							|  |  |  | 	    || ( ( MatrixObject * ) m2 )->flag > 0 ) | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_ArithmeticError, | 
					
						
							|  |  |  | 					      "cannot add scalar to a matrix\n" ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( ( ( MatrixObject * ) m1 )->rowSize != | 
					
						
							|  |  |  | 	    ( ( MatrixObject * ) m2 )->rowSize | 
					
						
							|  |  |  | 	    || ( ( MatrixObject * ) m1 )->colSize != | 
					
						
							|  |  |  | 	    ( ( MatrixObject * ) m2 )->colSize ) | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 					      "matrices must be the same same for this operation\n" ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rowSize = ( ( ( MatrixObject * ) m1 )->rowSize ); | 
					
						
							|  |  |  | 	colSize = ( ( ( MatrixObject * ) m1 )->colSize ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	matSize = rowSize * colSize; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	mat = PyMem_Malloc( matSize * sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 	if( mat == NULL ) { | 
					
						
							|  |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 						"problem allocating mat\n\n" ) ); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	for( x = 0; x < rowSize; x++ ) { | 
					
						
							|  |  |  | 		for( y = 0; y < colSize; y++ ) { | 
					
						
							|  |  |  | 			mat[( ( x * rowSize ) + y )] = | 
					
						
							|  |  |  | 				( ( MatrixObject * ) m1 )->matrix[x][y] + | 
					
						
							|  |  |  | 				( ( MatrixObject * ) m2 )->matrix[x][y]; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return newMatrixObject( mat, rowSize, colSize ); | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *Matrix_sub( PyObject * m1, PyObject * m2 ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	float *mat; | 
					
						
							|  |  |  | 	int matSize, rowSize, colSize, x, y; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( ( !Matrix_CheckPyObject( m1 ) ) | 
					
						
							|  |  |  | 	    || ( !Matrix_CheckPyObject( m2 ) ) ) | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_TypeError, | 
					
						
							|  |  |  | 					      "unsupported type for this operation\n" ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( ( ( MatrixObject * ) m1 )->flag > 0 | 
					
						
							|  |  |  | 	    || ( ( MatrixObject * ) m2 )->flag > 0 ) | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_ArithmeticError, | 
					
						
							|  |  |  | 					      "cannot subtract a scalar from a matrix\n" ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( ( ( MatrixObject * ) m1 )->rowSize != | 
					
						
							|  |  |  | 	    ( ( MatrixObject * ) m2 )->rowSize | 
					
						
							|  |  |  | 	    || ( ( MatrixObject * ) m1 )->colSize != | 
					
						
							|  |  |  | 	    ( ( MatrixObject * ) m2 )->colSize ) | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 					      "matrices must be the same same for this operation\n" ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rowSize = ( ( ( MatrixObject * ) m1 )->rowSize ); | 
					
						
							|  |  |  | 	colSize = ( ( ( MatrixObject * ) m1 )->colSize ); | 
					
						
							|  |  |  | 	matSize = rowSize * colSize; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	mat = PyMem_Malloc( matSize * sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 	if( mat == NULL ) { | 
					
						
							|  |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 						"problem allocating mat\n\n" ) ); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	for( x = 0; x < rowSize; x++ ) { | 
					
						
							|  |  |  | 		for( y = 0; y < colSize; y++ ) { | 
					
						
							|  |  |  | 			mat[( ( x * rowSize ) + y )] = | 
					
						
							|  |  |  | 				( ( MatrixObject * ) m1 )->matrix[x][y] - | 
					
						
							|  |  |  | 				( ( MatrixObject * ) m2 )->matrix[x][y]; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return newMatrixObject( mat, rowSize, colSize ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *Matrix_mul( PyObject * m1, PyObject * m2 ) | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	float *mat; | 
					
						
							|  |  |  | 	int matSizeV, rowSizeV, colSizeV, rowSizeW, colSizeW, matSizeW, x, y, | 
					
						
							|  |  |  | 		z; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	float dot = 0; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	MatrixObject *matV; | 
					
						
							|  |  |  | 	MatrixObject *matW; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( ( !Matrix_CheckPyObject( m1 ) ) | 
					
						
							|  |  |  | 	    || ( !Matrix_CheckPyObject( m2 ) ) ) | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_TypeError, | 
					
						
							|  |  |  | 					      "unsupported type for this operation\n" ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//get some vars
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	rowSizeV = ( ( ( MatrixObject * ) m1 )->rowSize ); | 
					
						
							|  |  |  | 	colSizeV = ( ( ( MatrixObject * ) m1 )->colSize ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	matSizeV = rowSizeV * colSizeV; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	rowSizeW = ( ( ( MatrixObject * ) m2 )->rowSize ); | 
					
						
							|  |  |  | 	colSizeW = ( ( ( MatrixObject * ) m2 )->colSize ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	matSizeW = rowSizeW * colSizeW; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	matV = ( ( MatrixObject * ) m1 ); | 
					
						
							|  |  |  | 	matW = ( ( MatrixObject * ) m2 ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//coerced int or float for scalar multiplication
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( matW->flag > 1 || matW->flag > 2 ) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if( rowSizeV != rowSizeW && colSizeV != colSizeW ) | 
					
						
							|  |  |  | 			return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 						      "Matrix dimension error during scalar multiplication\n" ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		mat = PyMem_Malloc( matSizeV * sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 		if( mat == NULL ) { | 
					
						
							|  |  |  | 			return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 							"problem allocating mat\n\n" ) ); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		for( x = 0; x < rowSizeV; x++ ) { | 
					
						
							|  |  |  | 			for( y = 0; y < colSizeV; y++ ) { | 
					
						
							|  |  |  | 				mat[( ( x * rowSizeV ) + y )] = | 
					
						
							|  |  |  | 					matV->matrix[x][y] * | 
					
						
							|  |  |  | 					matW->matrix[x][y]; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		return newMatrixObject( mat, rowSizeV, colSizeV ); | 
					
						
							|  |  |  | 	} else if( matW->flag == 0 && matV->flag == 0 ) {	//true matrix multiplication
 | 
					
						
							|  |  |  | 		if( colSizeV != rowSizeW ) { | 
					
						
							|  |  |  | 			return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 						      "Matrix multiplication undefined...\n" ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		mat = PyMem_Malloc( ( rowSizeV * colSizeW ) * | 
					
						
							|  |  |  | 				    sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 		if( mat == NULL ) { | 
					
						
							|  |  |  | 			return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 							"problem allocating mat\n\n" ) ); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		for( x = 0; x < rowSizeV; x++ ) { | 
					
						
							|  |  |  | 			for( y = 0; y < colSizeW; y++ ) { | 
					
						
							|  |  |  | 				for( z = 0; z < colSizeV; z++ ) { | 
					
						
							|  |  |  | 					dot += ( matV->matrix[x][z] * | 
					
						
							|  |  |  | 						 matW->matrix[z][y] ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 				mat[( ( x * rowSizeV ) + y )] = dot; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 				dot = 0; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		return newMatrixObject( mat, rowSizeV, colSizeW ); | 
					
						
							|  |  |  | 	} else | 
					
						
							|  |  |  | 		return EXPP_ReturnPyObjError( PyExc_AttributeError, | 
					
						
							|  |  |  | 					      "Error in matrix_mul...\n" ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //coercion of unknown types to type MatrixObject for numeric protocols
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | int Matrix_coerce( PyObject ** m1, PyObject ** m2 ) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	long *tempI; | 
					
						
							|  |  |  | 	double *tempF; | 
					
						
							|  |  |  | 	float *mat; | 
					
						
							|  |  |  | 	int x, matSize; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	matSize = | 
					
						
							|  |  |  | 		( ( ( MatrixObject * ) * m1 )->rowSize ) * | 
					
						
							|  |  |  | 		( ( ( MatrixObject * ) * m1 )->rowSize ); | 
					
						
							|  |  |  | 	if( Matrix_CheckPyObject( *m1 ) ) { | 
					
						
							|  |  |  | 		if( Matrix_CheckPyObject( *m2 ) ) {	//matrix & matrix
 | 
					
						
							|  |  |  | 			Py_INCREF( *m1 ); | 
					
						
							|  |  |  | 			Py_INCREF( *m2 ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 			return 0; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			if( VectorObject_Check( *m2 ) ) {	//matrix & vector?
 | 
					
						
							|  |  |  | 				printf( "use MatMultVec() for column vector multiplication\n" ); | 
					
						
							|  |  |  | 				Py_INCREF( *m1 ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 				return 0; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 			} else if( PyNumber_Check( *m2 ) ) {	//& scalar?
 | 
					
						
							|  |  |  | 				if( PyInt_Check( *m2 ) ) {	//it's a int
 | 
					
						
							|  |  |  | 					tempI = PyMem_Malloc( 1 * | 
					
						
							|  |  |  | 							      sizeof( long ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 					if( tempI == NULL ) { | 
					
						
							|  |  |  | 						return ( EXPP_ReturnPyObjError | 
					
						
							|  |  |  | 							 ( PyExc_MemoryError, | 
					
						
							|  |  |  | 							   "problem allocating tempI\n\n" ) ); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 					*tempI = PyInt_AsLong( *m2 ); | 
					
						
							|  |  |  | 					mat = PyMem_Malloc( matSize * | 
					
						
							|  |  |  | 							    sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 					if( mat == NULL ) { | 
					
						
							|  |  |  | 						PyMem_Free( tempI ); | 
					
						
							|  |  |  | 						return ( EXPP_ReturnPyObjError | 
					
						
							|  |  |  | 							 ( PyExc_MemoryError, | 
					
						
							|  |  |  | 							   "problem allocating mat\n\n" ) ); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 					for( x = 0; x < matSize; x++ ) { | 
					
						
							|  |  |  | 						mat[x] = ( float ) *tempI; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 					PyMem_Free( tempI ); | 
					
						
							|  |  |  | 					*m2 = newMatrixObject( mat, | 
					
						
							|  |  |  | 							       ( ( ( MatrixObject * ) * m1 )->rowSize ), ( ( ( MatrixObject * ) * m1 )->colSize ) ); | 
					
						
							|  |  |  | 					( ( MatrixObject * ) * m2 )->flag = 1;	//int coercion
 | 
					
						
							|  |  |  | 					PyMem_Free( mat ); | 
					
						
							|  |  |  | 					Py_INCREF( *m1 ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 					return 0; | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 				} else if( PyFloat_Check( *m2 ) ) {	//it's a float
 | 
					
						
							|  |  |  | 					tempF = PyMem_Malloc( 1 * | 
					
						
							|  |  |  | 							      sizeof | 
					
						
							|  |  |  | 							      ( double ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 					if( tempF == NULL ) { | 
					
						
							|  |  |  | 						return ( EXPP_ReturnPyObjError | 
					
						
							|  |  |  | 							 ( PyExc_MemoryError, | 
					
						
							|  |  |  | 							   "problem allocating tempF\n\n" ) ); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 					*tempF = PyFloat_AsDouble( *m2 ); | 
					
						
							|  |  |  | 					mat = PyMem_Malloc( matSize * | 
					
						
							|  |  |  | 							    sizeof( float ) ); | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 					if( mat == NULL ) { | 
					
						
							|  |  |  | 						PyMem_Free( tempF ); | 
					
						
							|  |  |  | 						return ( EXPP_ReturnPyObjError | 
					
						
							|  |  |  | 							 ( PyExc_MemoryError, | 
					
						
							|  |  |  | 							   "problem allocating mat\n\n" ) ); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 					for( x = 0; x < matSize; x++ ) { | 
					
						
							|  |  |  | 						mat[x] = ( float ) *tempF; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 					PyMem_Free( tempF ); | 
					
						
							|  |  |  | 					*m2 = newMatrixObject( mat, | 
					
						
							|  |  |  | 							       ( ( ( MatrixObject * ) * m1 )->rowSize ), ( ( ( MatrixObject * ) * m1 )->colSize ) ); | 
					
						
							|  |  |  | 					( ( MatrixObject * ) * m2 )->flag = 2;	//float coercion
 | 
					
						
							|  |  |  | 					PyMem_Free( mat ); | 
					
						
							|  |  |  | 					Py_INCREF( *m1 ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 					return 0; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			//unknom2n type or numeric cast failure
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 			printf( "attempting matrix operation m2ith unsupported type...\n" ); | 
					
						
							|  |  |  | 			Py_INCREF( *m1 ); | 
					
						
							|  |  |  | 			return 0;	//operation m2ill type check
 | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 		//1st not Matrix
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		printf( "numeric protocol failure...\n" ); | 
					
						
							|  |  |  | 		return -1;	//this should not occur - fail
 | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return -1; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //******************************************************************
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | //                                      Matrix definition
 | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | //******************************************************************
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PySequenceMethods Matrix_SeqMethods = { | 
					
						
							|  |  |  | 	( inquiry ) Matrix_len,	/* sq_length */ | 
					
						
							|  |  |  | 	( binaryfunc ) 0,	/* sq_concat */ | 
					
						
							|  |  |  | 	( intargfunc ) 0,	/* sq_repeat */ | 
					
						
							|  |  |  | 	( intargfunc ) Matrix_item,	/* sq_item */ | 
					
						
							|  |  |  | 	( intintargfunc ) Matrix_slice,	/* sq_slice */ | 
					
						
							|  |  |  | 	( intobjargproc ) Matrix_ass_item,	/* sq_ass_item */ | 
					
						
							|  |  |  | 	( intintobjargproc ) Matrix_ass_slice,	/* sq_ass_slice */ | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | static PyNumberMethods Matrix_NumMethods = { | 
					
						
							|  |  |  | 	( binaryfunc ) Matrix_add,	/* __add__ */ | 
					
						
							|  |  |  | 	( binaryfunc ) Matrix_sub,	/* __sub__ */ | 
					
						
							|  |  |  | 	( binaryfunc ) Matrix_mul,	/* __mul__ */ | 
					
						
							|  |  |  | 	( binaryfunc ) 0,	/* __div__ */ | 
					
						
							|  |  |  | 	( binaryfunc ) 0,	/* __mod__ */ | 
					
						
							|  |  |  | 	( binaryfunc ) 0,	/* __divmod__ */ | 
					
						
							|  |  |  | 	( ternaryfunc ) 0,	/* __pow__ */ | 
					
						
							|  |  |  | 	( unaryfunc ) 0,	/* __neg__ */ | 
					
						
							|  |  |  | 	( unaryfunc ) 0,	/* __pos__ */ | 
					
						
							|  |  |  | 	( unaryfunc ) 0,	/* __abs__ */ | 
					
						
							|  |  |  | 	( inquiry ) 0,		/* __nonzero__ */ | 
					
						
							|  |  |  | 	( unaryfunc ) 0,	/* __invert__ */ | 
					
						
							|  |  |  | 	( binaryfunc ) 0,	/* __lshift__ */ | 
					
						
							|  |  |  | 	( binaryfunc ) 0,	/* __rshift__ */ | 
					
						
							|  |  |  | 	( binaryfunc ) 0,	/* __and__ */ | 
					
						
							|  |  |  | 	( binaryfunc ) 0,	/* __xor__ */ | 
					
						
							|  |  |  | 	( binaryfunc ) 0,	/* __or__ */ | 
					
						
							|  |  |  | 	( coercion ) Matrix_coerce,	/* __coerce__ */ | 
					
						
							|  |  |  | 	( unaryfunc ) 0,	/* __int__ */ | 
					
						
							|  |  |  | 	( unaryfunc ) 0,	/* __long__ */ | 
					
						
							|  |  |  | 	( unaryfunc ) 0,	/* __float__ */ | 
					
						
							|  |  |  | 	( unaryfunc ) 0,	/* __oct__ */ | 
					
						
							|  |  |  | 	( unaryfunc ) 0,	/* __hex__ */ | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyTypeObject matrix_Type = { | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 	PyObject_HEAD_INIT( NULL )    /*   required python macro   */ | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	0,	/*ob_size */ | 
					
						
							|  |  |  | 	"Matrix",		/*tp_name */ | 
					
						
							|  |  |  | 	sizeof( MatrixObject ),	/*tp_basicsize */ | 
					
						
							|  |  |  | 	0,			/*tp_itemsize */ | 
					
						
							|  |  |  | 	( destructor ) Matrix_dealloc,	/*tp_dealloc */ | 
					
						
							|  |  |  | 	( printfunc ) 0,	/*tp_print */ | 
					
						
							|  |  |  | 	( getattrfunc ) Matrix_getattr,	/*tp_getattr */ | 
					
						
							|  |  |  | 	( setattrfunc ) Matrix_setattr,	/*tp_setattr */ | 
					
						
							|  |  |  | 	0,			/*tp_compare */ | 
					
						
							|  |  |  | 	( reprfunc ) Matrix_repr,	/*tp_repr */ | 
					
						
							|  |  |  | 	&Matrix_NumMethods,	/*tp_as_number */ | 
					
						
							|  |  |  | 	&Matrix_SeqMethods,	/*tp_as_sequence */ | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | //******************************************************************
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | //Function:                              newMatrixObject
 | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | //******************************************************************
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | PyObject *newMatrixObject( float *mat, int rowSize, int colSize ) | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	MatrixObject *self; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	int row, col, x; | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	if( rowSize < 2 || rowSize > 4 || colSize < 2 || colSize > 4 ) | 
					
						
							|  |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_RuntimeError, | 
					
						
							|  |  |  | 						"row and column sizes must be between 2 and 4\n" ) ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	self = PyObject_NEW( MatrixObject, &matrix_Type ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//generate contigous memory space
 | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 	self->contigPtr = PyMem_Malloc( rowSize * colSize * sizeof( float ) ); | 
					
						
							|  |  |  | 	if( self->contigPtr == NULL ) { | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 						"problem allocating array space\n\n" ) ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	//create pointer array
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	self->matrix = PyMem_Malloc( rowSize * sizeof( float * ) ); | 
					
						
							|  |  |  | 	if( self->matrix == NULL ) { | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 		PyMem_Free( self->contigPtr ); | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 		return ( EXPP_ReturnPyObjError( PyExc_MemoryError, | 
					
						
							|  |  |  | 						"problem allocating pointer space\n\n" ) ); | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	//pointer array points to contigous memory
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	for( x = 0; x < rowSize; x++ ) { | 
					
						
							| 
									
										
										
										
											2004-10-11 17:05:14 +00:00
										 |  |  | 		self->matrix[x] = self->contigPtr + ( x * colSize ); | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if( mat ) {		//if a float array passed
 | 
					
						
							|  |  |  | 		for( row = 0; row < rowSize; row++ ) { | 
					
						
							|  |  |  | 			for( col = 0; col < colSize; col++ ) { | 
					
						
							|  |  |  | 				self->matrix[row][col] = | 
					
						
							|  |  |  | 					mat[( row * colSize ) + col]; | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	} else {		//or if NULL passed
 | 
					
						
							|  |  |  | 		for( row = 0; row < rowSize; row++ ) { | 
					
						
							|  |  |  | 			for( col = 0; col < colSize; col++ ) { | 
					
						
							| 
									
										
										
										
											2004-02-29 13:20:34 +00:00
										 |  |  | 				self->matrix[row][col] = 0.0f; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//set size vars of matrix
 | 
					
						
							|  |  |  | 	self->rowSize = rowSize; | 
					
						
							|  |  |  | 	self->colSize = colSize; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//set coercion flag
 | 
					
						
							|  |  |  | 	self->flag = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-25 20:30:40 +00:00
										 |  |  | 	return ( ( PyObject * ) self ); | 
					
						
							| 
									
										
										
										
											2003-06-22 20:14:11 +00:00
										 |  |  | } |