samer@0: /* samer@0: * Mathx.java samer@0: * samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.maths; samer@0: samer@0: /** samer@0: A Class full of static functions, mainly for manipulating samer@0: double arrays. samer@0: */ samer@0: samer@0: public class Mathx samer@0: { samer@0: private static double [] zeros = null; samer@0: private static int zlength = 0; samer@0: samer@0: public double [] doubleArray(int n) { return new double[n]; } samer@0: samer@0: /** set to zeros */ samer@0: public static void zero(double [] x) samer@0: { samer@0: if (zlengthxmax) xmax=x[i]; samer@0: return xmax; samer@0: } samer@0: samer@0: /** return minimum element of x */ samer@0: public static double min(double [] x) samer@0: { samer@0: double xmin=x[0]; samer@0: for (int i=1; ixmax) { xmax=x[i]; imax=i; } samer@0: } samer@0: return imax; samer@0: } samer@0: samer@0: /** return dot product of a and b: sum_i a[i]*b[i] */ samer@0: public static double dot( double [] a, double [] b) samer@0: { samer@0: double t=0; samer@0: for (int i=0; i24) return 1; samer@0: if (t<0) { double z=Math.exp(2*t); return (z-1)/(z+1); } samer@0: else { double z=Math.exp(-2*t); return (1-z)/(1+z); } samer@0: } samer@0: samer@0: public static double atanh(double t) { return Math.log((1+t)/(1-t))/2; } samer@0: public static double cosh(double t) { double z=Math.exp(-t); return (1/z+z)/2; } samer@0: public static double sinh(double t) { double z=Math.exp(-t); return (1/z-z)/2; } samer@0: samer@0: /** matrix multiplication: y[i] = sum_j A[i][j] x[j] */ samer@0: public static void mul( double [] y, double [][]A, double [] x) samer@0: { samer@0: int n=y.length; samer@0: int m=x.length; samer@0: double [] arow; samer@0: samer@0: for (int i=0; i