IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 10605


Ignore:
Timestamp:
Dec 9, 2006, 6:13:25 PM (20 years ago)
Author:
magnier
Message:

added polynomial copy, recycle, derivative functions, derivative tests

Location:
trunk/psLib
Files:
3 edited

Legend:

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

    r10598 r10605  
    77*  polynomials.  It also contains a Gaussian functions.
    88*
    9 *  @version $Revision: 1.153 $ $Name: not supported by cvs2svn $
    10 *  @date $Date: 2006-12-09 14:19:56 $
     9*  @version $Revision: 1.154 $ $Name: not supported by cvs2svn $
     10*  @date $Date: 2006-12-10 04:13:25 $
    1111*
    1212*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    690690{
    691691    if (out == NULL) {
    692         psPolynomial2DAlloc (poly->type, poly->nX, poly->nY);
     692        out = psPolynomial2DAlloc (poly->type, poly->nX, poly->nY);
    693693    } else {
    694694        psPolynomial2DRecycle (out, poly->type, poly->nX, poly->nY);
  • trunk/psLib/src/math/psPolynomialUtils.c

    r10598 r10605  
    180180psPolynomial2D *psPolynomial2D_dX (psPolynomial2D *out, psPolynomial2D *poly)
    181181{
     182    psPolynomial2D *dest = NULL;
    182183    int nXout = poly->nX - 1;
    183184    int nYout = poly->nY;
    184185
     186    if (poly->nX == 0)
     187        return NULL;
     188
     189    bool inPlace = false;
     190
    185191    if (out == NULL) {
    186         out = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, nXout, nYout);
    187     } else {
    188         psPolynomial2DRecycle (out, poly->type, nXout, nYout);
     192        dest = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, nXout, nYout);
     193        out = dest;
     194    } else {
     195        if (out == poly) {
     196            inPlace = true;
     197            dest = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, nXout, nYout);
     198        } else {
     199            dest = out;
     200            psPolynomial2DRecycle (dest, poly->type, nXout, nYout);
     201        }
    189202    }
    190203
    191204    for (int i = 0; i < nXout + 1; i++) {
    192205        for (int j = 0; j < nYout + 1; j++) {
    193             out->mask[i][j] = poly->mask[i+1][j];
    194             out->coeff[i][j] = poly->coeff[i+1][j] * (i+1);
    195         }
     206            dest->mask[i][j] = poly->mask[i+1][j];
     207            dest->coeff[i][j] = poly->coeff[i+1][j] * (i+1);
     208        }
     209    }
     210
     211    if (inPlace) {
     212        psFree (poly);
     213        out = dest;
    196214    }
    197215    return out;
     
    200218psPolynomial2D *psPolynomial2D_dY (psPolynomial2D *out, psPolynomial2D *poly)
    201219{
    202 
    203     int nXout = poly->nX - 1;
    204     int nYout = poly->nY;
     220    psPolynomial2D *dest = NULL;
     221    int nXout = poly->nX;
     222    int nYout = poly->nY - 1;
     223
     224    if (poly->nY == 0)
     225        return NULL;
     226
     227    bool inPlace = false;
    205228
    206229    if (out == NULL) {
    207         out = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, nXout, nYout);
    208     } else {
    209         psPolynomial2DRecycle (out, poly->type, nXout, nYout);
     230        dest = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, nXout, nYout);
     231        out = dest;
     232    } else {
     233        if (out == poly) {
     234            inPlace = true;
     235            dest = psPolynomial2DAlloc (PS_POLYNOMIAL_ORD, nXout, nYout);
     236        } else {
     237            dest = out;
     238            psPolynomial2DRecycle (dest, poly->type, nXout, nYout);
     239        }
    210240    }
    211241
    212242    for (int i = 0; i < nXout + 1; i++) {
    213243        for (int j = 0; j < nYout + 1; j++) {
    214             out->mask[i][j] = poly->mask[i][j+1];
    215             out->coeff[i][j] = poly->coeff[i][j+1] * (j+1);
    216         }
     244            dest->mask[i][j] = poly->mask[i][j+1];
     245            dest->coeff[i][j] = poly->coeff[i][j+1] * (j+1);
     246        }
     247    }
     248
     249    if (inPlace) {
     250        psFree (poly);
     251        out = dest;
    217252    }
    218253    return out;
  • trunk/psLib/test/math/tap_psPolynomialUtils_Derivatives.c

    r10597 r10605  
    88int main (void)
    99{
    10     plan_tests(48);
     10    plan_tests(72);
    1111
    1212    diag("psPolynomial2D Derivative tests");
     
    109109    }
    110110
     111    // test psPolynomial2D_dX (inPlace: supplied output == supplied input)
     112    {
     113        psMemId id = psMemGetId();
     114
     115        diag ("test psPolynomial2D_dX (supplied output)");
     116
     117        psPolynomial2D *poly = psPolynomial2DAlloc(PS_POLYNOMIAL_ORD, 2, 2);
     118        ok(poly != NULL, "psPolynomial2D successfully allocated");
     119        skip_start(poly == NULL, 5, "Skipping tests because psPolynomial2DAlloc() failed");
     120
     121        // build sample polynomial (upper-left elements only)
     122        // z = 5 + 2x + 3y - 3x^2 + 4xy - 2y^2
     123        // dz/dx = 2 - 6x + 4y
     124        poly->coeff[0][0] = 5.0;
     125        poly->coeff[1][0] = 2.0;
     126        poly->coeff[0][1] = 3.0;
     127        poly->coeff[2][0] = -3.0;
     128        poly->coeff[1][1] = 4.0;
     129        poly->coeff[0][2] = -2.0;
     130
     131        // mask remaining elements
     132        poly->mask[2][1] = 1;
     133        poly->mask[1][2] = 1;
     134        poly->mask[2][2] = 1;
     135
     136        poly = psPolynomial2D_dX (poly, poly);
     137
     138        ok(poly->nX == 1, "new x order is %d", poly->nX);
     139        ok(poly->nY == 2, "new y order is %d", poly->nY);
     140
     141        ok_float(poly->coeff[0][0], +2.0, "x^0 y^0 coeff is %f", poly->coeff[0][0]);
     142        ok_float(poly->coeff[1][0], -6.0, "x^1 y^0 coeff is %f", poly->coeff[1][0]);
     143        ok_float(poly->coeff[0][1], +4.0, "x^0 y^1 coeff is %f", poly->coeff[0][1]);
     144
     145        ok(!poly->mask[0][0], "x^0 y^0 coeff is unmasked");
     146        ok(!poly->mask[1][0], "x^1 y^0 coeff is unmasked");
     147        ok(!poly->mask[0][1], "x^0 y^1 coeff is unmasked");
     148
     149        ok(poly->mask[1][1], "x^1 y^1 coeff is masked");
     150        ok(poly->mask[1][2], "x^1 y^2 coeff is masked");
     151
     152        psFree (poly);
     153
     154        skip_end();
     155        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
     156    }
     157
    111158    // test psPolynomial2D_dY (no supplied output)
    112159    {
     
    136183        psPolynomial2D *dY = psPolynomial2D_dY (NULL, poly);
    137184
    138         ok(dY->nX == 1, "new x order is %d", dY->nX);
    139         ok(dY->nY == 2, "new y order is %d", dY->nY);
     185        ok(dY->nX == 2, "new x order is %d", dY->nX);
     186        ok(dY->nY == 1, "new y order is %d", dY->nY);
    140187
    141188        ok_float(dY->coeff[0][0], +3.0, "x^0 y^0 coeff is %f", dY->coeff[0][0]);
     
    185232        psPolynomial2D_dY (dY, poly);
    186233
    187         ok(dY->nX == 1, "new x order is %d", dY->nX);
    188         ok(dY->nY == 2, "new y order is %d", dY->nY);
     234        ok(dY->nX == 2, "new x order is %d", dY->nX);
     235        ok(dY->nY == 1, "new y order is %d", dY->nY);
    189236
    190237        ok_float(dY->coeff[0][0], +3.0, "x^0 y^0 coeff is %f", dY->coeff[0][0]);
     
    206253    }
    207254
     255    // test psPolynomial2D_dY (supplied output)
     256    {
     257        psMemId id = psMemGetId();
     258
     259        diag ("test psPolynomial2D_dY (supplied output)");
     260
     261        psPolynomial2D *poly = psPolynomial2DAlloc(PS_POLYNOMIAL_ORD, 2, 2);
     262        ok(poly != NULL, "psPolynomial2D successfully allocated");
     263        skip_start(poly == NULL, 5, "Skipping tests because psPolynomial2DAlloc() failed");
     264
     265        // build sample polynomial (upper-left elements only)
     266        // z = 5 + 2x + 3y - 3x^2 + 4xy - 2y^2
     267        // dz/dy = 3 + 4x - 4y
     268        poly->coeff[0][0] = 5.0;
     269        poly->coeff[1][0] = 2.0;
     270        poly->coeff[0][1] = 3.0;
     271        poly->coeff[2][0] = -3.0;
     272        poly->coeff[1][1] = 4.0;
     273        poly->coeff[0][2] = -2.0;
     274
     275        // mask remaining elements
     276        poly->mask[2][1] = 1;
     277        poly->mask[1][2] = 1;
     278        poly->mask[2][2] = 1;
     279
     280        poly = psPolynomial2D_dY (poly, poly);
     281
     282        ok(poly->nX == 2, "new x order is %d", poly->nX);
     283        ok(poly->nY == 1, "new y order is %d", poly->nY);
     284
     285        ok_float(poly->coeff[0][0], +3.0, "x^0 y^0 coeff is %f", poly->coeff[0][0]);
     286        ok_float(poly->coeff[1][0], +4.0, "x^1 y^0 coeff is %f", poly->coeff[1][0]);
     287        ok_float(poly->coeff[0][1], -4.0, "x^0 y^1 coeff is %f", poly->coeff[0][1]);
     288
     289        ok(!poly->mask[0][0], "x^0 y^0 coeff is unmasked");
     290        ok(!poly->mask[1][0], "x^1 y^0 coeff is unmasked");
     291        ok(!poly->mask[0][1], "x^0 y^1 coeff is unmasked");
     292
     293        ok(poly->mask[1][1], "x^1 y^1 coeff is masked");
     294        ok(poly->mask[1][2], "x^1 y^2 coeff is masked");
     295
     296        psFree (poly);
     297
     298        skip_end();
     299        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
     300    }
     301
    208302    return exit_status();
    209303}
Note: See TracChangeset for help on using the changeset viewer.