IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 4, 2004, 3:43:58 PM (22 years ago)
Author:
gusciora
Message:

Added F32 types for the VectorFit routine.

File:
1 edited

Legend:

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

    r1945 r1953  
    99 *  @author George Gusciora, MHPCC
    1010 *
    11  *  @version $Revision: 1.51 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-10-03 23:35:47 $
     11 *  @version $Revision: 1.52 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-10-05 01:43:58 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2424/* INCLUDE FILES                                                             */
    2525/*****************************************************************************/
    26 #include <stdlib.h>
    27 #include <stdio.h>
    28 #include <string.h>
    29 #include <stdarg.h>
    30 #include <float.h>
    31 #include <math.h>
    32 
    33 #include <gsl/gsl_multifit_nlin.h>
    34 #include <gsl/gsl_multimin.h>
    35 #include <gsl/gsl_rng.h>
    36 #include <gsl/gsl_randist.h>
    37 #include <gsl/gsl_vector.h>
    38 #include <gsl/gsl_blas.h>
    39 
    40 #include "psMemory.h"
    41 #include "psVector.h"
    42 #include "psImage.h"
    43 #include "psTrace.h"
    44 #include "psLogMsg.h"
    45 #include "psError.h"
    46 #include "psAbort.h"
    47 #include "psFunctions.h"
    4826#include "psMinimize.h"
    49 #include "psMatrix.h"
    50 #include "psConstants.h"
    51 
    5227/*****************************************************************************/
    5328/* DEFINE STATEMENTS                                                         */
     
    655630}
    656631
     632#define GEN_STATIC_RECYCLED_VECTOR(NAME, SIZE, TYPE) \
     633static psVector *NAME = NULL; \
     634NAME = psVectorRecycle(NAME, SIZE, TYPE); \
     635p_psMemSetPersistent(NAME, true); \
     636p_psMemSetPersistent(NAME->data.V, true); \
     637
    657638
    658639/******************************************************************************
     
    661642coefficients of that polynomial.
    662643 
    663 XXX: yErr is currently ignored.
    664  
    665 XXX: must add type F32 (currently F64 only).
    666 
    667 XXX: Use private name?
    668  *****************************************************************************/
     644XXX:
     645yErr is currently ignored.
     646 
     647XXX:
     648Use private name?
     649*****************************************************************************/
    669650psPolynomial1D *p_psVectorFitPolynomial1DCheby(psPolynomial1D* myPoly,
    670651        const psVector* restrict x,
     
    675656    int k;
    676657    int n = x->n;
    677     psVector *f = psVectorAlloc(n, PS_TYPE_F64);
    678658    double fac;
    679659    double sum;
    680     // XXX: Use static memory here.
    681     psScalar *tmpScalar = psScalarAlloc(0.0, PS_TYPE_F32);
     660    GEN_STATIC_RECYCLED_VECTOR(f, n, PS_TYPE_F64);
    682661    psScalar *fScalar;
     662    psScalar tmpScalar;
     663    tmpScalar.type.type = PS_TYPE_F64;
    683664
    684665    // XXX: These assignments appear too simple to warrant code and
     
    690671    double bpa = 0.5 * (max+min);  // 0
    691672
    692     // XXX: Eliminate this later by generating a F64 version of the
    693     // LaGrange interpolation routines.
    694     PS_VECTOR_F64_TO_F32(x, x32);
    695     PS_VECTOR_F64_TO_F32(y, y32);
    696 
    697673    // In this loop, we first calculate the values of X for which the
    698674    // Chebyshev polynomials are zero (see NR, section 5.4).  Then we
     
    707683        double Y = cos(M_PI * (0.5 + ((float) i)) / ((float) n));
    708684        double X = (Y + bma + bpa) - 1.0;
    709         tmpScalar->data.F32 = (float) X;
     685        tmpScalar.data.F64 = X;
    710686
    711687        // We interpolate against are tabluated x,y vectors to determine the
    712688        // function value at X.
    713         fScalar = p_psVectorInterpolate((psVector *) x32, (psVector *) y32,
    714                                         3, tmpScalar);
    715 
    716         f->data.F64[i] = (double) fScalar->data.F32;
     689        fScalar = p_psVectorInterpolate((psVector *) x,
     690                                        (psVector *) y,
     691                                        3,
     692                                        &tmpScalar);
     693
     694        f->data.F64[i] = fScalar->data.F64;
    717695        psFree(fScalar);
    718696
     
    724702    // We have the values for f() at the zero points, we now calculate the
    725703    // coefficients of the Chebyshev polynomial: NR 5.8.7.
     704
    726705    fac = 2.0/((float) n);
    727 
    728706    // XXX: is this loop bound correct?
    729707    for (j=0;j<myPoly->n;j++) {
     
    737715    }
    738716
    739     // XXX: Must free memory.
    740     psFree(f);
    741     psFree(x32);
    742     psFree(y32);
    743     psFree(tmpScalar);
    744 
    745717    return(myPoly);
    746718}
     
    750722polynomial of degree myPoly to the data points (x, y) and return the
    751723coefficients of that polynomial.
    752  
    753 XXX: must add type F32 (currently F64 only).
    754724 
    755725XXX: Use private name?
     
    875845polynomial.
    876846 
    877 XXX: must add type F32 (currently F64 only).
     847XXX: type F32 is done via vector conversion only.
    878848 *****************************************************************************/
    879849psPolynomial1D* psVectorFitPolynomial1D(psPolynomial1D* myPoly,
     
    882852                                        const psVector* restrict yErr)
    883853{
    884     bool mustFreeMyYErr = false;
    885     bool mustFreemyX = false;
    886854    int i;
    887855    psPolynomial1D *tmpPoly;
    888     psVector *myX = NULL;
    889     psVector *myYErr = NULL;
    890 
    891856    PS_CHECK_NULL_1DPOLY_ACTION(myPoly, 0);
    892857    PS_CHECK_NULL_VECTOR_ACTION(y, 0);
    893858    PS_CHECK_EMPTY_VECTOR_ACTION(y, 0);
     859    psVector *x64 = NULL;
     860    psVector *y64 = NULL;
     861    psVector *yErr64 = NULL;
     862    static psVector *x64Static = NULL;
     863    static psVector *y64Static = NULL;
     864    static psVector *yErr64Static = NULL;
     865
     866    if (y->type.type == PS_TYPE_F64) {
     867        y64 = (psVector *) y;
     868    } else if (y->type.type == PS_TYPE_F32) {
     869        y64Static = psVectorRecycle(y64Static, y->n, PS_TYPE_F64);
     870        p_psMemSetPersistent(y64Static, true);
     871        p_psMemSetPersistent(y64Static->data.V, true);
     872        y64 = y64Static;
     873    } else {
     874        // XXX: psError() bad type.
     875        psAbort(__func__, "Bad type for y64 (%d)", y->type.type);
     876    }
     877
    894878
    895879    // If yErr==NULL, set all errors equal.
    896880    if (yErr == NULL) {
    897         myYErr = psVectorAlloc(y->n, y->type.type);
    898         mustFreeMyYErr = true;
    899 
    900         if (y->type.type == PS_TYPE_F32) {
    901             for (i=0;i<myYErr->n;i++) {
    902                 myYErr->data.F32[i] = 1.0;
    903             }
    904         } else if (y->type.type == PS_TYPE_F64) {
    905             for (i=0;i<myYErr->n;i++) {
    906                 myYErr->data.F64[i] = 1.0;
    907             }
    908         }
     881        yErr64Static = psVectorRecycle(yErr64Static, y->n, PS_TYPE_F64);
     882        p_psMemSetPersistent(yErr64Static, true);
     883        p_psMemSetPersistent(yErr64Static->data.V, true);
     884
     885        for (i=0;i<yErr64Static->n;i++) {
     886            yErr64Static->data.F64[i] = 1.0;
     887        }
     888        yErr64 = yErr64Static;
    909889    } else {
    910         myYErr = (psVector *) yErr;
    911     }
    912 
    913     // If x==NULL, create an myX vector with x values set to (0:n), and if
     890        if (yErr->type.type == PS_TYPE_F64) {
     891            yErr64 = (psVector *) yErr;
     892        } else if (yErr->type.type == PS_TYPE_F32) {
     893            yErr64Static = psVectorRecycle(yErr64Static, yErr->n, PS_TYPE_F64);
     894            p_psMemSetPersistent(yErr64Static, true);
     895            p_psMemSetPersistent(yErr64Static->data.V, true);
     896        } else {
     897            // XXX: psError() bad type.
     898            psAbort(__func__, "Bad type for yErr64");
     899        }
     900    }
     901
     902    // If x==NULL, create an x64 vector with x values set to (0:n), and if
    914903    // this is a Chebyshev polynomial, we must scale to (-1:1).
    915 
    916904    // XXX: Verify that this is the correct action.
    917905    if (x == NULL) {
    918         myX = psVectorAlloc(y->n, y->type.type);
    919         mustFreemyX = true;
    920 
    921         if (y->type.type == PS_TYPE_F32) {
    922             if (myPoly->type == PS_POLYNOMIAL_ORD) {
    923                 for (i=0;i<myX->n;i++) {
    924                     myX->data.F32[i] = (float) i;
    925                 }
    926             } else if (myPoly->type == PS_POLYNOMIAL_CHEB) {
    927                 float min = 0.0;
    928                 float max = (float) (y->n - 1);
    929 
    930                 for (i=0;i<myX->n;i++) {
    931                     myX->data.F32[i] = (((float) i) - 0.5 * (min + max)) /
    932                                        (0.5 * (max - min));
    933                 }
    934             }
    935         } else if (y->type.type == PS_TYPE_F64) {
    936             if (myPoly->type == PS_POLYNOMIAL_ORD) {
    937                 for (i=0;i<myX->n;i++) {
    938                     myX->data.F64[i] = (float) i;
    939                 }
    940             } else if (myPoly->type == PS_POLYNOMIAL_CHEB) {
    941                 double min = 0.0;
    942                 double max = (double) (y->n - 1);
    943 
    944                 for (i=0;i<myX->n;i++) {
    945                     myX->data.F64[i] = (((float) i) - 0.5 * (min + max)) /
    946                                        (0.5 * (max - min));
    947                 }
     906        x64Static = psVectorRecycle(x64Static, y->n, PS_TYPE_F64);
     907        p_psMemSetPersistent(x64Static, true);
     908        p_psMemSetPersistent(x64Static->data.V, true);
     909        x64 = x64Static;
     910
     911        if (myPoly->type == PS_POLYNOMIAL_ORD) {
     912            for (i=0;i<x64->n;i++) {
     913                x64->data.F64[i] = (float) i;
     914            }
     915        } else if (myPoly->type == PS_POLYNOMIAL_CHEB) {
     916            double min = 0.0;
     917            double max = (double) (y->n - 1);
     918
     919            for (i=0;i<x64->n;i++) {
     920                x64->data.F64[i] = (((double) i) - 0.5 * (min + max)) /
     921                                   (0.5 * (max - min));
    948922            }
    949923        }
    950924    } else {
    951         myX = (psVector *) x;
    952     }
    953 
    954     PS_CHECK_VECTOR_SIZE_EQUAL(y, myX);
    955     PS_CHECK_VECTOR_SIZE_EQUAL(y, myYErr);
     925        if (x->type.type == PS_TYPE_F64) {
     926            x64 = (psVector *) x;
     927        } else if (x->type.type == PS_TYPE_F32) {
     928            x64Static = psVectorRecycle(x64Static, x->n, PS_TYPE_F64);
     929            p_psMemSetPersistent(x64Static, true);
     930            p_psMemSetPersistent(x64Static->data.V, true);
     931            x64 = x64Static;
     932        } else {
     933            // XXX: psError() bad type.
     934            psAbort(__func__, "Bad type for x64");
     935        }
     936    }
     937
     938    PS_CHECK_VECTOR_SIZE_EQUAL(y64, x64);
     939    PS_CHECK_VECTOR_SIZE_EQUAL(y64, yErr64);
    956940
    957941    // Call the appropriate vector fitting routine.
    958942    if (myPoly->type == PS_POLYNOMIAL_CHEB) {
    959         tmpPoly = p_psVectorFitPolynomial1DCheby(myPoly, myX, y, myYErr);
     943        tmpPoly = p_psVectorFitPolynomial1DCheby(myPoly, x64, y64, yErr64);
    960944    } else if (myPoly->type == PS_POLYNOMIAL_ORD) {
    961         tmpPoly = p_psVectorFitPolynomial1DOrd(myPoly, myX, y, myYErr);
     945        tmpPoly = p_psVectorFitPolynomial1DOrd(myPoly, x64, y64, yErr64);
    962946    } else {
    963947        // XXX: psErrorMsg()
     
    966950
    967951    // Free any allocated memory.
    968     if (mustFreeMyYErr == true) {
    969         psFree(myYErr);
    970     }
    971     if (mustFreemyX == true) {
    972         psFree(myX);
    973     }
     952    /*
     953        if ((yErr == NULL) || (yErr->type.type != PS_TYPE_F64)) {
     954            psFree(yErr64);
     955        }
     956        if ((x == NULL) || (x->type.type != PS_TYPE_F64)) {
     957            psFree(x64);
     958        }
     959        if ((y == NULL) || (y->type.type != PS_TYPE_F64)) {
     960            psFree(y64);
     961        }
     962    */
    974963
    975964    return(myPoly);
Note: See TracChangeset for help on using the changeset viewer.