/*****************************************************************************
    This routine must ensure that psMinimizeLM() works correctly.
 *****************************************************************************/
#include <stdio.h>
#include "pslib.h"
#include "psTest.h"
#include <math.h>
#define NUM_ITERATIONS 10000
#define ERR_TOL 0.0
#define N 20
#define MIN_VALUE 5.0
#define NUM_PARAMS 3
float expectedParm[NUM_PARAMS];
psS32 testStatus = true;

/*****************************************************************************
myFunc(): This routine subtracts the associate value in expectedParm[] from
each parameter and then squares it, then sums that for all parameters, then
adds MIN_VALUE to it.  The minimum for this function will be MIN_VALUE, and
will occur when each parameter equals the associated value in expectedParm[].
 
This procedure ignores the coordinates, other than to ensure that they were
passed correctly from psMinimizePowell().
 
 *****************************************************************************/
psVector *myFunc(psImage *myDeriv,
                 psVector *myParams,
                 psArray *myCoords)
{
    psVector *sum = psVectorAlloc(myCoords->n, PS_TYPE_F32);
    psS32 i;
    psS32 j;


    if (myDeriv == NULL) {
        myDeriv = psImageAlloc(myParams->n, myCoords->n, PS_TYPE_F32);
        psError(PS_ERR_UNKNOWN, true, "myDeriv is NULL.\n");
    }

    for (i=0;i<N;i++) {
        sum->data.F32[i] = MIN_VALUE;
        for (j=0;j<NUM_PARAMS;j++) {
            sum->data.F32[i]+= (myParams->data.F32[j] - expectedParm[j]) *
                               (myParams->data.F32[j] - expectedParm[j]);

            myDeriv->data.F32[i][j] = (2.0 * myParams->data.F32[j]) -
                                      (2.0 * expectedParm[j]);
        }
    }

    return(sum);
}

psS32 t01()
{
    psS32 currentId = psMemGetId();
    psS32 memLeaks = 0;
    psS32 i = 0;
    psArray *myCoords;
    psVector *myParams;
    psImage *myCovar;
    psMinimization *min;
    psVector *y;

    psTraceSetLevel(".psLib", 0);
    /**************************************************************************
     *************************************************************************/
    min = psMinimizationAlloc(NUM_ITERATIONS, ERR_TOL);
    myCovar = psImageAlloc(NUM_PARAMS, NUM_PARAMS, PS_TYPE_F32);
    myParams = psVectorAlloc(NUM_PARAMS, PS_TYPE_F32);
    myCoords = psArrayAlloc(N);
    y = psVectorAlloc(N, PS_TYPE_F32);

    for (i=0;i<N;i++) {
        myCoords->data[i] = (psPtr *) psVectorAlloc(2, PS_TYPE_F32);
        ((psVector *) (myCoords->data[i]))->data.F32[0] = (float) (i+10);
        ((psVector *) (myCoords->data[i]))->data.F32[1] = (float) (i+3);
        y->data.F32[i] = (float) i;
    }
    for (i=0;i<NUM_PARAMS;i++) {
        expectedParm[i] = 2.42 + (float) (2 * i);
        myParams->data.F32[i] = (float) i;
        myParams->data.F32[i] = expectedParm[i] * 1.3;
        myParams->data.F32[i] = (float) (5 + i);
        myParams->data.F32[i] = 0.0;
    }

    psMinimizeLMChi2(min,
                     myCovar,
                     myParams,
                     NULL,
                     myCoords,
                     y,
                     NULL,
                     (psMinimizeLMChi2Func) myFunc);

    printf("\nThe chi-squared is %f\n", min->value);
    for (i=0;i<NUM_PARAMS;i++) {
        printf("Parameter %d at the minimum is %f (expected: %f)\n", i,
               myParams->data.F32[i], expectedParm[i]);
    }

    psFree(min);
    psFree(myCovar);
    psFree(myParams);
    psFree(myCoords);
    psFree(y);

    psMemCheckCorruption(1);
    memLeaks = psMemCheckLeaks(currentId,NULL,NULL,false);
    if (0 != memLeaks) {
        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    }
    return (!testStatus);
}

psS32 main()
{
    t01();
}
