IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 7, 2004, 1:02:21 PM (22 years ago)
Author:
gusciora
Message:

...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/math/psMinimize.c

    r1189 r1190  
    2222#include "psSort.h"
    2323#include "psMinimize.h"
     24#include "psMatrix.h"
    2425#include "float.h"
    2526#include <math.h>
     
    739740}
    740741
    741 
    742 
    743 
    744 
    745 
    746 
    747 
    748 
    749742/******************************************************************************
    750743    This routine must fit a polynomial of degree myPoly to the data points
     
    753746 *****************************************************************************/
    754747psPolynomial1D *
    755 psGetArrayPolynomial(psPolynomial1D myPoly,
     748psGetArrayPolynomial(psPolynomial1D *myPoly,
    756749                     const psVector *restrict x,
    757750                     const psVector *restrict y,
    758751                     const psVector *restrict yErr)
    759752{
    760     /*
    761         int polyOrder = myPoly->n;
    762         float **A;
    763      
    764      
    765         // Numerical Recipes routines are all index offset 1.
    766         B = (float *) psAlloc((polyOrder+2) * sizeof(float));
    767         ludIndex = (int *) psAlloc((polyOrder+2) * sizeof(int));
    768      
    769         A = (float **) psAlloc((polyOrder+2) * sizeof(float *));
    770         for(i=0;i<(polyOrder+2);i++) {
    771             A[i] = (float *) psAlloc((polyOrder+2) * sizeof(float));
    772         }
    773      
    774         // Initialize data structures.
    775         for(i=0;i<(polyOrder+2);i++) {
    776             B[i] = 0.0;
    777             ludIndex[i] = 0;
    778             for(j=0;j<(polyOrder+2);j++) {
    779                 A[i][j] = 0.0;
    780             }
    781         }
    782      
    783         for(k=1;k<(polyOrder+2);k++) {
    784             for (i=0;i<x->n;i++) {
    785                 B[k]+= y->data.F32[i] * (pow(x->data.F32[i], k));
    786             }
    787         }
    788         for(k=1;k<(polyOrder+2);k++) {
    789             for(i=1;i<(polyOrder+2);i++) {
    790                 for (i=0;i<x->n;i++) {
    791                     A[k][j]+= (pow(y->data.F32[i], k) * pow(x->data.F32[i], j));
    792                 }
    793             }
    794         }
    795      
    796      
    797     */
     753    int polyOrder = myPoly->n;
     754    psImage *A = NULL;
     755    psImage *ALUD = NULL;
     756    psVector *B = NULL;
     757    psVector *outPerm = NULL;
     758    psVector *X = NULL;   // NOTE: do we need this?
     759    psVector *coeffs = NULL;
     760    int i = 0;
     761    int j = 0;
     762    int k = 0;
     763
     764    if (x->n != y->n) {
     765        psAbort(__func__, "x and y arguments have different sizes.\n");
     766    }
     767    if (x->n != yErr->n) {
     768        psAbort(__func__, "y and yErr arguments have different sizes.\n");
     769    }
     770
     771    A = psImageAlloc(polyOrder, polyOrder, PS_TYPE_F64);
     772    ALUD = psImageAlloc(polyOrder, polyOrder, PS_TYPE_F64);
     773    B = psVectorAlloc(polyOrder, PS_TYPE_F64);
     774    coeffs = psVectorAlloc(polyOrder, PS_TYPE_F64);
     775    X = psVectorAlloc(x->n, PS_TYPE_F64);
     776    outPerm = psVectorAlloc(polyOrder, PS_TYPE_F64);
     777
     778    // Initialize data structures.
     779    for(i=0;i<(polyOrder);i++) {
     780        B->data.F64[i] = 0.0;
     781        coeffs->data.F64[i] = 0.0;
     782        outPerm->data.F64[i] = 0.0;
     783        for(j=0;j<(polyOrder);j++) {
     784            A->data.F64[i][j] = 0.0;
     785            ALUD->data.F64[i][j] = 0.0;
     786        }
     787    }
     788    for (i=0;i<X->n;i++) {
     789        X->data.F64[i] = x->data.F64[i];
     790    }
     791
     792    for(k=0;k<(polyOrder);k++) {
     793        for (i=0;i<X->n;i++) {
     794            B->data.F64[k]+= y->data.F64[i] * (pow(X->data.F64[i], (float) k));
     795        }
     796    }
     797
     798    for(k=0;k<(polyOrder);k++) {
     799        for(j=0;j<(polyOrder);j++) {
     800            for (i=0;i<X->n;i++) {
     801                A->data.F64[k][j]+= pow(X->data.F64[i], (float) (k + j));
     802            }
     803        }
     804    }
     805
     806    ALUD = psMatrixLUD(ALUD, outPerm, A);
     807    coeffs = psMatrixLUSolve(coeffs, ALUD, B, outPerm);
     808
     809    for(k=0;k<(polyOrder);k++) {
     810        myPoly->coeff[k] = coeffs->data.F64[k];
     811    }
     812
     813    psFree(A);
     814    psFree(ALUD);
     815    psFree(B);
     816    psFree(coeffs);
     817    psFree(X);
     818    psFree(outPerm);
     819
    798820    return(NULL);
    799821}
Note: See TracChangeset for help on using the changeset viewer.