Changeset 4388 for trunk/psLib/src/dataManip/psMatrix.c
- Timestamp:
- Jun 24, 2005, 3:15:01 PM (21 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/dataManip/psMatrix.c (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/dataManip/psMatrix.c
r4385 r4388 21 21 * @author Robert DeSonia, MHPCC 22 22 * 23 * @version $Revision: 1.3 3$ $Name: not supported by cvs2svn $24 * @date $Date: 2005-06-25 0 0:51:28$23 * @version $Revision: 1.34 $ $Name: not supported by cvs2svn $ 24 * @date $Date: 2005-06-25 01:15:01 $ 25 25 * 26 26 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 185 185 /*****************************************************************************/ 186 186 187 psImage* psMatrixLUD(psImage* out Image, psVector** outPerm, psImage* inImage)187 psImage* psMatrixLUD(psImage* out, psVector** perm, psImage* in) 188 188 { 189 189 psS32 signum = 0; … … 194 194 195 195 196 #define psMatrixLUD_EXIT {psFree(out Image); return NULL;}197 198 // Error checks 199 PS_ASSERT_GENERAL_IMAGE_NON_NULL(in Image, psMatrixLUD_EXIT);200 PS_CHECK_POINTERS(in Image, outImage, psMatrixLUD_EXIT);201 PS_CHECK_DIMEN_AND_TYPE(in Image, PS_DIMEN_IMAGE, psMatrixLUD_EXIT);202 PS_ASSERT_GENERAL_PTR_NON_NULL( outPerm, psMatrixLUD_EXIT);203 204 out Image = psImageRecycle(outImage, inImage->numCols, inImage->numRows, inImage->type.type);205 206 PS_CHECK_SQUARE(in Image, psMatrixLUD_EXIT); // gsl_linalg_LU_decomp would fail on non-square input.207 PS_CHECK_SQUARE(out Image, psMatrixLUD_EXIT);196 #define psMatrixLUD_EXIT {psFree(out); return NULL;} 197 198 // Error checks 199 PS_ASSERT_GENERAL_IMAGE_NON_NULL(in, psMatrixLUD_EXIT); 200 PS_CHECK_POINTERS(in, out, psMatrixLUD_EXIT); 201 PS_CHECK_DIMEN_AND_TYPE(in, PS_DIMEN_IMAGE, psMatrixLUD_EXIT); 202 PS_ASSERT_GENERAL_PTR_NON_NULL(perm, psMatrixLUD_EXIT); 203 204 out = psImageRecycle(out, in->numCols, in->numRows, in->type.type); 205 206 PS_CHECK_SQUARE(in, psMatrixLUD_EXIT); // gsl_linalg_LU_decomp would fail on non-square input. 207 PS_CHECK_SQUARE(out, psMatrixLUD_EXIT); 208 208 209 209 // Initialize data 210 numRows = in Image->numRows;211 numCols = in Image->numCols;210 numRows = in->numRows; 211 numCols = in->numCols; 212 212 213 213 // Initialize GSL data 214 214 permGSL.size = numCols; 215 215 if (sizeof(size_t) == 4) { 216 * outPerm = psVectorRecycle(*outPerm, numCols, PS_TYPE_S32);216 *perm = psVectorRecycle(*perm, numCols, PS_TYPE_S32); 217 217 } else if (sizeof(size_t) == 8) { 218 * outPerm = psVectorRecycle(*outPerm, numCols, PS_TYPE_S64);218 *perm = psVectorRecycle(*perm, numCols, PS_TYPE_S64); 219 219 } else { 220 220 psError(PS_ERR_UNKNOWN, true, … … 224 224 } 225 225 226 (* outPerm)->n = numCols;227 permGSL.data = (psPtr)((* outPerm)->data.U8);226 (*perm)->n = numCols; 227 permGSL.data = (psPtr)((*perm)->data.U8); 228 228 lu = gsl_matrix_alloc(numRows, numCols); 229 229 230 230 // Copy psImage data into GSL matrix data 231 psImageToGslMatrix(lu, in Image);231 psImageToGslMatrix(lu, in); 232 232 233 233 // Calculate LU decomposition … … 235 235 236 236 // Copy GSL matrix data to psImage data 237 gslMatrixToPsImage(out Image, lu);237 gslMatrixToPsImage(out, lu); 238 238 239 239 // Free GSL data 240 240 gsl_matrix_free(lu); 241 241 242 return out Image;243 } 244 245 psVector* psMatrixLUSolve(psVector* out Vector, const psImage* inImage, const psVector* inVector,246 const psVector* inPerm)242 return out; 243 } 244 245 psVector* psMatrixLUSolve(psVector* out, const psImage* LU, const psVector* RHS, 246 const psVector* perm) 247 247 { 248 248 psS32 numRows = 0; 249 249 psS32 numCols = 0; 250 250 gsl_matrix *lu; 251 gsl_permutation perm ;251 gsl_permutation permGSL; 252 252 gsl_vector *b = NULL; 253 253 gsl_vector *x = NULL; 254 254 255 #define LUSOLVE_CLEANUP {psFree(out Vector); return NULL;}256 257 // Error checks 258 PS_ASSERT_GENERAL_IMAGE_NON_NULL( inImage, LUSOLVE_CLEANUP);259 PS_CHECK_DIMEN_AND_TYPE( inImage, PS_DIMEN_IMAGE, LUSOLVE_CLEANUP);260 PS_ASSERT_GENERAL_IMAGE_NON_EMPTY( inImage, LUSOLVE_CLEANUP);261 PS_ASSERT_GENERAL_VECTOR_NON_NULL( inVector, LUSOLVE_CLEANUP);262 PS_CHECK_DIMEN_AND_TYPE( inVector, PS_DIMEN_VECTOR, LUSOLVE_CLEANUP);263 PS_ASSERT_GENERAL_VECTOR_NON_NULL( inPerm, LUSOLVE_CLEANUP);264 265 out Vector = psVectorRecycle(outVector, inImage->numRows, inImage->type.type);266 267 PS_CHECK_POINTERS(out Vector, inVector, LUSOLVE_CLEANUP);268 PS_CHECK_POINTERS( inVector, inPerm, LUSOLVE_CLEANUP);269 PS_CHECK_POINTERS(out Vector, inPerm, LUSOLVE_CLEANUP);255 #define LUSOLVE_CLEANUP {psFree(out); return NULL;} 256 257 // Error checks 258 PS_ASSERT_GENERAL_IMAGE_NON_NULL(LU, LUSOLVE_CLEANUP); 259 PS_CHECK_DIMEN_AND_TYPE(LU, PS_DIMEN_IMAGE, LUSOLVE_CLEANUP); 260 PS_ASSERT_GENERAL_IMAGE_NON_EMPTY(LU, LUSOLVE_CLEANUP); 261 PS_ASSERT_GENERAL_VECTOR_NON_NULL(RHS, LUSOLVE_CLEANUP); 262 PS_CHECK_DIMEN_AND_TYPE(RHS, PS_DIMEN_VECTOR, LUSOLVE_CLEANUP); 263 PS_ASSERT_GENERAL_VECTOR_NON_NULL(perm, LUSOLVE_CLEANUP); 264 265 out = psVectorRecycle(out, LU->numRows, LU->type.type); 266 267 PS_CHECK_POINTERS(out, RHS, LUSOLVE_CLEANUP); 268 PS_CHECK_POINTERS(RHS, perm, LUSOLVE_CLEANUP); 269 PS_CHECK_POINTERS(out, perm, LUSOLVE_CLEANUP); 270 270 271 271 // Initialize data 272 numRows = inImage->numRows;273 numCols = inImage->numCols;272 numRows = LU->numRows; 273 numCols = LU->numCols; 274 274 275 275 // Initialize GSL data 276 276 lu = gsl_matrix_alloc(numRows, numCols); 277 psImageToGslMatrix(lu, inImage);278 b = gsl_vector_alloc( inVector->n);279 psVectorToGslVector(b, inVector);280 x = gsl_vector_alloc( inVector->n);281 282 out Vector->n = numCols;283 perm .size = inPerm->n;284 perm .data = (psPtr)(inPerm->data.U8);277 psImageToGslMatrix(lu, LU); 278 b = gsl_vector_alloc(RHS->n); 279 psVectorToGslVector(b, RHS); 280 x = gsl_vector_alloc(RHS->n); 281 282 out->n = numCols; 283 permGSL.size = perm->n; 284 permGSL.data = (psPtr)(perm->data.U8); 285 285 286 286 // Solve for {x} in equation: {b} = [A]{x} 287 gsl_linalg_LU_solve(lu, &perm , b, x);287 gsl_linalg_LU_solve(lu, &permGSL, b, x); 288 288 289 289 // Copy GSL vector data to psVector data 290 gslVectorToPsVector(out Vector, x);290 gslVectorToPsVector(out, x); 291 291 292 292 // Free GSL data … … 295 295 gsl_matrix_free(lu); 296 296 297 return out Vector;297 return out; 298 298 } 299 299 … … 383 383 } 384 384 385 psImage* psMatrixMultiply(psImage* out Image, psImage* inImage1, psImage* inImage2)385 psImage* psMatrixMultiply(psImage* out, psImage* in1, psImage* in2) 386 386 { 387 387 gsl_matrix *m1 = NULL; … … 389 389 gsl_matrix *m3 = NULL; 390 390 391 #define MULTIPLY_CLEANUP { psFree(out Image); return NULL; }392 393 // Error checks 394 PS_ASSERT_GENERAL_IMAGE_NON_NULL(in Image1, MULTIPLY_CLEANUP);395 PS_ASSERT_GENERAL_IMAGE_NON_NULL(in Image2, MULTIPLY_CLEANUP);396 PS_ASSERT_GENERAL_IMAGE_NON_EMPTY(in Image1, MULTIPLY_CLEANUP);397 PS_ASSERT_GENERAL_IMAGE_NON_EMPTY(in Image2, MULTIPLY_CLEANUP);398 PS_CHECK_DIMEN_AND_TYPE(in Image1, PS_DIMEN_IMAGE, MULTIPLY_CLEANUP);399 PS_CHECK_DIMEN_AND_TYPE(in Image2, PS_DIMEN_IMAGE, MULTIPLY_CLEANUP);400 PS_CHECK_POINTERS(in Image1, outImage, MULTIPLY_CLEANUP);401 PS_CHECK_POINTERS(in Image1, inImage2, MULTIPLY_CLEANUP);402 403 if (in Image1->numRows != inImage2->numCols) {404 psError(PS_ERR_BAD_PARAMETER_SIZE, true, "Invalid operation: number of rows of in Image1 != number of cols of inImage2.");391 #define MULTIPLY_CLEANUP { psFree(out); return NULL; } 392 393 // Error checks 394 PS_ASSERT_GENERAL_IMAGE_NON_NULL(in1, MULTIPLY_CLEANUP); 395 PS_ASSERT_GENERAL_IMAGE_NON_NULL(in2, MULTIPLY_CLEANUP); 396 PS_ASSERT_GENERAL_IMAGE_NON_EMPTY(in1, MULTIPLY_CLEANUP); 397 PS_ASSERT_GENERAL_IMAGE_NON_EMPTY(in2, MULTIPLY_CLEANUP); 398 PS_CHECK_DIMEN_AND_TYPE(in1, PS_DIMEN_IMAGE, MULTIPLY_CLEANUP); 399 PS_CHECK_DIMEN_AND_TYPE(in2, PS_DIMEN_IMAGE, MULTIPLY_CLEANUP); 400 PS_CHECK_POINTERS(in1, out, MULTIPLY_CLEANUP); 401 PS_CHECK_POINTERS(in1, in2, MULTIPLY_CLEANUP); 402 403 if (in1->numRows != in2->numCols) { 404 psError(PS_ERR_BAD_PARAMETER_SIZE, true, "Invalid operation: number of rows of in1 != number of cols of in2."); 405 405 MULTIPLY_CLEANUP; 406 406 } 407 if (in Image1->type.type != inImage2->type.type) {408 psError(PS_ERR_BAD_PARAMETER_SIZE, true, "Invalid operation: data types of in Image1 and inImage2 must match.");407 if (in1->type.type != in2->type.type) { 408 psError(PS_ERR_BAD_PARAMETER_SIZE, true, "Invalid operation: data types of in1 and in2 must match."); 409 409 MULTIPLY_CLEANUP; 410 410 } 411 411 412 out Image = psImageRecycle(outImage, inImage2->numCols, inImage1->numRows, inImage2->type.type);412 out = psImageRecycle(out, in2->numCols, in1->numRows, in2->type.type); 413 413 414 414 // Initialize GSL data 415 m1 = gsl_matrix_alloc(in Image1->numRows, inImage1->numCols);416 psImageToGslMatrix(m1, in Image1);417 m2 = gsl_matrix_alloc(in Image2->numRows, inImage2->numCols);418 psImageToGslMatrix(m2, in Image2);419 m3 = gsl_matrix_alloc(out Image->numRows, outImage->numCols);415 m1 = gsl_matrix_alloc(in1->numRows, in1->numCols); 416 psImageToGslMatrix(m1, in1); 417 m2 = gsl_matrix_alloc(in2->numRows, in2->numCols); 418 psImageToGslMatrix(m2, in2); 419 m3 = gsl_matrix_alloc(out->numRows, out->numCols); 420 420 421 421 // Perform multiplication … … 424 424 425 425 // Copy GSL matrix data to psImage data 426 gslMatrixToPsImage(out Image, m3);426 gslMatrixToPsImage(out, m3); 427 427 428 428 // Free GSL structs … … 431 431 gsl_matrix_free(m3); 432 432 433 return out Image;434 } 435 436 psImage* psMatrixTranspose(psImage* out Image, const psImage* inImage)433 return out; 434 } 435 436 psImage* psMatrixTranspose(psImage* out, const psImage* in) 437 437 { 438 438 psU32 i = 0; … … 443 443 psS32 numColsOut = 0; 444 444 445 #define TRANSPOSE_CLEANUP { psFree(out Image); return NULL; }446 // Error checks 447 PS_ASSERT_GENERAL_IMAGE_NON_NULL(in Image, TRANSPOSE_CLEANUP);448 PS_CHECK_DIMEN_AND_TYPE(in Image, PS_DIMEN_IMAGE, TRANSPOSE_CLEANUP);449 PS_ASSERT_GENERAL_IMAGE_NON_EMPTY(in Image, TRANSPOSE_CLEANUP);450 PS_CHECK_POINTERS(in Image, outImage, TRANSPOSE_CLEANUP);451 452 out Image = psImageRecycle(outImage, inImage->numCols, inImage->numRows, inImage->type.type);445 #define TRANSPOSE_CLEANUP { psFree(out); return NULL; } 446 // Error checks 447 PS_ASSERT_GENERAL_IMAGE_NON_NULL(in, TRANSPOSE_CLEANUP); 448 PS_CHECK_DIMEN_AND_TYPE(in, PS_DIMEN_IMAGE, TRANSPOSE_CLEANUP); 449 PS_ASSERT_GENERAL_IMAGE_NON_EMPTY(in, TRANSPOSE_CLEANUP); 450 PS_CHECK_POINTERS(in, out, TRANSPOSE_CLEANUP); 451 452 out = psImageRecycle(out, in->numCols, in->numRows, in->type.type); 453 453 454 454 // Initialize data 455 numRowsIn = in Image->numRows;456 numColsIn = in Image->numCols;457 numRowsOut = out Image->numRows;458 numColsOut = out Image->numCols;455 numRowsIn = in->numRows; 456 numColsIn = in->numCols; 457 numRowsOut = out->numRows; 458 numColsOut = out->numCols; 459 459 460 460 if(numRowsIn!=numColsOut && numRowsOut!=numColsIn) { … … 463 463 } 464 464 465 if(out Image->type.type == PS_TYPE_F32) {465 if(out->type.type == PS_TYPE_F32) { 466 466 for(i=0; i<numRowsOut; i++) { 467 467 for(j=0; j<numColsOut; j++) { 468 out Image->data.F32[i][j] = inImage->data.F32[j][i];468 out->data.F32[i][j] = in->data.F32[j][i]; 469 469 } 470 470 } … … 472 472 for(i=0; i<numRowsOut; i++) { 473 473 for(j=0; j<numColsOut; j++) { 474 out Image->data.F64[i][j] = inImage->data.F64[j][i];474 out->data.F64[i][j] = in->data.F64[j][i]; 475 475 } 476 476 } 477 477 } 478 478 479 return out Image;479 return out; 480 480 } 481 481
Note:
See TracChangeset
for help on using the changeset viewer.
