Index: trunk/psLib/src/math/psMinimize.c
===================================================================
--- trunk/psLib/src/math/psMinimize.c	(revision 2252)
+++ trunk/psLib/src/math/psMinimize.c	(revision 2265)
@@ -9,6 +9,6 @@
  *  @author GLG, MHPCC
  *
- *  @version $Revision: 1.81 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-11-02 01:04:37 $
+ *  @version $Revision: 1.82 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-11-02 03:57:21 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -1361,5 +1361,5 @@
         min->value = func(params, coords);
         psTrace(".psLib.dataManip.p_psLineMin", 2,
-                "p_psLineMin() called with zero line vector.  Return 0.0.  Function valus is %f\n", min->value);
+                "p_psLineMin() called with zero line vector.  Return 0.0.  Function value is %f\n", min->value);
         return(0.0);
     }
@@ -1469,8 +1469,9 @@
 XXX: Define constants for default dummy number of iterations and tolerance.
  
-XXX: Must define behavior on various NULL inputs.
- 
 XXX: Check for F32 types?
  *****************************************************************************/
+#define PS_MINIMIZE_POWELL_LINEMIN_MAX_ITERATIONS 20
+#define PS_MINIMIZE_POWELL_LINEMIN_ERROR_TOLERANCE 0.01
+
 psBool psMinimizePowell(psMinimization *min,
                         psVector *params,
@@ -1485,5 +1486,4 @@
     PS_PTR_CHECK_NULL(coords, NULL);
     PS_PTR_CHECK_NULL(func, NULL);
-
     psS32 numDims = params->n;
     PS_VECTOR_GEN_STATIC_RECYCLED(pQP, numDims, PS_TYPE_F32);
@@ -1492,6 +1492,5 @@
     psS32 i = 0;
     psS32 j = 0;
-    psVector **v = NULL;
-    psVector *myParamMask = NULL;  // XXX: Use a recylced vector here.
+    psVector *myParamMask = NULL;
     psMinimization dummyMin;
     float mul = 0.0;
@@ -1500,8 +1499,4 @@
     psS32 biggestIter = 0;
     float biggestDiff = 0.0;
-    float term1 = 0.0;
-    float term2 = 0.0;
-    float fqp = 0.0;
-    float diff = 0.0;
     int iterationNumber = 0;
 
@@ -1526,12 +1521,13 @@
 
     // 1: Set v[i] to be the unit vectors for each dimension in params
-    v = (psVector **) psAlloc(params->n * sizeof(psVector **));
+
+    psArray *v = psArrayAlloc(numDims);
     for (i=0;i<numDims;i++) {
-        v[i] = psVectorAlloc(numDims, PS_TYPE_F32);
+        (v->data[i]) = (psVector *) psVectorAlloc(numDims, PS_TYPE_F32);
         for (j=0;j<numDims;j++) {
             if (i == j) {
-                v[i]->data.F32[j] = 1.0;
+                ((psVector *) (v->data[i]))->data.F32[j] = 1.0;
             } else {
-                v[i]->data.F32[j] = 0.0;
+                ((psVector *) (v->data[i]))->data.F32[j] = 0.0;
             }
         }
@@ -1558,17 +1554,13 @@
                 "Current function value is %f\n", currFuncVal);
 
-        diff = 0.0;
         biggestDiff = 0;
         biggestIter = 0;
         for (i=0;i<numDims;i++) {
             if (myParamMask->data.U8[i] == 0) {
-                dummyMin.maxIter = 20;
-                dummyMin.tol = 0.01;
-                mul = p_psLineMin(&dummyMin, Q, v[i], myParamMask, coords, func);
+                dummyMin.maxIter = PS_MINIMIZE_POWELL_LINEMIN_MAX_ITERATIONS;
+                dummyMin.tol = PS_MINIMIZE_POWELL_LINEMIN_ERROR_TOLERANCE;
+                mul = p_psLineMin(&dummyMin, Q, ((psVector *) v->data[i]), myParamMask, coords, func);
                 if (isnan(mul)) {
                     psError(__func__, "Could not perform line minimization (1).\n");
-                    for (j=0;j<numDims;j++) {
-                        psFree(v[j]);
-                    }
                     psFree(v);
                     return(false);
@@ -1608,10 +1600,7 @@
         }
 
-        mul = p_psLineMin(min, params, u, myParamMask, coords, func);
+        mul = p_psLineMin(&dummyMin, params, u, myParamMask, coords, func);
         if (isnan(mul)) {
             psError(__func__, "Could not perform line minimization. (2)\n");
-            for (i=0;i<numDims;i++) {
-                psFree(v[i]);
-            }
             psFree(v);
             return(false);
@@ -1619,8 +1608,5 @@
 
         // 6:
-        if (min->value > currFuncVal) {
-            for (i=0;i<numDims;i++) {
-                psFree(v[i]);
-            }
+        if (dummyMin.value > currFuncVal) {
             psFree(v);
             min->iter = iterationNumber;
@@ -1637,15 +1623,14 @@
             }
         }
-        fqp = func(pQP, coords);
-
-        term1 = (baseFuncVal - currFuncVal) - biggestDiff;
+        float fqp = func(pQP, coords);
+        float term1 = (baseFuncVal - currFuncVal) - biggestDiff;
         term1*= term1;
         term1*= 2.0 * (baseFuncVal - (2.0 * currFuncVal) + fqp);
-        term2 = baseFuncVal - fqp;
+        float term2 = baseFuncVal - fqp;
         term2*= term2 * biggestDiff;
         if (term1 < term2) {
             for (i=0;i<numDims;i++) {
                 if (myParamMask->data.U8[i] == 0) {
-                    v[biggestIter]->data.F32[i] = u->data.F32[i];
+                    ((psVector *) v->data[biggestIter])->data.F32[i] = u->data.F32[i];
                 }
             }
@@ -1661,7 +1646,4 @@
         // 8: Go to step 3 until the change is less than some tolerance.
         if (fabs(baseFuncVal - currFuncVal) <= min->tol) {
-            for (i=0;i<numDims;i++) {
-                psFree(v[i]);
-            }
             psFree(v);
             min->iter = iterationNumber;
@@ -1672,7 +1654,4 @@
     }
 
-    for (i=0;i<numDims;i++) {
-        psFree(v[i]);
-    }
     psFree(v);
     min->iter = iterationNumber;
