package com.dwave.math; /* * DMath * * Copyright (C) 2000 D-Wave Systems Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import java.util.Vector; public final class DMath { private DMath() {} /** * Calculate the hyperbolic tangent */ public static double tanh(double x) { try { if(x == 0.0D) { double d1 = x; return d1; } double a = x; if(a < 0.0D) a = Math.abs(x); a = Math.exp(2D * a); if(x < 0.0D) { double d2 = -(1.0D - 2D / (a + 1.0D)); return d2; } else { double d3 = 1.0D - 2D / (a + 1.0D); return d3; } } catch(Exception e) { e.printStackTrace(); } double d = 0.0D; return d; } /** * Calculates the log with base 10 */ public static double log10(double x) { return Math.log(x) / Math.log(10D); } /** * Returns the absolute value of a complex number * @param real the real part of the complex number * @param imag the imaginary part of the complex number * @return the absolute value */ public static double cabs(double real, double imag) { double x = Math.abs(real); double y = Math.abs(imag); if(Double.isInfinite(x) || Double.isInfinite(y)) return (1.0D / 0.0D); if(x + y == 0.0D) return 0.0D; if(x > y) { y /= x; return x * Math.sqrt(1.0D + y * y); } else { x /= y; return y * Math.sqrt(x * x + 1.0D); } } /** * Returns the argument (phase) of a complex number, in radians, * with a branch cut along the negative real axis. * @param real the real part of the complex number * @param imag the imaginary part of the complex number * @return A double value equal to the argument (or phase) of a complex number. * It is in the interval [-pi,pi]. */ public static double cargument(double real, double imag) { return Math.atan2(imag, real); } /** * Rounds a double to a certain number of decimal places * @param x the double to be rounded * @param decimalPlaces the number of decimal places wanted * @return the rounded double */ public static double round(double x, int decimalPlaces) { return ((int)(x*Math.pow(10, decimalPlaces)))/Math.pow(10, decimalPlaces); } /** * Rounds a float to a certain number of decimal places * @param x the float to be rounded * @param decimalPlaces the number of decimal places wanted * @return the rounded float */ public static float round(float x, int decimalPlaces) { return (float)(((int)(x*Math.pow(10, decimalPlaces)))/Math.pow(10, decimalPlaces)); } public static double fraction(double d, int i) { return Math.round(Math.pow(10.00, (double) i) * d) / Math.pow(10.00, (double) i); } /** * computes the max element.
* converts all to double for comparison.
* should specialize to EFullVector and SparseVector eventually */ public static double maxElement(Vector aVect) { double theMax = ((Number)aVect.elementAt(0)).doubleValue(); int size = aVect.size(); for(int i = 1; i < size; i++) { if(((Number)aVect.elementAt(i)).doubleValue() > theMax) theMax = ((Number)aVect.elementAt(i)).doubleValue(); } return theMax; } /** * computes the min element.
* converts all to double for comparison.
* should specialize to EFullVector and SparseVector eventually */ public static double minElement(Vector aVect) { double theMin = ((Number)aVect.elementAt(0)).doubleValue(); int size = aVect.size(); for(int i = 1; i < size; i++) { if(((Number)aVect.elementAt(i)).doubleValue() < theMin) theMin = ((Number)aVect.elementAt(i)).doubleValue(); } return theMin; } }