📄 mathutil.java,v
字号:
head 1.5;access;symbols;locks; strict;comment @# @;1.5date 2005.06.10.18.46.46; author rirwin; state Exp;branches;next 1.4;1.4date 2005.05.24.14.50.55; author rirwin; state Exp;branches;next 1.3;1.3date 2005.05.24.14.41.55; author rirwin; state Exp;branches;next 1.2;1.2date 2005.03.11.20.57.39; author patil; state Exp;branches;next 1.1;1.1date 2004.12.28.00.04.32; author patil; state Exp;branches;next ;desc@@1.5log@Establishing RCS verision.@text@/** * file: MathUtil.java * * last edited: Ryan Irwin */import java.util.*;import java.awt.*;//These imports are not needed - Phil T. 6-23-03//import javax.swing.*;/** * class that round off floating point numbers to the specified number * of decimal places givem * */public class MathUtil{ static final double DEF_BIAS = 0.0; static final double DEF_PENALTY = 50.0; static final double DEF_EPISLON = 1e-12; static final double DEF_TOLERANCE = 1e-3; static final double DEF_POLYNOMIAL_DEGREE = 3.0; static final double DEF_RBF_GAMMA = 0.5; static final double DEF_SIGMOID_KAPPA = 1.0; static final double DEF_SIGMOID_DELTA = 1.0; static final double MIN_DIFF_RATE = 0.00001; // declare the random number generator // public final static long RAND_SEED = 20022000; public final static Random random = new Random(RAND_SEED); // ********************************************************************* // // class constructor // // ********************************************************************* /** * Default constructor. Empty body */ public MathUtil() { // default constructor } // ********************************************************************* // // declare class methods // // ********************************************************************* /** * method generates a random distribution centered about the mean with a * variance that is specified by the standard deviation. * * @@param mean mean of the distribution * @@param stddev standard deviation of the distribution * * @@return random number centered about the mean * */ public static double grand(double mean, double stddev) { // declare variables // double val = 0.0; double r1 = 0.0; double r0 = 0.0; // generate random number // r0 = random.nextDouble(); r0 = r0 + 0.00000000001; r1 = random.nextDouble(); val = Math.sqrt(-2.0 * Math.log(r0)) * stddev * Math.cos(2 * Math.PI * r1) + mean; return val; } /** * method to round off floating point numbers to the specified number * of decimal places given * * @@param doubleNumber input number * @@param decimalPlaces number of decimal places to round * */ static public double SetDecimal(double doubleNumber, int decimalPlaces) { int wholeNumber = (int)doubleNumber; double pastDecimal = ((doubleNumber - wholeNumber) * Math.pow(10, decimalPlaces)); int roundedDecimal = (int)Math.round(pastDecimal); int combine = (int) (wholeNumber * Math.pow(10, decimalPlaces) + roundedDecimal); double result = combine / Math.pow(10, decimalPlaces); return result; } /** * calculate the euclidean distance between the two points * * @@param x1 x-coordinate of the first point * @@param y1 y-coordinate of the first point * @@param x2 x-coordinate of the second point * @@param y2 y-coordinate of the second point * * @@return distance between the points * */ static public double distance(double x1, double y1, double x2, double y2) { double distance = 0.0; // claculate the euclidean distance // double deltaX = x2 - x1; double deltaY = y2 - y1; double sqrX = deltaX * deltaX; double sqrY = deltaY * deltaY; distance = Math.sqrt((double)sqrX + (double)sqrY); // return the distance // return distance; } /** * methods computes and returns the mean of the given cluster * * @@param vec Vector of points from the cluster * @@return MyPoint value of the cluster mean */ static public MyPoint computeClusterMean(Vector vec) { // declare local variables // double xval = 0.0; double yval = 0.0; // compute the mean of the given cluster // for (int i = 0; i < vec.size(); i++) { MyPoint point = (MyPoint)vec.elementAt(i); xval += (double)point.x; yval += (double)point.y; } xval = xval / vec.size(); yval = yval / vec.size(); return (new MyPoint(xval, yval)); } /** * methods computes and returns the mean of the given cluster * * @@param vec Vector of points from the cluster * @@return MyPoint value of the mean * */ static public MyPoint computePointMean(Vector vec) { // declare local variables // double xval = 0.0; double yval = 0.0; // compute the mean of the given cluster // for (int i = 0; i < vec.size(); i++) { MyPoint point = (MyPoint)vec.elementAt(i); xval += (double)point.x; yval += (double)point.y; } xval = SetDecimal(xval / vec.size(), 3); yval = SetDecimal(yval / vec.size(), 3); return (new MyPoint((double)xval, (double)yval)); } /** * methods computes and returns the mean of the given cluster * * @@param vec point from the cluster * @@return MyPoint value of the mean * */ static public MyPoint computeMyPointMean(Vector vec) { // declare local variables // double xval = 0.0; double yval = 0.0; // compute the mean of the given cluster // for (int i = 0; i < vec.size(); i++) { MyPoint point = (MyPoint)vec.elementAt(i); xval += (double)point.x; yval += (double)point.y; } xval = xval / vec.size(); yval = yval / vec.size(); return (new MyPoint(xval, yval)); } /** * method takes in a decimal number and rounds to the given number * of decimal places passed * * @@param doubleNumber input number * @@param decimalPlaces number of significant decimal places * * @@return double value of decimal with correct signifcant figures */ public static double setDecimal(double doubleNumber, int decimalPlaces) { int wholeNumber = (int)doubleNumber; double pastDecimal = ((doubleNumber - wholeNumber) * Math.pow(10, decimalPlaces)); int roundedDecimal = (int)Math.round(pastDecimal); int combine = (int) (wholeNumber * Math.pow(10, decimalPlaces) + roundedDecimal); double result = combine / Math.pow(10, decimalPlaces); // return the rounded decimal number // return result; } /** * this method evaluates the linear Kernel on the input vectors * K(x,y) = (x . y) * * @@param point1 First vector of points * @@param point2 Second vector of points * * @@return double value of Kernel evaluation * */ public static double linearKernel(Vector point1, Vector point2) { // declare local variables // double result = 0.0; // if the length of the vectors are not equal error // /* if (point1.length() != point2.length()) { return Error::handle(name(), L"setPenalty", Error::ARG, __FILE__, __LINE__); } */ // compute the final result // result = vectorProduct(point1, point2); // return the result // return result; } /** * this method evaluates the redial basis function Kernel on the * input vectors with standard deviation sigma K(x,y) = exp(-gamma * * ((a.a)-2*(a.b)+(b.b))) * * @@param point1 First vector of points * @@param point2 Second vector of points * * @@return double value of Kernel evaluation * */ public static double rbfKernel(Vector point1, Vector point2) { // declare local variables // double result = 0.0; // compute the dot products for the rbf Kernel // double points1_sq = vectorProduct(point1, point1); double cross_prod = vectorProduct(point1, point2); double points2_sq = vectorProduct(point2, point2); // compute the final result // result = Math.exp(-DEF_RBF_GAMMA * (points1_sq - 2 * cross_prod + points2_sq)); // return the result // return result; } /** * this method evaluates the redial basis function Kernel on the * input vectors with standard deviation sigma K(x,y) = exp(-gamma * * ((a.a)-2*(a.b)+(b.b))) * * @@param point1 First vector of points * @@param point2 Second vector of points * @@param gamma_a Double value of gamma * @@return double value of Kernel evaluation * */ public static double rbfKernel(Vector point1, Vector point2, double gamma_a) { // declare local variables // double result = 0.0; // compute the dot products for the rbf Kernel // double points1_sq = vectorProduct(point1, point1); double cross_prod = vectorProduct(point1, point2); double points2_sq = vectorProduct(point2, point2); // compute the final result // result = Math.exp( -gamma_a * (points1_sq - 2 * cross_prod + points2_sq)); // return the result // return result; } /** * this method evaluates the second degree polynomial Kernel on the * input vectors K(x,y) = (x . y + 1)^p * * @@param points1 First vector of points * @@param points2 Second vector of points * @@return Double value of Kernel evaluation * */ public static double polynomialKernel(Vector points1, Vector points2) { // declare local variables // double result = 0.0; // compute the cross product // double cross_prod = vectorProduct(points1, points2); // compute the final result // result = Math.pow(cross_prod + 1, DEF_POLYNOMIAL_DEGREE); // return the result // return result; } /** * this method evaluates the vector dot product * * @@param vec1 First vector of points * @@param vec2 Second vector of points * * @@return dot product * */ public static double vectorProduct(Vector vec1, Vector vec2) { // declare local variables // double result = 0.0; // if the length of the vectors are not equal error // /* if (vec1.size() != vec2.size()) { return Error::handle(name(), L"setPenalty", Error::ARG, __FILE__, __LINE__); } */ // compute the dot product // for (int i = 0; i < vec1.size(); i++) { double val1 = ((Double)vec1.get(i)).doubleValue(); double val2 = ((Double)vec2.get(i)).doubleValue(); result += (val1 * val2); } // return the result // return result; } /** * method sees if input vectors are "almost" equal * * @@param vec1 First vector of points * @@param vec2 Second vector of points * @@return True if all input vector components are "almost" equal */ public static boolean almostEqual(Vector vec1, Vector vec2) { // compute the dot product // for (int i = 0; i < vec1.size(); i++) { double val1 = ((Double)vec1.get(i)).doubleValue(); double val2 = ((Double)vec2.get(i)).doubleValue(); if (!almostEqual(val1, val2)) { return false; } } // return the result // return true; } /** * method sees if input vectors are "almost" equal * * @@param vec1 First vector of points * @@param val2 Value to compare to * @@return True if all input vector components are "almost" equal */ public static boolean almostEqual(Vector vec1, double val2) { // compute the dot product // for (int i = 0; i < vec1.size(); i++) { double val1 = ((Double)vec1.get(i)).doubleValue(); if (!almostEqual(val1, val2)) { return false; } } // return the result // return true; } /** * method sees if input doubles are "almost" equal * * @@param val1 First double value * @@param val2 Second double value * @@return True if doubles are "almost" equal */ public static boolean almostEqual(double val1, double val2) { // declare local variables // if (Math.abs(val1 - val2) >= MIN_DIFF_RATE * (Math.abs(val1 + val2) + 0.01)) { return false; } // return the result // return true; } /** * initializes components of vector to double value passed * * @@param vec_a point from the cluster * @@param value_a value to set to vector * */ static public void initDoubleVector(Vector<Double> vec_a, double value_a) { // loop over all elements
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -