IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 5, 2004, 12:47:21 PM (22 years ago)
Author:
gusciora
Message:

Added macros in psConstants.h to check for NULL and mis-typed vectors.

File:
1 edited

Legend:

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

    r1953 r1963  
    99 *  @author George Gusciora, MHPCC
    1010 *
    11  *  @version $Revision: 1.52 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-10-05 01:43:58 $
     11 *  @version $Revision: 1.53 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-10-05 22:47:21 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    5656 
    5757XXX: change name
     58XXX: Use a static vector.
    5859 *****************************************************************************/
    5960void p_psBuildSums1D(double x,
     
    6364    int i = 0;
    6465    double xSum = 0.0;
     66
    6567    if (sums == NULL) {
    6668        sums = psVectorAlloc(polyOrder, PS_TYPE_F64);
     69    }
     70
     71    if (polyOrder > sums->n) {
     72        sums = psVectorRealloc(sums, polyOrder);
    6773    }
    6874
     
    7783VectorNormalizeGen(): this routine returns a psVector with "x" elements.  The
    7884values of the vector will be scaled uniformly between -1.0 and 1.0.
     85 
     86XXX: use a static vector.
    7987*****************************************************************************/
    8088psVector* VectorNormalizeGen(int x)
     
    100108 
    101109XXX: This algorithm is derived from the Numerical Recipes.
     110XXX: use recyvled vectors for internal data.
    102111 *****************************************************************************/
    103112float *CalculateSecondDerivs(const psVector* restrict x,        ///< Ordinates (or NULL to just use the indices)
     
    119128    float qn;
    120129
    121     if (x == NULL) {
    122         X = (float *) psAlloc(n * sizeof(float));
    123         for(i=0;i<n;i++) {
    124             X[i] = (float) i;
    125         }
    126         mustFreeX = true;
    127     }
    128 
    129130    // XXX: The second derivatives at the endpoints, undefined in the SDR,
    130131    // are set in psConstants.h: LEFT_SPLINE_DERIV, RIGHT_SPLINE_DERIV.
     
    167168    return(derivs2);
    168169}
    169 
    170170
    171171/******************************************************************************
     
    212212}
    213213
    214 
    215214/*****************************************************************************/
    216215/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
     
    258257    int i;
    259258    float slope;
    260 
    261     if (mySpline == NULL) {
    262         //XXX psErrorMsg()
    263     }
    264     if (y == NULL) {
    265         //XXX psErrorMsg()
    266     }
     259    psVector *x32 = NULL;
     260    psVector *y32 = NULL;
     261    psVector *yErr32 = NULL;
     262    static psVector *x32Static = NULL;
     263    static psVector *y32Static = NULL;
     264    static psVector *yErr32Static = NULL;
     265
     266    PS_CHECK_NULL_PTR_RETURN_NULL(mySpline);
     267    PS_CHECK_NULL_PTR_RETURN_NULL(y);
     268    VECTOR_CHECK_TYPE_F32_OR_F64_RETURN_NULL(y);
     269    CONVERT_VECTOR_F64_TO_F32(y, y32, y32Static);
     270
     271    // If yErr==NULL, set all errors equal.
     272    if (yErr == NULL) {
     273        GEN_PERSIST_YERR_VEC_F32(yErr32Static, y->n);
     274        yErr32 = yErr32Static;
     275    } else {
     276        VECTOR_CHECK_TYPE_F32_OR_F64_RETURN_NULL(yErr);
     277        CONVERT_VECTOR_F64_TO_F32(yErr, yErr32, yErr32Static);
     278    }
     279
     280    // If x==NULL, create an x32 vector with x values set to (0:n).
     281    if (x == NULL) {
     282        GEN_PERSIST_X_INDEX_VEC_F32(x32Static, y->n);
     283        x32 = x32Static;
     284    } else {
     285        VECTOR_CHECK_TYPE_F32_OR_F64_RETURN_NULL(x);
     286        CONVERT_VECTOR_F64_TO_F32(x, x32, x32Static);
     287    }
     288    PS_CHECK_VECTOR_SIZE_EQUAL_RETURN_NULL(x, y);
     289    PS_CHECK_VECTOR_SIZE_EQUAL_RETURN_NULL(yErr, y);
    267290
    268291    if (y->n != (1 + mySpline->n)) {
    269         psLogMsg(__func__, PS_LOG_WARN,
    270                  "data size / spline size mismatch (%d %d)\n",
    271                  y->n, mySpline->n);
    272         // XXX: psErrorMsg()
     292        psError(__func__, "data size / spline size mismatch (%d %d)\n",
     293                y->n, mySpline->n);
     294        return(NULL);
    273295    }
    274296
     
    295317    // Check if these are cubic splines (n==4).  If not, psError.
    296318    if (4 != (mySpline->spline[0])->n) {
    297         psLogMsg(__func__, PS_LOG_WARN,
    298                  "Don't know how to generate %d-order splines.",
    299                  (mySpline->spline[0])->n-1);
    300         // XXX: psErrorMsg()
     319        psError(__func__, "Don't know how to generate %d-order splines.",
     320                (mySpline->spline[0])->n-1);
    301321        return(NULL);
    302322    }
     
    315335        H = x->data.F32[i+1] - x->data.F32[i];
    316336        psTrace(".psLib.dataManip.psVectorFitSpline1D", 4,
    317                 "X data (%f - %f) (%f)\n", x->data.F32[i], x->data.F32[i+1], H);
     337                "x data (%f - %f) (%f)\n", x->data.F32[i], x->data.F32[i+1], H);
    318338        //
    319339        // ******** Calculate 0-order term ********
     
    630650}
    631651
    632 #define GEN_STATIC_RECYCLED_VECTOR(NAME, SIZE, TYPE) \
    633 static psVector *NAME = NULL; \
    634 NAME = psVectorRecycle(NAME, SIZE, TYPE); \
    635 p_psMemSetPersistent(NAME, true); \
    636 p_psMemSetPersistent(NAME->data.V, true); \
    637 
    638 
    639652/******************************************************************************
    640653p_psVectorFitPolynomial1DCheb():  This routine will fit a Chebyshev
     
    854867    int i;
    855868    psPolynomial1D *tmpPoly;
    856     PS_CHECK_NULL_1DPOLY_ACTION(myPoly, 0);
    857     PS_CHECK_NULL_VECTOR_ACTION(y, 0);
    858     PS_CHECK_EMPTY_VECTOR_ACTION(y, 0);
     869    PS_CHECK_NULL_1DPOLY_RETURN_NULL(myPoly);
     870    PS_CHECK_NULL_VECTOR_RETURN_NULL(y);
     871    PS_CHECK_EMPTY_VECTOR_RETURN_NULL(y);
    859872    psVector *x64 = NULL;
    860873    psVector *y64 = NULL;
     
    864877    static psVector *yErr64Static = NULL;
    865878
    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 
     879    VECTOR_CHECK_TYPE_F32_OR_F64_RETURN_NULL(y);
     880    CONVERT_VECTOR_F32_TO_F64(y, y64, y64Static);
    878881
    879882    // If yErr==NULL, set all errors equal.
    880883    if (yErr == NULL) {
    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         }
     884        GEN_PERSIST_YERR_VEC_F64(yErr64Static, y->n);
    888885        yErr64 = yErr64Static;
    889886    } else {
    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
    903     // this is a Chebyshev polynomial, we must scale to (-1:1).
    904     // XXX: Verify that this is the correct action.
     887        VECTOR_CHECK_TYPE_F32_OR_F64_RETURN_NULL(yErr);
     888        CONVERT_VECTOR_F32_TO_F64(yErr, yErr64, yErr64Static);
     889    }
     890
     891    // If x==NULL, create an x64 vector with x values set to (0:n).
    905892    if (x == NULL) {
    906         x64Static = psVectorRecycle(x64Static, y->n, PS_TYPE_F64);
    907         p_psMemSetPersistent(x64Static, true);
    908         p_psMemSetPersistent(x64Static->data.V, true);
     893        GEN_PERSIST_X_INDEX_VEC_F64(x64Static, y->n);
     894        if (myPoly->type == PS_POLYNOMIAL_CHEB) {
     895            p_psNormalizeVector(x64Static);
     896        }
    909897        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));
    922             }
    923         }
    924898    } else {
    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);
     899        VECTOR_CHECK_TYPE_F32_OR_F64_RETURN_NULL(x);
     900        CONVERT_VECTOR_F32_TO_F64(x, x64, x64Static);
     901    }
     902    PS_CHECK_VECTOR_SIZE_EQUAL_RETURN_NULL(x64, y64);
     903    PS_CHECK_VECTOR_SIZE_EQUAL_RETURN_NULL(yErr64, y64);
    940904
    941905    // Call the appropriate vector fitting routine.
    942906    if (myPoly->type == PS_POLYNOMIAL_CHEB) {
    943         tmpPoly = p_psVectorFitPolynomial1DCheby(myPoly, x64, y64, yErr64);
     907        p_psVectorFitPolynomial1DCheby(myPoly, x64, y64, yErr64);
    944908    } else if (myPoly->type == PS_POLYNOMIAL_ORD) {
    945909        tmpPoly = p_psVectorFitPolynomial1DOrd(myPoly, x64, y64, yErr64);
     
    948912        return(NULL);
    949913    }
    950 
    951     // Free any allocated memory.
    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     */
    963914
    964915    return(myPoly);
Note: See TracChangeset for help on using the changeset viewer.