Index: /trunk/psLib/test/math/Makefile.am
===================================================================
--- /trunk/psLib/test/math/Makefile.am	(revision 4994)
+++ /trunk/psLib/test/math/Makefile.am	(revision 4995)
@@ -37,4 +37,7 @@
     tst_psMinimize05 \
     tst_psMinimize06 \
+    tst_psMinimizeVector2D_F64 \
+    tst_psMinimizeVector2D_F32 \
+    tst_psMinimizeVector2D_F64Clipped \
     tst_psStats00 \
     tst_psStats01 \
@@ -75,7 +78,10 @@
 tst_psMatrixVectorArithmetic04_SOURCES =  tst_psMatrixVectorArithmetic04.c
 tst_psMinimize04_SOURCES =  tst_psMinimize04.c
-tst_psMinimize04_F32_SOURCES =  tst_psMinimize04.c
+tst_psMinimize04_F32_SOURCES =  tst_psMinimize04_F32.c
 tst_psMinimize04b_SOURCES =  tst_psMinimize04b.c
-tst_psMinimize04b_F32_SOURCES =  tst_psMinimize04b.c
+tst_psMinimize04b_F32_SOURCES =  tst_psMinimize04b_F32.c
+tst_psMinimizeVector2D_F64_SOURCES = tst_psMinimizeVector2D_F64.c
+tst_psMinimizeVector2D_F32_SOURCES = tst_psMinimizeVector2D_F32.c
+tst_psMinimizeVector2D_F64Clipped_SOURCES = tst_psMinimizeVector2D_F64Clipped.c
 tst_psMinimize05_SOURCES =  tst_psMinimize05.c
 tst_psMinimize06_SOURCES =  tst_psMinimize06.c
Index: /trunk/psLib/test/math/tst_psMinimize04.c
===================================================================
--- /trunk/psLib/test/math/tst_psMinimize04.c	(revision 4994)
+++ /trunk/psLib/test/math/tst_psMinimize04.c	(revision 4995)
@@ -1,7 +1,7 @@
 /*****************************************************************************
-This routine must ensure that the psVectorFitPolynomial1D works correctly.
+This routine must ensure that the psVectorFitPolynomial2D works correctly.
 We create a vectors of data points (x and y), and populate them with the
 values from an arbitrary function setData().  We then call
-psVectorFitPolynomial1D() with a regular polynomial data structure.  We then
+psVectorFitPolynomial2D() with a regular polynomial data structure.  We then
 evaluate the polynomial with the coefficients generated above and determine
 if they are within an error tolerance of the expected values.
@@ -25,18 +25,18 @@
 #define B 2.0
 #define C 3.0
+#define D 3.0
+#define E 3.0
+#define F 3.0
 #define ERROR_TOLERANCE 0.10
-#define YERR 1.0
-double setData(double x)
+#define FERR 1.0
+#define PRINT_RESULTS 0
+
+double setData(double x, double y)
 {
-    return(A + (B * x) + (C * x * x));
+    return(A + (B * x) + (C * x * x) + (D * y) + (E * y * y) + (E * x * y));
 }
 
 psS32 t00()
 {
-    psPolynomial1D *myPoly = NULL;
-    psVector *x = NULL;
-    psVector *y = NULL;
-    psVector *yErr = NULL;
-    psS32 i = 0;
     psS32 currentId = psMemGetId();
     psS32 testStatus = true;
@@ -45,21 +45,23 @@
     double actualData;
 
-    myPoly = psPolynomial1DAlloc(POLY_ORDER+1, PS_POLYNOMIAL_ORD);
-    x = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
-    y = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
-    yErr = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
+    psPolynomial2D *myPoly = psPolynomial2DAlloc(POLY_ORDER+1, POLY_ORDER+1, PS_POLYNOMIAL_ORD);
+    psVector *x = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
+    psVector *y = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
+    psVector *f = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
+    psVector *fErr = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
 
-    for (i=0;i<NUM_DATA;i++) {
-        x->data.F64[i] = (double) i;
-        y->data.F64[i] = setData(x->data.F64[i]);
-        yErr->data.F64[i] = YERR;
-        //        printf("Original data %d: (%.1f %.1f)\n", i, x->data.F64[i], y->data.F64[i]);
+    for (psS32 i=0;i<NUM_DATA;i++) {
+        x->data.F32[i] = (double) i;
+        y->data.F32[i] = (double) i;
+        f->data.F32[i] = setData(x->data.F32[i], y->data.F32[i]);
+        fErr->data.F32[i] = FERR;
     }
 
     printPositiveTestHeader(stdout,
                             "psMinimize functions",
-                            "psVectorFitPolynomial1D(): equal difference in variable yErr");
+                            "psVectorFitPolynomial2D(): equal difference in variable fErr");
 
-    psVectorFitPolynomial1D(myPoly, x, y, yErr);
+    printf("TEST: calling psVectorFitPolynomial2D() with f, fErr, y, x as F32 vectors.\n");
+    psVectorFitPolynomial2D(myPoly, NULL, 0, f, fErr, x, y);
 
     //    for (i=0;i<POLY_ORDER+1;i++) {
@@ -67,26 +69,46 @@
     //    }
 
-    for (i=0;i<NUM_DATA;i++) {
-        expectData = setData(x->data.F64[i]);
-        actualData = psPolynomial1DEval(
-                         myPoly,
-                         x->data.F64[i]
-                     );
+    for (psS32 i=0;i<NUM_DATA;i++) {
+        expectData = setData(x->data.F32[i],  y->data.F32[i]);
+        actualData = psPolynomial2DEval(myPoly, x->data.F32[i], y->data.F32[i]);
         if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
-            printf("ERROR: Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
-                   i, x->data.F64[i], actualData, expectData);
+            printf("ERROR: Fitted data %d: (%.1f %.1f %.1f), expected was (%.1f)\n",
+                   i, x->data.F32[i], y->data.F32[i], actualData, expectData);
             testStatus = false;
         }
-        //        } else {
-        //            printf("Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
-        //                   i, x->data.F64[i], actualData, expectData);
-        //        }
+        if (PRINT_RESULTS) {
+            printf("Data point [%d] is %f, should be %f.\n", i, actualData, expectData);
+        }
+    }
+
+
+    printf("Running test with fErr set to NULL.\n");
+    psVectorFitPolynomial2D(myPoly, NULL, 0, f, NULL, x, y);
+    for (psS32 i=0;i<NUM_DATA;i++) {
+        expectData = setData(x->data.F32[i],  y->data.F32[i]);
+        actualData = psPolynomial2DEval(myPoly, x->data.F32[i], y->data.F32[i]);
+        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
+            printf("ERROR: Fitted data %d: (%.1f %.1f %.1f), expected was (%.1f)\n",
+                   i, x->data.F32[i], y->data.F32[i], actualData, expectData);
+            testStatus = false;
+        }
+        if (PRINT_RESULTS) {
+            printf("Data point [%d] is %f, should be %f.\n", i, actualData, expectData);
+        }
+    }
+    psFree(myPoly);
+
+    printf("Running test with x, y set to NULL.  Should generate error.\n");
+    myPoly = psVectorFitPolynomial2D(NULL, NULL, 0, f, fErr, NULL, NULL);
+    if (myPoly != NULL) {
+        printf("TEST ERROR: psVectorFitPolynomial2D() returned non-NULL.\n");
+        testStatus = false;
     }
 
     psMemCheckCorruption(1);
-    psFree(myPoly);
     psFree(x);
     psFree(y);
-    psFree(yErr);
+    psFree(f);
+    psFree(fErr);
     psMemCheckCorruption(1);
     memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
@@ -97,5 +119,5 @@
     printFooter(stdout,
                 "psMinimize functions",
-                "psVectorFitPolynomial1D(): equal differences in variable yErr",
+                "psVectorFitPolynomial2D(): equal differences in variable fErr",
                 testStatus);
 
@@ -103,172 +125,7 @@
 }
 
-psS32 t01()
-{
-    psPolynomial1D *myPoly = NULL;
-    psVector *x = NULL;
-    psVector *y = NULL;
-    psS32 i = 0;
-    psS32 currentId = psMemGetId();
-    psS32 testStatus = true;
-    psS32 memLeaks = 0;
-    double expectData;
-    double actualData;
-
-    myPoly = psPolynomial1DAlloc(POLY_ORDER+1, PS_POLYNOMIAL_ORD);
-    x = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
-    y = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
-
-    for (i=0;i<NUM_DATA;i++) {
-        x->data.F64[i] = (double) i;
-        y->data.F64[i] = setData(x->data.F64[i]);
-        //        printf("Original data %d: (%.1f %.1f)\n", i, x->data.F64[i], y->data.F64[i]);
-    }
-
-    printPositiveTestHeader(stdout,
-                            "psMinimize functions",
-                            "psVectorFitPolynomial1D(): yErr is NULL");
-
-    psVectorFitPolynomial1D(myPoly, x, y, NULL);
-
-    //    for (i=0;i<POLY_ORDER+1;i++) {
-    //        printf("Polynomial coefficient %d is %0.1f\n", i, myPoly->coeff[i]);
-    //    }
-
-    for (i=0;i<NUM_DATA;i++) {
-        expectData = setData(x->data.F64[i]);
-        actualData = psPolynomial1DEval(
-                         myPoly,
-                         x->data.F64[i]
-                     );
-        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
-            printf("ERROR: Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
-                   i, x->data.F64[i], actualData, expectData);
-            testStatus = false;
-            //        } else {
-            //            printf("Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
-            //                   i, x->data.F64[i], actualData, expectData);
-        }
-    }
-
-    psMemCheckCorruption(1);
-    psFree(myPoly);
-    psFree(x);
-    psFree(y);
-    psMemCheckCorruption(1);
-    memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
-    if (0 != memLeaks) {
-        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
-    }
-
-    printFooter(stdout,
-                "psMinimize functions",
-                "psVectorFitPolynomial1D(): yErr is NULL",
-                testStatus);
-
-    return (!testStatus);
-}
-
-psS32 t02()
-{
-    psPolynomial1D *myPoly = NULL;
-    psVector *y = NULL;
-    psS32 i = 0;
-    psS32 currentId = psMemGetId();
-    psS32 testStatus = true;
-    psS32 memLeaks = 0;
-    double expectData;
-    double actualData;
-
-    myPoly = psPolynomial1DAlloc(POLY_ORDER+1, PS_POLYNOMIAL_ORD);
-    y = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
-
-    for (i=0;i<NUM_DATA;i++) {
-        y->data.F64[i] = setData((double) i);
-        //        printf("Original data %d: (%.1f)\n", i, y->data.F64[i]);
-    }
-
-    printPositiveTestHeader(stdout,
-                            "psMinimize functions",
-                            "psVectorFitPolynomial1D(): x, yErr is NULL");
-
-    psVectorFitPolynomial1D(myPoly, NULL, y, NULL);
-
-    //    for (i=0;i<POLY_ORDER+1;i++) {
-    //        printf("Polynomial coefficient %d is %0.1f\n", i, myPoly->coeff[i]);
-    //    }
-
-    for (i=0;i<NUM_DATA;i++) {
-        expectData = setData((double) i);
-        actualData = psPolynomial1DEval(
-                         myPoly,
-                         (double) i
-                     );
-        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
-            printf("ERROR: Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
-                   i, (double) i, actualData, expectData);
-            testStatus = false;
-            //        } else {
-            //            printf("Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
-            //                   i, (double) i, actualData, expectData);
-        }
-    }
-
-    psMemCheckCorruption(1);
-    psFree(myPoly);
-    psFree(y);
-    psMemCheckCorruption(1);
-    memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
-    if (0 != memLeaks) {
-        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
-    }
-
-    printFooter(stdout,
-                "psMinimize functions",
-                "psVectorFitPolynomial1D(): x, yErr is NULL",
-                testStatus);
-
-    return (!testStatus);
-}
-
-psS32 t03()
-{
-    psS32 currentId = psMemGetId();
-    psS32 testStatus = true;
-    psS32 memLeaks = 0;
-    psPolynomial1D *myPoly = NULL;
-
-    printPositiveTestHeader(stdout,
-                            "psMinimize functions",
-                            "psVectorFitPolynomial1D(): all inputs are NULL");
-
-    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error for null input polynomial.");
-    myPoly = psVectorFitPolynomial1D(NULL, NULL, NULL, NULL);
-    if ( myPoly != NULL ) {
-        psError(PS_ERR_UNKNOWN, true,"A null polynomial should have returned a null pointer.");
-        testStatus = false;
-    }
-
-
-    psMemCheckCorruption(1);
-    memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
-    if (0 != memLeaks) {
-        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
-    }
-
-    printFooter(stdout,
-                "psMinimize functions",
-                "psVectorFitPolynomial1D(): all inputs are NULL",
-                testStatus);
-
-    return (!testStatus);
-}
-
-
 psS32 main()
 {
     psLogSetFormat("HLNM");
     t00();
-    t01();
-    t02();
-    t03();
 }
Index: /trunk/psLib/test/math/tst_psMinimize04_F32.c
===================================================================
--- /trunk/psLib/test/math/tst_psMinimize04_F32.c	(revision 4994)
+++ /trunk/psLib/test/math/tst_psMinimize04_F32.c	(revision 4995)
@@ -61,5 +61,5 @@
                             "psVectorFitPolynomial1D(): equal errors in yErr");
 
-    psVectorFitPolynomial1D(myPoly, x, y, yErr);
+    psVectorFitPolynomial1D(myPoly, NULL, 0, y, yErr, x);
 
     //    for (i=0;i<POLY_ORDER+1;i++) {
@@ -128,5 +128,5 @@
                             "psVectorFitPolynomial1D(): yErr is NULL");
 
-    psVectorFitPolynomial1D(myPoly, x, y, NULL);
+    psVectorFitPolynomial1D(myPoly, NULL, 0, y, NULL, x);
 
     //    for (i=0;i<POLY_ORDER+1;i++) {
@@ -191,5 +191,5 @@
                             "psVectorFitPolynomial1D(): x, yErr is NULL");
 
-    psVectorFitPolynomial1D(myPoly, NULL, y, NULL);
+    psVectorFitPolynomial1D(myPoly, NULL, 0, y, NULL, NULL);
 
     //    for (i=0;i<POLY_ORDER+1;i++) {
@@ -241,6 +241,6 @@
 
     psLogMsg(__func__,PS_LOG_INFO,"Following should generate error for null arguments.");
-    psVectorFitPolynomial1D(NULL, NULL, NULL, NULL);
-
+
+    psVectorFitPolynomial1D(NULL, NULL, 0, NULL, NULL, NULL);
 
     psMemCheckCorruption(1);
Index: /trunk/psLib/test/math/tst_psMinimize04b.c
===================================================================
--- /trunk/psLib/test/math/tst_psMinimize04b.c	(revision 4994)
+++ /trunk/psLib/test/math/tst_psMinimize04b.c	(revision 4995)
@@ -1,4 +1,6 @@
 /*****************************************************************************
     This routine must ensure that the psVectorFitPolynomial1D works correctly.
+ 
+    We use Chebyshev polynomials here.
  *****************************************************************************/
 #include <stdio.h>
@@ -52,5 +54,5 @@
                             "psVectorFitPolynomial1D(): CHEB, equal errors in yErr");
 
-    psVectorFitPolynomial1D(myPoly, x, y, yErr);
+    psVectorFitPolynomial1D(myPoly, NULL, 0, y, yErr, x);
 
     //  Remove for testing since print out differs from platforms
@@ -121,5 +123,5 @@
                             "psVectorFitPolynomial1D(): CHEB, yErr is NULL");
 
-    psVectorFitPolynomial1D(myPoly, x, y, NULL);
+    psVectorFitPolynomial1D(myPoly, NULL, 0, y, NULL, x);
 
     // We don't test the first or last few data items.
@@ -184,5 +186,5 @@
                             "psVectorFitPolynomial1D(): CHEB, x, yErr is NULL");
 
-    psVectorFitPolynomial1D(myPoly, NULL, y, NULL);
+    psVectorFitPolynomial1D(myPoly, NULL, 0, y, NULL, NULL);
     //    psVectorFitPolynomial1D(myPoly, x, y, NULL);
 
@@ -232,5 +234,5 @@
 
     psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error for null arguments.");
-    myPoly = psVectorFitPolynomial1D(NULL, NULL, NULL, NULL);
+    psVectorFitPolynomial1D(myPoly, NULL, 0, NULL, NULL, NULL);
     if (myPoly != NULL) {
         printf("ERROR: psVectorFitPolynomial1D() returned a non-NULL polynomial.\n");
Index: /trunk/psLib/test/math/tst_psMinimize04b_F32.c
===================================================================
--- /trunk/psLib/test/math/tst_psMinimize04b_F32.c	(revision 4994)
+++ /trunk/psLib/test/math/tst_psMinimize04b_F32.c	(revision 4995)
@@ -1,4 +1,6 @@
 /*****************************************************************************
     This routine must ensure that the psVectorFitPolynomial1D works correctly.
+ 
+    We use Chebyshev polynomials here.
  *****************************************************************************/
 #include <stdio.h>
@@ -52,5 +54,5 @@
                             "psVectorFitPolynomial1D(): CHEB, equal errors in yErr");
 
-    psVectorFitPolynomial1D(myPoly, x, y, yErr);
+    psVectorFitPolynomial1D(myPoly, NULL, 0, y, yErr, x);
 
     //  Remove for testing since printout differs between platforms
@@ -121,5 +123,5 @@
                             "psVectorFitPolynomial1D(): CHEB, yErr is NULL");
 
-    psVectorFitPolynomial1D(myPoly, x, y, NULL);
+    psVectorFitPolynomial1D(myPoly, NULL, 0, y, NULL, x);
 
     // We don't test the first or last few data items.
@@ -184,5 +186,5 @@
                             "psVectorFitPolynomial1D(): CHEB, x, yErr is NULL");
 
-    psVectorFitPolynomial1D(myPoly, NULL, y, NULL);
+    psVectorFitPolynomial1D(myPoly, NULL, 0, y, NULL, NULL);
 
     // We don't test the first or last few data items.
@@ -234,5 +236,5 @@
 
     psLogMsg(__func__,PS_LOG_INFO,"Following should generate error for null arguments.");
-    myPoly = psVectorFitPolynomial1D(NULL, NULL, NULL, NULL);
+    psVectorFitPolynomial1D(myPoly, NULL, 0, NULL, NULL, NULL);
     if (myPoly != NULL) {
         printf("ERROR: psVectorFitPolynomial1D() returned a non-NULL polynomial.\n");
Index: /trunk/psLib/test/math/tst_psMinimizeVector2D_F32.c
===================================================================
--- /trunk/psLib/test/math/tst_psMinimizeVector2D_F32.c	(revision 4995)
+++ /trunk/psLib/test/math/tst_psMinimizeVector2D_F32.c	(revision 4995)
@@ -0,0 +1,132 @@
+/*****************************************************************************
+This routine must ensure that the psVectorFitPolynomial2D works correctly.
+We create a vectors of data points (x and y), and populate them with the
+values from an arbitrary function setData().  We then call
+psVectorFitPolynomial2D() with a regular polynomial data structure.  We then
+evaluate the polynomial with the coefficients generated above and determine
+if they are within an error tolerance of the expected values.
+ 
+    t00(): all input vectors are non-NULL.
+    t01(): yErr is NULL.
+    t02(): x, yErr is NULL.
+    t03(): x, y, yErr is NULL.
+ *****************************************************************************/
+#include <stdio.h>
+#include "pslib_strict.h"
+#include "psTest.h"
+#include "psMemory.h"
+#include "psVector.h"
+#include "psImage.h"
+#include "psMinimize.h"
+#include <math.h>
+#define NUM_DATA 10
+#define POLY_ORDER 5
+#define A 3.0
+#define B 2.0
+#define C 3.0
+#define D 3.0
+#define E 3.0
+#define F 3.0
+#define ERROR_TOLERANCE 0.10
+#define FERR 1.0
+#define PRINT_RESULTS 0
+
+double setData(double x, double y)
+{
+    return(A + (B * x) + (C * x * x) + (D * y) + (E * y * y) + (E * x * y));
+}
+
+psS32 t00()
+{
+    psS32 currentId = psMemGetId();
+    psS32 testStatus = true;
+    psS32 memLeaks = 0;
+    double expectData;
+    double actualData;
+
+    psPolynomial2D *myPoly = psPolynomial2DAlloc(POLY_ORDER+1, POLY_ORDER+1, PS_POLYNOMIAL_ORD);
+    psVector *x = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
+    psVector *y = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
+    psVector *f = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
+    psVector *fErr = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
+
+    for (psS32 i=0;i<NUM_DATA;i++) {
+        x->data.F64[i] = (double) i;
+        y->data.F64[i] = (double) i;
+        f->data.F64[i] = setData(x->data.F64[i], y->data.F64[i]);
+        fErr->data.F64[i] = FERR;
+    }
+
+    printPositiveTestHeader(stdout,
+                            "psMinimize functions",
+                            "psVectorFitPolynomial2D(): equal difference in variable fErr");
+
+    printf("Calling psVectorFitPolynomial2D() with f, fErr, x, y as F32 vectors.\n");
+    psVectorFitPolynomial2D(myPoly, NULL, 0, f, fErr, x, y);
+
+    //    for (i=0;i<POLY_ORDER+1;i++) {
+    //        printf("Polynomial coefficient %d is %0.1f\n", i, myPoly->coeff[i]);
+    //    }
+
+    for (psS32 i=0;i<NUM_DATA;i++) {
+        expectData = setData(x->data.F64[i],  y->data.F64[i]);
+        actualData = psPolynomial2DEval(myPoly, x->data.F64[i], y->data.F64[i]);
+        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
+            printf("ERROR: Fitted data %d: (%.1f %.1f %.1f), expected was (%.1f)\n",
+                   i, x->data.F64[i], y->data.F64[i], actualData, expectData);
+            testStatus = false;
+        }
+        if (PRINT_RESULTS) {
+            printf("Data point [%d] is %f, should be %f.\n", i, actualData, expectData);
+        }
+    }
+
+
+    printf("Calling psVectorFitPolynomial2D() with fErr set to NULL.\n");
+    printf("Running test with fErr set to NULL.\n");
+    psVectorFitPolynomial2D(myPoly, NULL, 0, f, NULL, x, y);
+    for (psS32 i=0;i<NUM_DATA;i++) {
+        expectData = setData(x->data.F64[i],  y->data.F64[i]);
+        actualData = psPolynomial2DEval(myPoly, x->data.F64[i], y->data.F64[i]);
+        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
+            printf("ERROR: Fitted data %d: (%.1f %.1f %.1f), expected was (%.1f)\n",
+                   i, x->data.F64[i], y->data.F64[i], actualData, expectData);
+            testStatus = false;
+        }
+        if (PRINT_RESULTS) {
+            printf("Data point [%d] is %f, should be %f.\n", i, actualData, expectData);
+        }
+    }
+    psFree(myPoly);
+
+    printf("Running test with x, y set to NULL.  Should generate error.\n");
+    myPoly = psVectorFitPolynomial2D(NULL, NULL, 0, f, fErr, NULL, NULL);
+    if (myPoly != NULL) {
+        printf("TEST ERROR: psVectorFitPolynomial2D() returned non-NULL.\n");
+        testStatus = false;
+    }
+
+    psMemCheckCorruption(1);
+    psFree(x);
+    psFree(y);
+    psFree(f);
+    psFree(fErr);
+    psMemCheckCorruption(1);
+    memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
+    if (0 != memLeaks) {
+        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
+    }
+
+    printFooter(stdout,
+                "psMinimize functions",
+                "psVectorFitPolynomial2D(): equal differences in variable fErr",
+                testStatus);
+
+    return (!testStatus);
+}
+
+psS32 main()
+{
+    psLogSetFormat("HLNM");
+    t00();
+}
Index: /trunk/psLib/test/math/tst_psMinimizeVector2D_F64.c
===================================================================
--- /trunk/psLib/test/math/tst_psMinimizeVector2D_F64.c	(revision 4995)
+++ /trunk/psLib/test/math/tst_psMinimizeVector2D_F64.c	(revision 4995)
@@ -0,0 +1,131 @@
+/*****************************************************************************
+This routine must ensure that the psVectorFitPolynomial2D works correctly.
+We create a vectors of data points (x and y), and populate them with the
+values from an arbitrary function setData().  We then call
+psVectorFitPolynomial2D() with a regular polynomial data structure.  We then
+evaluate the polynomial with the coefficients generated above and determine
+if they are within an error tolerance of the expected values.
+ 
+    t00(): all input vectors are non-NULL.
+    t01(): yErr is NULL.
+    t02(): x, yErr is NULL.
+    t03(): x, y, yErr is NULL.
+ *****************************************************************************/
+#include <stdio.h>
+#include "pslib_strict.h"
+#include "psTest.h"
+#include "psMemory.h"
+#include "psVector.h"
+#include "psImage.h"
+#include "psMinimize.h"
+#include <math.h>
+#define NUM_DATA 10
+#define POLY_ORDER 5
+#define A 3.0
+#define B 2.0
+#define C 3.0
+#define D 3.0
+#define E 3.0
+#define F 3.0
+#define ERROR_TOLERANCE 0.10
+#define FERR 1.0
+#define PRINT_RESULTS 0
+
+double setData(double x, double y)
+{
+    return(A + (B * x) + (C * x * x) + (D * y) + (E * y * y) + (E * x * y));
+}
+
+psS32 t00()
+{
+    psS32 currentId = psMemGetId();
+    psS32 testStatus = true;
+    psS32 memLeaks = 0;
+    double expectData;
+    double actualData;
+
+    psPolynomial2D *myPoly = psPolynomial2DAlloc(POLY_ORDER+1, POLY_ORDER+1, PS_POLYNOMIAL_ORD);
+    psVector *x = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
+    psVector *y = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
+    psVector *f = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
+    psVector *fErr = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
+
+    for (psS32 i=0;i<NUM_DATA;i++) {
+        x->data.F32[i] = (double) i;
+        y->data.F32[i] = (double) i;
+        f->data.F32[i] = setData(x->data.F32[i], y->data.F32[i]);
+        fErr->data.F32[i] = FERR;
+    }
+
+    printPositiveTestHeader(stdout,
+                            "psMinimize functions",
+                            "psVectorFitPolynomial2D(): equal difference in variable fErr");
+
+    printf("TEST: calling psVectorFitPolynomial2D() with f, fErr, y, x as F32 vectors.\n");
+    psVectorFitPolynomial2D(myPoly, NULL, 0, f, fErr, x, y);
+
+    //    for (i=0;i<POLY_ORDER+1;i++) {
+    //        printf("Polynomial coefficient %d is %0.1f\n", i, myPoly->coeff[i]);
+    //    }
+
+    for (psS32 i=0;i<NUM_DATA;i++) {
+        expectData = setData(x->data.F32[i],  y->data.F32[i]);
+        actualData = psPolynomial2DEval(myPoly, x->data.F32[i], y->data.F32[i]);
+        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
+            printf("ERROR: Fitted data %d: (%.1f %.1f %.1f), expected was (%.1f)\n",
+                   i, x->data.F32[i], y->data.F32[i], actualData, expectData);
+            testStatus = false;
+        }
+        if (PRINT_RESULTS) {
+            printf("Data point [%d] is %f, should be %f.\n", i, actualData, expectData);
+        }
+    }
+
+
+    printf("Running test with fErr set to NULL.\n");
+    psVectorFitPolynomial2D(myPoly, NULL, 0, f, NULL, x, y);
+    for (psS32 i=0;i<NUM_DATA;i++) {
+        expectData = setData(x->data.F32[i],  y->data.F32[i]);
+        actualData = psPolynomial2DEval(myPoly, x->data.F32[i], y->data.F32[i]);
+        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
+            printf("ERROR: Fitted data %d: (%.1f %.1f %.1f), expected was (%.1f)\n",
+                   i, x->data.F32[i], y->data.F32[i], actualData, expectData);
+            testStatus = false;
+        }
+        if (PRINT_RESULTS) {
+            printf("Data point [%d] is %f, should be %f.\n", i, actualData, expectData);
+        }
+    }
+    psFree(myPoly);
+
+    printf("Running test with x, y set to NULL.  Should generate error.\n");
+    myPoly = psVectorFitPolynomial2D(NULL, NULL, 0, f, fErr, NULL, NULL);
+    if (myPoly != NULL) {
+        printf("TEST ERROR: psVectorFitPolynomial2D() returned non-NULL.\n");
+        testStatus = false;
+    }
+
+    psMemCheckCorruption(1);
+    psFree(x);
+    psFree(y);
+    psFree(f);
+    psFree(fErr);
+    psMemCheckCorruption(1);
+    memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
+    if (0 != memLeaks) {
+        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
+    }
+
+    printFooter(stdout,
+                "psMinimize functions",
+                "psVectorFitPolynomial2D(): equal differences in variable fErr",
+                testStatus);
+
+    return (!testStatus);
+}
+
+psS32 main()
+{
+    psLogSetFormat("HLNM");
+    t00();
+}
