IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 15, 2005, 2:06:33 PM (21 years ago)
Author:
gusciora
Message:

...

File:
1 edited

Legend:

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

    r4540 r4568  
    99 *  @author GLG, MHPCC
    1010 *
    11  *  @version $Revision: 1.124 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2005-07-12 19:12:01 $
     11 *  @version $Revision: 1.125 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2005-07-16 00:06:32 $
    1313 *
    1414 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    166166 
    167167XXX: spline->knots must be psF32
     168 
     169XXXX: Remove this for next code shipment.
    168170 *****************************************************************************/
    169171/*
     
    262264        PS_ASSERT_VECTOR_TYPE(mySpline->knots, PS_TYPE_F32, NULL);
    263265    }
    264 
    265266    psTrace(".psLib.dataManip.psVectorFitSpline1D", 4,
    266267            "---- psVectorFitSpline1D() begin ----\n");
     
    425426
    426427    }
     428
     429    psTrace(".psLib.dataManip.psVectorFitSpline1D", 4,
     430            "---- psVectorFitSpline1D() end ----\n");
     431    return(mySpline);
     432}
     433
     434
     435
     436
     437
     438
     439
     440
     441
     442/*****************************************************************************
     443psVectorFitSpline1DNEW(): given a psSpline1D data structure and a set of x/y
     444 
     445xF32 and yF32 are internal psVectors which are used to hold the psF32 versions
     446of the input data, if necessary.  xPtr and yPtr are pointers to either xF32 or
     447the x argument.  All computation is done on xPtr and yPtr.  xF32 and yF32 will
     448simply be psFree() at the end.
     449 
     450XXX: nKnots makes no sense.  This number is always equal to the size of the x
     451an y vectors.
     452 
     453XXX: How do we specify the spline order?  For now, order=3.
     454 *****************************************************************************/
     455#define PS_XXX_SPLINE_ORDER 3
     456psSpline1D *psVectorFitSpline1DNEW(const psVector* x,        ///< Ordinates (or NULL to just use the indices)
     457                                   const psVector* y,        ///< Coordinates
     458                                   int nKnots)
     459{
     460    psTrace(".psLib.dataManip.psVectorFitSpline1D", 4, "---- psVectorFitSpline1DNEW() begin ----\n");
     461    PS_ASSERT_VECTOR_NON_NULL(y, NULL);
     462    PS_ASSERT_VECTOR_TYPE_F32_OR_F64(y, NULL);
     463    //    PS_ASSERT_INT_EQUAL(y->n, nKnots);
     464
     465    //
     466    // The following code ensures that xPtr points to a psF32 version of the
     467    // ordinate data.
     468    //
     469    psVector *xF32 = NULL;
     470    psVector *xPtr = NULL;
     471    if (x != NULL) {
     472        PS_ASSERT_VECTORS_SIZE_EQUAL(x, y, NULL);
     473        if (PS_TYPE_F64 == x->type.type) {
     474            xF32 = psVectorAlloc(y->n, PS_TYPE_F32);
     475            for (psS32 i = 0 ; i < x->n ; i++) {
     476                xF32->data.F32[i] = (psF32) x->data.F64[i];
     477            }
     478            xPtr = xF32;
     479        } else if (PS_TYPE_F32 == x->type.type) {
     480            xPtr = (psVector *) x;
     481        } else {
     482            printf("XXX: Gen Error message: x is wrong type.\n");
     483            return(NULL);
     484        }
     485    } else {
     486        // If x==NULL, create an x32 vector with x values set to (0:n).
     487        xF32 = psVectorAlloc(y->n, PS_TYPE_F32);
     488        for (psS32 i = 0 ; i < x->n ; i++) {
     489            xF32->data.F32[i] = (psF32) i;
     490        }
     491        xPtr = xF32;
     492    }
     493
     494    //
     495    // If y is of type psF64, then create a new vector yF32 and convert the
     496    // y elements.  Regardless of y's type, we create a yPtr which will be
     497    // used in the remainder of this function.
     498    //
     499    psVector *yF32 = NULL;
     500    psVector *yPtr = NULL;
     501    if (PS_TYPE_F64 == y->type.type) {
     502        yF32 = psVectorAlloc(y->n, PS_TYPE_F32);
     503        for (psS32 i = 0 ; i < y->n ; i++) {
     504            yF32->data.F32[i] = (psF32) y->data.F64[i];
     505        }
     506        yPtr = yF32;
     507    } else {
     508        yPtr = (psVector *) y;
     509    }
     510
     511    psSpline1D *mySpline = psSpline1DAllocGeneric(xPtr, PS_XXX_SPLINE_ORDER);
     512
     513    psS32 numSplines = nKnots - 1;
     514    psF32 tmp;
     515    psF32 H;
     516    psS32 i;
     517    psF32 slope;
     518    // XXX: get rid of x32 and y32 (this is from old code)
     519    psVector *x32 = xPtr;
     520    psVector *y32 = yPtr;
     521
     522
     523    // If these are linear splines, which means their polynomials will have
     524    // two coefficients, then we do the simple calculation.
     525    if (1 == PS_XXX_SPLINE_ORDER) {
     526        for (i=0;i<mySpline->n;i++) {
     527            slope = (y32->data.F32[i+1] - y32->data.F32[i]) /
     528                    (mySpline->knots->data.F32[i+1] - mySpline->knots->data.F32[i]);
     529            (mySpline->spline[i])->coeff[0] = y32->data.F32[i] -
     530                                              (slope * mySpline->knots->data.F32[i]);
     531
     532            (mySpline->spline[i])->coeff[1] = slope;
     533            psTrace(".psLib.dataManip.psMinimize.psVectorFitSpline1D", 4,
     534                    "---- mySpline %d coeffs are (%f, %f)\n", i,
     535                    (mySpline->spline[i])->coeff[0],
     536                    (mySpline->spline[i])->coeff[1]);
     537        }
     538        psTrace(".psLib.dataManip.psMinimize.psVectorFitSpline1D", 4,
     539                "---- Exiting psVectorFitSpline1D()()\n");
     540        return((psSpline1D *) mySpline);
     541    }
     542
     543    //
     544    // Check if these are cubic splines (n==4).  If not, psError.
     545    //
     546    if (3 != PS_XXX_SPLINE_ORDER) {
     547        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
     548                "Don't know how to generate %d-order splines.", PS_XXX_SPLINE_ORDER);
     549        return(NULL);
     550    }
     551
     552    //
     553    // If we get here, then we know these are cubic splines.  We first
     554    // generate the second derivatives at each data point.
     555    //
     556    mySpline->p_psDeriv2 = calculateSecondDerivs(x32, y32);
     557    for (i=0;i<y32->n;i++)
     558        psTrace(".psLib.dataManip.psVectorFitSpline1D", 6,
     559                "Second deriv[%d] is %f\n", i, mySpline->p_psDeriv2[i]);
     560
     561    //
     562    // We generate the coefficients of the spline polynomials.  I can't
     563    // concisely explain how this code works.  See above function comments
     564    // and Numerical Recipes in C.
     565    //
     566    for (i=0;i<numSplines;i++) {
     567        H = x32->data.F32[i+1] - x32->data.F32[i];
     568        psTrace(".psLib.dataManip.psVectorFitSpline1D", 4,
     569                "x data (%f - %f) (%f)\n",
     570                x32->data.F32[i],
     571                x32->data.F32[i+1], H);
     572        //
     573        // ******** Calculate 0-order term ********
     574        //
     575        // From (1)
     576        (mySpline->spline[i])->coeff[0] = (y32->data.F32[i] * x32->data.F32[i+1]/H);
     577        // From (2)
     578        ((mySpline->spline[i])->coeff[0])-= ((y32->data.F32[i+1] * x32->data.F32[i])/H);
     579        // From (3)
     580        tmp = (x32->data.F32[i+1] * x32->data.F32[i+1] * x32->data.F32[i+1]) / (H * H * H);
     581        tmp-= (x32->data.F32[i+1] / H);
     582        tmp*= (mySpline->p_psDeriv2)[i] * H * H / 6.0;
     583        ((mySpline->spline[i])->coeff[0])+= tmp;
     584        // From (4)
     585        tmp = -(x32->data.F32[i] * x32->data.F32[i] * x32->data.F32[i]) / (H * H * H);
     586        tmp+= (x32->data.F32[i] / H);
     587        tmp*= (mySpline->p_psDeriv2)[i+1] * H * H / 6.0;
     588        ((mySpline->spline[i])->coeff[0])+= tmp;
     589
     590        //
     591        // ******** Calculate 1-order term ********
     592        //
     593        // From (1)
     594        (mySpline->spline[i])->coeff[1] = -(y32->data.F32[i]) / H;
     595        // From (2)
     596        ((mySpline->spline[i])->coeff[1])+= (y32->data.F32[i+1] / H);
     597        // From (3)
     598        tmp = -3.0 * (x32->data.F32[i+1] * x32->data.F32[i+1]) / (H * H * H);
     599        tmp+= (1.0 / H);
     600        tmp*= ((mySpline->p_psDeriv2)[i]) * H * H / 6.0;
     601        ((mySpline->spline[i])->coeff[1])+= tmp;
     602        // From (4)
     603        tmp = 3.0 * (x32->data.F32[i] * x32->data.F32[i]) / (H * H * H);
     604        tmp-= (1.0 / H);
     605        tmp*= ((mySpline->p_psDeriv2)[i+1]) * H * H / 6.0;
     606        ((mySpline->spline[i])->coeff[1])+= tmp;
     607
     608        //
     609        // ******** Calculate 2-order term ********
     610        //
     611        // From (3)
     612        (mySpline->spline[i])->coeff[2] = ((mySpline->p_psDeriv2)[i]) * 3.0 * x32->data.F32[i+1] / (6.0 * H);
     613        // From (4)
     614        ((mySpline->spline[i])->coeff[2])-= (((mySpline->p_psDeriv2)[i+1]) * 3.0 * x32->data.F32[i] / (6.0 * H));
     615
     616        //
     617        // ******** Calculate 3-order term ********
     618        //
     619        // From (3)
     620        (mySpline->spline[i])->coeff[3] = -((mySpline->p_psDeriv2)[i]) / (6.0 * H);
     621        // From (4)
     622        ((mySpline->spline[i])->coeff[3])+=  ((mySpline->p_psDeriv2)[i+1]) / (6.0 * H);
     623
     624        psTrace(".psLib.dataManip.psVectorFitSpline1D", 6,
     625                "(mySpline->spline[%d])->coeff[0] is %f\n", i, (mySpline->spline[i])->coeff[0]);
     626        psTrace(".psLib.dataManip.psVectorFitSpline1D", 6,
     627                "(mySpline->spline[%d])->coeff[1] is %f\n", i, (mySpline->spline[i])->coeff[1]);
     628        psTrace(".psLib.dataManip.psVectorFitSpline1D", 6,
     629                "(mySpline->spline[%d])->coeff[2] is %f\n", i, (mySpline->spline[i])->coeff[2]);
     630        psTrace(".psLib.dataManip.psVectorFitSpline1D", 6,
     631                "(mySpline->spline[%d])->coeff[3] is %f\n", i, (mySpline->spline[i])->coeff[3]);
     632
     633    }
     634
     635    psFree(xF32);
     636    psFree(yF32);
    427637
    428638    psTrace(".psLib.dataManip.psVectorFitSpline1D", 4,
Note: See TracChangeset for help on using the changeset viewer.