wiki:Manual/Library/Plugins/Seamcatmath

You are here: Manual / Seamcat / seamcatmath

TracNav


Seamcat Math

package org.seamcat.mathematics;
import static java.lang.Math.PI;
/**
 * This class contains the needed mathematic functions not included in the J2SE
 * standard Math-class
 * 
 * @author Christian Petersen 
 */
public class Mathematics {
	private static double DEFAULT_DELOG = -1000;
	private static double last_dbm;
	private static double last_res;
	public static double calculateDistance(double x1, double y1, double x2,
	      double y2) {
		return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
	}
	public static final double delogaritmize(double value) {
		if (value == 0) {
			return DEFAULT_DELOG;
		}
		return Math.pow(10, value / 10);
	}
	public static final double fromdBm2Watt(double dbm) {
		if (last_dbm != dbm) {
			last_res = Math.pow(10, (dbm - 30) / 10);
			last_dbm = dbm;
		}
		return last_res;
	}
	public static double fromLinearTodB(double value) {
		return 10 * Math.log10(value);
	}
	public static final double fromWatt2dBm(double watt) {
		if (watt == 0) {
			return DEFAULT_DELOG;
		}
		return 10 * Math.log10(watt) + 30;
	}
	public static final double powerSummation(double… powers) {
		double total = 0;
		for (double power : powers) {
			if (power != 0.0) {
				total += Math.pow(10, (power - 30) / 10);
			}
		}
		return 10 * Math.log10(total) + 30;
	}
	public static final double round(double d) {
		if (d == 0) {
			return 0.0d;
		}
		return Math.rint(d * 1000) / 1000;
	}

	private static final double DEGRAD = PI / 180.0;
	private static final double RADEG = 180.0 / PI;
	/**
	 * Returns the arc cosine of an angle, in the range of 0.0 through pi.
	 * 
	 * @param angle
	 *           given in degrees
	 * @return The arc cosine of an angle given in degrees, in the range of 0.0
	 *         through pi.
	 */
	public static double acosD(double angle) {
		return Math.acos(angle) * RADEG;
	}
	/**
	 * Returns the arc sine of an angle, in the range of -pi/2 through pi/2.
	 * 
	 * @param x
	 *           angle given in degrees
	 * @return The arc sine of an angle given in degrees, in the range of -pi/2
	 *         through pi/2.
	 */
	public static double asinD(double x) {
		return Math.asin(x) * RADEG;
	}
	/**
	 * Converts rectangular coordinates (x, y) to polar (r, theta).
	 * 
	 * @param x
	 * @param y
	 * @return The arc tangent of an angle in degrees specified by rectangular
	 *         coordinates.
	 */
	public static double atan2D(double x, double y) {
		return Math.atan2(y, x) * RADEG;
	}
	/**
	 * Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.
	 * 
	 * @param angle
	 *           given in degrees
	 * @return The arc tangent of an angle given degrees, in the range of -pi/2
	 *         through pi/2.
	 */
	public static double atanD(double angle) {
		return RADEG * Math.atan(angle);
	}
	/**
	 * Returns the trigonometric cosine of an angle
	 * 
	 * @param angle
	 *           in degrees
	 * @return The trigonometric cosine of an angle in degrees
	 */
	public static double cosD(double angle) {
		return Math.cos(angle * DEGRAD);
	}
	/**
	 * Hyberbolic Cosine function
	 * 
	 * @param x
	 * @return The hyberbolic cosine value of given argument
	 */
	public static double cosh(double x) {
		return (Math.exp(x) + Math.exp(-x)) / 2;
	}
	/**
	 * Return average value of an array of <code>double</code> values.
	 * 
	 * @param p
	 *           Array of double values
	 * @return The average value of array range from 0 - p.length
	 */
	public static double getAverage(double[] p) {
		return Mathematics.getAverage(p, p.length);
	}
	/**
	 * Return average value of an array of <code>double</code> values, for
	 * <code>length</code> length of array.
	 * 
	 * @param p
	 *           Array of double values
	 * @param length
	 *           The number of array entries to average over
	 * @return The average value of array range from 0 - <code>length</code>
	 */
	public static double getAverage(double[] p, int length) {
		double sum = 0;
		for (int i = 0; i < length && i < p.length; i++) {
			sum += p[i];
		}
		return sum / length;
	}
	/**
	 * Return Standard Deviation of array
	 * 
	 * @param p
	 *           Array of double values
	 * @return getStdDev(p,ave,p.length)
	 */
	public static double getStdDev(double[] p) {
		return Mathematics.getStdDev(p, Mathematics.getAverage(p), p.length);
	}
	/**
	 * Return Standard Deviation of array with given average.
	 * 
	 * @param p
	 *           Array of double values
	 * @param ave
	 *           Average value of p
	 * @return getStdDev(p,ave,p.length)
	 */
	public static double getStdDev(double[] p, double ave) {
		return Mathematics.getStdDev(p, ave, p.length);
	}
	/**
	 * Return Standard Deviation of array with given average and length
	 * 
	 * @param p
	 *           Array of double values
	 * @param ave
	 *           Average value of p
	 * @param length
	 *           The number of array which has been averaged over
	 * @return Standard deviation of array values ranging from 0 - length
	 */
	public static double getStdDev(double[] p, double ave, int length) {
		double sum = 0;
		for (int i = 0; i < length && i < p.length; i++) {
			double dev = ave - p[i];
			sum += dev * dev;
		}
		return Math.sqrt(sum / (length - 1));
	}
	/**
	 * Return maximum value of double array
	 * 
	 * @param p
	 *           Array of double values
	 * @return highest entry value.
	 */
	public static double max(double[] p) {
		double max = Double.NEGATIVE_INFINITY;
		for (int i = 0; i < p.length; i++) {
			if (p[i] > max) {
				max = p[i];
			}
		}
		return max;
	}
	/**
	 * Return minimum value of double array
	 * 
	 * @param p
	 *           Array of double values
	 * @return lowest entry value
	 */
	public static double min(double[] p) {
		double min = Double.MAX_VALUE;
		for (int i = 0; i < p.length; i++) {
			if (p[i] < min) {
				min = p[i];
			}
		}
		return min;
	}
	/**
	 * Returns the trigonometric sine of an angle
	 * 
	 * @param angle
	 *           in degrees
	 * @return The trigonometric sine of an angle in degrees
	 */
	public static double sinD(double angle) {
		return Math.sin(angle * DEGRAD);
	}
	/**
	 * Hyberbolic Sine function
	 * 
	 * @param x
	 * @return The hyberbolic sine value of given argument
	 */
	public static double sinh(double x) {
		return (Math.exp(x) - Math.exp(-x)) / 2;
	}
	/**
	 * Returns the trigonometric tangent of an angle
	 * 
	 * @param angle
	 *           in degrees
	 * @return The trigonometric tangent of an angle in degrees
	 */
	public static double tanD(double angle) {
		return Math.tan(angle * DEGRAD);
	}
	/**
	 * Hyberbolic trigonometric tangent function
	 * 
	 * @param x
	 * @return The hyberbolic trigonometric tangent value of given argument
	 */
	public static double tanh(double x) {
		return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
	}
	/**
	 * All methods are static - this constructor prevents instantiation of this
	 * class
	 */
	private Mathematics() {
	}
}