Index: trunk/psLib/src/math/psMatrix.c
===================================================================
--- trunk/psLib/src/math/psMatrix.c	(revision 2214)
+++ trunk/psLib/src/math/psMatrix.c	(revision 2273)
@@ -20,6 +20,6 @@
  *  @author Ross Harman, MHPCC
  *   
- *  @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-10-27 20:20:11 $
+ *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-11-04 01:04:59 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -40,53 +40,17 @@
 #include "psVector.h"
 #include "psMatrix.h"
-
-/** Preprocessor macro to generate error a NULL image */
-#define PS_VECTOR_CHECK_NULL(NAME, RETURN)                                                          \
-if (NAME == NULL || NAME->data.V == NULL) {                                                         \
-    psError(__func__,"Invalid operation: %s or its data is NULL.", #NAME);                          \
-    return RETURN;                                                                                  \
-}
-
-/** Preprocessor macro to create vector based on another */
-#define PS_CHECK_ALLOC_VECTOR(NAME, SIZE, PS_TYPE)                                                  \
-if(NAME == NULL) {                                                                                  \
-    NAME = psVectorAlloc(SIZE, PS_TYPE);                                                            \
-}
-
-/** Preprocessor macro to generate error for zero length vector */
-#define PS_CHECK_SIZE_VECTOR(NAME, RETURN)                                                          \
-if (NAME->n < 1) {                                                                                  \
-    psError(__func__,"Invalid operation: %s has zero n value.", #NAME);                             \
-    return RETURN;                                                                                  \
-}
-
-/** Preprocessor macro to generate error a NULL image */
-#define PS_IMAGE_CHECK_NULL(NAME, RETURN)                                                           \
-if (NAME == NULL || NAME->data.V == NULL) {                                                         \
-    psError(__func__,"Invalid operation: %s or its data is NULL.", #NAME);                          \
-    return RETURN;                                                                                  \
-}
-
-/** Preprocessor macro to create image based on another */
-#define PS_CHECK_ALLOC_IMAGE(NAME, NCOLS, NROWS, PS_TYPE)                                           \
-if(NAME == NULL) {                                                                                  \
-    NAME = psImageAlloc(NCOLS, NROWS, PS_TYPE);                                                     \
-}
-
-/** Preprocessor macro to generate error for zero length rows or columns */
-#define PS_CHECK_SIZE_IMAGE(NAME, RETURN)                                                           \
-if (NAME->numCols < 1 || NAME->numRows < 1) {                                                       \
-    psError(__func__,"Invalid operation: %s has zero rows or columns (%dx%d).", #NAME,              \
-            NAME->numCols, NAME->numRows);                                                          \
-    return RETURN;                                                                                  \
-}
+#include "psConstants.h"
+
+
 
 /** Preprocessor macro to generate error for image dimensionality not set to PS_DIMEN_IMAGE */
 #define PS_CHECK_DIMEN_AND_TYPE(NAME, PS_DIMEN, RETURN)                                             \
 if (NAME->type.dimen != PS_DIMEN) {                                                                 \
-    psError(__func__,"Invalid operation: %s incorrect dimensionality %d.", #NAME, PS_DIMEN);        \
+    psError(PS_ERR_BAD_PARAMETER_TYPE, true,                                                        \
+            "Invalid operation. %s has incorrect dimensionality %d.", #NAME, PS_DIMEN);             \
     return RETURN;                                                                                  \
 } else if(NAME->type.type != PS_TYPE_F64) {                                                         \
-    psError(__func__, "Invalid operation: %s not PS_TYPE_F64.", #NAME);                             \
+    psError(PS_ERR_BAD_PARAMETER_TYPE, true,                                                        \
+            "Invalid operation. %s not PS_TYPE_F64.", #NAME);                                       \
     return RETURN;                                                                                  \
 }
@@ -95,5 +59,6 @@
 #define PS_CHECK_POINTERS(NAME1, NAME2, RETURN)                                                     \
 if (NAME1 == NAME2) {                                                                               \
-    psError(__func__,"Invalid operation: Pointer to %s is same as %s.", #NAME1, #NAME2);            \
+    psError(PS_ERR_BAD_PARAMETER_VALUE, true,                                                       \
+            "Invalid operation: Pointer to %s is same as %s.", #NAME1, #NAME2);                     \
     return RETURN;                                                                                  \
 }
@@ -102,5 +67,6 @@
 #define PS_CHECK_SQUARE(NAME, RETURN)                                                               \
 if (NAME->numCols != NAME->numRows) {                                                               \
-    psError(__func__,"Invalid operation: %s not square array.", #NAME);                             \
+    psError(PS_ERR_BAD_PARAMETER_SIZE, true,                                                       \
+            "Invalid operation: %s not square array.", #NAME);                             \
     return RETURN;                                                                                  \
 }
@@ -126,15 +92,20 @@
     gsl_permutation perm;
 
+    #define psMatrixLUD_EXIT { \
+                               psFree(outImage); \
+                               return NULL; \
+                             }
+
     // Error checks
     PS_CHECK_POINTERS(inImage, outImage, outImage);
-    PS_IMAGE_CHECK_NULL(inImage, outImage);
     PS_CHECK_DIMEN_AND_TYPE(inImage, PS_DIMEN_IMAGE, outImage);
-    PS_CHECK_SIZE_IMAGE(inImage, outImage);
-    PS_CHECK_ALLOC_IMAGE(outImage, inImage->numCols, inImage->numRows, inImage->type.type);
-    PS_CHECK_DIMEN_AND_TYPE(outImage, PS_DIMEN_IMAGE, outImage);
-    PS_CHECK_SIZE_IMAGE(outImage, outImage);
-    PS_CHECK_ALLOC_VECTOR(outPerm, inImage->numRows, inImage->type.type);
-    PS_VECTOR_CHECK_NULL(outPerm, outImage);
+
+    PS_IMAGE_CHECK_NULL_GENERAL(inImage, psMatrixLUD_EXIT);
+    PS_VECTOR_CHECK_NULL_GENERAL(outPerm, psMatrixLUD_EXIT);
+
     PS_CHECK_DIMEN_AND_TYPE(outPerm, PS_DIMEN_VECTOR, outImage);
+
+    psImageRecycle(outImage, inImage->numCols, inImage->numRows, inImage->type.type);
+    psVectorRecycle(outPerm, inImage->numRows, inImage->type.type);
 
     // Initialize data
@@ -179,6 +150,5 @@
     PS_IMAGE_CHECK_NULL(inImage, outVector);
     PS_CHECK_DIMEN_AND_TYPE(inImage, PS_DIMEN_IMAGE, outVector);
-    PS_CHECK_SIZE_IMAGE(inImage, outVector);
-    PS_CHECK_ALLOC_VECTOR(outVector, inImage->numRows, inImage->type.type);
+    PS_IMAGE_CHECK_EMPTY(inImage, outVector);
     PS_VECTOR_CHECK_NULL(outVector, outVector);
     PS_CHECK_DIMEN_AND_TYPE(outVector, PS_DIMEN_VECTOR, outVector);
@@ -188,4 +158,7 @@
     PS_CHECK_DIMEN_AND_TYPE(inPerm, PS_DIMEN_VECTOR, outVector);
 
+    psVectorRecycle(outVector, inImage->numRows, inImage->type.type);
+
+
     // Initialize data
     numRows = inImage->numRows;
@@ -226,15 +199,11 @@
 
     // Error checks
-    if (det == NULL) {
-        psError(__func__, "Invalid operation: determinant argument is NULL.");
-        return outImage;
-    }
+    PS_PTR_CHECK_NULL(det, outImage);
     PS_CHECK_POINTERS(inImage, outImage, outImage);
     PS_IMAGE_CHECK_NULL(inImage, outImage);
     PS_CHECK_DIMEN_AND_TYPE(inImage, PS_DIMEN_IMAGE, outImage);
-    PS_CHECK_SIZE_IMAGE(inImage, outImage);
-    PS_CHECK_ALLOC_IMAGE(outImage, inImage->numCols, inImage->numRows, inImage->type.type);
-    PS_CHECK_DIMEN_AND_TYPE(outImage, PS_DIMEN_IMAGE, outImage);
-    PS_CHECK_SIZE_IMAGE(outImage, outImage);
+    PS_IMAGE_CHECK_EMPTY(inImage, outImage);
+
+    psImageRecycle(outImage, inImage->numCols, inImage->numRows, inImage->type.type);
 
     // Initialize data
@@ -282,5 +251,5 @@
     PS_IMAGE_CHECK_NULL(inImage, NULL);
     PS_CHECK_DIMEN_AND_TYPE(inImage, PS_DIMEN_IMAGE, NULL);
-    PS_CHECK_SIZE_IMAGE(inImage, NULL);
+    PS_IMAGE_CHECK_EMPTY(inImage, NULL);
 
     // Initialize data
@@ -325,11 +294,11 @@
     PS_IMAGE_CHECK_NULL(inImage1, outImage);
     PS_CHECK_DIMEN_AND_TYPE(inImage1, PS_DIMEN_IMAGE, outImage);
-    PS_CHECK_SIZE_IMAGE(inImage1, outImage);
+    PS_IMAGE_CHECK_EMPTY(inImage1, outImage);
     PS_IMAGE_CHECK_NULL(inImage2, outImage);
     PS_CHECK_DIMEN_AND_TYPE(inImage2, PS_DIMEN_IMAGE, outImage);
-    PS_CHECK_SIZE_IMAGE(inImage2, outImage);
-    PS_CHECK_ALLOC_IMAGE(outImage, inImage2->numCols, inImage2->numRows, inImage2->type.type);
+    PS_IMAGE_CHECK_EMPTY(inImage2, outImage);
     PS_CHECK_DIMEN_AND_TYPE(inImage1, PS_DIMEN_IMAGE, outImage);
-    PS_CHECK_SIZE_IMAGE(outImage, outImage);
+
+    psImageRecycle(outImage, inImage2->numCols, inImage2->numRows, inImage2->type.type);
 
     // Initialize data
@@ -364,8 +333,7 @@
     PS_IMAGE_CHECK_NULL(inImage, outImage);
     PS_CHECK_DIMEN_AND_TYPE(inImage, PS_DIMEN_IMAGE, outImage);
-    PS_CHECK_SIZE_IMAGE(inImage, outImage);
-    PS_CHECK_ALLOC_IMAGE(outImage, inImage->numCols, inImage->numRows, inImage->type.type);
-    PS_CHECK_DIMEN_AND_TYPE(outImage, PS_DIMEN_IMAGE, outImage);
-    PS_CHECK_SIZE_IMAGE(outImage, outImage);
+    PS_IMAGE_CHECK_EMPTY(inImage, outImage);
+
+    psImageRecycle(outImage, inImage->numCols, inImage->numRows, inImage->type.type);
 
     // Initialize data
@@ -403,8 +371,7 @@
     PS_IMAGE_CHECK_NULL(inImage, outImage);
     PS_CHECK_DIMEN_AND_TYPE(inImage, PS_DIMEN_IMAGE, outImage);
-    PS_CHECK_SIZE_IMAGE(inImage, outImage);
-    PS_CHECK_ALLOC_IMAGE(outImage, inImage->numCols, inImage->numRows, inImage->type.type);
-    PS_CHECK_DIMEN_AND_TYPE(outImage, PS_DIMEN_IMAGE, outImage);
-    PS_CHECK_SIZE_IMAGE(outImage, outImage);
+    PS_IMAGE_CHECK_EMPTY(inImage, outImage);
+
+    psImageRecycle(outImage, inImage->numCols, inImage->numRows, inImage->type.type);
 
     // Initialize data
@@ -438,19 +405,25 @@
     psS32 size = 0;
 
-    // Error checks
-    PS_IMAGE_CHECK_NULL(inImage, outVector);
+    #define psMatrixToVector_EXIT { \
+                                    psFree(outVector); \
+                                    return NULL; \
+                                  }
+
+    // Error checks
+    PS_IMAGE_CHECK_NULL_GENERAL(inImage, psMatrixToVector_EXIT);
     PS_CHECK_DIMEN_AND_TYPE(inImage, PS_DIMEN_IMAGE, outVector);
-    PS_CHECK_SIZE_IMAGE(inImage, outVector);
+    PS_IMAGE_CHECK_EMPTY_GENERAL(inImage, psMatrixToVector_EXIT);
 
     if (inImage->numRows == 1) {
         // Create transposed row vector
-        PS_CHECK_ALLOC_VECTOR(outVector, inImage->numCols, inImage->type.type);
+        psVectorRecycle(outVector, inImage->numCols, inImage->type.type);
         outVector->type.dimen = PS_DIMEN_TRANSV;
     } else if (inImage->numCols == 1) {
         // Create non-transposed column vector
-        PS_CHECK_ALLOC_VECTOR(outVector, inImage->numRows, inImage->type.type);
+        psVectorRecycle(outVector, inImage->numRows, inImage->type.type);
     } else {
-        psError(__func__, "Image does not have dim with 1 col or 1 row: (%d x %d).", inImage->numRows,
-                inImage->numCols);
+        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                "Image does not have dim with 1 col or 1 row: (%d x %d).",
+                inImage->numRows, inImage->numCols);
         return outVector;
     }
@@ -467,5 +440,7 @@
 
         if (outVector->n != inImage->numRows) {
-            psError(__func__, "Image and vector sizes differ: (%d vs %d).", inImage->numRows, outVector->n);
+            psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                    "Image and vector sizes differ: (%d vs %d).",
+                    inImage->numRows, outVector->n);
             return outVector;
         }
@@ -481,5 +456,7 @@
 
         if (outVector->n != inImage->numCols) {
-            psError(__func__, "Image and vector sizes differ: (%d vs %d).", inImage->numCols, outVector->n);
+            psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                    "Image and vector sizes differ: (%d vs %d).",
+                    inImage->numCols, outVector->n);
             return outVector;
         }
@@ -502,12 +479,16 @@
     if (inVector->type.dimen == PS_DIMEN_VECTOR) {
         PS_CHECK_DIMEN_AND_TYPE(inVector, PS_DIMEN_VECTOR, outImage);
-        PS_CHECK_SIZE_VECTOR(inVector, outImage);
-        PS_CHECK_ALLOC_IMAGE(outImage, 1, inVector->n, PS_TYPE_F64)
+        PS_VECTOR_CHECK_EMPTY(inVector, outImage);
+        psImageRecycle(outImage, 1, inVector->n, PS_TYPE_F64);
         // More checks for PS_DIMEN_VECTOR
         if (outImage->numCols > 1) {
-            psError(__func__, "Image has more than 1 column: numCols = %d.", outImage->numCols);
+            psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                    "Image has more than 1 column: numCols = %d.",
+                    outImage->numCols);
             return outImage;
         } else if (outImage->numRows != inVector->n) {
-            psError(__func__, "Image and vector sizes differ: (%d vs %d).", outImage->numRows, inVector->n);
+            psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                    "Image and vector sizes differ: (%d vs %d).",
+                    outImage->numRows, inVector->n);
             return outImage;
         }
@@ -517,12 +498,16 @@
     } else if (inVector->type.dimen == PS_DIMEN_TRANSV) {
         PS_CHECK_DIMEN_AND_TYPE(inVector, PS_DIMEN_TRANSV, outImage);
-        PS_CHECK_SIZE_VECTOR(inVector, outImage);
-        PS_CHECK_ALLOC_IMAGE(outImage, inVector->n, 1, PS_TYPE_F64)
+        PS_VECTOR_CHECK_EMPTY(inVector, outImage);
+        psImageRecycle(outImage, inVector->n, 1, PS_TYPE_F64);
         // More checks for PS_DIMEN_TRANSV
         if (outImage->numRows > 1) {
-            psError(__func__, "Image has more than 1 row: numRows = %d.", outImage->numRows);
+            psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                    "Image has more than 1 row: numRows = %d.",
+                    outImage->numRows);
             return outImage;
         } else if (outImage->numCols != inVector->n) {
-            psError(__func__, "Image and vector sizes differ: (%d vs %d).", outImage->numCols, inVector->n);
+            psError(PS_ERR_BAD_PARAMETER_SIZE, true,
+                    "Image and vector sizes differ: (%d vs %d).",
+                    outImage->numCols, inVector->n);
             return outImage;
         }
