IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 2265


Ignore:
Timestamp:
Nov 1, 2004, 5:57:21 PM (22 years ago)
Author:
gusciora
Message:

Code cleaning in psMinimize.c

Location:
trunk/psLib/src
Files:
4 edited

Legend:

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

    r2252 r2265  
    99 *  @author GLG, MHPCC
    1010 *
    11  *  @version $Revision: 1.81 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-11-02 01:04:37 $
     11 *  @version $Revision: 1.82 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-11-02 03:57:21 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    13611361        min->value = func(params, coords);
    13621362        psTrace(".psLib.dataManip.p_psLineMin", 2,
    1363                 "p_psLineMin() called with zero line vector.  Return 0.0.  Function valus is %f\n", min->value);
     1363                "p_psLineMin() called with zero line vector.  Return 0.0.  Function value is %f\n", min->value);
    13641364        return(0.0);
    13651365    }
     
    14691469XXX: Define constants for default dummy number of iterations and tolerance.
    14701470 
    1471 XXX: Must define behavior on various NULL inputs.
    1472  
    14731471XXX: Check for F32 types?
    14741472 *****************************************************************************/
     1473#define PS_MINIMIZE_POWELL_LINEMIN_MAX_ITERATIONS 20
     1474#define PS_MINIMIZE_POWELL_LINEMIN_ERROR_TOLERANCE 0.01
     1475
    14751476psBool psMinimizePowell(psMinimization *min,
    14761477                        psVector *params,
     
    14851486    PS_PTR_CHECK_NULL(coords, NULL);
    14861487    PS_PTR_CHECK_NULL(func, NULL);
    1487 
    14881488    psS32 numDims = params->n;
    14891489    PS_VECTOR_GEN_STATIC_RECYCLED(pQP, numDims, PS_TYPE_F32);
     
    14921492    psS32 i = 0;
    14931493    psS32 j = 0;
    1494     psVector **v = NULL;
    1495     psVector *myParamMask = NULL;  // XXX: Use a recylced vector here.
     1494    psVector *myParamMask = NULL;
    14961495    psMinimization dummyMin;
    14971496    float mul = 0.0;
     
    15001499    psS32 biggestIter = 0;
    15011500    float biggestDiff = 0.0;
    1502     float term1 = 0.0;
    1503     float term2 = 0.0;
    1504     float fqp = 0.0;
    1505     float diff = 0.0;
    15061501    int iterationNumber = 0;
    15071502
     
    15261521
    15271522    // 1: Set v[i] to be the unit vectors for each dimension in params
    1528     v = (psVector **) psAlloc(params->n * sizeof(psVector **));
     1523
     1524    psArray *v = psArrayAlloc(numDims);
    15291525    for (i=0;i<numDims;i++) {
    1530         v[i] = psVectorAlloc(numDims, PS_TYPE_F32);
     1526        (v->data[i]) = (psVector *) psVectorAlloc(numDims, PS_TYPE_F32);
    15311527        for (j=0;j<numDims;j++) {
    15321528            if (i == j) {
    1533                 v[i]->data.F32[j] = 1.0;
     1529                ((psVector *) (v->data[i]))->data.F32[j] = 1.0;
    15341530            } else {
    1535                 v[i]->data.F32[j] = 0.0;
     1531                ((psVector *) (v->data[i]))->data.F32[j] = 0.0;
    15361532            }
    15371533        }
     
    15581554                "Current function value is %f\n", currFuncVal);
    15591555
    1560         diff = 0.0;
    15611556        biggestDiff = 0;
    15621557        biggestIter = 0;
    15631558        for (i=0;i<numDims;i++) {
    15641559            if (myParamMask->data.U8[i] == 0) {
    1565                 dummyMin.maxIter = 20;
    1566                 dummyMin.tol = 0.01;
    1567                 mul = p_psLineMin(&dummyMin, Q, v[i], myParamMask, coords, func);
     1560                dummyMin.maxIter = PS_MINIMIZE_POWELL_LINEMIN_MAX_ITERATIONS;
     1561                dummyMin.tol = PS_MINIMIZE_POWELL_LINEMIN_ERROR_TOLERANCE;
     1562                mul = p_psLineMin(&dummyMin, Q, ((psVector *) v->data[i]), myParamMask, coords, func);
    15681563                if (isnan(mul)) {
    15691564                    psError(__func__, "Could not perform line minimization (1).\n");
    1570                     for (j=0;j<numDims;j++) {
    1571                         psFree(v[j]);
    1572                     }
    15731565                    psFree(v);
    15741566                    return(false);
     
    16081600        }
    16091601
    1610         mul = p_psLineMin(min, params, u, myParamMask, coords, func);
     1602        mul = p_psLineMin(&dummyMin, params, u, myParamMask, coords, func);
    16111603        if (isnan(mul)) {
    16121604            psError(__func__, "Could not perform line minimization. (2)\n");
    1613             for (i=0;i<numDims;i++) {
    1614                 psFree(v[i]);
    1615             }
    16161605            psFree(v);
    16171606            return(false);
     
    16191608
    16201609        // 6:
    1621         if (min->value > currFuncVal) {
    1622             for (i=0;i<numDims;i++) {
    1623                 psFree(v[i]);
    1624             }
     1610        if (dummyMin.value > currFuncVal) {
    16251611            psFree(v);
    16261612            min->iter = iterationNumber;
     
    16371623            }
    16381624        }
    1639         fqp = func(pQP, coords);
    1640 
    1641         term1 = (baseFuncVal - currFuncVal) - biggestDiff;
     1625        float fqp = func(pQP, coords);
     1626        float term1 = (baseFuncVal - currFuncVal) - biggestDiff;
    16421627        term1*= term1;
    16431628        term1*= 2.0 * (baseFuncVal - (2.0 * currFuncVal) + fqp);
    1644         term2 = baseFuncVal - fqp;
     1629        float term2 = baseFuncVal - fqp;
    16451630        term2*= term2 * biggestDiff;
    16461631        if (term1 < term2) {
    16471632            for (i=0;i<numDims;i++) {
    16481633                if (myParamMask->data.U8[i] == 0) {
    1649                     v[biggestIter]->data.F32[i] = u->data.F32[i];
     1634                    ((psVector *) v->data[biggestIter])->data.F32[i] = u->data.F32[i];
    16501635                }
    16511636            }
     
    16611646        // 8: Go to step 3 until the change is less than some tolerance.
    16621647        if (fabs(baseFuncVal - currFuncVal) <= min->tol) {
    1663             for (i=0;i<numDims;i++) {
    1664                 psFree(v[i]);
    1665             }
    16661648            psFree(v);
    16671649            min->iter = iterationNumber;
     
    16721654    }
    16731655
    1674     for (i=0;i<numDims;i++) {
    1675         psFree(v[i]);
    1676     }
    16771656    psFree(v);
    16781657    min->iter = iterationNumber;
  • trunk/psLib/src/image/psImageStats.c

    r2262 r2265  
    99*  @author GLG, MHPCC
    1010*
    11 *  @version $Revision: 1.48 $ $Name: not supported by cvs2svn $
    12 *  @date $Date: 2004-11-02 02:03:56 $
     11*  @version $Revision: 1.49 $ $Name: not supported by cvs2svn $
     12*  @date $Date: 2004-11-02 03:57:21 $
    1313*
    1414*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    149149}
    150150
    151 double *CalcScaleFactors(psS32 n)
    152 {
    153     psS32 i = 0;
    154     double tmp = 0.0;
    155     double *scalingFactors = (double *)psAlloc(n * sizeof(double));
    156 
    157     for (i = 0; i < n; i++) {
    158         tmp = (double)(n - i);
    159         tmp = (M_PI * (tmp - 0.5)) / ((double)n);
    160         scalingFactors[i] = cos(tmp);
    161     }
    162 
    163     return (scalingFactors);
    164 }
    165 
    166 // XXX: Why do we have this function?
    167 double *CalcScaleFactorsFit(psS32 n)
    168 {
    169     psS32 i = 0;
    170     double tmp = 0.0;
    171     double *scalingFactors = (double *)psAlloc(n * sizeof(double));
    172 
    173     for (i = 0; i < n; i++) {
    174         tmp = (double)(n - i);
    175         tmp = (M_PI * (tmp - 0.5)) / ((double)n);
    176         scalingFactors[i] = cos(tmp);
    177     }
    178 
    179     return (scalingFactors);
    180 }
    181 
    182151/*****************************************************************************
    183152CalcScaleFactorsEval(n): The Chebyshev polynomials are defined over the
     
    187156output a vector of evenly spaced floating point values between -1.0:1.0.
    188157 *****************************************************************************/
    189 double *CalcScaleFactorsEval(psS32 n)
    190 {
     158double *CalcScaleFactors(psS32 n)
     159{
     160    PS_INT_CHEC_NON_NEGATIVE(n, NULL);
    191161    psS32 i = 0;
    192162    double tmp = 0.0;
    193 
    194     return(CalcScaleFactorsFit(n));
    195 
    196163    double *scalingFactors = (double *)psAlloc(n * sizeof(double));
    197164
     
    201168        scalingFactors[i] = cos(tmp);
    202169    }
     170
    203171    return (scalingFactors);
    204172}
    205173
    206 // XXX: Use a static array of CHebyshev polynomials.
     174psVector *VecCalcScaleFactors(psVector *scalingFactors, psS32 n)
     175{
     176    PS_VECTOR_CHECK_NULL(scalingFactors, NULL);
     177    PS_VECTOR_CHECK_TYPE(scalingFactors, PS_TYPE_F64, NULL);
     178    PS_INT_CHEC_NON_NEGATIVE(n, NULL);
     179    psS32 i = 0;
     180    double tmp = 0.0;
     181
     182    for (i = 0; i < n; i++) {
     183        tmp = (double)(n - i);
     184        tmp = (M_PI * (tmp - 0.5)) / ((double)n);
     185        scalingFactors->data.F64[i] = cos(tmp);
     186    }
     187
     188    return (scalingFactors);
     189}
     190
     191// XXX: Use a static array of Chebyshev polynomials.
    207192psPolynomial1D **p_psCreateChebyshevPolys(psS32 maxChebyPoly)
    208193{
  • trunk/psLib/src/imageops/psImageStats.c

    r2262 r2265  
    99*  @author GLG, MHPCC
    1010*
    11 *  @version $Revision: 1.48 $ $Name: not supported by cvs2svn $
    12 *  @date $Date: 2004-11-02 02:03:56 $
     11*  @version $Revision: 1.49 $ $Name: not supported by cvs2svn $
     12*  @date $Date: 2004-11-02 03:57:21 $
    1313*
    1414*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    149149}
    150150
    151 double *CalcScaleFactors(psS32 n)
    152 {
    153     psS32 i = 0;
    154     double tmp = 0.0;
    155     double *scalingFactors = (double *)psAlloc(n * sizeof(double));
    156 
    157     for (i = 0; i < n; i++) {
    158         tmp = (double)(n - i);
    159         tmp = (M_PI * (tmp - 0.5)) / ((double)n);
    160         scalingFactors[i] = cos(tmp);
    161     }
    162 
    163     return (scalingFactors);
    164 }
    165 
    166 // XXX: Why do we have this function?
    167 double *CalcScaleFactorsFit(psS32 n)
    168 {
    169     psS32 i = 0;
    170     double tmp = 0.0;
    171     double *scalingFactors = (double *)psAlloc(n * sizeof(double));
    172 
    173     for (i = 0; i < n; i++) {
    174         tmp = (double)(n - i);
    175         tmp = (M_PI * (tmp - 0.5)) / ((double)n);
    176         scalingFactors[i] = cos(tmp);
    177     }
    178 
    179     return (scalingFactors);
    180 }
    181 
    182151/*****************************************************************************
    183152CalcScaleFactorsEval(n): The Chebyshev polynomials are defined over the
     
    187156output a vector of evenly spaced floating point values between -1.0:1.0.
    188157 *****************************************************************************/
    189 double *CalcScaleFactorsEval(psS32 n)
    190 {
     158double *CalcScaleFactors(psS32 n)
     159{
     160    PS_INT_CHEC_NON_NEGATIVE(n, NULL);
    191161    psS32 i = 0;
    192162    double tmp = 0.0;
    193 
    194     return(CalcScaleFactorsFit(n));
    195 
    196163    double *scalingFactors = (double *)psAlloc(n * sizeof(double));
    197164
     
    201168        scalingFactors[i] = cos(tmp);
    202169    }
     170
    203171    return (scalingFactors);
    204172}
    205173
    206 // XXX: Use a static array of CHebyshev polynomials.
     174psVector *VecCalcScaleFactors(psVector *scalingFactors, psS32 n)
     175{
     176    PS_VECTOR_CHECK_NULL(scalingFactors, NULL);
     177    PS_VECTOR_CHECK_TYPE(scalingFactors, PS_TYPE_F64, NULL);
     178    PS_INT_CHEC_NON_NEGATIVE(n, NULL);
     179    psS32 i = 0;
     180    double tmp = 0.0;
     181
     182    for (i = 0; i < n; i++) {
     183        tmp = (double)(n - i);
     184        tmp = (M_PI * (tmp - 0.5)) / ((double)n);
     185        scalingFactors->data.F64[i] = cos(tmp);
     186    }
     187
     188    return (scalingFactors);
     189}
     190
     191// XXX: Use a static array of Chebyshev polynomials.
    207192psPolynomial1D **p_psCreateChebyshevPolys(psS32 maxChebyPoly)
    208193{
  • trunk/psLib/src/math/psMinimize.c

    r2252 r2265  
    99 *  @author GLG, MHPCC
    1010 *
    11  *  @version $Revision: 1.81 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-11-02 01:04:37 $
     11 *  @version $Revision: 1.82 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-11-02 03:57:21 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    13611361        min->value = func(params, coords);
    13621362        psTrace(".psLib.dataManip.p_psLineMin", 2,
    1363                 "p_psLineMin() called with zero line vector.  Return 0.0.  Function valus is %f\n", min->value);
     1363                "p_psLineMin() called with zero line vector.  Return 0.0.  Function value is %f\n", min->value);
    13641364        return(0.0);
    13651365    }
     
    14691469XXX: Define constants for default dummy number of iterations and tolerance.
    14701470 
    1471 XXX: Must define behavior on various NULL inputs.
    1472  
    14731471XXX: Check for F32 types?
    14741472 *****************************************************************************/
     1473#define PS_MINIMIZE_POWELL_LINEMIN_MAX_ITERATIONS 20
     1474#define PS_MINIMIZE_POWELL_LINEMIN_ERROR_TOLERANCE 0.01
     1475
    14751476psBool psMinimizePowell(psMinimization *min,
    14761477                        psVector *params,
     
    14851486    PS_PTR_CHECK_NULL(coords, NULL);
    14861487    PS_PTR_CHECK_NULL(func, NULL);
    1487 
    14881488    psS32 numDims = params->n;
    14891489    PS_VECTOR_GEN_STATIC_RECYCLED(pQP, numDims, PS_TYPE_F32);
     
    14921492    psS32 i = 0;
    14931493    psS32 j = 0;
    1494     psVector **v = NULL;
    1495     psVector *myParamMask = NULL;  // XXX: Use a recylced vector here.
     1494    psVector *myParamMask = NULL;
    14961495    psMinimization dummyMin;
    14971496    float mul = 0.0;
     
    15001499    psS32 biggestIter = 0;
    15011500    float biggestDiff = 0.0;
    1502     float term1 = 0.0;
    1503     float term2 = 0.0;
    1504     float fqp = 0.0;
    1505     float diff = 0.0;
    15061501    int iterationNumber = 0;
    15071502
     
    15261521
    15271522    // 1: Set v[i] to be the unit vectors for each dimension in params
    1528     v = (psVector **) psAlloc(params->n * sizeof(psVector **));
     1523
     1524    psArray *v = psArrayAlloc(numDims);
    15291525    for (i=0;i<numDims;i++) {
    1530         v[i] = psVectorAlloc(numDims, PS_TYPE_F32);
     1526        (v->data[i]) = (psVector *) psVectorAlloc(numDims, PS_TYPE_F32);
    15311527        for (j=0;j<numDims;j++) {
    15321528            if (i == j) {
    1533                 v[i]->data.F32[j] = 1.0;
     1529                ((psVector *) (v->data[i]))->data.F32[j] = 1.0;
    15341530            } else {
    1535                 v[i]->data.F32[j] = 0.0;
     1531                ((psVector *) (v->data[i]))->data.F32[j] = 0.0;
    15361532            }
    15371533        }
     
    15581554                "Current function value is %f\n", currFuncVal);
    15591555
    1560         diff = 0.0;
    15611556        biggestDiff = 0;
    15621557        biggestIter = 0;
    15631558        for (i=0;i<numDims;i++) {
    15641559            if (myParamMask->data.U8[i] == 0) {
    1565                 dummyMin.maxIter = 20;
    1566                 dummyMin.tol = 0.01;
    1567                 mul = p_psLineMin(&dummyMin, Q, v[i], myParamMask, coords, func);
     1560                dummyMin.maxIter = PS_MINIMIZE_POWELL_LINEMIN_MAX_ITERATIONS;
     1561                dummyMin.tol = PS_MINIMIZE_POWELL_LINEMIN_ERROR_TOLERANCE;
     1562                mul = p_psLineMin(&dummyMin, Q, ((psVector *) v->data[i]), myParamMask, coords, func);
    15681563                if (isnan(mul)) {
    15691564                    psError(__func__, "Could not perform line minimization (1).\n");
    1570                     for (j=0;j<numDims;j++) {
    1571                         psFree(v[j]);
    1572                     }
    15731565                    psFree(v);
    15741566                    return(false);
     
    16081600        }
    16091601
    1610         mul = p_psLineMin(min, params, u, myParamMask, coords, func);
     1602        mul = p_psLineMin(&dummyMin, params, u, myParamMask, coords, func);
    16111603        if (isnan(mul)) {
    16121604            psError(__func__, "Could not perform line minimization. (2)\n");
    1613             for (i=0;i<numDims;i++) {
    1614                 psFree(v[i]);
    1615             }
    16161605            psFree(v);
    16171606            return(false);
     
    16191608
    16201609        // 6:
    1621         if (min->value > currFuncVal) {
    1622             for (i=0;i<numDims;i++) {
    1623                 psFree(v[i]);
    1624             }
     1610        if (dummyMin.value > currFuncVal) {
    16251611            psFree(v);
    16261612            min->iter = iterationNumber;
     
    16371623            }
    16381624        }
    1639         fqp = func(pQP, coords);
    1640 
    1641         term1 = (baseFuncVal - currFuncVal) - biggestDiff;
     1625        float fqp = func(pQP, coords);
     1626        float term1 = (baseFuncVal - currFuncVal) - biggestDiff;
    16421627        term1*= term1;
    16431628        term1*= 2.0 * (baseFuncVal - (2.0 * currFuncVal) + fqp);
    1644         term2 = baseFuncVal - fqp;
     1629        float term2 = baseFuncVal - fqp;
    16451630        term2*= term2 * biggestDiff;
    16461631        if (term1 < term2) {
    16471632            for (i=0;i<numDims;i++) {
    16481633                if (myParamMask->data.U8[i] == 0) {
    1649                     v[biggestIter]->data.F32[i] = u->data.F32[i];
     1634                    ((psVector *) v->data[biggestIter])->data.F32[i] = u->data.F32[i];
    16501635                }
    16511636            }
     
    16611646        // 8: Go to step 3 until the change is less than some tolerance.
    16621647        if (fabs(baseFuncVal - currFuncVal) <= min->tol) {
    1663             for (i=0;i<numDims;i++) {
    1664                 psFree(v[i]);
    1665             }
    16661648            psFree(v);
    16671649            min->iter = iterationNumber;
     
    16721654    }
    16731655
    1674     for (i=0;i<numDims;i++) {
    1675         psFree(v[i]);
    1676     }
    16771656    psFree(v);
    16781657    min->iter = iterationNumber;
Note: See TracChangeset for help on using the changeset viewer.