[mlpack-svn] r10050 - mlpack/trunk/src/mlpack/core/math

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Wed Oct 26 20:31:53 EDT 2011


Author: rcurtin
Date: 2011-10-26 20:31:53 -0400 (Wed, 26 Oct 2011)
New Revision: 10050

Added:
   mlpack/trunk/src/mlpack/core/math/math_misc.hpp
Removed:
   mlpack/trunk/src/mlpack/core/math/math_lib.hpp
Modified:
   mlpack/trunk/src/mlpack/core/math/math_test.cpp
   mlpack/trunk/src/mlpack/core/math/range.cpp
   mlpack/trunk/src/mlpack/core/math/range.hpp
Log:
Reformat files in the math directory; comment them, and adhere to some new
coding standards (#153).


Deleted: mlpack/trunk/src/mlpack/core/math/math_lib.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/math/math_lib.hpp	2011-10-27 00:15:12 UTC (rev 10049)
+++ mlpack/trunk/src/mlpack/core/math/math_lib.hpp	2011-10-27 00:31:53 UTC (rev 10050)
@@ -1,87 +0,0 @@
-/**
- * @file math_lib.h
- *
- * Includes all basic FASTlib non-vector math utilities.
- */
-
-#ifndef __MLPACK_CORE_MATH_MATH_LIB_HPP
-#define __MLPACK_CORE_MATH_MATH_LIB_HPP
-
-#include "../io/cli.hpp"
-#include "../io/log.hpp"
-
-#include <math.h>
-#include <float.h>
-/**
- * Math routines.
- *
- * The hope is that this should contain most of the useful math routines
- * you can think of.  Currently, this is very sparse.
- */
-namespace mlpack {
-namespace math {
-  /**
-   * Forces a number to be non-negative, turning negative numbers into zero.
-   *
-   * Avoids branching costs (yes, we've discovered measurable improvements).
-   */
-  inline double ClampNonNegative(double d) {
-    return (d + fabs(d)) / 2;
-  }
-
-  /**
-   * Forces a number to be non-positive, turning positive numbers into zero.
-   *
-   * Avoids branching costs (yes, we've discovered measurable improvements).
-   */
-  inline double ClampNonPositive(double d) {
-    return (d - fabs(d)) / 2;
-  }
-
-  /**
-   * Clips a number between a particular range.
-   *
-   * @param value the number to clip
-   * @param range_min the first of the range
-   * @param range_max the last of the range
-   * @return max(range_min, min(range_max, d))
-   */
-  inline double ClampRange(double value, double range_min, double range_max)
-  {
-    value -= range_max;
-    value = ClampNonPositive (value) + range_max;
-    value -= range_min;
-    value = ClampNonNegative (value) + range_min;
-    return value;
-  }
-
-  /**
-   * Generates a uniform random number between 0 and 1.
-   */
-  inline double Random() {
-    return rand() * (1.0 / RAND_MAX);
-  }
-
-  /**
-   * Generates a uniform random number in the specified range.
-   */
-  inline double Random(double lo, double hi) {
-    return Random() * (hi - lo) + lo;
-  }
-
-  /**
-   * Generates a uniform random integer.
-   */
-  inline int RandInt(int hi_exclusive) {
-    return rand() % hi_exclusive;
-  }
-  /**
-   * Generates a uniform random integer.
-   */
-  inline int RandInt(int lo, int hi_exclusive) {
-    return (rand() % (hi_exclusive - lo)) + lo;
-  }
-}; // namespace math
-}; // namespace mlpack
-
-#endif // __MLPACK_CORE_MATH_MATH_LIB_HPP

Copied: mlpack/trunk/src/mlpack/core/math/math_misc.hpp (from rev 10049, mlpack/trunk/src/mlpack/core/math/math_lib.hpp)
===================================================================
--- mlpack/trunk/src/mlpack/core/math/math_misc.hpp	                        (rev 0)
+++ mlpack/trunk/src/mlpack/core/math/math_misc.hpp	2011-10-27 00:31:53 UTC (rev 10050)
@@ -0,0 +1,95 @@
+/**
+ * @file math_misc.hpp
+ *
+ * Miscellaneous math routines.
+ */
+
+#ifndef __MLPACK_CORE_MATH_MATH_LIB_HPP
+#define __MLPACK_CORE_MATH_MATH_LIB_HPP
+
+#include "../io/cli.hpp"
+#include "../io/log.hpp"
+
+#include <math.h>
+#include <float.h>
+
+namespace mlpack {
+namespace math /** Miscellaneous math routines. */ {
+
+/**
+ * Forces a number to be non-negative, turning negative numbers into zero.
+ * Avoids branching costs (this is a measurable improvement).
+ *
+ * @param d Double to clamp.
+ * @return 0 if d < 0, d otherwise.
+ */
+inline double ClampNonNegative(double d)
+{
+  return (d + fabs(d)) / 2;
+}
+
+/**
+ * Forces a number to be non-positive, turning positive numbers into zero.
+ * Avoids branching costs (this is a measurable improvement).
+ *
+ * @param d Double to clamp.
+ * @param 0 if d > 0, d otherwise.
+ */
+inline double ClampNonPositive(double d)
+{
+  return (d - fabs(d)) / 2;
+}
+
+/**
+ * Clamp a number between a particular range.
+ *
+ * @param value The number to clamp.
+ * @param range_min The first of the range.
+ * @param range_max The last of the range.
+ * @return max(range_min, min(range_max, d)).
+ */
+inline double ClampRange(double value, double range_min, double range_max)
+{
+  value -= range_max;
+  value = ClampNonPositive (value) + range_max;
+  value -= range_min;
+  value = ClampNonNegative (value) + range_min;
+  return value;
+}
+
+/**
+ * Generates a uniform random number between 0 and 1.
+ */
+inline double Random()
+{
+  return rand() * (1.0 / RAND_MAX);
+}
+
+/**
+ * Generates a uniform random number in the specified range.
+ */
+inline double Random(double lo, double hi)
+{
+  return Random() * (hi - lo) + lo;
+}
+
+/**
+ * Generates a uniform random integer.
+ */
+inline int RandInt(int hi_exclusive)
+{
+  return rand() % hi_exclusive;
+}
+
+/**
+ * Generates a uniform random integer.
+ */
+inline int RandInt(int lo, int hi_exclusive)
+{
+  return (rand() % (hi_exclusive - lo)) + lo;
+}
+
+}; // namespace math
+}; // namespace mlpack
+
+#endif // __MLPACK_CORE_MATH_MATH_LIB_HPP

Modified: mlpack/trunk/src/mlpack/core/math/math_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/math/math_test.cpp	2011-10-27 00:15:12 UTC (rev 10049)
+++ mlpack/trunk/src/mlpack/core/math/math_test.cpp	2011-10-27 00:31:53 UTC (rev 10050)
@@ -1,5 +1,5 @@
 /***
- * @file math_test.cc
+ * @file math_test.cpp
  * @author Ryan Curtin
  *
  * Tests for everything in the math:: namespace.
@@ -16,7 +16,8 @@
 /***
  * Verify that the empty constructor creates an empty range.
  */
-BOOST_AUTO_TEST_CASE(RangeEmptyConstructor) {
+BOOST_AUTO_TEST_CASE(RangeEmptyConstructor)
+{
   Range x = Range();
 
   // Just verify that it is empty.
@@ -27,7 +28,8 @@
  * Verify that the point constructor correctly creates a range that is just a
  * point.
  */
-BOOST_AUTO_TEST_CASE(RangePointConstructor) {
+BOOST_AUTO_TEST_CASE(RangePointConstructor)
+{
   Range x(10.0);
 
   BOOST_REQUIRE_CLOSE(x.lo, x.hi, 1e-25);
@@ -39,7 +41,8 @@
 /***
  * Verify that the range constructor correctly creates the range.
  */
-BOOST_AUTO_TEST_CASE(RangeConstructor) {
+BOOST_AUTO_TEST_CASE(RangeConstructor)
+{
   Range x(0.5, 5.5);
 
   BOOST_REQUIRE_CLOSE(x.lo, 0.5, 1e-25);
@@ -49,7 +52,8 @@
 /***
  * Test that we get the width correct.
  */
-BOOST_AUTO_TEST_CASE(RangeWidth) {
+BOOST_AUTO_TEST_CASE(RangeWidth)
+{
   Range x(0.0, 10.0);
 
   BOOST_REQUIRE_CLOSE(x.width(), 10.0, 1e-20);
@@ -74,7 +78,8 @@
 /**
  * Test that we get the midpoint correct.
  */
-BOOST_AUTO_TEST_CASE(RangeMidpoint) {
+BOOST_AUTO_TEST_CASE(RangeMidpoint)
+{
   Range x(0.0, 10.0);
 
   BOOST_REQUIRE_CLOSE(x.mid(), 5.0, 1e-5);
@@ -87,7 +92,8 @@
 /***
  * Test that we can expand to include other ranges correctly.
  */
-BOOST_AUTO_TEST_CASE(RangeIncludeOther) {
+BOOST_AUTO_TEST_CASE(RangeIncludeOther)
+{
   // We need to test both |= and |.
   // We have three cases: non-overlapping; overlapping; equivalent, and then a
   // couple permutations (switch left with right and make sure it still works).
@@ -163,7 +169,8 @@
 /***
  * Test that we can 'and' ranges correctly.
  */
-BOOST_AUTO_TEST_CASE(RangeIntersectOther) {
+BOOST_AUTO_TEST_CASE(RangeIntersectOther)
+{
   // We need to test both &= and &.
   // We have three cases: non-overlapping, overlapping; equivalent, and then a
   // couple permutations (switch left with right and make sure it still works).
@@ -223,10 +230,11 @@
   BOOST_REQUIRE_CLOSE(w.hi, 4.0, 1e-5);
 }
 
-/***
+/**
  * Test multiplication of a range with a double.
  */
-BOOST_AUTO_TEST_CASE(RangeMultiply) {
+BOOST_AUTO_TEST_CASE(RangeMultiply)
+{
   // We need to test both * and *=, as well as both cases of *.
   // We'll try with a couple of numbers: -1, 0, 2.
   // And we'll have a couple of cases for bounds: strictly less than zero;
@@ -350,10 +358,11 @@
   BOOST_REQUIRE_CLOSE(w.hi, 10.0, 1e-5);
 }
 
-/***
+/**
  * Test equality operator.
  */
-BOOST_AUTO_TEST_CASE(RangeEquality) {
+BOOST_AUTO_TEST_CASE(RangeEquality)
+{
   // Three cases: non-overlapping, overlapping, equivalent.  We should also
   // consider empty ranges, which are not necessarily equal...
   Range x(0.0, 2.0);
@@ -383,10 +392,11 @@
   // because we are not forcing behavior for that.
 }
 
-/***
+/**
  * Test inequality operator.
  */
-BOOST_AUTO_TEST_CASE(RangeInequality) {
+BOOST_AUTO_TEST_CASE(RangeInequality)
+{
   // We will use the same three cases as the RangeEquality test.
   Range x(0.0, 2.0);
   Range y(3.0, 5.0);
@@ -412,10 +422,11 @@
   BOOST_REQUIRE_EQUAL((y != x), false);
 }
 
-/***
+/**
  * Test strict less-than operator.
  */
-BOOST_AUTO_TEST_CASE(RangeStrictLessThan) {
+BOOST_AUTO_TEST_CASE(RangeStrictLessThan)
+{
   // Three cases: non-overlapping, overlapping, and equivalent.
   Range x(0.0, 2.0);
   Range y(3.0, 5.0);
@@ -434,10 +445,11 @@
   BOOST_REQUIRE_EQUAL((y < x), false);
 }
 
-/***
+/**
  * Test strict greater-than operator.
  */
-BOOST_AUTO_TEST_CASE(RangeStrictGreaterThan) {
+BOOST_AUTO_TEST_CASE(RangeStrictGreaterThan)
+{
   // Three cases: non-overlapping, overlapping, and equivalent.
   Range x(0.0, 2.0);
   Range y(3.0, 5.0);
@@ -456,10 +468,11 @@
   BOOST_REQUIRE_EQUAL((y > x), false);
 }
 
-/***
+/**
  * Test the Contains() operator.
  */
-BOOST_AUTO_TEST_CASE(RangeContains) {
+BOOST_AUTO_TEST_CASE(RangeContains)
+{
   // We have three Range cases: strictly less than 0; overlapping 0; and
   // strictly greater than 0.  Then the numbers we check can be the same three
   // cases, including one greater than and one less than the range.  This should

Modified: mlpack/trunk/src/mlpack/core/math/range.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/math/range.cpp	2011-10-27 00:15:12 UTC (rev 10049)
+++ mlpack/trunk/src/mlpack/core/math/range.cpp	2011-10-27 00:31:53 UTC (rev 10050)
@@ -1,5 +1,5 @@
 /**
- * @file range.cc
+ * @file range.cpp
  *
  * Implementation of the Range class.
  */
@@ -30,7 +30,8 @@
 /**
  * Gets the span of the range, hi - lo.  Returns 0 if the range is negative.
  */
-double Range::width() const {
+double Range::width() const
+{
   if (lo < hi)
     return (hi - lo);
   else
@@ -40,14 +41,16 @@
 /**
  * Gets the midpoint of this range.
  */
-double Range::mid() const {
+double Range::mid() const
+{
   return (hi + lo) / 2;
 }
 
 /**
  * Expands range to include the other range.
  */
-Range& Range::operator|=(const Range& rhs) {
+Range& Range::operator|=(const Range& rhs)
+{
   if (rhs.lo < lo)
     lo = rhs.lo;
   if (rhs.hi > hi)
@@ -56,7 +59,8 @@
   return *this;
 }
 
-Range Range::operator|(const Range& rhs) const {
+Range Range::operator|(const Range& rhs) const
+{
   return Range((rhs.lo < lo) ? rhs.lo : lo,
                (rhs.hi > hi) ? rhs.hi : hi);
 }
@@ -65,7 +69,8 @@
  * Shrinks range to be the overlap with another range, becoming an empty
  * set if there is no overlap.
  */
-Range& Range::operator&=(const Range& rhs) {
+Range& Range::operator&=(const Range& rhs)
+{
   if (rhs.lo > lo)
     lo = rhs.lo;
   if (rhs.hi < hi)
@@ -74,7 +79,8 @@
   return *this;
 }
 
-Range Range::operator&(const Range& rhs) const {
+Range Range::operator&(const Range& rhs) const
+{
   return Range((rhs.lo > lo) ? rhs.lo : lo,
                (rhs.hi < hi) ? rhs.hi : hi);
 }
@@ -82,12 +88,14 @@
 /**
  * Scale the bounds by the given double.
  */
-Range& Range::operator*=(const double d) {
+Range& Range::operator*=(const double d)
+{
   lo *= d;
   hi *= d;
 
   // Now if we've negated, we need to flip things around so the bound is valid.
-  if (lo > hi) {
+  if (lo > hi)
+  {
     double tmp = hi;
     hi = lo;
     lo = tmp;
@@ -96,7 +104,8 @@
   return *this;
 }
 
-Range Range::operator*(const double d) const {
+Range Range::operator*(const double d) const
+{
   double nlo = lo * d;
   double nhi = hi * d;
 
@@ -107,7 +116,8 @@
 }
 
 // Symmetric case.
-Range operator*(const double d, const Range& r) {
+Range operator*(const double d, const Range& r)
+{
   double nlo = r.lo * d;
   double nhi = r.hi * d;
 
@@ -120,11 +130,13 @@
 /**
  * Compare with another range for strict equality.
  */
-bool Range::operator==(const Range& rhs) const {
+bool Range::operator==(const Range& rhs) const
+{
   return (lo == rhs.lo) && (hi == rhs.hi);
 }
 
-bool Range::operator!=(const Range& rhs) const {
+bool Range::operator!=(const Range& rhs) const
+{
   return (lo != rhs.lo) || (hi != rhs.hi);
 }
 
@@ -132,18 +144,21 @@
  * Compare with another range.  For Range objects x and y, x < y means that x is
  * strictly less than y and does not overlap at all.
  */
-bool Range::operator<(const Range& rhs) const {
+bool Range::operator<(const Range& rhs) const
+{
   return hi < rhs.lo;
 }
 
-bool Range::operator>(const Range& rhs) const {
+bool Range::operator>(const Range& rhs) const
+{
   return lo > rhs.hi;
 }
 
 /**
  * Determines if a point is contained within the range.
  */
-bool Range::Contains(double d) const {
+bool Range::Contains(double d) const
+{
   return d >= lo && d <= hi;
 }
 

Modified: mlpack/trunk/src/mlpack/core/math/range.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/math/range.hpp	2011-10-27 00:15:12 UTC (rev 10049)
+++ mlpack/trunk/src/mlpack/core/math/range.hpp	2011-10-27 00:31:53 UTC (rev 10050)
@@ -1,10 +1,9 @@
 /**
- * @file range.h
+ * @file range.hpp
  *
  * Definition of the Range class, which represents a simple range with a lower
  * and upper bound.
  */
-
 #ifndef __MLPACK_CORE_MATH_RANGE_HPP
 #define __MLPACK_CORE_MATH_RANGE_HPP
 
@@ -12,9 +11,10 @@
 namespace math {
 
 /**
- * Simple real-valued range.
+ * Simple real-valued range.  It contains an upper and lower bound.
  */
-class Range {
+class Range
+{
  public:
   double lo; /// The lower bound.
   double hi; /// The upper bound.
@@ -25,14 +25,21 @@
   /***
    * Initialize a range to enclose only the given point (lo = point, hi =
    * point).
+   *
+   * @param point Point that this range will enclose.
    */
   Range(double point);
 
-  /** Initializes to specified values. */
+  /**
+   * Initializes to specified range.
+   *
+   * @param lo_in Lower bound of the range.
+   * @param hi_in Upper bound of the range.
+   */
   Range(double lo_in, double hi_in);
 
   /**
-   * Gets the span of the range, hi - lo.
+   * Gets the span of the range (hi - lo).
    */
   double width() const;
 
@@ -43,40 +50,89 @@
 
   /**
    * Expands this range to include another range.
+   *
+   * @param rhs Range to include.
    */
   Range& operator|=(const Range& rhs);
+
+  /**
+   * Expands this range to include another range.
+   *
+   * @param rhs Range to include.
+   */
   Range operator|(const Range& rhs) const;
 
   /**
    * Shrinks this range to be the overlap with another range; this makes an
    * empty set if there is no overlap.
+   *
+   * @param rhs Other range.
    */
   Range& operator&=(const Range& rhs);
+
+  /**
+   * Shrinks this range to be the overlap with another range; this makes an
+   * empty set if there is no overlap.
+   *
+   * @param rhs Other range.
+   */
   Range operator&(const Range& rhs) const;
 
   /**
    * Scale the bounds by the given double.
+   *
+   * @param d Scaling factor.
    */
   Range& operator*=(const double d);
+
+  /**
+   * Scale the bounds by the given double.
+   *
+   * @param d Scaling factor.
+   */
   Range operator*(const double d) const;
 
+  /**
+   * Scale the bounds by the given double.
+   *
+   * @param d Scaling factor.
+   */
   friend Range operator*(const double d, const Range& r); // Symmetric case.
 
-  /***
+  /**
    * Compare with another range for strict equality.
+   *
+   * @param rhs Other range.
    */
   bool operator==(const Range& rhs) const;
+
+  /**
+   * Compare with another range for strict equality.
+   *
+   * @param rhs Other range.
+   */
   bool operator!=(const Range& rhs) const;
 
-  /***
+  /**
    * Compare with another range.  For Range objects x and y, x < y means that x
    * is strictly less than y and does not overlap at all.
+   *
+   * @param rhs Other range.
    */
   bool operator<(const Range& rhs) const;
+
+  /**
+   * Compare with another range.  For Range objects x and y, x < y means that x
+   * is strictly less than y and does not overlap at all.
+   *
+   * @param rhs Other range.
+   */
   bool operator>(const Range& rhs) const;
 
   /**
    * Determines if a point is contained within the range.
+   *
+   * @param d Point to check.
    */
   bool Contains(double d) const;
 };




More information about the mlpack-svn mailing list