Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

matrix_basics.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- 00002 @COPYRIGHT : 00003 Copyright 1993,1994,1995 David MacDonald, 00004 Peter Neelin, Louis Collins, 00005 McConnell Brain Imaging Centre, 00006 Montreal Neurological Institute, McGill University. 00007 Permission to use, copy, modify, and distribute this 00008 software and its documentation for any purpose and without 00009 fee is hereby granted, provided that the above copyright 00010 notice appear in all copies. The author and McGill University 00011 make no representations about the suitability of this 00012 software for any purpose. It is provided "as is" without 00013 express or implied warranty. 00014 ---------------------------------------------------------------------------- */ 00015 00016 /* ----------------------------- MNI Header ----------------------------------- 00017 @NAME : matrix_basics.c 00018 @DESCRIPTION: File containing routines for doing basic matrix calculations 00019 @METHOD : Contains routines : 00020 transpose 00021 matrix_multiply 00022 @CALLS : 00023 @CREATED : January 31, 1992 (Peter Neelin) 00024 @MODIFIED : Jul 6, 1995 D. MacDonald - removed recipes-style code 00025 ---------------------------------------------------------------------------- */ 00026 00027 #include <volume_io/internal_volume_io.h> 00028 #include <bicpl/trans.h> 00029 00030 #ifndef lint 00031 static char rcsid[] = "$Header: /software/source//libraries/bicpl/Transforms/matrix_basics.c,v 1.16 2000/02/06 15:30:50 stever Exp $"; 00032 #endif 00033 00034 /* ----------------------------- MNI Header ----------------------------------- 00035 @NAME : transpose 00036 @INPUT : rows - number of rows 00037 cols - number of columns 00038 mat - original matrix 00039 @OUTPUT : mat_transpose - transposed matrix 00040 Matrix mat_transpose can be the original matrix mat. 00041 @RETURNS : (nothing) 00042 @DESCRIPTION: Transposes a matrix. 00043 @METHOD : 00044 @GLOBALS : (none) 00045 @CALLS : (nothing special) 00046 @CREATED : Feb. 26, 1990 (Weiqian Dai) 00047 @MODIFIED : January 31, 1992 (Peter Neelin) 00048 - change to roughly NIL-abiding code and modified calling 00049 sequence. 00050 @MODIFIED : July 4, 1995 D. MacDonald - removed recipes-style code 00051 ---------------------------------------------------------------------------- */ 00052 00053 public void transpose( 00054 int rows, 00055 int cols, 00056 Real **mat, 00057 Real **mat_transpose ) 00058 { 00059 Real swap; 00060 int i, j, square; 00061 00062 if( mat == mat_transpose ) 00063 { 00064 square = MIN( rows, cols ); 00065 00066 for_less( i, 0, rows ) 00067 { 00068 for_less( j, 0, cols ) 00069 { 00070 if( i < square && j < square ) 00071 { 00072 if( i < j ) 00073 { 00074 swap = mat[i][j]; 00075 mat_transpose[i][j] = mat[j][i]; 00076 mat_transpose[j][i] = swap; 00077 } 00078 } 00079 else 00080 mat_transpose[j][i] = mat[i][j]; 00081 } 00082 } 00083 } 00084 else 00085 { 00086 for_less( i, 0, rows ) 00087 { 00088 for_less( j, 0, cols ) 00089 mat_transpose[j][i] = mat[i][j]; 00090 } 00091 } 00092 } 00093 00094 00095 /* ----------------------------- mni Header ----------------------------------- 00096 @NAME : raw_matrix_multiply 00097 @INPUT : ldim, mdim, ndim - dimensions of matrices. Matrix Amat has 00098 dimensions (ldim x mdim), matrix Bmat has dimension 00099 (mdim x ndim) and resultant matrix has dimension 00100 (ldim x ndim). 00101 Amat - First matrix of multiply 00102 Bmat - Second matrix of multiply 00103 @OUTPUT : Cmat - Resulting matrix. This matrix cannot 00104 be either Amat or Bmat. 00105 @RETURNS : (nothing) 00106 @DESCRIPTION: Multiplies two matrices. 00107 @METHOD : 00108 @GLOBALS : (none) 00109 @CALLS : (nothing special) 00110 @CREATED : Feb. 26, 1990 (Weiqian Dai) 00111 @MODIFIED : January 31, 1992 (Peter Neelin) 00112 - change to roughly NIL-abiding code and modified calling 00113 sequence. 00114 @MODIFIED : July 4, 1995 D. MacDonald - removed recipes-style code 00115 ---------------------------------------------------------------------------- */ 00116 00117 private void raw_matrix_multiply( 00118 int ldim, 00119 int mdim, 00120 int ndim, 00121 Real **Amat, 00122 Real **Bmat, 00123 Real **Cmat ) 00124 { 00125 Real sum; 00126 int i, j, k; 00127 00128 /* Calculate the product */ 00129 00130 for_less( i, 0, ldim ) 00131 { 00132 for_less( j, 0, ndim ) 00133 { 00134 sum = 0.0; 00135 for_less( k, 0, mdim ) 00136 sum += Amat[i][k] * Bmat[k][j]; 00137 00138 Cmat[i][j] = sum; 00139 } 00140 } 00141 } 00142 00143 00144 /* ----------------------------- MNI Header ----------------------------------- 00145 @NAME : matrix_multiply 00146 @INPUT : ldim, mdim, ndim - dimensions of matrices. Matrix Amat has 00147 dimensions (ldim x mdim), matrix Bmat has dimension 00148 (mdim x ndim) and resultant matrix has dimension 00149 (ldim x ndim). 00150 Amat - First matrix of multiply 00151 Bmat - Second matrix of multiply 00152 @OUTPUT : Cmat - Resulting matrix. This matrix is allowed to be 00153 matrix Amat or Bmat. 00154 @RETURNS : (nothing) 00155 @DESCRIPTION: Multiplies two matrices. 00156 @METHOD : 00157 @GLOBALS : (none) 00158 @CALLS : (nothing special) 00159 @CREATED : March 2, 1992 (Peter Neelin) 00160 @MODIFIED : March 2, 1992 (P.N.) 00161 - Changed so that calling program can use an input matrix for 00162 output. 00163 @MODIFIED : July 4, 1995 D. MacDonald - removed recipes-style code 00164 ---------------------------------------------------------------------------- */ 00165 00166 public void matrix_multiply( 00167 int ldim, 00168 int mdim, 00169 int ndim, 00170 Real **Amat, 00171 Real **Bmat, 00172 Real **Cmat ) 00173 { 00174 int i, j; 00175 Real **Ctemp; 00176 00177 /* Allocate a temporary matrix */ 00178 00179 ALLOC2D( Ctemp, ldim, ndim ); 00180 00181 /* Do the multiplication */ 00182 00183 raw_matrix_multiply( ldim, mdim, ndim, Amat, Bmat, Ctemp ); 00184 00185 /* Copy the result */ 00186 00187 for_less( i, 0, ldim ) 00188 for_less( j, 0, ndim ) 00189 Cmat[i][j] = Ctemp[i][j]; 00190 00191 /* Free the temporary matrix */ 00192 00193 FREE2D( Ctemp ); 00194 }

Generated on Wed Jul 28 09:10:57 2004 for BICPL by doxygen 1.3.7