view src/samer/maths/opt/Util.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line source
/*
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
 *	without even the implied warranty of MERCHANTABILITY or 
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.maths.opt;

public class Util
{
	public static double maxabs(double [] x)
	{
		double m=Math.abs(x[0]);
		for (int i=1; i<x.length; i++) {
			if (x[i]<-m)	 m=-x[i];
			else if (x[i]>m) m=x[i];
		}
		return m;
	}

	// returns abs(x)	> tol ? x : tol*sgn(x)
	public static double unzero(double x, double tol) {
		if (x>tol) return x;
		else if (x<-tol) return x;
		else if (x>0) return tol;
		else return -tol;
	}

	public static boolean between(double b, double u, double c)
	{
		if (b<c) return ((b<u) && (u<c));
		else     return ((c<u) && (u<b));
	}

	public static double clip(double lower, double t, double upper)
	{
		if (t<=lower) return lower;
		else if (t>=upper) return upper;
		else return t;
	}

	public static double cubici( double f1, double g1, double f2, double g2, double lambda)
	{
		double z=g2+g1-3*(f2-f1)/lambda;
		double w=z*z-g1*g2;
		w = (w>0) ? Math.sqrt(w) : 0;
		return lambda*((z+g1+w)/(g2+g1+2*z));
	}

	public static int find( int i, int [] A, int n)
	{
		for (int j=0; j<n; j++) {
			if (A[j]==i) return j;
		}
		return -1;		
	}
}