Index: trunk/psLib/src/dataManip/psMinimize.c
===================================================================
--- trunk/psLib/src/dataManip/psMinimize.c	(revision 1719)
+++ trunk/psLib/src/dataManip/psMinimize.c	(revision 1734)
@@ -9,14 +9,15 @@
  *  @author George Gusciora, MHPCC
  *
- *  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-09-08 06:02:23 $
+ *  @version $Revision: 1.35 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-09-08 23:32:03 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
+ *
+ *  XXX: must follow coding name standards on local functions.
+ *
  */
 
 /*****************************************************************************/
-
 /* INCLUDE FILES                                                             */
-
 /*****************************************************************************/
 #include <stdlib.h>
@@ -46,8 +47,7 @@
 
 /*****************************************************************************/
-
 /* DEFINE STATEMENTS                                                         */
-
 /*****************************************************************************/
+
 #define MAX_LMM_ITERATIONS 100
 #define MAX_MINIMIZE_ITERATIONS 100
@@ -91,8 +91,7 @@
 
 /*****************************************************************************/
-
 /* TYPE DEFINITIONS                                                          */
-
 /*****************************************************************************/
+
 typedef struct
 {
@@ -121,23 +120,20 @@
 
 /*****************************************************************************/
-
 /* GLOBAL VARIABLES                                                          */
-
 /*****************************************************************************/
 
+static psMinimizeChi2PowellFunc Chi2PowellFunc = NULL;
+//static psMinimizePowellFunc PowellFunc = NULL;
+static psVector *myValue;
+static psVector *myError;
+
+/*****************************************************************************/
+/* FILE STATIC VARIABLES                                                     */
+/*****************************************************************************/
+
 // None
 
 /*****************************************************************************/
-
-/* FILE STATIC VARIABLES                                                     */
-
-/*****************************************************************************/
-
-// None
-
-/*****************************************************************************/
-
 /* FUNCTION IMPLEMENTATION - LOCAL                                           */
-
 /*****************************************************************************/
 
@@ -543,7 +539,5 @@
 
 /*****************************************************************************/
-
 /* FUNCTION IMPLEMENTATION - PUBLIC                                          */
-
 /*****************************************************************************/
 
@@ -850,4 +844,34 @@
     return (initialGuess);
 }
+/******************************************************************************
+GUS
+ 
+XXX: bug report for the psMatrix covar argument.
+ *****************************************************************************/
+bool psMinimizeLM(psMinimization *min,
+                  psImage *covar,
+                  psVector *params,
+                  const psVector *paramMask,
+                  const psArray *coords,
+                  psMinimizeLMFunc func)
+{
+    /*
+        psVector *beta = psVectorAlloc(params->n, PS_TYPE_F32);
+        psImage *alpha = psImageAlloc(params->n, params->n, PS_TYPE_F32);
+        psVector *deriv;
+        int i;
+        int k;
+        float tmp;
+     
+        tmp = func(deriv, params, coords);
+        for (k=0;k<params->n;k++) {
+            beta->data.F32[k] = 0.0;
+            for (i=0;i<params->n;i++) {
+                beta->data.F32[k]+= deriv
+            }
+    */
+    return(false);
+}
+
 
 /******************************************************************************
@@ -856,5 +880,5 @@
     error for each data point (yErr).
  
-NOTE: yErr is currently ignored.
+XXX: NOTE: yErr is currently ignored.
  *****************************************************************************/
 psPolynomial1D* psVectorFitPolynomial1D(psPolynomial1D* myPoly,
@@ -1505,5 +1529,14 @@
 
 /******************************************************************************
-    This routine must minimize a possibly multi-dimensional function.
+This routine must minimize a possibly multi-dimensional function.  The
+function to be minimized "func" is:
+    float func(psVector *params, psArray *coords)
+The "params" are the parameters of the function which are varied.  The data
+points at which the function is varied are in the argument "coords" which is
+a psArray of psVectors: each vector represents a different coordinate.
+ 
+XXX: We do not use Brent's method.
+ 
+XXX: We do not use the
  *****************************************************************************/
 bool psMinimizePowell(psMinimization *min,
@@ -1672,2 +1705,53 @@
     return(false);
 }
+
+/******************************************************************************
+This routine is to be used with the psMinimizeChi2Powell() function below.
+and the psMinimizePowell() function above.
+ 
+The basic idea is calculate chi-squared for a set of params/coords/errors.
+This functions uses globale variables to receive the function pointer, the
+data values, and the data errors.
+ *****************************************************************************/
+float myPowellChi2Func(const psVector *params,
+                       const psArray *coords)
+{
+    float chi2 = 0.0;
+    float d;
+    int i;
+    psVector *tmp;
+
+    tmp = Chi2PowellFunc(params, coords);
+    for (i=0;i<coords->n;i++) {
+        d = (tmp->data.F32[i] - myValue->data.F32[i]) / myError->data.F32[i];
+        chi2+= d * d;
+    }
+    psFree(tmp);
+    return(chi2);
+}
+
+
+/******************************************************************************
+This routine must minimize the chi-squared match of a set of data points and
+values for a possibly multi-dimensional function.
+ 
+The basic idea is to use the psMinimizePowell() function defined above.  In
+order to do so, we defined above a function myPowellChi2Func() which takes
+the "func" function and returns chi-squared over the params/coords/values.
+We then use that function myPowellChi2Func() in the call to
+psMinimizePowell().
+ *****************************************************************************/
+bool psMinimizeChi2Powell(psMinimization *min,
+                          psVector *params,
+                          const psVector *paramMask,
+                          const psArray *coords,
+                          const psVector *value,
+                          const psVector *error,
+                          psMinimizeChi2PowellFunc func)
+{
+    myValue = (psVector *) value;
+    myError = (psVector *) error;
+    Chi2PowellFunc = func;
+
+    return(psMinimizePowell(min, params, paramMask, coords, myPowellChi2Func));
+}
