Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

# qwt_math.cpp

    00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
    00002  * Qwt Widget Library
    00003  * Copyright (C) 1997   Josef Wilgen
    00004  * Copyright (C) 2002   Uwe Rathmann
    00005  *
    00006  * This library is free software; you can redistribute it and/or
    00007  * modify it under the terms of the Qwt License, Version 1.0
    00008  *****************************************************************************/
    00009
    00010 // vim: expandtab
    00011
    00012 #include "qwt_math.h"
    00013
    00019 double qwtGetMin(double *array, int size)
    00020 {
    00021     if (size <= 0)
    00022        return 0.0;
    00023
    00024     double rv = array[0];
    00025     for (int i = 1; i < size; i++)
    00026        rv = qwtMin(rv, array[i]);
    00027
    00028     return rv;
    00029 }
    00030
    00031
    00037 double qwtGetMax(double *array, int size)
    00038 {
    00039     if (size <= 0)
    00040        return 0.0;
    00041
    00042     double rv = array[0];
    00043     for (int i = 1; i < size; i++)
    00044        rv = qwtMax(rv, array[i]);
    00045
    00046     return rv;
    00047 }
    00048
    00049
    00056 double qwtCeil125( double x)
    00057 {
    00058     if (x == 0.0)
    00059         return 0.0;
    00060
    00061     const double sign = (x > 0) ? 1.0 : -1.0;
    00062     const double lx = log10(fabs(x));
    00063     const double p10 = floor(lx);
    00064
    00065     double fr = pow(10.0, lx - p10);
    00066     if (fr <=1.0)
    00067        fr = 1.0;
    00068     else if (fr <= 2.0)
    00069        fr = 2.0;
    00070     else if (fr <= 5.0)
    00071        fr = 5.0;
    00072     else
    00073        fr = 10.0;
    00074
    00075     return sign * fr * pow(10.0, p10);
    00076 }
    00077
    00078
    00085 double qwtFloor125( double x)
    00086 {
    00087     if (x == 0.0)
    00088         return 0.0;
    00089
    00090     double sign = (x > 0) ? 1.0 : -1.0;
    00091     const double lx = log10(fabs(x));
    00092     const double p10 = floor(lx);
    00093
    00094     double fr = pow(10.0, lx - p10);
    00095     if (fr >= 10.0)
    00096        fr = 10.0;
    00097     else if (fr >= 5.0)
    00098        fr = 5.0;
    00099     else if (fr >= 2.0)
    00100        fr = 2.0;
    00101     else
    00102        fr = 1.0;
    00103
    00104     return sign * fr * pow(10.0, p10);
    00105 }
    00106
    00107
    00119 int qwtChkMono(double *array, int size)
    00120 {
    00121     if (size < 2)
    00122         return 0;
    00123
    00124     int rv = qwtSign(array[1] - array[0]);
    00125     for (int i = 1; i < size - 1; i++)
    00126     {
    00127         if ( qwtSign(array[i+1] - array[i]) != rv )
    00128         {
    00129             rv = 0;
    00130             break;
    00131         }
    00132     }
    00133     return rv;
    00134 }
    00135
    00141 void qwtTwistArray(double *array, int size)
    00142 {
    00143     const int s2 = size / 2;
    00144
    00145     for (int i=0; i < s2; i++)
    00146     {
    00147         const int itmp = size - 1 - i;
    00148         const double dtmp = array[i];
    00149         array[i] = array[itmp];
    00150         array[itmp] = dtmp;
    00151     }
    00152 }
    00153
    00161 void qwtLinSpace(double *array, int size, double xmin, double xmax)
    00162 {
    00163     if (size <= 0)
    00164         return;
    00165
    00166     const int imax = size -1;
    00167
    00168     array[0] = xmin;
    00169     array[imax] = xmax;
    00170
    00171     const double step = (xmax - xmin) / double(imax);
    00172     const double tiny = 1e-6;
    00173
    00174     for (int i = 1; i < imax; i++)
    00175     {
    00176        array[i] = xmin + double(i) * step;
    00177        if (fabs(array[i]) < tiny*fabs(step))
    00178           array[i] = step*floor(array[i]/step + tiny/2);
    00179     }
    00180 }
    00181
    00189 void qwtLogSpace(double *array, int size, double xmin, double xmax)
    00190 {
    00191     if ((xmin <= 0.0) || (xmax <= 0.0) || (size <= 0))
    00192        return;
    00193
    00194     const int imax = size -1;
    00195
    00196     array[0] = xmin;
    00197     array[imax] = xmax;
    00198
    00199     const double lxmin = log(xmin);
    00200     const double lxmax = log(xmax);
    00201     const double lstep = (lxmax - lxmin) / double(imax);
    00202
    00203     for (int i = 1; i < imax; i++)
    00204        array[i] = exp(lxmin + double(i) * lstep);
    00205 }