Chaste Release::3.1
MathsCustomFunctions.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "MathsCustomFunctions.hpp"
00037 
00038 #include <cmath>
00039 #include <iostream>
00040 
00041 double SmallPow(double x, unsigned exponent)
00042 {
00043     switch (exponent)
00044     {
00045         case 0:
00046         {
00047             return 1.0;
00048         }
00049         case 1:
00050         {
00051             return x;
00052         }
00053         case 2:
00054         {
00055             return x*x;
00056         }
00057         case 3:
00058         {
00059             return x*x*x;
00060         }
00061         default:
00062         {
00063             if (exponent % 2 == 0)
00064             {
00065                 // Even power
00066                 double partial_answer = SmallPow(x, exponent/2);
00067                 return partial_answer*partial_answer;
00068             }
00069             else
00070             {
00071                 // Odd power
00072                 return SmallPow(x, exponent-1)*x;
00073             }
00074         }
00075     }
00076 }
00077 
00078 bool Divides(double smallerNumber, double largerNumber)
00079 {
00080     double remainder = fmod(largerNumber, smallerNumber);
00081     /*
00082      * Is the remainder close to zero? Note that the comparison is scaled
00083      * with respect to the larger of the numbers.
00084      */
00085     if (remainder < DBL_EPSILON*largerNumber)
00086     {
00087         return true;
00088     }
00089     /*
00090      * Is the remainder close to smallerNumber? Note that the comparison
00091      * is scaled with respect to the larger of the numbers.
00092      */
00093     if (fabs(remainder-smallerNumber) < DBL_EPSILON*largerNumber)
00094     {
00095         return true;
00096     }
00097 
00098     return false;
00099 }
00100 
00101 bool CompareDoubles::IsNearZero(double number, double tolerance)
00102 {
00103     return fabs(number) <= fabs(tolerance);
00104 }
00105 
00111 double SafeDivide(double number, double divisor)
00112 {
00113     // Avoid overflow
00114     if (divisor < 1.0 && number > divisor*DBL_MAX)
00115     {
00116         return DBL_MAX;
00117     }
00118 
00119     // Avoid underflow
00120     if (number == 0.0 || (divisor > 1.0 && number < divisor*DBL_MIN))
00121     {
00122         return 0.0;
00123     }
00124 
00125     return number/divisor;
00126 
00127 }
00128 
00129 bool CompareDoubles::WithinRelativeTolerance(double number1, double number2, double tolerance)
00130 {
00131     double difference = fabs(number1 - number2);
00132     double d1 = SafeDivide(difference, fabs(number1));
00133     double d2 = SafeDivide(difference, fabs(number2));
00134 
00135     return d1 <= tolerance && d2 <= tolerance;
00136 }
00137 
00138 bool CompareDoubles::WithinAbsoluteTolerance(double number1, double number2, double tolerance)
00139 {
00140     return fabs(number1 - number2) <= tolerance;
00141 }
00142 
00143 bool CompareDoubles::WithinAnyTolerance(double number1, double number2, double relTol, double absTol, bool printError)
00144 {
00145     bool ok = WithinAbsoluteTolerance(number1, number2, absTol) || WithinRelativeTolerance(number1, number2, relTol);
00146     if (printError && !ok)
00147     {
00148         std::cout << "CompareDoubles::WithinAnyTolerance: " << number1 << " and " << number2
00149                   << " differ by more than relative tolerance of " << relTol
00150                   << " and absolute tolerance of " << absTol << std::endl;
00151     }
00152     return ok;
00153 }
00154 
00155 bool CompareDoubles::WithinTolerance(double number1, double number2, double tolerance, bool toleranceIsAbsolute)
00156 {
00157     bool ok;
00158     if (toleranceIsAbsolute)
00159     {
00160         ok = WithinAbsoluteTolerance(number1, number2, tolerance);
00161     }
00162     else
00163     {
00164         ok = WithinRelativeTolerance(number1, number2, tolerance);
00165     }
00166     if (!ok)
00167     {
00168         std::cout << "CompareDoubles::WithinTolerance: " << number1 << " and " << number2
00169                   << " differ by more than " << (toleranceIsAbsolute ? "absolute" : "relative")
00170                   << " tolerance of " << tolerance << std::endl;
00171     }
00172     return ok;
00173 }
00174 
00175 double CompareDoubles::Difference(double number1, double number2, bool toleranceIsAbsolute)
00176 {
00177     if (toleranceIsAbsolute)
00178     {
00179         return fabs(number1 - number2);
00180     }
00181     else
00182     {
00183         double difference = fabs(number1 - number2);
00184         double d1 = SafeDivide(difference, fabs(number1));
00185         double d2 = SafeDivide(difference, fabs(number2));
00186         return d1 > d2 ? d1 : d2;
00187     }
00188 }