samer@0
|
1 /*
|
samer@0
|
2 * Copyright (c) 2000, Samer Abdallah, King's College London.
|
samer@0
|
3 * All rights reserved.
|
samer@0
|
4 *
|
samer@0
|
5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
|
samer@0
|
6 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
7 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
8 */
|
samer@0
|
9
|
samer@0
|
10 package samer.maths.opt;
|
samer@0
|
11
|
samer@0
|
12 public class Util
|
samer@0
|
13 {
|
samer@0
|
14 public static double maxabs(double [] x)
|
samer@0
|
15 {
|
samer@0
|
16 double m=Math.abs(x[0]);
|
samer@0
|
17 for (int i=1; i<x.length; i++) {
|
samer@0
|
18 if (x[i]<-m) m=-x[i];
|
samer@0
|
19 else if (x[i]>m) m=x[i];
|
samer@0
|
20 }
|
samer@0
|
21 return m;
|
samer@0
|
22 }
|
samer@0
|
23
|
samer@0
|
24 // returns abs(x) > tol ? x : tol*sgn(x)
|
samer@0
|
25 public static double unzero(double x, double tol) {
|
samer@0
|
26 if (x>tol) return x;
|
samer@0
|
27 else if (x<-tol) return x;
|
samer@0
|
28 else if (x>0) return tol;
|
samer@0
|
29 else return -tol;
|
samer@0
|
30 }
|
samer@0
|
31
|
samer@0
|
32 public static boolean between(double b, double u, double c)
|
samer@0
|
33 {
|
samer@0
|
34 if (b<c) return ((b<u) && (u<c));
|
samer@0
|
35 else return ((c<u) && (u<b));
|
samer@0
|
36 }
|
samer@0
|
37
|
samer@0
|
38 public static double clip(double lower, double t, double upper)
|
samer@0
|
39 {
|
samer@0
|
40 if (t<=lower) return lower;
|
samer@0
|
41 else if (t>=upper) return upper;
|
samer@0
|
42 else return t;
|
samer@0
|
43 }
|
samer@0
|
44
|
samer@0
|
45 public static double cubici( double f1, double g1, double f2, double g2, double lambda)
|
samer@0
|
46 {
|
samer@0
|
47 double z=g2+g1-3*(f2-f1)/lambda;
|
samer@0
|
48 double w=z*z-g1*g2;
|
samer@0
|
49 w = (w>0) ? Math.sqrt(w) : 0;
|
samer@0
|
50 return lambda*((z+g1+w)/(g2+g1+2*z));
|
samer@0
|
51 }
|
samer@0
|
52
|
samer@0
|
53 public static int find( int i, int [] A, int n)
|
samer@0
|
54 {
|
samer@0
|
55 for (int j=0; j<n; j++) {
|
samer@0
|
56 if (A[j]==i) return j;
|
samer@0
|
57 }
|
samer@0
|
58 return -1;
|
samer@0
|
59 }
|
samer@0
|
60 }
|