IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 18312


Ignore:
Timestamp:
Jun 24, 2008, 11:58:57 AM (18 years ago)
Author:
Paul Price
Message:

Using a select instead of sort when calculating medians. Hopefully, this should be faster.

File:
1 edited

Legend:

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

    r18155 r18312  
    77 *  on those data structures.
    88 *
    9  *  @author GLG (MHPCC), EAM (IfA)
     9 *  @author GLG (MHPCC), EAM (IfA), PAP (IfA)
    1010 *
    1111 * XXX: Must do
     
    1313 * use ->min and ->max (PS_STAT_USE_RANGE)
    1414 *
    15  *  @version $Revision: 1.224 $ $Name: not supported by cvs2svn $
    16  *  @date $Date: 2008-06-17 02:26:34 $
     15 *  @version $Revision: 1.225 $ $Name: not supported by cvs2svn $
     16 *  @date $Date: 2008-06-24 21:58:57 $
    1717 *
    1818 *  Copyright 2006 IfA, University of Hawaii
     
    340340    }
    341341
    342     // Sort the temporary vector.
    343     if (!psVectorSort(outVector, outVector)) { // Sort in-place (since it's a copy, it's OK)
     342    // Using selection instead of sorting, because it's faster.  It does make the code a bit longer here tho.
     343    // Select in-place (since it's a copy, it's OK)
     344    if (!psVectorSelectInPlace(outVector, count/2)) {
    344345        psError(PS_ERR_UNEXPECTED_NULL, false, _("Failed to sort input data."));
    345346        stats->sampleUQ = NAN;
     
    347348        stats->sampleMedian = NAN;
    348349        psFree(outVector);
    349         return true;
    350     }
    351 
    352     // Calculate the median.  Use the average if the number of samples if even.
     350        return false;
     351    }
     352    double median = output[count/2]; // Median value
     353
    353354    if (count % 2 == 0) {
    354         stats->sampleMedian = 0.5 * (output[(count/2) - 1] + output[count/2]);
     355        if (!psVectorSelectInPlace(outVector, count/2 - 1)) {
     356            psError(PS_ERR_UNEXPECTED_NULL, false, _("Failed to sort input data."));
     357            stats->sampleUQ = NAN;
     358            stats->sampleLQ = NAN;
     359            stats->sampleMedian = NAN;
     360            psFree(outVector);
     361            return false;
     362        }
     363        stats->sampleMedian = 0.5 * (output[count/2 - 1] + median);
    355364    } else {
    356         stats->sampleMedian = output[count/2];
    357     }
    358 
    359     // Calculate the quartile points exactly.
     365        stats->sampleMedian = median;
     366    }
     367    stats->results |= PS_STAT_SAMPLE_MEDIAN;
     368
     369    if (!psVectorSelectInPlace(outVector, 0.75 * count)) {
     370        psError(PS_ERR_UNEXPECTED_NULL, false, _("Failed to sort input data."));
     371        stats->sampleUQ = NAN;
     372        stats->sampleLQ = NAN;
     373        psFree(outVector);
     374        return false;
     375    }
    360376    stats->sampleUQ = output[(int)(0.75*count)];
     377
     378    if (!psVectorSelectInPlace(outVector, 0.25 * count)) {
     379        psError(PS_ERR_UNEXPECTED_NULL, false, _("Failed to sort input data."));
     380        stats->sampleLQ = NAN;
     381        psFree(outVector);
     382        return false;
     383    }
    361384    stats->sampleLQ = output[(int)(0.25*count)];
    362385
    363     stats->results |= PS_STAT_SAMPLE_MEDIAN;
    364386    stats->results |= PS_STAT_SAMPLE_QUARTILE;
    365387
Note: See TracChangeset for help on using the changeset viewer.