vec_math
Class Math2

java.lang.Object
  extended by vec_math.Math2

public class Math2
extends Object

Provides some more sophisticated mathemtical functions. All methods are static.


Nested Class Summary
static class Math2.Airy
           
static class Math2.InterQuartile
           
static class Math2.Newton
           
static class Math2.Spline
           
 
Field Summary
private static double ACCNEWTON
          Default accuracy to reach with Newton's root-finding.
static double DOUBLE_NORM
          Smallest normalized double.
static double ERRF1
          The value of the error function at (1/sqrt(2)) for 1-sigam dev.
static float FLOAT_NORM
          Smallest normalized float.
static double FWHM
          Multiply sigma of a normal distribution with this to get the FWHM.
static double LN10
          The natural logarithm of ten.
static double LN2
          The natural logarithm of two.
static double LN2PI
          The natural logarithm of two.
private static int MAXNEWTON
          Maximum number of iteration in Newton's root-finding.
static String NAN
          A string representation of NaN, different to the question mark.
static char PM
          The plus-minus char.
static double SQR2
          The square root of two.
static double SQR3
          The square root of three.
static double SQRLN12
          The square root of minus the natural logarithm of 1/2.
static double STD2IQR
          For a normal distrib.
 
Constructor Summary
Math2()
           
 
Method Summary
static int aoverb(int a, int b)
          Return a over b.
static double arcosh(double x)
          Area cosinus hyperbolicus.
static double arsinh(double x)
          Area sinus hyperbolicus.
static double artanh(double x)
          Area tangens hyperbolicus.
static double confine24h(double arg)
          Special use of confinePeriod for angles in hours.
static double confine2PI(double arg)
          Special use of confinePeriod for angles in radian.
static double confine360(double arg)
          Special use of confinePeriod for angles in degrees.
static double confinePeriod(double arg, double period)
          Periodic arguments are reduced to first intervall, 0-360.
static boolean contains(double[] arr, double val)
          Returns true, if the double val is found in array arr.
static boolean contains(Object[] arr, Object val)
          Returns true if the target object is found within the object array.
static double cosh(double x)
          Cosinus hyperbolicus.
static Number[] cubicEquation(double a, double b, double c, double d)
          Solves a cubic equation of form
static Vector2D[] cubicSpline(Vector2D[] tab, double[] step, Vector2D yprim)
          Takes a tabulated function as an array of Vector2D and does cubic spline interpolation at all x-values given in the step array.
static double diffraction(double x)
          Bessel function J1(x)/x for any real x, also x=0.
static long fac(int n)
          Returns n!.
static double frac(double val)
          Returns the part of the double behind the point.
static double gauss(double arg)
          Returns the valus of the Gauss function.
static double gauss(double arg, double sigma)
          Returns the valus of the Gauss function.
static int greatestCommonFactor(int a, int b)
          Returns the greatest common factor using Euklid's algorithm.
static Vector3D interpolateExtremum(double x1, double y1, double x2, double y2, double x3, double y3)
          Parabolic extremum.
static double interpolatingParabel(double x1, double y1, double x2, double y2, double x3, double y3, double xd)
          Parabolic interpolation.
static VectorG interpolatingParabel(VectorG a1, VectorG a2, VectorG a3, double xd)
          Parabolic interpolation.
static double j0(double x)
          Bessel function J0 for any real x.
static double j1(double x)
          Bessel function J1 for any real x.
static int leastCommonMultiple(int a, int b)
          Returns the least common multiple of two integers.
static Vector2D[] linearSpline(Vector2D[] tab, double[] step)
          A linear spline is simply a linear interpolation between the borders of the tabulated values.
static double lnFac(int n)
          Estimates factorials with the gamma function estimate.
static double log10(double a)
          Decadic logarithm.
static void main(String[] arg)
           
static double moffat(double alpha, double beta, double r)
          The normalized Moffat function.
static double moffatFwhm(double fwhm, double beta, double r)
          The normalized Moffat function for beta and FWHM, with
private static Vector3D parabolarFit(Vector2D v1, Vector2D v2, Vector2D v3, double xd)
          Returns the three parabolic constants a_i from
static Number[] quadraticEquation(double a, double b, double c)
          Solves a quadratic equation of form
static double restrict(double x, double min, double max)
          Confines the argument so that it is not less than the minimum and not bigger than the maximum.
static int restrict(int x, int min, int max)
          Confines the argument so that it is not less than the minimum and not bigger than the maximum.
static long restrict(long x, long min, long max)
          Confines the argument so that it is not less than the minimum and not bigger than the maximum.
static double sign(double val)
          Sign function returning +1 if the argument is positive or -1 if the argument is negative.
static double simhoerl(double arg, int n)
          Returns the value of the Gauss function multiplied with x-1/n.
static double sinh(double x)
          Sinus hyperbolicus.
private static Vector3D[] spline(Vector2D[] tab, Vector2D yprim)
          Does the spline coefficients for the second derivatives on the entire data set in one sweep.
static double square(double x)
          Square the argument.
static double tanh(double x)
          Tangens hyperbolicus.
static double[] toHexArray(double val)
          Converts a normal number like grad.fgrad to deg[0], min[1], sec[2] doubles.
static double zeroBisection(Function f, double xplus, double xminus, double acc, int nmax)
          Uses bisection to find a root of the Function f in the intervals with given accuracy.
static double zeroNewton(Derivative f, double xs)
          Tries to find a zero point of the function using the starting value supplied.
static double zeroNewton(Derivative f, double xs, double acc)
          Tries to find a zero point of the function using the starting value supplied.
static double zeroNewton(Derivative f, double xs, double acc, int nmax)
          Tries to find a zero point of the function using the starting value supplied.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FLOAT_NORM

public static final float FLOAT_NORM
Smallest normalized float.


DOUBLE_NORM

public static final double DOUBLE_NORM
Smallest normalized double.


NAN

public static final String NAN
A string representation of NaN, different to the question mark.

See Also:
Constant Field Values

PM

public static final char PM
The plus-minus char.

See Also:
Constant Field Values

LN10

public static final double LN10
The natural logarithm of ten.


LN2

public static final double LN2
The natural logarithm of two.


LN2PI

public static final double LN2PI
The natural logarithm of two.


SQRLN12

public static final double SQRLN12
The square root of minus the natural logarithm of 1/2.


FWHM

public static final double FWHM
Multiply sigma of a normal distribution with this to get the FWHM.


SQR2

public static final double SQR2
The square root of two.


SQR3

public static final double SQR3
The square root of three.


ERRF1

public static final double ERRF1
The value of the error function at (1/sqrt(2)) for 1-sigam dev.

See Also:
Constant Field Values

STD2IQR

public static final double STD2IQR
For a normal distrib. the inter-quartile range is std.dev times this.

See Also:
Constant Field Values

MAXNEWTON

private static final int MAXNEWTON
Maximum number of iteration in Newton's root-finding.

See Also:
Constant Field Values

ACCNEWTON

private static final double ACCNEWTON
Default accuracy to reach with Newton's root-finding.

See Also:
Constant Field Values
Constructor Detail

Math2

public Math2()
Method Detail

restrict

public static final double restrict(double x,
                                    double min,
                                    double max)
Confines the argument so that it is not less than the minimum and not bigger than the maximum.

Parameters:
x - The value that should be confined
min - The minimum allowed value.
max - The maximum allowed value.
Returns:
A double between min and max.

restrict

public static final long restrict(long x,
                                  long min,
                                  long max)
Confines the argument so that it is not less than the minimum and not bigger than the maximum.

Parameters:
x - The value that should be confined
min - The minimum allowed value.
max - The maximum allowed value.
Returns:
A double between min and max.

restrict

public static final int restrict(int x,
                                 int min,
                                 int max)
Confines the argument so that it is not less than the minimum and not bigger than the maximum.

Parameters:
x - The value that should be confined
min - The minimum allowed value.
max - The maximum allowed value.
Returns:
A double between min and max.

square

public static final double square(double x)
Square the argument.


tanh

public static final double tanh(double x)
Tangens hyperbolicus.


sinh

public static final double sinh(double x)
Sinus hyperbolicus.


cosh

public static final double cosh(double x)
Cosinus hyperbolicus.


arsinh

public static final double arsinh(double x)
Area sinus hyperbolicus.


arcosh

public static final double arcosh(double x)
Area cosinus hyperbolicus. Returns the positive solution


artanh

public static final double artanh(double x)
Area tangens hyperbolicus.


log10

public static final double log10(double a)
Decadic logarithm.


moffat

public static double moffat(double alpha,
                            double beta,
                            double r)
The normalized Moffat function. Normalized for area, i.e. the integral equals one. The value at r=0 has to be normalized by the value
 (β-1)/πα²
 
 psf(r)=(β-1)/πα²[1+(r/α)²],
 
Its integral for r=0->&infinity; equals 1.


moffatFwhm

public static double moffatFwhm(double fwhm,
                                double beta,
                                double r)
The normalized Moffat function for beta and FWHM, with
 FWHM = 2α√(21/β-1)
 
. Normalized for area, i.e. the integral equals one. The value at r=0 has to be normalized by the value
 4*(β-1)*(2^(1/β-1))/πFWHM²
 


j0

public static final double j0(double x)
Bessel function J0 for any real x. Numerical Recipes, p. 233


j1

public static final double j1(double x)
Bessel function J1 for any real x. Numerical Recipes, p. 233


diffraction

public static final double diffraction(double x)
Bessel function J1(x)/x for any real x, also x=0. Used in diffraction formular for intensity
 I=I0*(2.*J1(x)/x)^2, x = k · D/2 · sin(θ), 
        k=2π/λ
 
Numerical Recipes, p. 233


interpolatingParabel

public static VectorG interpolatingParabel(VectorG a1,
                                           VectorG a2,
                                           VectorG a3,
                                           double xd)
Parabolic interpolation. Supply three Vectors where the function is defined, the zero index beeing the functional parameter. Returned is the interpolated point at the given parameter value. Might also be used for extrapolation. A parabola of type
 y_i = a0*(x-xd)ˆ2+a1*(x-xd)+a2
 
is fitted to the the input points at the given three values and the y_i at xd is calculated and returned as a vector, whoses zero'th component is the interpolating point xd and the remaining indices are filled with the corresponding parabolic fit.

Parameters:
a1 - A cornerpoint where the functional value is known
a2 - A cornerpoint where the functional value is known
a3 - A cornerpoint where the functional value is known
xd - The parameter value to which we should interpolate.

interpolatingParabel

public static double interpolatingParabel(double x1,
                                          double y1,
                                          double x2,
                                          double y2,
                                          double x3,
                                          double y3,
                                          double xd)
Parabolic interpolation. Supply three points where the function is defined. Returned is the interpolated point at the given parameter value. Might also be used for extrapolation. A parabola of type
 y_i = a0*(x-xd)ˆ2+a1*(x-xd)+a2
 
is fitted to the the input points at the given three values and the y_i at xd is calculated and returned.

Parameters:
x1 - A cornerpoint where the functional value is known
y1 - A cornerpoint where the functional value is known
x2 - A cornerpoint where the functional value is known
y2 - A cornerpoint where the functional value is known
x3 - A cornerpoint where the functional value is known
y3 - A cornerpoint where the functional value is known
xd - The parameter value to which we should interpolate.

interpolateExtremum

public static Vector3D interpolateExtremum(double x1,
                                           double y1,
                                           double x2,
                                           double y2,
                                           double x3,
                                           double y3)
Parabolic extremum. Supply three points where the function is defined, the zero index beeing the functional parameter. Returned is the extremum of an interpolating parabel of type
 y_i=a0*(x-xd)^2+a1*(x-xd)+a2,
        where y'(x)=0
 
Returned is the x/y value of the extremum of the parabel.

Parameters:
x1 - A cornerpoint where the functional value is known
y1 - A cornerpoint where the functional value is known
x2 - A cornerpoint where the functional value is known
y2 - A cornerpoint where the functional value is known
x3 - A cornerpoint where the functional value is known
y3 - A cornerpoint where the functional value is known
Returns:
A Vector3D, x is the position of the maximum and y its value, z is the square of the fwhm of the interpolating parabolar.

parabolarFit

private static Vector3D parabolarFit(Vector2D v1,
                                     Vector2D v2,
                                     Vector2D v3,
                                     double xd)
Returns the three parabolic constants a_i from
 y_i = a0*(x-xd)ˆ2+a1*(x-xd)+a2
 
as a three-dimensional vector with according indices.


cubicSpline

public static final Vector2D[] cubicSpline(Vector2D[] tab,
                                           double[] step,
                                           Vector2D yprim)
Takes a tabulated function as an array of Vector2D and does cubic spline interpolation at all x-values given in the step array. A natural cubic spline, meaning that the second derivative at the borders is set to zero is implied if the vector defining the values of the first derivatives at the boundaries is null.
Lit. Num. Rec. p.116f

Parameters:
tab - The tabulated function, ordered.
step - Where the spline should be evaluated, order kept in output.
bond - Boundary values of the first derivative, null for natural spline.
Returns:
An array of Vector2D with desired x and interpolated y.

linearSpline

public static final Vector2D[] linearSpline(Vector2D[] tab,
                                            double[] step)
A linear spline is simply a linear interpolation between the borders of the tabulated values.


spline

private static final Vector3D[] spline(Vector2D[] tab,
                                       Vector2D yprim)
Does the spline coefficients for the second derivatives on the entire data set in one sweep. Returns x/y and y''. Only used in cubicSpline(vec_math.Vector2D[], double[], vec_math.Vector2D)

Parameters:
tab - The tabulated function, ordered.
bond - Boundary values of the first derivative, null for natural spline.

confinePeriod

public static double confinePeriod(double arg,
                                   double period)
Periodic arguments are reduced to first intervall, 0-360. An angle of 400 degrees is reduced to 40 degrees.

Parameters:
arg - The value
period - Its period
See Also:
confine2PI(double), confine360(double), confine24h(double)

confine2PI

public static double confine2PI(double arg)
Special use of confinePeriod for angles in radian.

Parameters:
arg - Angle in radians.
See Also:
confinePeriod(double, double)

confine360

public static double confine360(double arg)
Special use of confinePeriod for angles in degrees.

Parameters:
arg - Angle in degrees.
See Also:
confinePeriod(double, double)

confine24h

public static double confine24h(double arg)
Special use of confinePeriod for angles in hours.

Parameters:
arg - Angle in hours.
See Also:
confinePeriod(double, double)

sign

public static double sign(double val)
Sign function returning +1 if the argument is positive or -1 if the argument is negative. A zero argument returns zero.


quadraticEquation

public static Number[] quadraticEquation(double a,
                                         double b,
                                         double c)
Solves a quadratic equation of form
 a·x²+b·x+c=0
 
in real space, i.e. a,b,c are real numbers, not complex. The returned numbers, however, are either a pair of Doubles or a pair of Complex.

Parameters:
a - The factor to x².
b - The factor to x.
c - The constant.
Returns:
length-2 array of Numbers.

cubicEquation

public static Number[] cubicEquation(double a,
                                     double b,
                                     double c,
                                     double d)
Solves a cubic equation of form
 a·x³+b·x²+c·x+d=0
 
in real space, i.e. a, b, c, and d are real numbers, not complex. The returned numbers, however, are either a Double and a pair of Complex, or three Doubles.

Parameters:
a - The factor to x³.
b - The factor to x².
c - The factor to x.
d - The constant.
Returns:
length-3 array of Numbers. First always Double.

zeroNewton

public static double zeroNewton(Derivative f,
                                double xs)
Tries to find a zero point of the function using the starting value supplied. Uses Newton's method without any bookkeeping, thus may fail.

Parameters:
f - the differntiable function where the root should be found.
xs - Starting value
acc - Absoulte accuracy in the first derivative to be obtained.
Returns:
The zero-point of the function or NaN.

zeroNewton

public static double zeroNewton(Derivative f,
                                double xs,
                                double acc)
Tries to find a zero point of the function using the starting value supplied. Uses Newton's method without any bookkeeping, thus may fail.

Parameters:
f - the differntiable function where the root should be found.
xs - Starting value
acc - Absoulte accuracy in the first derivative to be obtained.
Returns:
The zero-point of the function or NaN.

zeroNewton

public static double zeroNewton(Derivative f,
                                double xs,
                                double acc,
                                int nmax)
Tries to find a zero point of the function using the starting value supplied. Uses Newton's method without any bookkeeping, thus may fail.

Parameters:
f - the differntiable function where the root should be found.
xs - Starting value
acc - Absoulte accuracy in the root improvement to be obtained.
Returns:
The zero-point of the function or NaN.

zeroBisection

public static double zeroBisection(Function f,
                                   double xplus,
                                   double xminus,
                                   double acc,
                                   int nmax)
Uses bisection to find a root of the Function f in the intervals with given accuracy. Note that the accuracy reached is
 ½nmax*abs(xplus-xminus)
 

Parameters:
f - the function where the root should be found.
xplus - Value, where f(xplus) > 0
xminus - Value, where f(xminus) < 0.
acc - The desired accuracy.
nmax - Maximum number of iterations.

gauss

public static double gauss(double arg,
                           double sigma)
Returns the valus of the Gauss function. f(x)=exp(-x²/2sigma²).


gauss

public static double gauss(double arg)
Returns the valus of the Gauss function. f(x)=exp(-x²).


simhoerl

public static double simhoerl(double arg,
                              int n)
Returns the value of the Gauss function multiplied with x-1/n. Note that this is not the hoerl function.


contains

public static boolean contains(double[] arr,
                               double val)
Returns true, if the double val is found in array arr.


contains

public static boolean contains(Object[] arr,
                               Object val)
Returns true if the target object is found within the object array. Test for equal, not identity.


frac

public static double frac(double val)
Returns the part of the double behind the point.


fac

public static long fac(int n)
Returns n!.


lnFac

public static double lnFac(int n)
Estimates factorials with the gamma function estimate. Returns the natural logarithm of the factorial. Uses Wikipedia
 \ln \Gamma(z) \approx \frac{1}{2} \left(\ln(2 \pi) - \ln z \right) + z \left(\ln \left( z + \frac{1}{12z- \frac{1}{10z}} \right)-1\right).
 


aoverb

public static int aoverb(int a,
                         int b)
Return a over b.


leastCommonMultiple

public static int leastCommonMultiple(int a,
                                      int b)
Returns the least common multiple of two integers.


greatestCommonFactor

public static int greatestCommonFactor(int a,
                                       int b)
Returns the greatest common factor using Euklid's algorithm.


toHexArray

public static double[] toHexArray(double val)
Converts a normal number like grad.fgrad to deg[0], min[1], sec[2] doubles. If argument is negativ, all array values get negativ.


main

public static void main(String[] arg)