source: src/main/java/agents/anac/y2015/agentBuyogV2/flanagan/math/Fmath.java

Last change on this file was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 157.7 KB
Line 
1/*
2* Class Fmath
3*
4* USAGE: Mathematical class that supplements java.lang.Math and contains:
5* the main physical constants
6* trigonemetric functions absent from java.lang.Math
7* some useful additional mathematical functions
8* some conversion functions
9*
10* WRITTEN BY: Dr Michael Thomas Flanagan
11*
12* DATE: June 2002
13* AMENDED: 6 January 2006, 12 April 2006, 5 May 2006, 28 July 2006, 27 December 2006,
14* 29 March 2007, 29 April 2007, 2,9,15 & 26 June 2007, 20 October 2007, 4-6 December 2007
15* 27 February 2008, 25 April 2008, 26 April 2008, 13 May 2008, 25/26 May 2008, 3-7 July 2008
16* 11 November 2010, 9-18 January 2011
17*
18* DOCUMENTATION:
19* See Michael Thomas Flanagan's Java library on-line web pages:
20* http://www.ee.ucl.ac.uk/~mflanaga/java/
21* http://www.ee.ucl.ac.uk/~mflanaga/java/Fmath.html
22*
23* Copyright (c) 2002 - 2011
24*
25* PERMISSION TO COPY:
26* Permission to use, copy and modify this software and its documentation for
27* NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement
28* to the author, Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies.
29*
30* Dr Michael Thomas Flanagan makes no representations about the suitability
31* or fitness of the software for any or for a particular purpose.
32* Michael Thomas Flanagan shall not be liable for any damages suffered
33* as a result of using, modifying or distributing this software or its derivatives.
34*
35***************************************************************************************/
36
37package agents.anac.y2015.agentBuyogV2.flanagan.math;
38
39
40import java.util.ArrayList;
41import java.util.Vector;
42import java.io.IOException;
43import java.io.ByteArrayInputStream;
44import java.io.ByteArrayOutputStream;
45import java.io.ObjectOutputStream;
46import java.io.ObjectInputStream;
47import java.math.BigDecimal;
48import java.math.BigInteger;
49import java.util.HashMap;
50import java.util.Map;
51
52public class Fmath{
53
54 // PHYSICAL CONSTANTS
55
56 public static final double N_AVAGADRO = 6.0221419947e23; /* mol^-1 */
57 public static final double K_BOLTZMANN = 1.380650324e-23; /* J K^-1 */
58 public static final double H_PLANCK = 6.6260687652e-34; /* J s */
59 public static final double H_PLANCK_RED = H_PLANCK/(2*Math.PI); /* J s */
60 public static final double C_LIGHT = 2.99792458e8; /* m s^-1 */
61 public static final double R_GAS = 8.31447215; /* J K^-1 mol^-1 */
62 public static final double F_FARADAY = 9.6485341539e4; /* C mol^-1 */
63 public static final double T_ABS = -273.15; /* Celsius */
64 public static final double Q_ELECTRON = -1.60217646263e-19; /* C */
65 public static final double M_ELECTRON = 9.1093818872e-31; /* kg */
66 public static final double M_PROTON = 1.6726215813e-27; /* kg */
67 public static final double M_NEUTRON = 1.6749271613e-27; /* kg */
68 public static final double EPSILON_0 = 8.854187817e-12; /* F m^-1 */
69 public static final double MU_0 = Math.PI*4e-7; /* H m^-1 (N A^-2) */
70
71 // MATHEMATICAL CONSTANTS
72 public static final double EULER_CONSTANT_GAMMA = 0.5772156649015627;
73 public static final double PI = Math.PI; /* 3.141592653589793D */
74 public static final double E = Math.E; /* 2.718281828459045D */
75
76 // HashMap for 'arithmetic integer' recognition nmethod
77 private static final Map<Object,Object> integers = new HashMap<Object,Object>();
78 static{
79 integers.put(Integer.class, BigDecimal.valueOf(Integer.MAX_VALUE));
80 integers.put(Long.class, BigDecimal.valueOf(Long.MAX_VALUE));
81 integers.put(Byte.class, BigDecimal.valueOf(Byte.MAX_VALUE));
82 integers.put(Short.class, BigDecimal.valueOf(Short.MAX_VALUE));
83 integers.put(BigInteger.class, BigDecimal.valueOf(-1));
84 }
85
86 // METHODS
87
88 // LOGARITHMS
89 // Log to base 10 of a double number
90 public static double log10(double a){
91 return Math.log(a)/Math.log(10.0D);
92 }
93
94 // Log to base 10 of a float number
95 public static float log10(float a){
96 return (float) (Math.log((double)a)/Math.log(10.0D));
97 }
98
99 // Base 10 antilog of a double
100 public static double antilog10(double x){
101 return Math.pow(10.0D, x);
102 }
103
104 // Base 10 antilog of a float
105 public static float antilog10(float x){
106 return (float)Math.pow(10.0D, (double)x);
107 }
108
109 // Log to base e of a double number
110 public static double log(double a){
111 return Math.log(a);
112 }
113
114 // Log to base e of a float number
115 public static float log(float a){
116 return (float)Math.log((double)a);
117 }
118
119 // Base e antilog of a double
120 public static double antilog(double x){
121 return Math.exp(x);
122 }
123
124 // Base e antilog of a float
125 public static float antilog(float x){
126 return (float)Math.exp((double)x);
127 }
128
129 // Log to base 2 of a double number
130 public static double log2(double a){
131 return Math.log(a)/Math.log(2.0D);
132 }
133
134 // Log to base 2 of a float number
135 public static float log2(float a){
136 return (float) (Math.log((double)a)/Math.log(2.0D));
137 }
138
139 // Base 2 antilog of a double
140 public static double antilog2(double x){
141 return Math.pow(2.0D, x);
142 }
143
144 // Base 2 antilog of a float
145 public static float antilog2(float x){
146 return (float)Math.pow(2.0D, (double)x);
147 }
148
149 // Log to base b of a double number and double base
150 public static double log10(double a, double b){
151 return Math.log(a)/Math.log(b);
152 }
153
154 // Log to base b of a double number and int base
155 public static double log10(double a, int b){
156 return Math.log(a)/Math.log((double)b);
157 }
158
159 // Log to base b of a float number and flaot base
160 public static float log10(float a, float b){
161 return (float) (Math.log((double)a)/Math.log((double)b));
162 }
163
164 // Log to base b of a float number and int base
165 public static float log10(float a, int b){
166 return (float) (Math.log((double)a)/Math.log((double)b));
167 }
168
169 // SQUARES
170 // Square of a double number
171 public static double square(double a){
172 return a*a;
173 }
174
175 // Square of a float number
176 public static float square(float a){
177 return a*a;
178 }
179
180 // Square of a BigDecimal number
181 public static BigDecimal square(BigDecimal a){
182 return a.multiply(a);
183 }
184
185 // Square of an int number
186 public static int square(int a){
187 return a*a;
188 }
189
190 // Square of a long number
191 public static long square(long a){
192 return a*a;
193 }
194
195 // Square of a BigInteger number
196 public static BigInteger square(BigInteger a){
197 return a.multiply(a);
198 }
199
200 // FACTORIALS
201 // factorial of n
202 // argument and return are integer, therefore limited to 0<=n<=12
203 // see below for long and double arguments
204 public static int factorial(int n){
205 if(n<0)throw new IllegalArgumentException("n must be a positive integer");
206 if(n>12)throw new IllegalArgumentException("n must less than 13 to avoid integer overflow\nTry long or double argument");
207 int f = 1;
208 for(int i=2; i<=n; i++)f*=i;
209 return f;
210 }
211
212 // factorial of n
213 // argument and return are long, therefore limited to 0<=n<=20
214 // see below for double argument
215 public static long factorial(long n){
216 if(n<0)throw new IllegalArgumentException("n must be a positive integer");
217 if(n>20)throw new IllegalArgumentException("n must less than 21 to avoid long integer overflow\nTry double argument");
218 long f = 1;
219 long iCount = 2L;
220 while(iCount<=n){
221 f*=iCount;
222 iCount += 1L;
223 }
224 return f;
225 }
226
227 // factorial of n
228 // Argument is of type BigInteger
229 public static BigInteger factorial(BigInteger n){
230 if(n.compareTo(BigInteger.ZERO)==-1)throw new IllegalArgumentException("\nn must be a positive integer\nIs a Gamma funtion [Fmath.gamma(x)] more appropriate?");
231 BigInteger one = BigInteger.ONE;
232 BigInteger f = one;
233 BigInteger iCount = new BigInteger("2");
234 while(iCount.compareTo(n)!=1){
235 f = f.multiply(iCount);
236 iCount = iCount.add(one);
237 }
238 one = null;
239 iCount = null;
240 return f;
241 }
242
243 // factorial of n
244 // Argument is of type double but must be, numerically, an integer
245 // factorial returned as double but is, numerically, should be an integer
246 // numerical rounding may makes this an approximation after n = 21
247 public static double factorial(double n){
248 if(n<0.0 || (n-Math.floor(n))!=0)throw new IllegalArgumentException("\nn must be a positive integer\nIs a Gamma funtion [Fmath.gamma(x)] more appropriate?");
249 double f = 1.0D;
250 double iCount = 2.0D;
251 while(iCount<=n){
252 f*=iCount;
253 iCount += 1.0D;
254 }
255 return f;
256 }
257
258 // factorial of n
259 // Argument is of type BigDecimal but must be, numerically, an integer
260 public static BigDecimal factorial(BigDecimal n){
261 if(n.compareTo(BigDecimal.ZERO)==-1 || !Fmath.isInteger(n))throw new IllegalArgumentException("\nn must be a positive integer\nIs a Gamma funtion [Fmath.gamma(x)] more appropriate?");
262 BigDecimal one = BigDecimal.ONE;
263 BigDecimal f = one;
264 BigDecimal iCount = new BigDecimal(2.0D);
265 while(iCount.compareTo(n)!=1){
266 f = f.multiply(iCount);
267 iCount = iCount.add(one);
268 }
269 one = null;
270 iCount = null;
271 return f;
272 }
273
274
275
276 // log to base e of the factorial of n
277 // log[e](factorial) returned as double
278 // numerical rounding may makes this an approximation
279 public static double logFactorial(int n){
280 if(n<0)throw new IllegalArgumentException("\nn must be a positive integer\nIs a Gamma funtion [Fmath.gamma(x)] more appropriate?");
281 double f = 0.0D;
282 for(int i=2; i<=n; i++)f+=Math.log(i);
283 return f;
284 }
285
286 // log to base e of the factorial of n
287 // Argument is of type double but must be, numerically, an integer
288 // log[e](factorial) returned as double
289 // numerical rounding may makes this an approximation
290 public static double logFactorial(long n){
291 if(n<0L)throw new IllegalArgumentException("\nn must be a positive integer\nIs a Gamma funtion [Fmath.gamma(x)] more appropriate?");
292 double f = 0.0D;
293 long iCount = 2L;
294 while(iCount<=n){
295 f+=Math.log(iCount);
296 iCount += 1L;
297 }
298 return f;
299 }
300
301 // log to base e of the factorial of n
302 // Argument is of type double but must be, numerically, an integer
303 // log[e](factorial) returned as double
304 // numerical rounding may makes this an approximation
305 public static double logFactorial(double n){
306 if(n<0 || (n-Math.floor(n))!=0)throw new IllegalArgumentException("\nn must be a positive integer\nIs a Gamma funtion [Fmath.gamma(x)] more appropriate?");
307 double f = 0.0D;
308 double iCount = 2.0D;
309 while(iCount<=n){
310 f+=Math.log(iCount);
311 iCount += 1.0D;
312 }
313 return f;
314 }
315
316
317 // SIGN
318 /* returns -1 if x < 0 else returns 1 */
319 // double version
320 public static double sign(double x){
321 if (x<0.0){
322 return -1.0;
323 }
324 else{
325 return 1.0;
326 }
327 }
328
329 /* returns -1 if x < 0 else returns 1 */
330 // float version
331 public static float sign(float x){
332 if (x<0.0F){
333 return -1.0F;
334 }
335 else{
336 return 1.0F;
337 }
338 }
339
340 /* returns -1 if x < 0 else returns 1 */
341 // int version
342 public static int sign(int x){
343 if (x<0){
344 return -1;
345 }
346 else{
347 return 1;
348 }
349 }
350
351 /* returns -1 if x < 0 else returns 1 */
352 // long version
353 public static long sign(long x){
354 if (x<0){
355 return -1;
356 }
357 else{
358 return 1;
359 }
360 }
361
362 // ADDITIONAL TRIGONOMETRIC FUNCTIONS
363
364 // Returns the length of the hypotenuse of a and b
365 // i.e. sqrt(a*a+b*b) [without unecessary overflow or underflow]
366 // double version
367 public static double hypot(double aa, double bb){
368 double amod=Math.abs(aa);
369 double bmod=Math.abs(bb);
370 double cc = 0.0D, ratio = 0.0D;
371 if(amod==0.0){
372 cc=bmod;
373 }
374 else{
375 if(bmod==0.0){
376 cc=amod;
377 }
378 else{
379 if(amod>=bmod){
380 ratio=bmod/amod;
381 cc=amod*Math.sqrt(1.0 + ratio*ratio);
382 }
383 else{
384 ratio=amod/bmod;
385 cc=bmod*Math.sqrt(1.0 + ratio*ratio);
386 }
387 }
388 }
389 return cc;
390 }
391
392 // Returns the length of the hypotenuse of a and b
393 // i.e. sqrt(a*a+b*b) [without unecessary overflow or underflow]
394 // float version
395 public static float hypot(float aa, float bb){
396 return (float) hypot((double) aa, (double) bb);
397 }
398
399 // Angle (in radians) subtended at coordinate C
400 // given x, y coordinates of all apices, A, B and C, of a triangle
401 public static double angle(double xAtA, double yAtA, double xAtB, double yAtB, double xAtC, double yAtC){
402
403 double ccos = Fmath.cos(xAtA, yAtA, xAtB, yAtB, xAtC, yAtC);
404 return Math.acos(ccos);
405 }
406
407 // Angle (in radians) between sides sideA and sideB given all side lengths of a triangle
408 public static double angle(double sideAC, double sideBC, double sideAB){
409
410 double ccos = Fmath.cos(sideAC, sideBC, sideAB);
411 return Math.acos(ccos);
412 }
413
414 // Sine of angle subtended at coordinate C
415 // given x, y coordinates of all apices, A, B and C, of a triangle
416 public static double sin(double xAtA, double yAtA, double xAtB, double yAtB, double xAtC, double yAtC){
417 double angle = Fmath.angle(xAtA, yAtA, xAtB, yAtB, xAtC, yAtC);
418 return Math.sin(angle);
419 }
420
421 // Sine of angle between sides sideA and sideB given all side lengths of a triangle
422 public static double sin(double sideAC, double sideBC, double sideAB){
423 double angle = Fmath.angle(sideAC, sideBC, sideAB);
424 return Math.sin(angle);
425 }
426
427 // Sine given angle in radians
428 // for completion - returns Math.sin(arg)
429 public static double sin(double arg){
430 return Math.sin(arg);
431 }
432
433 // Inverse sine
434 // Fmath.asin Checks limits - Java Math.asin returns NaN if without limits
435 public static double asin(double a){
436 if(a<-1.0D && a>1.0D) throw new IllegalArgumentException("Fmath.asin argument (" + a + ") must be >= -1.0 and <= 1.0");
437 return Math.asin(a);
438 }
439
440 // Cosine of angle subtended at coordinate C
441 // given x, y coordinates of all apices, A, B and C, of a triangle
442 public static double cos(double xAtA, double yAtA, double xAtB, double yAtB, double xAtC, double yAtC){
443 double sideAC = Fmath.hypot(xAtA - xAtC, yAtA - yAtC);
444 double sideBC = Fmath.hypot(xAtB - xAtC, yAtB - yAtC);
445 double sideAB = Fmath.hypot(xAtA - xAtB, yAtA - yAtB);
446 return Fmath.cos(sideAC, sideBC, sideAB);
447 }
448
449 // Cosine of angle between sides sideA and sideB given all side lengths of a triangle
450 public static double cos(double sideAC, double sideBC, double sideAB){
451 return 0.5D*(sideAC/sideBC + sideBC/sideAC - (sideAB/sideAC)*(sideAB/sideBC));
452 }
453
454 // Cosine given angle in radians
455 // for completion - returns Java Math.cos(arg)
456 public static double cos(double arg){
457 return Math.cos(arg);
458 }
459
460 // Inverse cosine
461 // Fmath.asin Checks limits - Java Math.asin returns NaN if without limits
462 public static double acos(double a){
463 if(a<-1.0D || a>1.0D) throw new IllegalArgumentException("Fmath.acos argument (" + a + ") must be >= -1.0 and <= 1.0");
464 return Math.acos(a);
465 }
466
467 // Tangent of angle subtended at coordinate C
468 // given x, y coordinates of all apices, A, B and C, of a triangle
469 public static double tan(double xAtA, double yAtA, double xAtB, double yAtB, double xAtC, double yAtC){
470 double angle = Fmath.angle(xAtA, yAtA, xAtB, yAtB, xAtC, yAtC);
471 return Math.tan(angle);
472 }
473
474 // Tangent of angle between sides sideA and sideB given all side lengths of a triangle
475 public static double tan(double sideAC, double sideBC, double sideAB){
476 double angle = Fmath.angle(sideAC, sideBC, sideAB);
477 return Math.tan(angle);
478 }
479
480 // Tangent given angle in radians
481 // for completion - returns Math.tan(arg)
482 public static double tan(double arg){
483 return Math.tan(arg);
484 }
485
486 // Inverse tangent
487 // for completion - returns Math.atan(arg)
488 public static double atan(double a){
489 return Math.atan(a);
490 }
491
492 // Inverse tangent - ratio numerator and denominator provided
493 // for completion - returns Math.atan2(arg)
494 public static double atan2(double a, double b){
495 return Math.atan2(a, b);
496 }
497
498 // Cotangent
499 public static double cot(double a){
500 return 1.0D/Math.tan(a);
501 }
502
503 // Inverse cotangent
504 public static double acot(double a){
505 return Math.atan(1.0D/a);
506 }
507
508 // Inverse cotangent - ratio numerator and denominator provided
509 public static double acot2(double a, double b){
510 return Math.atan2(b, a);
511 }
512
513 // Secant
514 public static double sec(double a){
515 return 1.0/Math.cos(a);
516 }
517
518 // Inverse secant
519 public static double asec(double a){
520 if(a<1.0D && a>-1.0D) throw new IllegalArgumentException("asec argument (" + a + ") must be >= 1 or <= -1");
521 return Math.acos(1.0/a);
522 }
523
524 // Cosecant
525 public static double csc(double a){
526 return 1.0D/Math.sin(a);
527 }
528
529 // Inverse cosecant
530 public static double acsc(double a){
531 if(a<1.0D && a>-1.0D) throw new IllegalArgumentException("acsc argument (" + a + ") must be >= 1 or <= -1");
532 return Math.asin(1.0/a);
533 }
534
535 // Exsecant
536 public static double exsec(double a){
537 return (1.0/Math.cos(a)-1.0D);
538 }
539
540 // Inverse exsecant
541 public static double aexsec(double a){
542 if(a<0.0D && a>-2.0D) throw new IllegalArgumentException("aexsec argument (" + a + ") must be >= 0.0 and <= -2");
543 return Math.asin(1.0D/(1.0D + a));
544 }
545
546 // Versine
547 public static double vers(double a){
548 return (1.0D - Math.cos(a));
549 }
550
551 // Inverse versine
552 public static double avers(double a){
553 if(a<0.0D && a>2.0D) throw new IllegalArgumentException("avers argument (" + a + ") must be <= 2 and >= 0");
554 return Math.acos(1.0D - a);
555 }
556
557 // Coversine
558 public static double covers(double a){
559 return (1.0D - Math.sin(a));
560 }
561
562 // Inverse coversine
563 public static double acovers(double a){
564 if(a<0.0D && a>2.0D) throw new IllegalArgumentException("acovers argument (" + a + ") must be <= 2 and >= 0");
565 return Math.asin(1.0D - a);
566 }
567
568 // Haversine
569 public static double hav(double a){
570 return 0.5D*Fmath.vers(a);
571 }
572
573 // Inverse haversine
574 public static double ahav(double a){
575 if(a<0.0D && a>1.0D) throw new IllegalArgumentException("ahav argument (" + a + ") must be >= 0 and <= 1");
576 return Fmath.acos(1.0D - 2.0D*a);
577 }
578
579 // Unnormalised sinc (unnormalised sine cardinal) sin(x)/x
580 public static double sinc(double a){
581 if(Math.abs(a)<1e-40){
582 return 1.0D;
583 }
584 else{
585 return Math.sin(a)/a;
586 }
587 }
588
589 // Normalised sinc (normalised sine cardinal) sin(pi.x)/(pi.x)
590 public static double nsinc(double a){
591 if(Math.abs(a)<1e-40){
592 return 1.0D;
593 }
594 else{
595 return Math.sin(Math.PI*a)/(Math.PI*a);
596 }
597 }
598
599 //Hyperbolic sine of a double number
600 public static double sinh(double a){
601 return 0.5D*(Math.exp(a)-Math.exp(-a));
602 }
603
604 // Inverse hyperbolic sine of a double number
605 public static double asinh(double a){
606 double sgn = 1.0D;
607 if(a<0.0D){
608 sgn = -1.0D;
609 a = -a;
610 }
611 return sgn*Math.log(a+Math.sqrt(a*a+1.0D));
612 }
613
614 //Hyperbolic cosine of a double number
615 public static double cosh(double a){
616 return 0.5D*(Math.exp(a)+Math.exp(-a));
617 }
618
619 // Inverse hyperbolic cosine of a double number
620 public static double acosh(double a){
621 if(a<1.0D) throw new IllegalArgumentException("acosh real number argument (" + a + ") must be >= 1");
622 return Math.log(a+Math.sqrt(a*a-1.0D));
623 }
624
625 //Hyperbolic tangent of a double number
626 public static double tanh(double a){
627 return sinh(a)/cosh(a);
628 }
629
630 // Inverse hyperbolic tangent of a double number
631 public static double atanh(double a){
632 double sgn = 1.0D;
633 if(a<0.0D){
634 sgn = -1.0D;
635 a = -a;
636 }
637 if(a>1.0D) throw new IllegalArgumentException("atanh real number argument (" + sgn*a + ") must be >= -1 and <= 1");
638 return 0.5D*sgn*(Math.log(1.0D + a)-Math.log(1.0D - a));
639 }
640
641 //Hyperbolic cotangent of a double number
642 public static double coth(double a){
643 return 1.0D/tanh(a);
644 }
645
646 // Inverse hyperbolic cotangent of a double number
647 public static double acoth(double a){
648 double sgn = 1.0D;
649 if(a<0.0D){
650 sgn = -1.0D;
651 a = -a;
652 }
653 if(a<1.0D) throw new IllegalArgumentException("acoth real number argument (" + sgn*a + ") must be <= -1 or >= 1");
654 return 0.5D*sgn*(Math.log(1.0D + a)-Math.log(a - 1.0D));
655 }
656
657 //Hyperbolic secant of a double number
658 public static double sech(double a){
659 return 1.0D/cosh(a);
660 }
661
662 // Inverse hyperbolic secant of a double number
663 public static double asech(double a){
664 if(a>1.0D || a<0.0D) throw new IllegalArgumentException("asech real number argument (" + a + ") must be >= 0 and <= 1");
665 return 0.5D*(Math.log(1.0D/a + Math.sqrt(1.0D/(a*a) - 1.0D)));
666 }
667
668 //Hyperbolic cosecant of a double number
669 public static double csch(double a){
670 return 1.0D/sinh(a);
671 }
672
673 // Inverse hyperbolic cosecant of a double number
674 public static double acsch(double a){
675 double sgn = 1.0D;
676 if(a<0.0D){
677 sgn = -1.0D;
678 a = -a;
679 }
680 return 0.5D*sgn*(Math.log(1.0/a + Math.sqrt(1.0D/(a*a) + 1.0D)));
681 }
682
683 // DETERMINING PRECISION i.e. number of mantissa places
684 public static int checkPrecision(double number){
685 boolean test = true;
686 int prec = 0;
687 if(Fmath.isNaN(number))test=false;
688 if(Fmath.isPlusInfinity(number))test=false;
689 if(Fmath.isMinusInfinity(number))test=false;
690 while(test){
691 if(number==Fmath.truncate(number, prec)){
692 test = false;
693 }
694 else{
695 prec++;
696 if(prec>20)test=false;
697 }
698 }
699 return prec;
700 }
701
702 public static int checkPrecision(float number){
703 return checkPrecision((double)number);
704 }
705
706
707 // MANTISSA ROUNDING (TRUNCATING)
708 // returns a value of xDouble truncated to trunc decimal places
709 public static double truncate(double xDouble, int trunc){
710 double xTruncated = xDouble;
711 if(!Fmath.isNaN(xDouble)){
712 if(!Fmath.isPlusInfinity(xDouble)){
713 if(!Fmath.isMinusInfinity(xDouble)){
714 if(xDouble!=0.0D){
715 String xString = ((new Double(xDouble)).toString()).trim();
716 xTruncated = Double.parseDouble(truncateProcedure(xString, trunc));
717 }
718 }
719 }
720 }
721 return xTruncated;
722 }
723
724 // returns a value of xFloat truncated to trunc decimal places
725 public static float truncate(float xFloat, int trunc){
726 float xTruncated = xFloat;
727 if(!Fmath.isNaN(xFloat)){
728 if(!Fmath.isPlusInfinity(xFloat)){
729 if(!Fmath.isMinusInfinity(xFloat)){
730 if(xFloat!=0.0D){
731 String xString = ((new Float(xFloat)).toString()).trim();
732 xTruncated = Float.parseFloat(truncateProcedure(xString, trunc));
733 }
734 }
735 }
736 }
737 return xTruncated;
738 }
739
740 // private method for truncating a float or double expressed as a String
741 private static String truncateProcedure(String xValue, int trunc){
742
743 String xTruncated = xValue;
744 String xWorking = xValue;
745 String exponent = " ";
746 String first = "+";
747 int expPos = xValue.indexOf('E');
748 int dotPos = xValue.indexOf('.');
749 int minPos = xValue.indexOf('-');
750
751 if(minPos!=-1){
752 if(minPos==0){
753 xWorking = xWorking.substring(1);
754 first = "-";
755 dotPos--;
756 expPos--;
757 }
758 }
759 if(expPos>-1){
760 exponent = xWorking.substring(expPos);
761 xWorking = xWorking.substring(0,expPos);
762 }
763 String xPreDot = null;
764 String xPostDot = "0";
765 String xDiscarded = null;
766 String tempString = null;
767 double tempDouble = 0.0D;
768 if(dotPos>-1){
769 xPreDot = xWorking.substring(0,dotPos);
770 xPostDot = xWorking.substring(dotPos+1);
771 int xLength = xPostDot.length();
772 if(trunc<xLength){
773 xDiscarded = xPostDot.substring(trunc);
774 tempString = xDiscarded.substring(0,1) + ".";
775 if(xDiscarded.length()>1){
776 tempString += xDiscarded.substring(1);
777 }
778 else{
779 tempString += "0";
780 }
781 tempDouble = Math.round(Double.parseDouble(tempString));
782
783 if(trunc>0){
784 if(tempDouble>=5.0){
785 int[] xArray = new int[trunc+1];
786 xArray[0] = 0;
787 for(int i=0; i<trunc; i++){
788 xArray[i+1] = Integer.parseInt(xPostDot.substring(i,i+1));
789 }
790 boolean test = true;
791 int iCounter = trunc;
792 while(test){
793 xArray[iCounter] += 1;
794 if(iCounter>0){
795 if(xArray[iCounter]<10){
796 test = false;
797 }
798 else{
799 xArray[iCounter]=0;
800 iCounter--;
801 }
802 }
803 else{
804 test = false;
805 }
806 }
807 int preInt = Integer.parseInt(xPreDot);
808 preInt += xArray[0];
809 xPreDot = (new Integer(preInt)).toString();
810 tempString = "";
811 for(int i=1; i<=trunc; i++){
812 tempString += (new Integer(xArray[i])).toString();
813 }
814 xPostDot = tempString;
815 }
816 else{
817 xPostDot = xPostDot.substring(0, trunc);
818 }
819 }
820 else{
821 if(tempDouble>=5.0){
822 int preInt = Integer.parseInt(xPreDot);
823 preInt++;
824 xPreDot = (new Integer(preInt)).toString();
825 }
826 xPostDot = "0";
827 }
828 }
829 xTruncated = first + xPreDot.trim() + "." + xPostDot.trim() + exponent;
830 }
831 return xTruncated.trim();
832 }
833
834 // Returns true if x is infinite, i.e. is equal to either plus or minus infinity
835 // x is double
836 public static boolean isInfinity(double x){
837 boolean test=false;
838 if(x==Double.POSITIVE_INFINITY || x==Double.NEGATIVE_INFINITY)test=true;
839 return test;
840 }
841
842 // Returns true if x is infinite, i.e. is equal to either plus or minus infinity
843 // x is float
844 public static boolean isInfinity(float x){
845 boolean test=false;
846 if(x==Float.POSITIVE_INFINITY || x==Float.NEGATIVE_INFINITY)test=true;
847 return test;
848 }
849
850 // Returns true if x is plus infinity
851 // x is double
852 public static boolean isPlusInfinity(double x){
853 boolean test=false;
854 if(x==Double.POSITIVE_INFINITY)test=true;
855 return test;
856 }
857
858 // Returns true if x is plus infinity
859 // x is float
860 public static boolean isPlusInfinity(float x){
861 boolean test=false;
862 if(x==Float.POSITIVE_INFINITY)test=true;
863 return test;
864 }
865
866 // Returns true if x is minus infinity
867 // x is double
868 public static boolean isMinusInfinity(double x){
869 boolean test=false;
870 if(x==Double.NEGATIVE_INFINITY)test=true;
871 return test;
872 }
873
874 // Returns true if x is minus infinity
875 // x is float
876 public static boolean isMinusInfinity(float x){
877 boolean test=false;
878 if(x==Float.NEGATIVE_INFINITY)test=true;
879 return test;
880 }
881
882
883 // Returns true if x is 'Not a Number' (NaN)
884 // x is double
885 public static boolean isNaN(double x){
886 boolean test=false;
887 if(x!=x)test=true;
888 return test;
889 }
890
891 // Returns true if x is 'Not a Number' (NaN)
892 // x is float
893 public static boolean isNaN(float x){
894 boolean test=false;
895 if(x!=x)test=true;
896 return test;
897 }
898
899 // Returns true if x equals y
900 // x and y are double
901 // x may be float within range, PLUS_INFINITY, NEGATIVE_INFINITY, or NaN
902 // NB!! This method treats two NaNs as equal
903 public static boolean isEqual(double x, double y){
904 boolean test=false;
905 if(Fmath.isNaN(x)){
906 if(Fmath.isNaN(y))test=true;
907 }
908 else{
909 if(Fmath.isPlusInfinity(x)){
910 if(Fmath.isPlusInfinity(y))test=true;
911 }
912 else{
913 if(Fmath.isMinusInfinity(x)){
914 if(Fmath.isMinusInfinity(y))test=true;
915 }
916 else{
917 if(x==y)test=true;
918 }
919 }
920 }
921 return test;
922 }
923
924 // Returns true if x equals y
925 // x and y are float
926 // x may be float within range, PLUS_INFINITY, NEGATIVE_INFINITY, or NaN
927 // NB!! This method treats two NaNs as equal
928 public static boolean isEqual(float x, float y){
929 boolean test=false;
930 if(Fmath.isNaN(x)){
931 if(Fmath.isNaN(y))test=true;
932 }
933 else{
934 if(Fmath.isPlusInfinity(x)){
935 if(Fmath.isPlusInfinity(y))test=true;
936 }
937 else{
938 if(Fmath.isMinusInfinity(x)){
939 if(Fmath.isMinusInfinity(y))test=true;
940 }
941 else{
942 if(x==y)test=true;
943 }
944 }
945 }
946 return test;
947 }
948
949 // Returns true if x equals y
950 // x and y are int
951 public static boolean isEqual(int x, int y){
952 boolean test=false;
953 if(x==y)test=true;
954 return test;
955 }
956
957 // Returns true if x equals y
958 // x and y are char
959 public static boolean isEqual(char x, char y){
960 boolean test=false;
961 if(x==y)test=true;
962 return test;
963 }
964
965 // Returns true if x equals y
966 // x and y are Strings
967 public static boolean isEqual(String x, String y){
968 boolean test=false;
969 if(x.equals(y))test=true;
970 return test;
971 }
972
973 // IS EQUAL WITHIN LIMITS
974 // Returns true if x equals y within limits plus or minus limit
975 // x and y are double
976 public static boolean isEqualWithinLimits(double x, double y, double limit){
977 boolean test=false;
978 if(Math.abs(x-y)<=Math.abs(limit))test=true;
979 return test;
980 }
981
982 // Returns true if x equals y within limits plus or minus limit
983 // x and y are float
984 public static boolean isEqualWithinLimits(float x, float y, float limit){
985 boolean test=false;
986 if(Math.abs(x-y)<=Math.abs(limit))test=true;
987 return test;
988 }
989
990 // Returns true if x equals y within limits plus or minus limit
991 // x and y are long
992 public static boolean isEqualWithinLimits(long x, long y, long limit){
993 boolean test=false;
994 if(Math.abs(x-y)<=Math.abs(limit))test=true;
995 return test;
996 }
997
998 // Returns true if x equals y within limits plus or minus limit
999 // x and y are int
1000 public static boolean isEqualWithinLimits(int x, int y, int limit){
1001 boolean test=false;
1002 if(Math.abs(x-y)<=Math.abs(limit))test=true;
1003 return test;
1004 }
1005
1006 // Returns true if x equals y within limits plus or minus limit
1007 // x and y are BigDecimal
1008 public static boolean isEqualWithinLimits(BigDecimal x, BigDecimal y, BigDecimal limit){
1009 boolean test=false;
1010 if(((x.subtract(y)).abs()).compareTo(limit.abs())<=0)test = true;
1011 return test;
1012 }
1013
1014 // Returns true if x equals y within limits plus or minus limit
1015 // x and y are BigInteger
1016 public static boolean isEqualWithinLimits(BigInteger x, BigInteger y, BigInteger limit){
1017 boolean test=false;
1018 if(((x.subtract(y)).abs()).compareTo(limit.abs())<=0)test = true;
1019 return test;
1020 }
1021
1022
1023 // IS EQUAL WITHIN A PERCENTAGE
1024 // Returns true if x equals y within a percentage of the mean
1025 // x and y are double
1026 public static boolean isEqualWithinPerCent(double x, double y, double perCent){
1027 boolean test=false;
1028 double limit = Math.abs((x+y)*perCent/200.0D);
1029 if(Math.abs(x-y)<=limit)test=true;
1030 return test;
1031 }
1032
1033 // Returns true if x equals y within a percentage of the mean
1034 // x and y are float
1035 public static boolean isEqualWithinPerCent(float x, float y, float perCent){
1036 boolean test=false;
1037 double limit = Math.abs((x+y)*perCent/200.0F);
1038 if(Math.abs(x-y)<=limit)test=true;
1039 return test;
1040 }
1041
1042 // Returns true if x equals y within a percentage of the mean
1043 // x and y are long, percentage provided as double
1044 public static boolean isEqualWithinPerCent(long x, long y, double perCent){
1045 boolean test=false;
1046 double limit = Math.abs((x+y)*perCent/200.0D);
1047 if(Math.abs(x-y)<=limit)test=true;
1048 return test;
1049 }
1050
1051 // Returns true if x equals y within a percentage of the mean
1052 // x and y are long, percentage provided as int
1053 public static boolean isEqualWithinPerCent(long x, long y, long perCent){
1054 boolean test=false;
1055 double limit = Math.abs((double)(x+y)*(double)perCent/200.0D);
1056 if(Math.abs(x-y)<=limit)test=true;
1057 return test;
1058 }
1059
1060 // Returns true if x equals y within a percentage of the mean
1061 // x and y are int, percentage provided as double
1062 public static boolean isEqualWithinPerCent(int x, int y, double perCent){
1063 boolean test=false;
1064 double limit = Math.abs((double)(x+y)*perCent/200.0D);
1065 if(Math.abs(x-y)<=limit)test=true;
1066 return test;
1067 }
1068
1069 // Returns true if x equals y within a percentage of the mean
1070 // x and y are int, percentage provided as int
1071 public static boolean isEqualWithinPerCent(int x, int y, int perCent){
1072 boolean test=false;
1073 double limit = Math.abs((double)(x+y)*(double)perCent/200.0D);
1074 if(Math.abs(x-y)<=limit)test=true;
1075 return test;
1076 }
1077
1078 // Returns true if x equals y within a percentage of the mean
1079 // x and y are BigDecimal
1080 public static boolean isEqualWithinPerCent(BigDecimal x, BigDecimal y, BigDecimal perCent){
1081 boolean test=false;
1082 BigDecimal limit = (x.add(y)).multiply(perCent).multiply(new BigDecimal("0.005"));
1083 if(((x.subtract(y)).abs()).compareTo(limit.abs())<=0)test = true;
1084 limit = null;
1085 return test;
1086 }
1087
1088 // Returns true if x equals y within a percentage of the mean
1089 // x and y are BigDInteger, percentage provided as BigDecimal
1090 public static boolean isEqualWithinPerCent(BigInteger x, BigInteger y, BigDecimal perCent){
1091 boolean test=false;
1092 BigDecimal xx = new BigDecimal(x);
1093 BigDecimal yy = new BigDecimal(y);
1094 BigDecimal limit = (xx.add(yy)).multiply(perCent).multiply(new BigDecimal("0.005"));
1095 if(((xx.subtract(yy)).abs()).compareTo(limit.abs())<=0)test = true;
1096 limit = null;
1097 xx = null;
1098 yy = null;
1099 return test;
1100 }
1101
1102 // Returns true if x equals y within a percentage of the mean
1103 // x and y are BigDInteger, percentage provided as BigInteger
1104 public static boolean isEqualWithinPerCent(BigInteger x, BigInteger y, BigInteger perCent){
1105 boolean test=false;
1106 BigDecimal xx = new BigDecimal(x);
1107 BigDecimal yy = new BigDecimal(y);
1108 BigDecimal pc = new BigDecimal(perCent);
1109 BigDecimal limit = (xx.add(yy)).multiply(pc).multiply(new BigDecimal("0.005"));
1110 if(((xx.subtract(yy)).abs()).compareTo(limit.abs())<=0)test = true;
1111 limit = null;
1112 xx = null;
1113 yy = null;
1114 pc = null;
1115 return test;
1116 }
1117
1118 // COMPARISONS
1119 // Returns 0 if x == y
1120 // Returns -1 if x < y
1121 // Returns 1 if x > y
1122 // x and y are double
1123 public static int compare(double x, double y){
1124 Double X = new Double(x);
1125 Double Y = new Double(y);
1126 return X.compareTo(Y);
1127 }
1128
1129 // Returns 0 if x == y
1130 // Returns -1 if x < y
1131 // Returns 1 if x > y
1132 // x and y are int
1133 public static int compare(int x, int y){
1134 Integer X = new Integer(x);
1135 Integer Y = new Integer(y);
1136 return X.compareTo(Y);
1137 }
1138
1139 // Returns 0 if x == y
1140 // Returns -1 if x < y
1141 // Returns 1 if x > y
1142 // x and y are long
1143 public static int compare(long x, long y){
1144 Long X = new Long(x);
1145 Long Y = new Long(y);
1146 return X.compareTo(Y);
1147 }
1148
1149 // Returns 0 if x == y
1150 // Returns -1 if x < y
1151 // Returns 1 if x > y
1152 // x and y are float
1153 public static int compare(float x, float y){
1154 Float X = new Float(x);
1155 Float Y = new Float(y);
1156 return X.compareTo(Y);
1157 }
1158
1159 // Returns 0 if x == y
1160 // Returns -1 if x < y
1161 // Returns 1 if x > y
1162 // x and y are short
1163 public static int compare(byte x, byte y){
1164 Byte X = new Byte(x);
1165 Byte Y = new Byte(y);
1166 return X.compareTo(Y);
1167 }
1168
1169 // Returns 0 if x == y
1170 // Returns -1 if x < y
1171 // Returns 1 if x > y
1172 // x and y are short
1173 public static int compare(short x, short y){
1174 Short X = new Short(x);
1175 Short Y = new Short(y);
1176 return X.compareTo(Y);
1177 }
1178
1179 // COMPARE ARRAYS
1180 // Returns true if arrays identical, false if not
1181 // arrays - double[]
1182 public static boolean compare(double[] array1, double[] array2){
1183 boolean ret = true;
1184 int n = array1.length;
1185 int m = array2.length;
1186 if(n!=m){
1187 ret = false;
1188 }
1189 else{
1190 for(int i=0; i<n; i++){
1191 if(array1[i]!=array2[i]){
1192 ret = false;
1193 break;
1194 }
1195 }
1196 }
1197 return ret;
1198 }
1199
1200 // Returns true if arrays identical, false if not
1201 // arrays - float[]
1202 public static boolean compare(float[] array1, float[] array2){
1203 boolean ret = true;
1204 int n = array1.length;
1205 int m = array2.length;
1206 if(n!=m){
1207 ret = false;
1208 }
1209 else{
1210 for(int i=0; i<n; i++){
1211 if(array1[i]!=array2[i]){
1212 ret = false;
1213 break;
1214 }
1215 }
1216 }
1217 return ret;
1218 }
1219
1220 // Returns true if arrays identical, false if not
1221 // arrays - int[]
1222 public static boolean compare(int[] array1, int[] array2){
1223 boolean ret = true;
1224 int n = array1.length;
1225 int m = array2.length;
1226 if(n!=m){
1227 ret = false;
1228 }
1229 else{
1230 for(int i=0; i<n; i++){
1231 if(array1[i]!=array2[i]){
1232 ret = false;
1233 break;
1234 }
1235 }
1236 }
1237 return ret;
1238 }
1239
1240 // Returns true if arrays identical, false if not
1241 // arrays - long[]
1242 public static boolean compare(long[] array1, long[] array2){
1243 boolean ret = true;
1244 int n = array1.length;
1245 int m = array2.length;
1246 if(n!=m){
1247 ret = false;
1248 }
1249 else{
1250 for(int i=0; i<n; i++){
1251 if(array1[i]!=array2[i]){
1252 ret = false;
1253 break;
1254 }
1255 }
1256 }
1257 return ret;
1258 }
1259
1260 // IS AN INTEGER
1261 // Returns true if x is, arithmetically, an integer
1262 // Returns false if x is not, arithmetically, an integer
1263 public static boolean isInteger(double x){
1264 boolean retn = false;
1265 double xfloor = Math.floor(x);
1266 if((x - xfloor)==0.0D) retn = true;
1267 return retn;
1268 }
1269
1270 // Returns true if all elements in the array x are, arithmetically, integers
1271 // Returns false if any element in the array x is not, arithmetically, an integer
1272 public static boolean isInteger(double[] x){
1273 boolean retn = true;
1274 boolean test = true;
1275 int ii = 0;
1276 while(test){
1277 double xfloor = Math.floor(x[ii]);
1278 if((x[ii] - xfloor)!=0.0D){
1279 retn = false;
1280 test = false;
1281 }
1282 else{
1283 ii++;
1284 if(ii==x.length)test=false;
1285 }
1286 }
1287 return retn;
1288 }
1289
1290 // Returns true if x is, arithmetically, an integer
1291 // Returns false if x is not, arithmetically, an integer
1292 public static boolean isInteger(float x){
1293 boolean ret = false;
1294 float xfloor = (float)Math.floor(x);
1295 if((x - xfloor)==0.0F) ret = true;
1296 return ret;
1297 }
1298
1299
1300 // Returns true if all elements in the array x are, arithmetically, integers
1301 // Returns false if any element in the array x is not, arithmetically, an integer
1302 public static boolean isInteger(float[] x){
1303 boolean retn = true;
1304 boolean test = true;
1305 int ii = 0;
1306 while(test){
1307 float xfloor = (float)Math.floor(x[ii]);
1308 if((x[ii] - xfloor)!=0.0D){
1309 retn = false;
1310 test = false;
1311 }
1312 else{
1313 ii++;
1314 if(ii==x.length)test=false;
1315 }
1316 }
1317 return retn;
1318 }
1319
1320 public static boolean isInteger (Number numberAsObject){
1321 boolean test = integers.containsKey(numberAsObject.getClass());
1322 if(!test){
1323 if(numberAsObject instanceof Double){
1324 double dd = numberAsObject.doubleValue();
1325 test = Fmath.isInteger(dd);
1326 }
1327 if(numberAsObject instanceof Float){
1328 float dd = numberAsObject.floatValue();
1329 test = Fmath.isInteger(dd);
1330 }
1331 if(numberAsObject instanceof BigDecimal){
1332 double dd = numberAsObject.doubleValue();
1333 test = Fmath.isInteger(dd);
1334 }
1335 }
1336 return test;
1337 }
1338
1339 public static boolean isInteger (Number[] numberAsObject){
1340 boolean testall = true;
1341 for(int i=0; i<numberAsObject.length; i++){
1342 boolean test = integers.containsKey(numberAsObject[i].getClass());
1343 if(!test){
1344 if(numberAsObject[i] instanceof Double){
1345 double dd = numberAsObject[i].doubleValue();
1346 test = Fmath.isInteger(dd);
1347 if(!test)testall = false;
1348 }
1349 if(numberAsObject[i] instanceof Float){
1350 float dd = numberAsObject[i].floatValue();
1351 test = Fmath.isInteger(dd);
1352 if(!test)testall = false;
1353 }
1354 if(numberAsObject[i] instanceof BigDecimal){
1355 double dd = numberAsObject[i].doubleValue();
1356 test = Fmath.isInteger(dd);
1357 if(!test)testall = false;
1358 }
1359 }
1360 }
1361 return testall;
1362 }
1363
1364 // IS EVEN
1365 // Returns true if x is an even number, false if x is an odd number
1366 // x is int
1367 public static boolean isEven(int x){
1368 boolean test=false;
1369 if(x%2 == 0.0D)test=true;
1370 return test;
1371 }
1372
1373 // Returns true if x is an even number, false if x is an odd number
1374 // x is float but must hold an integer value
1375 public static boolean isEven(float x){
1376 double y=Math.floor(x);
1377 if(((double)x - y)!= 0.0D)throw new IllegalArgumentException("the argument is not an integer");
1378 boolean test=false;
1379 y=Math.floor(x/2.0F);
1380 if(((double)(x/2.0F)-y) == 0.0D)test=true;
1381 return test;
1382 }
1383
1384 // Returns true if x is an even number, false if x is an odd number
1385 // x is double but must hold an integer value
1386 public static boolean isEven(double x){
1387 double y=Math.floor(x);
1388 if((x - y)!= 0.0D)throw new IllegalArgumentException("the argument is not an integer");
1389 boolean test=false;
1390 y=Math.floor(x/2.0F);
1391 if((x/2.0D-y) == 0.0D)test=true;
1392 return test;
1393 }
1394
1395 // IS ODD
1396 // Returns true if x is an odd number, false if x is an even number
1397 // x is int
1398 public static boolean isOdd(int x){
1399 boolean test=true;
1400 if(x%2 == 0.0D)test=false;
1401 return test;
1402 }
1403
1404 // Returns true if x is an odd number, false if x is an even number
1405 // x is float but must hold an integer value
1406 public static boolean isOdd(float x){
1407 double y=Math.floor(x);
1408 if(((double)x - y)!= 0.0D)throw new IllegalArgumentException("the argument is not an integer");
1409 boolean test=true;
1410 y=Math.floor(x/2.0F);
1411 if(((double)(x/2.0F)-y) == 0.0D)test=false;
1412 return test;
1413 }
1414
1415 // Returns true if x is an odd number, false if x is an even number
1416 // x is double but must hold an integer value
1417 public static boolean isOdd(double x){
1418 double y=Math.floor(x);
1419 if((x - y)!= 0.0D)throw new IllegalArgumentException("the argument is not an integer");
1420 boolean test=true;
1421 y=Math.floor(x/2.0F);
1422 if((x/2.0D-y) == 0.0D)test=false;
1423 return test;
1424 }
1425
1426 // LEAP YEAR
1427 // Returns true if year (argument) is a leap year
1428 public static boolean leapYear(int year){
1429 boolean test = false;
1430
1431 if(year%4 != 0){
1432 test = false;
1433 }
1434 else{
1435 if(year%400 == 0){
1436 test=true;
1437 }
1438 else{
1439 if(year%100 == 0){
1440 test=false;
1441 }
1442 else{
1443 test=true;
1444 }
1445 }
1446 }
1447 return test;
1448 }
1449
1450 // COMPUTER TIME
1451 // Returns milliseconds since 0 hours 0 minutes 0 seconds on 1 Jan 1970
1452 public static long dateToJavaMilliS(int year, int month, int day, int hour, int min, int sec){
1453
1454 long[] monthDays = {0L, 31L, 28L, 31L, 30L, 31L, 30L, 31L, 31L, 30L, 31L, 30L, 31L};
1455 long ms = 0L;
1456
1457 long yearDiff = 0L;
1458 int yearTest = year-1;
1459 while(yearTest>=1970){
1460 yearDiff += 365;
1461 if(Fmath.leapYear(yearTest))yearDiff++;
1462 yearTest--;
1463 }
1464 yearDiff *= 24L*60L*60L*1000L;
1465
1466 long monthDiff = 0L;
1467 int monthTest = month -1;
1468 while(monthTest>0){
1469 monthDiff += monthDays[monthTest];
1470 if(Fmath.leapYear(year))monthDiff++;
1471 monthTest--;
1472 }
1473
1474 monthDiff *= 24L*60L*60L*1000L;
1475
1476 ms = yearDiff + monthDiff + day*24L*60L*60L*1000L + hour*60L*60L*1000L + min*60L*1000L + sec*1000L;
1477
1478 return ms;
1479 }
1480
1481 // DEPRECATED METHODS
1482 // Several methods have been revised and moved to classes ArrayMaths, Conv or PrintToScreen
1483
1484 // ARRAY MAXIMUM (deprecated - see ArryMaths class)
1485 // Maximum of a 1D array of doubles, aa
1486 public static double maximum(double[] aa){
1487 int n = aa.length;
1488 double aamax=aa[0];
1489 for(int i=1; i<n; i++){
1490 if(aa[i]>aamax)aamax=aa[i];
1491 }
1492 return aamax;
1493 }
1494
1495 // Maximum of a 1D array of floats, aa
1496 public static float maximum(float[] aa){
1497 int n = aa.length;
1498 float aamax=aa[0];
1499 for(int i=1; i<n; i++){
1500 if(aa[i]>aamax)aamax=aa[i];
1501 }
1502 return aamax;
1503 }
1504
1505 // Maximum of a 1D array of ints, aa
1506 public static int maximum(int[] aa){
1507 int n = aa.length;
1508 int aamax=aa[0];
1509 for(int i=1; i<n; i++){
1510 if(aa[i]>aamax)aamax=aa[i];
1511 }
1512 return aamax;
1513 }
1514
1515 // Maximum of a 1D array of longs, aa
1516 public static long maximum(long[] aa){
1517 long n = aa.length;
1518 long aamax=aa[0];
1519 for(int i=1; i<n; i++){
1520 if(aa[i]>aamax)aamax=aa[i];
1521 }
1522 return aamax;
1523 }
1524
1525 // Minimum of a 1D array of doubles, aa
1526 public static double minimum(double[] aa){
1527 int n = aa.length;
1528 double aamin=aa[0];
1529 for(int i=1; i<n; i++){
1530 if(aa[i]<aamin)aamin=aa[i];
1531 }
1532 return aamin;
1533 }
1534
1535 // Minimum of a 1D array of floats, aa
1536 public static float minimum(float[] aa){
1537 int n = aa.length;
1538 float aamin=aa[0];
1539 for(int i=1; i<n; i++){
1540 if(aa[i]<aamin)aamin=aa[i];
1541 }
1542 return aamin;
1543 }
1544
1545 // ARRAY MINIMUM (deprecated - see ArryMaths class)
1546 // Minimum of a 1D array of ints, aa
1547 public static int minimum(int[] aa){
1548 int n = aa.length;
1549 int aamin=aa[0];
1550 for(int i=1; i<n; i++){
1551 if(aa[i]<aamin)aamin=aa[i];
1552 }
1553 return aamin;
1554 }
1555
1556 // Minimum of a 1D array of longs, aa
1557 public static long minimum(long[] aa){
1558 long n = aa.length;
1559 long aamin=aa[0];
1560 for(int i=1; i<n; i++){
1561 if(aa[i]<aamin)aamin=aa[i];
1562 }
1563 return aamin;
1564 }
1565
1566 // MAXIMUM DISTANCE BETWEEN ARRAY ELEMENTS (deprecated - see ArryMaths class)
1567 // Maximum distance between elements of a 1D array of doubles, aa
1568 public static double maximumDifference(double[] aa){
1569 return Fmath.maximum(aa) - Fmath.minimum(aa);
1570 }
1571
1572 // Maximum distance between elements of a 1D array of floats, aa
1573 public static float maximumDifference(float[] aa){
1574 return Fmath.maximum(aa) - Fmath.minimum(aa);
1575 }
1576
1577 // Maximum distance between elements of a 1D array of long, aa
1578 public static long maximumDifference(long[] aa){
1579 return Fmath.maximum(aa) - Fmath.minimum(aa);
1580 }
1581
1582 // Maximum distance between elements of a 1D array of ints, aa
1583 public static int maximumDifference(int[] aa){
1584 return Fmath.maximum(aa) - Fmath.minimum(aa);
1585 }
1586
1587
1588 // MINIMUM DISTANCE BETWEEN ARRAY ELEMENTS (deprecated - see ArryMaths class)
1589 // Minimum distance between elements of a 1D array of doubles, aa
1590 public static double minimumDifference(double[] aa){
1591 double[] sorted = Fmath.selectionSort(aa);
1592 double n = aa.length;
1593 double diff = sorted[1] - sorted[0];
1594 double minDiff = diff;
1595 for(int i=1; i<n-1; i++){
1596 diff = sorted[i+1] - sorted[i];
1597 if(diff<minDiff)minDiff = diff;
1598 }
1599 return minDiff;
1600 }
1601
1602 // Minimum distance between elements of a 1D array of floats, aa
1603 public static float minimumDifference(float[] aa){
1604 float[] sorted = Fmath.selectionSort(aa);
1605 float n = aa.length;
1606 float diff = sorted[1] - sorted[0];
1607 float minDiff = diff;
1608 for(int i=1; i<n-1; i++){
1609 diff = sorted[i+1] - sorted[i];
1610 if(diff<minDiff)minDiff = diff;
1611 }
1612 return minDiff;
1613 }
1614
1615 // Minimum distance between elements of a 1D array of longs, aa
1616 public static long minimumDifference(long[] aa){
1617 long[] sorted = Fmath.selectionSort(aa);
1618 long n = aa.length;
1619 long diff = sorted[1] - sorted[0];
1620 long minDiff = diff;
1621 for(int i=1; i<n-1; i++){
1622 diff = sorted[i+1] - sorted[i];
1623 if(diff<minDiff)minDiff = diff;
1624 }
1625 return minDiff;
1626 }
1627
1628 // Minimum distance between elements of a 1D array of ints, aa
1629 public static int minimumDifference(int[] aa){
1630 int[] sorted = Fmath.selectionSort(aa);
1631 int n = aa.length;
1632 int diff = sorted[1] - sorted[0];
1633 int minDiff = diff;
1634 for(int i=1; i<n-1; i++){
1635 diff = sorted[i+1] - sorted[i];
1636 if(diff<minDiff)minDiff = diff;
1637 }
1638 return minDiff;
1639 }
1640
1641 // REVERSE ORDER OF ARRAY ELEMENTS (deprecated - see ArryMaths class)
1642 // Reverse the order of the elements of a 1D array of doubles, aa
1643 public static double[] reverseArray(double[] aa){
1644 int n = aa.length;
1645 double[] bb = new double[n];
1646 for(int i=0; i<n; i++){
1647 bb[i] = aa[n-1-i];
1648 }
1649 return bb;
1650 }
1651
1652 // Reverse the order of the elements of a 1D array of floats, aa
1653 public static float[] reverseArray(float[] aa){
1654 int n = aa.length;
1655 float[] bb = new float[n];
1656 for(int i=0; i<n; i++){
1657 bb[i] = aa[n-1-i];
1658 }
1659 return bb;
1660 }
1661
1662 // Reverse the order of the elements of a 1D array of ints, aa
1663 public static int[] reverseArray(int[] aa){
1664 int n = aa.length;
1665 int[] bb = new int[n];
1666 for(int i=0; i<n; i++){
1667 bb[i] = aa[n-1-i];
1668 }
1669 return bb;
1670 }
1671
1672 // Reverse the order of the elements of a 1D array of longs, aa
1673 public static long[] reverseArray(long[] aa){
1674 int n = aa.length;
1675 long[] bb = new long[n];
1676 for(int i=0; i<n; i++){
1677 bb[i] = aa[n-1-i];
1678 }
1679 return bb;
1680 }
1681
1682 // Reverse the order of the elements of a 1D array of char, aa
1683 public static char[] reverseArray(char[] aa){
1684 int n = aa.length;
1685 char[] bb = new char[n];
1686 for(int i=0; i<n; i++){
1687 bb[i] = aa[n-1-i];
1688 }
1689 return bb;
1690 }
1691
1692 // ABSOLUTE VALUE OF ARRAY ELEMENTS (deprecated - see ArryMaths class)
1693 // return absolute values of an array of doubles
1694 public static double[] arrayAbs(double[] aa){
1695 int n = aa.length;
1696 double[] bb = new double[n];
1697 for(int i=0; i<n; i++){
1698 bb[i] = Math.abs(aa[i]);
1699 }
1700 return bb;
1701 }
1702
1703 // return absolute values of an array of floats
1704 public static float[] arrayAbs(float[] aa){
1705 int n = aa.length;
1706 float[] bb = new float[n];
1707 for(int i=0; i<n; i++){
1708 bb[i] = Math.abs(aa[i]);
1709 }
1710 return bb;
1711 }
1712
1713 // return absolute values of an array of long
1714 public static long[] arrayAbs(long[] aa){
1715 int n = aa.length;
1716 long[] bb = new long[n];
1717 for(int i=0; i<n; i++){
1718 bb[i] = Math.abs(aa[i]);
1719 }
1720 return bb;
1721 }
1722
1723 // return absolute values of an array of int
1724 public static int[] arrayAbs(int[] aa){
1725 int n = aa.length;
1726 int[] bb = new int[n];
1727 for(int i=0; i<n; i++){
1728 bb[i] = Math.abs(aa[i]);
1729 }
1730 return bb;
1731 }
1732
1733 // MULTIPLY ARRAY ELEMENTS BY A CONSTANT (deprecated - see ArryMaths class)
1734 // multiply all elements by a constant double[] by double -> double[]
1735 public static double[] arrayMultByConstant(double[] aa, double constant){
1736 int n = aa.length;
1737 double[] bb = new double[n];
1738 for(int i=0; i<n; i++){
1739 bb[i] = aa[i]*constant;
1740 }
1741 return bb;
1742 }
1743
1744 // multiply all elements by a constant int[] by double -> double[]
1745 public static double[] arrayMultByConstant(int[] aa, double constant){
1746 int n = aa.length;
1747 double[] bb = new double[n];
1748 for(int i=0; i<n; i++){
1749 bb[i] = (double)aa[i]*constant;
1750 }
1751 return bb;
1752 }
1753 // multiply all elements by a constant double[] by int -> double[]
1754 public static double[] arrayMultByConstant(double[] aa, int constant){
1755 int n = aa.length;
1756 double[] bb = new double[n];
1757 for(int i=0; i<n; i++){
1758 bb[i] = aa[i]*(double)constant;
1759 }
1760 return bb;
1761 }
1762
1763 // multiply all elements by a constant int[] by int -> double[]
1764 public static double[] arrayMultByConstant(int[] aa, int constant){
1765 int n = aa.length;
1766 double[] bb = new double[n];
1767 for(int i=0; i<n; i++){
1768 bb[i] = (double)(aa[i]*constant);
1769 }
1770 return bb;
1771 }
1772
1773 // LOG10 OF ARRAY ELEMENTS (deprecated - see ArryMaths class)
1774 // Log to base 10 of all elements of an array of doubles
1775 public static double[] log10Elements(double[] aa){
1776 int n = aa.length;
1777 double[] bb = new double[n];
1778 for(int i=0; i<n; i++)bb[i] = Math.log10(aa[i]);
1779 return bb;
1780 }
1781
1782 // Log to base 10 of all elements of an array of floats
1783 public static float[] log10Elements(float[] aa){
1784 int n = aa.length;
1785 float[] bb = new float[n];
1786 for(int i=0; i<n; i++)bb[i] = (float)Math.log10(aa[i]);
1787 return bb;
1788 }
1789
1790 // NATURAL LOG OF ARRAY ELEMENTS (deprecated - see ArryMaths class)
1791 // Log to base e of all elements of an array of doubles
1792 public static double[] lnElements(double[] aa){
1793 int n = aa.length;
1794 double[] bb = new double[n];
1795 for(int i=0; i<n; i++)bb[i] = Math.log10(aa[i]);
1796 return bb;
1797 }
1798
1799 // Log to base e of all elements of an array of floats
1800 public static float[] lnElements(float[] aa){
1801 int n = aa.length;
1802 float[] bb = new float[n];
1803 for(int i=0; i<n; i++)bb[i] = (float)Math.log10(aa[i]);
1804 return bb;
1805 }
1806
1807 // SQUARE ROOT OF ARRAY ELEMENTS (deprecated - see ArryMaths class)
1808 // Square root all elements of an array of doubles
1809 public static double[] squareRootElements(double[] aa){
1810 int n = aa.length;
1811 double[] bb = new double[n];
1812 for(int i=0; i<n; i++)bb[i] = Math.sqrt(aa[i]);
1813 return bb;
1814 }
1815
1816 // Square root all elements of an array of floats
1817 public static float[] squareRootElements(float[] aa){
1818 int n = aa.length;
1819 float[] bb = new float[n];
1820 for(int i=0; i<n; i++)bb[i] = (float)Math.sqrt(aa[i]);
1821 return bb;
1822 }
1823
1824 // POWER OF ARRAY ELEMENTS (deprecated - see ArryMaths class)
1825 // Raise all elements of an array of doubles to a double power
1826 public static double[] raiseElementsToPower(double[] aa, double power){
1827 int n = aa.length;
1828 double[] bb = new double[n];
1829 for(int i=0; i<n; i++)bb[i] = Math.pow(aa[i], power);
1830 return bb;
1831 }
1832
1833 // Raise all elements of an array of doubles to an int power
1834 public static double[] raiseElementsToPower(double[] aa, int power){
1835 int n = aa.length;
1836 double[] bb = new double[n];
1837 for(int i=0; i<n; i++)bb[i] = Math.pow(aa[i], power);
1838 return bb;
1839 }
1840
1841 // Raise all elements of an array of floats to a float power
1842 public static float[] raiseElementsToPower(float[] aa, float power){
1843 int n = aa.length;
1844 float[] bb = new float[n];
1845 for(int i=0; i<n; i++)bb[i] = (float)Math.pow(aa[i], power);
1846 return bb;
1847 }
1848
1849 // Raise all elements of an array of floats to an int power
1850 public static float[] raiseElementsToPower(float[] aa, int power){
1851 int n = aa.length;
1852 float[] bb = new float[n];
1853 for(int i=0; i<n; i++)bb[i] = (float)Math.pow(aa[i], power);
1854 return bb;
1855 }
1856
1857 // INVERT ARRAY ELEMENTS (deprecated - see ArryMaths class)
1858 // invert all elements of an array of doubles
1859 public static double[] invertElements(double[] aa){
1860 int n = aa.length;
1861 double[] bb = new double[n];
1862 for(int i=0; i<n; i++)bb[i] = 1.0D/aa[i];
1863 return bb;
1864 }
1865
1866 // invert all elements of an array of floats
1867 public static float[] invertElements(float[] aa){
1868 int n = aa.length;
1869 float[] bb = new float[n];
1870 for(int i=0; i<n; i++)bb[i] = 1.0F/aa[i];
1871 return bb;
1872 }
1873
1874
1875 // FIND INDICES OF ARRAY ELEMENTS EQUAL TO A VALUE (deprecated - see ArryMaths class)
1876 // finds the indices of the elements equal to a given value in an array of doubles
1877 // returns null if none found
1878 public static int[] indicesOf(double[] array, double value){
1879 int[] indices = null;
1880 int numberOfIndices = 0;
1881 ArrayList<Integer> arrayl = new ArrayList<Integer>();
1882 for(int i=0; i<array.length; i++){
1883 if(array[i]==value){
1884 numberOfIndices++;
1885 arrayl.add(new Integer(i));
1886 }
1887 }
1888 if(numberOfIndices!=0){
1889 indices = new int[numberOfIndices];
1890 for(int i=0; i<numberOfIndices; i++){
1891 indices[i] = (arrayl.get(i)).intValue();
1892 }
1893 }
1894 return indices;
1895 }
1896
1897 // finds the indices of the elements equal to a given value in an array of floats
1898 // returns null if none found
1899 public static int[] indicesOf(float[] array, float value){
1900 int[] indices = null;
1901 int numberOfIndices = 0;
1902 ArrayList<Integer> arrayl = new ArrayList<Integer>();
1903 for(int i=0; i<array.length; i++){
1904 if(array[i]==value){
1905 numberOfIndices++;
1906 arrayl.add(new Integer(i));
1907 }
1908 }
1909 if(numberOfIndices!=0){
1910 indices = new int[numberOfIndices];
1911 for(int i=0; i<numberOfIndices; i++){
1912 indices[i] = (arrayl.get(i)).intValue();
1913 }
1914 }
1915 return indices;
1916 }
1917
1918 // finds the indices of the elements equal to a given value in an array of longs
1919 // returns null if none found
1920 public static int[] indicesOf(long[] array, long value){
1921 int[] indices = null;
1922 int numberOfIndices = 0;
1923 ArrayList<Integer> arrayl = new ArrayList<Integer>();
1924 for(int i=0; i<array.length; i++){
1925 if(array[i]==value){
1926 numberOfIndices++;
1927 arrayl.add(new Integer(i));
1928 }
1929 }
1930 if(numberOfIndices!=0){
1931 indices = new int[numberOfIndices];
1932 for(int i=0; i<numberOfIndices; i++){
1933 indices[i] = (arrayl.get(i)).intValue();
1934 }
1935 }
1936 return indices;
1937 }
1938
1939 // finds the indices of the elements equal to a given value in an array of ints
1940 // returns null if none found
1941 public static int[] indicesOf(int[] array, int value){
1942 int[] indices = null;
1943 int numberOfIndices = 0;
1944 ArrayList<Integer> arrayl = new ArrayList<Integer>();
1945 for(int i=0; i<array.length; i++){
1946 if(array[i]==value){
1947 numberOfIndices++;
1948 arrayl.add(new Integer(i));
1949 }
1950 }
1951 if(numberOfIndices!=0){
1952 indices = new int[numberOfIndices];
1953 for(int i=0; i<numberOfIndices; i++){
1954 indices[i] = (arrayl.get(i)).intValue();
1955 }
1956 }
1957 return indices;
1958 }
1959
1960 // finds the indices of the elements equal to a given value in an array of shorts
1961 // returns null if none found
1962 public static int[] indicesOf(short[] array, short value){
1963 int[] indices = null;
1964 int numberOfIndices = 0;
1965 ArrayList<Integer> arrayl = new ArrayList<Integer>();
1966 for(int i=0; i<array.length; i++){
1967 if(array[i]==value){
1968 numberOfIndices++;
1969 arrayl.add(new Integer(i));
1970 }
1971 }
1972 if(numberOfIndices!=0){
1973 indices = new int[numberOfIndices];
1974 for(int i=0; i<numberOfIndices; i++){
1975 indices[i] = (arrayl.get(i)).intValue();
1976 }
1977 }
1978 return indices;
1979 }
1980
1981 // finds the indices of the elements equal to a given value in an array of bytes
1982 // returns null if none found
1983 public static int[] indicesOf(byte[] array, byte value){
1984 int[] indices = null;
1985 int numberOfIndices = 0;
1986 ArrayList<Integer> arrayl = new ArrayList<Integer>();
1987 for(int i=0; i<array.length; i++){
1988 if(array[i]==value){
1989 numberOfIndices++;
1990 arrayl.add(new Integer(i));
1991 }
1992 }
1993 if(numberOfIndices!=0){
1994 indices = new int[numberOfIndices];
1995 for(int i=0; i<numberOfIndices; i++){
1996 indices[i] = (arrayl.get(i)).intValue();
1997 }
1998 }
1999 return indices;
2000 }
2001
2002 // finds the indices of the elements equal to a given value in an array of chars
2003 // returns null if none found
2004 public static int[] indicesOf(char[] array, char value){
2005 int[] indices = null;
2006 int numberOfIndices = 0;
2007 ArrayList<Integer> arrayl = new ArrayList<Integer>();
2008 for(int i=0; i<array.length; i++){
2009 if(array[i]==value){
2010 numberOfIndices++;
2011 arrayl.add(new Integer(i));
2012 }
2013 }
2014 if(numberOfIndices!=0){
2015 indices = new int[numberOfIndices];
2016 for(int i=0; i<numberOfIndices; i++){
2017 indices[i] = (arrayl.get(i)).intValue();
2018 }
2019 }
2020 return indices;
2021 }
2022
2023 // finds the indices of the elements equal to a given value in an array of Strings
2024 // returns null if none found
2025 public static int[] indicesOf(String[] array, String value){
2026 int[] indices = null;
2027 int numberOfIndices = 0;
2028 ArrayList<Integer> arrayl = new ArrayList<Integer>();
2029 for(int i=0; i<array.length; i++){
2030 if(array[i].equals(value)){
2031 numberOfIndices++;
2032 arrayl.add(new Integer(i));
2033 }
2034 }
2035 if(numberOfIndices!=0){
2036 indices = new int[numberOfIndices];
2037 for(int i=0; i<numberOfIndices; i++){
2038 indices[i] = (arrayl.get(i)).intValue();
2039 }
2040 }
2041 return indices;
2042 }
2043
2044 // finds the indices of the elements equal to a given value in an array of Objectss
2045 // returns null if none found
2046 public static int[] indicesOf(Object[] array, Object value){
2047 int[] indices = null;
2048 int numberOfIndices = 0;
2049 ArrayList<Integer> arrayl = new ArrayList<Integer>();
2050 for(int i=0; i<array.length; i++){
2051 if(array[i].equals(value)){
2052 numberOfIndices++;
2053 arrayl.add(new Integer(i));
2054 }
2055 }
2056 if(numberOfIndices!=0){
2057 indices = new int[numberOfIndices];
2058 for(int i=0; i<numberOfIndices; i++){
2059 indices[i] = (arrayl.get(i)).intValue();
2060 }
2061 }
2062 return indices;
2063 }
2064
2065 // FIND FIRST INDEX OF ARRAY ELEMENT EQUAL TO A VALUE (deprecated - see ArryMaths class)
2066 // finds the index of the first occurence of the element equal to a given value in an array of doubles
2067 // returns -1 if none found
2068 public static int indexOf(double[] array, double value){
2069 int index = -1;
2070 boolean test = true;
2071 int counter = 0;
2072 while(test){
2073 if(array[counter]==value){
2074 index = counter;
2075 test = false;
2076 }
2077 else{
2078 counter++;
2079 if(counter>=array.length)test = false;
2080 }
2081 }
2082 return index;
2083 }
2084
2085 // finds the index of the first occurence of the element equal to a given value in an array of floats
2086 // returns -1 if none found
2087 public static int indexOf(float[] array, float value){
2088 int index = -1;
2089 boolean test = true;
2090 int counter = 0;
2091 while(test){
2092 if(array[counter]==value){
2093 index = counter;
2094 test = false;
2095 }
2096 else{
2097 counter++;
2098 if(counter>=array.length)test = false;
2099 }
2100 }
2101 return index;
2102 }
2103
2104 // finds the index of the first occurence of the element equal to a given value in an array of longs
2105 // returns -1 if none found
2106 public static int indexOf(long[] array, long value){
2107 int index = -1;
2108 boolean test = true;
2109 int counter = 0;
2110 while(test){
2111 if(array[counter]==value){
2112 index = counter;
2113 test = false;
2114 }
2115 else{
2116 counter++;
2117 if(counter>=array.length)test = false;
2118 }
2119 }
2120 return index;
2121 }
2122
2123 // finds the index of the first occurence of the element equal to a given value in an array of ints
2124 // returns -1 if none found
2125 public static int indexOf(int[] array, int value){
2126 int index = -1;
2127 boolean test = true;
2128 int counter = 0;
2129 while(test){
2130 if(array[counter]==value){
2131 index = counter;
2132 test = false;
2133 }
2134 else{
2135 counter++;
2136 if(counter>=array.length)test = false;
2137 }
2138 }
2139 return index;
2140 }
2141
2142 // finds the index of the first occurence of the element equal to a given value in an array of bytes
2143 // returns -1 if none found
2144 public static int indexOf(byte[] array, byte value){
2145 int index = -1;
2146 boolean test = true;
2147 int counter = 0;
2148 while(test){
2149 if(array[counter]==value){
2150 index = counter;
2151 test = false;
2152 }
2153 else{
2154 counter++;
2155 if(counter>=array.length)test = false;
2156 }
2157 }
2158 return index;
2159 }
2160
2161 // finds the index of the first occurence of the element equal to a given value in an array of shorts
2162 // returns -1 if none found
2163 public static int indexOf(short[] array, short value){
2164 int index = -1;
2165 boolean test = true;
2166 int counter = 0;
2167 while(test){
2168 if(array[counter]==value){
2169 index = counter;
2170 test = false;
2171 }
2172 else{
2173 counter++;
2174 if(counter>=array.length)test = false;
2175 }
2176 }
2177 return index;
2178 }
2179
2180 // finds the index of the first occurence of the element equal to a given value in an array of chars
2181 // returns -1 if none found
2182 public static int indexOf(char[] array, char value){
2183 int index = -1;
2184 boolean test = true;
2185 int counter = 0;
2186 while(test){
2187 if(array[counter]==value){
2188 index = counter;
2189 test = false;
2190 }
2191 else{
2192 counter++;
2193 if(counter>=array.length)test = false;
2194 }
2195 }
2196 return index;
2197 }
2198
2199 // finds the index of the first occurence of the element equal to a given value in an array of Strings
2200 // returns -1 if none found
2201 public static int indexOf(String[] array, String value){
2202 int index = -1;
2203 boolean test = true;
2204 int counter = 0;
2205 while(test){
2206 if(array[counter].equals(value)){
2207 index = counter;
2208 test = false;
2209 }
2210 else{
2211 counter++;
2212 if(counter>=array.length)test = false;
2213 }
2214 }
2215 return index;
2216 }
2217
2218 // finds the index of the first occurence of the element equal to a given value in an array of Objects
2219 // returns -1 if none found
2220 public static int indexOf(Object[] array, Object value){
2221 int index = -1;
2222 boolean test = true;
2223 int counter = 0;
2224 while(test){
2225 if(array[counter].equals(value)){
2226 index = counter;
2227 test = false;
2228 }
2229 else{
2230 counter++;
2231 if(counter>=array.length)test = false;
2232 }
2233 }
2234 return index;
2235 }
2236
2237 // FIND VALUE OF AND FIND VALUE OF ARRAY ELEMENTS NEAREST TO A VALUE (deprecated - see ArryMaths class)
2238 // finds the value of nearest element value in array to the argument value
2239 public static double nearestElementValue(double[] array, double value){
2240 double diff = Math.abs(array[0] - value);
2241 double nearest = array[0];
2242 for(int i=1; i<array.length; i++){
2243 if(Math.abs(array[i] - value)<diff){
2244 diff = Math.abs(array[i] - value);
2245 nearest = array[i];
2246 }
2247 }
2248 return nearest;
2249 }
2250
2251 // finds the index of nearest element value in array to the argument value
2252 public static int nearestElementIndex(double[] array, double value){
2253 double diff = Math.abs(array[0] - value);
2254 int nearest = 0;
2255 for(int i=1; i<array.length; i++){
2256 if(Math.abs(array[i] - value)<diff){
2257 diff = Math.abs(array[i] - value);
2258 nearest = i;
2259 }
2260 }
2261 return nearest;
2262 }
2263
2264 // finds the value of nearest lower element value in array to the argument value
2265 public static double nearestLowerElementValue(double[] array, double value){
2266 double diff0 = 0.0D;
2267 double diff1 = 0.0D;
2268 double nearest = 0.0D;
2269 int ii = 0;
2270 boolean test = true;
2271 double min = array[0];
2272 while(test){
2273 if(array[ii]<min)min = array[ii];
2274 if((value - array[ii])>=0.0D){
2275 diff0 = value - array[ii];
2276 nearest = array[ii];
2277 test = false;
2278 }
2279 else{
2280 ii++;
2281 if(ii>array.length-1){
2282 nearest = min;
2283 diff0 = min - value;
2284 test = false;
2285 }
2286 }
2287 }
2288 for(int i=0; i<array.length; i++){
2289 diff1 = value - array[i];
2290 if(diff1>=0.0D && diff1<diff0 ){
2291 diff0 = diff1;
2292 nearest = array[i];
2293 }
2294 }
2295 return nearest;
2296 }
2297
2298 // finds the index of nearest lower element value in array to the argument value
2299 public static int nearestLowerElementIndex(double[] array, double value){
2300 double diff0 = 0.0D;
2301 double diff1 = 0.0D;
2302 int nearest = 0;
2303 int ii = 0;
2304 boolean test = true;
2305 double min = array[0];
2306 int minI = 0;
2307 while(test){
2308 if(array[ii]<min){
2309 min = array[ii];
2310 minI = ii;
2311 }
2312 if((value - array[ii])>=0.0D){
2313 diff0 = value - array[ii];
2314 nearest = ii;
2315 test = false;
2316 }
2317 else{
2318 ii++;
2319 if(ii>array.length-1){
2320 nearest = minI;
2321 diff0 = min - value;
2322 test = false;
2323 }
2324 }
2325 }
2326 for(int i=0; i<array.length; i++){
2327 diff1 = value - array[i];
2328 if(diff1>=0.0D && diff1<diff0 ){
2329 diff0 = diff1;
2330 nearest = i;
2331 }
2332 }
2333 return nearest;
2334 }
2335
2336 // finds the value of nearest higher element value in array to the argument value
2337 public static double nearestHigherElementValue(double[] array, double value){
2338 double diff0 = 0.0D;
2339 double diff1 = 0.0D;
2340 double nearest = 0.0D;
2341 int ii = 0;
2342 boolean test = true;
2343 double max = array[0];
2344 while(test){
2345 if(array[ii]>max)max = array[ii];
2346 if((array[ii] - value )>=0.0D){
2347 diff0 = value - array[ii];
2348 nearest = array[ii];
2349 test = false;
2350 }
2351 else{
2352 ii++;
2353 if(ii>array.length-1){
2354 nearest = max;
2355 diff0 = value - max;
2356 test = false;
2357 }
2358 }
2359 }
2360 for(int i=0; i<array.length; i++){
2361 diff1 = array[i]- value;
2362 if(diff1>=0.0D && diff1<diff0 ){
2363 diff0 = diff1;
2364 nearest = array[i];
2365 }
2366 }
2367 return nearest;
2368 }
2369
2370 // finds the index of nearest higher element value in array to the argument value
2371 public static int nearestHigherElementIndex(double[] array, double value){
2372 double diff0 = 0.0D;
2373 double diff1 = 0.0D;
2374 int nearest = 0;
2375 int ii = 0;
2376 boolean test = true;
2377 double max = array[0];
2378 int maxI = 0;
2379 while(test){
2380 if(array[ii]>max){
2381 max = array[ii];
2382 maxI = ii;
2383 }
2384 if((array[ii] - value )>=0.0D){
2385 diff0 = value - array[ii];
2386 nearest = ii;
2387 test = false;
2388 }
2389 else{
2390 ii++;
2391 if(ii>array.length-1){
2392 nearest = maxI;
2393 diff0 = value - max;
2394 test = false;
2395 }
2396 }
2397 }
2398 for(int i=0; i<array.length; i++){
2399 diff1 = array[i]- value;
2400 if(diff1>=0.0D && diff1<diff0 ){
2401 diff0 = diff1;
2402 nearest = i;
2403 }
2404 }
2405 return nearest;
2406 }
2407
2408
2409 // finds the value of nearest element value in array to the argument value
2410 public static int nearestElementValue(int[] array, int value){
2411 int diff = Math.abs(array[0] - value);
2412 int nearest = array[0];
2413 for(int i=1; i<array.length; i++){
2414 if(Math.abs(array[i] - value)<diff){
2415 diff = Math.abs(array[i] - value);
2416 nearest = array[i];
2417 }
2418 }
2419 return nearest;
2420 }
2421
2422 // finds the index of nearest element value in array to the argument value
2423 public static int nearestElementIndex(int[] array, int value){
2424 int diff = Math.abs(array[0] - value);
2425 int nearest = 0;
2426 for(int i=1; i<array.length; i++){
2427 if(Math.abs(array[i] - value)<diff){
2428 diff = Math.abs(array[i] - value);
2429 nearest = i;
2430 }
2431 }
2432 return nearest;
2433 }
2434
2435 // finds the value of nearest lower element value in array to the argument value
2436 public static int nearestLowerElementValue(int[] array, int value){
2437 int diff0 = 0;
2438 int diff1 = 0;
2439 int nearest = 0;
2440 int ii = 0;
2441 boolean test = true;
2442 int min = array[0];
2443 while(test){
2444 if(array[ii]<min)min = array[ii];
2445 if((value - array[ii])>=0){
2446 diff0 = value - array[ii];
2447 nearest = array[ii];
2448 test = false;
2449 }
2450 else{
2451 ii++;
2452 if(ii>array.length-1){
2453 nearest = min;
2454 diff0 = min - value;
2455 test = false;
2456 }
2457 }
2458 }
2459 for(int i=0; i<array.length; i++){
2460 diff1 = value - array[i];
2461 if(diff1>=0 && diff1<diff0 ){
2462 diff0 = diff1;
2463 nearest = array[i];
2464 }
2465 }
2466 return nearest;
2467 }
2468
2469 // finds the index of nearest lower element value in array to the argument value
2470 public static int nearestLowerElementIndex(int[] array, int value){
2471 int diff0 = 0;
2472 int diff1 = 0;
2473 int nearest = 0;
2474 int ii = 0;
2475 boolean test = true;
2476 int min = array[0];
2477 int minI = 0;
2478 while(test){
2479 if(array[ii]<min){
2480 min = array[ii];
2481 minI = ii;
2482 }
2483 if((value - array[ii])>=0){
2484 diff0 = value - array[ii];
2485 nearest = ii;
2486 test = false;
2487 }
2488 else{
2489 ii++;
2490 if(ii>array.length-1){
2491 nearest = minI;
2492 diff0 = min - value;
2493 test = false;
2494 }
2495 }
2496 }
2497 for(int i=0; i<array.length; i++){
2498 diff1 = value - array[i];
2499 if(diff1>=0 && diff1<diff0 ){
2500 diff0 = diff1;
2501 nearest = i;
2502 }
2503 }
2504 return nearest;
2505 }
2506
2507 // finds the value of nearest higher element value in array to the argument value
2508 public static int nearestHigherElementValue(int[] array, int value){
2509 int diff0 = 0;
2510 int diff1 = 0;
2511 int nearest = 0;
2512 int ii = 0;
2513 boolean test = true;
2514 int max = array[0];
2515 while(test){
2516 if(array[ii]>max)max = array[ii];
2517 if((array[ii] - value )>=0){
2518 diff0 = value - array[ii];
2519 nearest = array[ii];
2520 test = false;
2521 }
2522 else{
2523 ii++;
2524 if(ii>array.length-1){
2525 nearest = max;
2526 diff0 = value - max;
2527 test = false;
2528 }
2529 }
2530 }
2531 for(int i=0; i<array.length; i++){
2532 diff1 = array[i]- value;
2533 if(diff1>=0 && diff1<diff0 ){
2534 diff0 = diff1;
2535 nearest = array[i];
2536 }
2537 }
2538 return nearest;
2539 }
2540
2541 // finds the index of nearest higher element value in array to the argument value
2542 public static int nearestHigherElementIndex(int[] array, int value){
2543 int diff0 = 0;
2544 int diff1 = 0;
2545 int nearest = 0;
2546 int ii = 0;
2547 boolean test = true;
2548 int max = array[0];
2549 int maxI = 0;
2550 while(test){
2551 if(array[ii]>max){
2552 max = array[ii];
2553 maxI = ii;
2554 }
2555 if((array[ii] - value )>=0){
2556 diff0 = value - array[ii];
2557 nearest = ii;
2558 test = false;
2559 }
2560 else{
2561 ii++;
2562 if(ii>array.length-1){
2563 nearest = maxI;
2564 diff0 = value - max;
2565 test = false;
2566 }
2567 }
2568 }
2569 for(int i=0; i<array.length; i++){
2570 diff1 = array[i]- value;
2571 if(diff1>=0 && diff1<diff0 ){
2572 diff0 = diff1;
2573 nearest = i;
2574 }
2575 }
2576 return nearest;
2577 }
2578
2579 // SUM OF ALL ELEMENTS (deprecated - see ArryMaths class)
2580 // Sum of all array elements - double array
2581 public static double arraySum(double[]array){
2582 double sum = 0.0D;
2583 for(double i:array)sum += i;
2584 return sum;
2585 }
2586
2587 // Sum of all array elements - float array
2588 public static float arraySum(float[]array){
2589 float sum = 0.0F;
2590 for(float i:array)sum += i;
2591 return sum;
2592 }
2593
2594 // Sum of all array elements - int array
2595 public static int arraySum(int[]array){
2596 int sum = 0;
2597 for(int i:array)sum += i;
2598 return sum;
2599 }
2600
2601 // Sum of all array elements - long array
2602 public static long arraySum(long[]array){
2603 long sum = 0L;
2604 for(long i:array)sum += i;
2605 return sum;
2606 }
2607
2608 // Sum of all positive array elements - long array
2609 public static long arrayPositiveElementsSum(long[]array){
2610 long sum = 0L;
2611 for(long i:array)if(i>0)sum += i;
2612 return sum;
2613 }
2614
2615
2616 // PRODUCT OF ALL ELEMENTS (deprecated - see ArryMaths class)
2617 // Product of all array elements - double array
2618 public static double arrayProduct(double[]array){
2619 double product = 1.0D;
2620 for(double i:array)product *= i;
2621 return product;
2622 }
2623
2624 // Product of all array elements - float array
2625 public static float arrayProduct(float[]array){
2626 float product = 1.0F;
2627 for(float i:array)product *= i;
2628 return product;
2629 }
2630
2631 // Product of all array elements - int array
2632 public static int arrayProduct(int[]array){
2633 int product = 1;
2634 for(int i:array)product *= i;
2635 return product;
2636 }
2637
2638 // Product of all array elements - long array
2639 public static long arrayProduct(long[]array){
2640 long product = 1L;
2641 for(long i:array)product *= i;
2642 return product;
2643 }
2644
2645 // CONCATENATE TWO ARRAYS (deprecated - see ArryMaths class)
2646 // Concatenate two double arrays
2647 public static double[] concatenate(double[] aa, double[] bb){
2648 int aLen = aa.length;
2649 int bLen = bb.length;
2650 int cLen = aLen + bLen;
2651 double[] cc = new double[cLen];
2652 for(int i=0; i<aLen; i++){
2653 cc[i] = aa[i];
2654 }
2655 for(int i=0; i<bLen; i++){
2656 cc[i+aLen] = bb[i];
2657 }
2658 return cc;
2659 }
2660
2661 // Concatenate two float arrays
2662 public static float[] concatenate(float[] aa, float[] bb){
2663 int aLen = aa.length;
2664 int bLen = bb.length;
2665 int cLen = aLen + bLen;
2666 float[] cc = new float[cLen];
2667 for(int i=0; i<aLen; i++){
2668 cc[i] = aa[i];
2669 }
2670 for(int i=0; i<bLen; i++){
2671 cc[i+aLen] = bb[i];
2672 }
2673
2674 return cc;
2675 }
2676
2677 // Concatenate two int arrays
2678 public static int[] concatenate(int[] aa, int[] bb){
2679 int aLen = aa.length;
2680 int bLen = bb.length;
2681 int cLen = aLen + bLen;
2682 int[] cc = new int[cLen];
2683 for(int i=0; i<aLen; i++){
2684 cc[i] = aa[i];
2685 }
2686 for(int i=0; i<bLen; i++){
2687 cc[i+aLen] = bb[i];
2688 }
2689
2690 return cc;
2691 }
2692
2693 // Concatenate two long arrays
2694 public static long[] concatenate(long[] aa, long[] bb){
2695 int aLen = aa.length;
2696 int bLen = bb.length;
2697 int cLen = aLen + bLen;
2698 long[] cc = new long[cLen];
2699 for(int i=0; i<aLen; i++){
2700 cc[i] = aa[i];
2701 }
2702 for(int i=0; i<bLen; i++){
2703 cc[i+aLen] = bb[i];
2704 }
2705
2706 return cc;
2707 }
2708
2709 // Concatenate two short arrays
2710 public static short[] concatenate(short[] aa, short[] bb){
2711 int aLen = aa.length;
2712 int bLen = bb.length;
2713 int cLen = aLen + bLen;
2714 short[] cc = new short[cLen];
2715 for(int i=0; i<aLen; i++){
2716 cc[i] = aa[i];
2717 }
2718 for(int i=0; i<bLen; i++){
2719 cc[i+aLen] = bb[i];
2720 }
2721 return cc;
2722 }
2723
2724 // Concatenate two byte arrays
2725 public static byte[] concatenate(byte[] aa, byte[] bb){
2726 int aLen = aa.length;
2727 int bLen = bb.length;
2728 int cLen = aLen + bLen;
2729 byte[] cc = new byte[cLen];
2730 for(int i=0; i<aLen; i++){
2731 cc[i] = aa[i];
2732 }
2733 for(int i=0; i<bLen; i++){
2734 cc[i+aLen] = bb[i];
2735 }
2736
2737 return cc;
2738 }
2739
2740 // Concatenate two char arrays
2741 public static char[] concatenate(char[] aa, char[] bb){
2742 int aLen = aa.length;
2743 int bLen = bb.length;
2744 int cLen = aLen + bLen;
2745 char[] cc = new char[cLen];
2746 for(int i=0; i<aLen; i++){
2747 cc[i] = aa[i];
2748 }
2749 for(int i=0; i<bLen; i++){
2750 cc[i+aLen] = bb[i];
2751 }
2752
2753 return cc;
2754 }
2755
2756 // Concatenate two String arrays
2757 public static String[] concatenate(String[] aa, String[] bb){
2758 int aLen = aa.length;
2759 int bLen = bb.length;
2760 int cLen = aLen + bLen;
2761 String[] cc = new String[cLen];
2762 for(int i=0; i<aLen; i++){
2763 cc[i] = aa[i];
2764 }
2765 for(int i=0; i<bLen; i++){
2766 cc[i+aLen] = bb[i];
2767 }
2768
2769 return cc;
2770 }
2771
2772 // Concatenate two Object arrays
2773 public static Object[] concatenate(Object[] aa, Object[] bb){
2774 int aLen = aa.length;
2775 int bLen = bb.length;
2776 int cLen = aLen + bLen;
2777 Object[] cc = new Object[cLen];
2778 for(int i=0; i<aLen; i++){
2779 cc[i] = aa[i];
2780 }
2781 for(int i=0; i<bLen; i++){
2782 cc[i+aLen] = bb[i];
2783 }
2784
2785 return cc;
2786 }
2787
2788 // RECAST ARRAY TYPE (deprecated - see Conv class)
2789 // recast an array of float as doubles
2790 public static double[] floatTOdouble(float[] aa){
2791 int n = aa.length;
2792 double[] bb = new double[n];
2793 for(int i=0; i<n; i++){
2794 bb[i] = (double)aa[i];
2795 }
2796 return bb;
2797 }
2798
2799 // recast an array of int as double
2800 public static double[] intTOdouble(int[] aa){
2801 int n = aa.length;
2802 double[] bb = new double[n];
2803 for(int i=0; i<n; i++){
2804 bb[i] = (double)aa[i];
2805 }
2806 return bb;
2807 }
2808
2809 // recast an array of int as float
2810 public static float[] intTOfloat(int[] aa){
2811 int n = aa.length;
2812 float[] bb = new float[n];
2813 for(int i=0; i<n; i++){
2814 bb[i] = (float)aa[i];
2815 }
2816 return bb;
2817 }
2818
2819 // recast an array of int as long
2820 public static long[] intTOlong(int[] aa){
2821 int n = aa.length;
2822 long[] bb = new long[n];
2823 for(int i=0; i<n; i++){
2824 bb[i] = (long)aa[i];
2825 }
2826 return bb;
2827 }
2828
2829 // recast an array of long as double
2830 // BEWARE POSSIBLE LOSS OF PRECISION
2831 public static double[] longTOdouble(long[] aa){
2832 int n = aa.length;
2833 double[] bb = new double[n];
2834 for(int i=0; i<n; i++){
2835 bb[i] = (double)aa[i];
2836 }
2837 return bb;
2838 }
2839
2840 // recast an array of long as float
2841 // BEWARE POSSIBLE LOSS OF PRECISION
2842 public static float[] longTOfloat(long[] aa){
2843 int n = aa.length;
2844 float[] bb = new float[n];
2845 for(int i=0; i<n; i++){
2846 bb[i] = (float)aa[i];
2847 }
2848 return bb;
2849 }
2850
2851 // recast an array of short as double
2852 public static double[] shortTOdouble(short[] aa){
2853 int n = aa.length;
2854 double[] bb = new double[n];
2855 for(int i=0; i<n; i++){
2856 bb[i] = (double)aa[i];
2857 }
2858 return bb;
2859 }
2860
2861 // recast an array of short as float
2862 public static float[] shortTOfloat(short[] aa){
2863 int n = aa.length;
2864 float[] bb = new float[n];
2865 for(int i=0; i<n; i++){
2866 bb[i] = (float)aa[i];
2867 }
2868 return bb;
2869 }
2870
2871 // recast an array of short as long
2872 public static long[] shortTOlong(short[] aa){
2873 int n = aa.length;
2874 long[] bb = new long[n];
2875 for(int i=0; i<n; i++){
2876 bb[i] = (long)aa[i];
2877 }
2878 return bb;
2879 }
2880
2881 // recast an array of short as int
2882 public static int[] shortTOint(short[] aa){
2883 int n = aa.length;
2884 int[] bb = new int[n];
2885 for(int i=0; i<n; i++){
2886 bb[i] = (int)aa[i];
2887 }
2888 return bb;
2889 }
2890
2891 // recast an array of byte as double
2892 public static double[] byteTOdouble(byte[] aa){
2893 int n = aa.length;
2894 double[] bb = new double[n];
2895 for(int i=0; i<n; i++){
2896 bb[i] = (int)aa[i];
2897 }
2898 return bb;
2899 }
2900
2901 // recast an array of byte as float
2902 public static float[] byteTOfloat(byte[] aa){
2903 int n = aa.length;
2904 float[] bb = new float[n];
2905 for(int i=0; i<n; i++){
2906 bb[i] = (float)aa[i];
2907 }
2908 return bb;
2909 }
2910
2911 // recast an array of byte as long
2912 public static long[] byteTOlong(byte[] aa){
2913 int n = aa.length;
2914 long[] bb = new long[n];
2915 for(int i=0; i<n; i++){
2916 bb[i] = (long)aa[i];
2917 }
2918 return bb;
2919 }
2920
2921 // recast an array of byte as int
2922 public static int[] byteTOint(byte[] aa){
2923 int n = aa.length;
2924 int[] bb = new int[n];
2925 for(int i=0; i<n; i++){
2926 bb[i] = (int)aa[i];
2927 }
2928 return bb;
2929 }
2930
2931 // recast an array of byte as short
2932 public static short[] byteTOshort(byte[] aa){
2933 int n = aa.length;
2934 short[] bb = new short[n];
2935 for(int i=0; i<n; i++){
2936 bb[i] = (short)aa[i];
2937 }
2938 return bb;
2939 }
2940
2941 // recast an array of double as int
2942 // BEWARE OF LOSS OF PRECISION
2943 public static int[] doubleTOint(double[] aa){
2944 int n = aa.length;
2945 int[] bb = new int[n];
2946 for(int i=0; i<n; i++){
2947 bb[i] = (int)aa[i];
2948 }
2949 return bb;
2950 }
2951
2952 // PRINT ARRAY TO SCREEN (deprecated - see PrintToScreen class)
2953 // print an array of doubles to screen
2954 // No line returns except at the end
2955 public static void print(double[] aa){
2956 for(int i=0; i<aa.length; i++){
2957 System.out.print(aa[i]+" ");
2958 }
2959 System.out.println();
2960 }
2961
2962 // print an array of doubles to screen
2963 // with line returns
2964 public static void println(double[] aa){
2965 for(int i=0; i<aa.length; i++){
2966 System.out.println(aa[i]+" ");
2967 }
2968 }
2969
2970 // print an array of floats to screen
2971 // No line returns except at the end
2972 public static void print(float[] aa){
2973 for(int i=0; i<aa.length; i++){
2974 System.out.print(aa[i]+" ");
2975 }
2976 System.out.println();
2977 }
2978
2979 // print an array of floats to screen
2980 // with line returns
2981 public static void println(float[] aa){
2982 for(int i=0; i<aa.length; i++){
2983 System.out.println(aa[i]+" ");
2984 }
2985 }
2986
2987 // print an array of ints to screen
2988 // No line returns except at the end
2989 public static void print(int[] aa){
2990 for(int i=0; i<aa.length; i++){
2991 System.out.print(aa[i]+" ");
2992 }
2993 System.out.println();
2994 }
2995
2996 // print an array of ints to screen
2997 // with line returns
2998 public static void println(int[] aa){
2999 for(int i=0; i<aa.length; i++){
3000 System.out.println(aa[i]+" ");
3001 }
3002 }
3003
3004 // print an array of longs to screen
3005 // No line returns except at the end
3006 public static void print(long[] aa){
3007 for(int i=0; i<aa.length; i++){
3008 System.out.print(aa[i]+" ");
3009 }
3010 System.out.println();
3011 }
3012
3013 // print an array of longs to screen
3014 // with line returns
3015 public static void println(long[] aa){
3016 for(int i=0; i<aa.length; i++){
3017 System.out.println(aa[i]+" ");
3018 }
3019 }
3020
3021 // print an array of char to screen
3022 // No line returns except at the end
3023 public static void print(char[] aa){
3024 for(int i=0; i<aa.length; i++){
3025 System.out.print(aa[i]+" ");
3026 }
3027 System.out.println();
3028 }
3029
3030 // print an array of char to screen
3031 // with line returns
3032 public static void println(char[] aa){
3033 for(int i=0; i<aa.length; i++){
3034 System.out.println(aa[i]+" ");
3035 }
3036 }
3037
3038 // print an array of String to screen
3039 // No line returns except at the end
3040 public static void print(String[] aa){
3041 for(int i=0; i<aa.length; i++){
3042 System.out.print(aa[i]+" ");
3043 }
3044 System.out.println();
3045 }
3046
3047 // print an array of Strings to screen
3048 // with line returns
3049 public static void println(String[] aa){
3050 for(int i=0; i<aa.length; i++){
3051 System.out.println(aa[i]+" ");
3052 }
3053 }
3054
3055 // print an array of shorts to screen
3056 // No line returns except at the end
3057 public static void print(short[] aa){
3058 for(int i=0; i<aa.length; i++){
3059 System.out.print(aa[i]+" ");
3060 }
3061 System.out.println();
3062 }
3063
3064 // print an array of shorts to screen
3065 // with line returns
3066 public static void println(short[] aa){
3067 for(int i=0; i<aa.length; i++){
3068 System.out.println(aa[i]+" ");
3069 }
3070 }
3071
3072 // print an array of bytes to screen
3073 // No line returns except at the end
3074 public static void print(byte[] aa){
3075 for(int i=0; i<aa.length; i++){
3076 System.out.print(aa[i]+" ");
3077 }
3078 System.out.println();
3079 }
3080
3081 // print an array of bytes to screen
3082 // with line returns
3083 public static void println(byte[] aa){
3084 for(int i=0; i<aa.length; i++){
3085 System.out.println(aa[i]+" ");
3086 }
3087 }
3088
3089
3090 // print a 2D array of doubles to screen
3091 public static void print(double[][] aa){
3092 for(int i=0; i<aa.length; i++){
3093 Fmath.print(aa[i]);
3094 }
3095 }
3096
3097 // SORT ELEMENTS OF ARRAY (deprecated - see ArryMaths class)
3098 // sort elements in an array of doubles into ascending order
3099 // using selection sort method
3100 // returns Vector containing the original array, the sorted array
3101 // and an array of the indices of the sorted array
3102 public static Vector<Object> selectSortVector(double[] aa){
3103 ArrayList<Object> list = Fmath.selectSortArrayList(aa);
3104 Vector<Object> ret = null;
3105 if(list!=null){
3106 int n = list.size();
3107 ret = new Vector<Object>(n);
3108 for(int i=0; i<n; i++)ret.addElement(list.get(i));
3109 }
3110 return ret;
3111 }
3112
3113
3114 // sort elements in an array of doubles into ascending order
3115 // using selection sort method
3116 // returns ArrayList containing the original array, the sorted array
3117 // and an array of the indices of the sorted array
3118 public static ArrayList<Object> selectSortArrayList(double[] aa){
3119 int index = 0;
3120 int lastIndex = -1;
3121 int n = aa.length;
3122 double holdb = 0.0D;
3123 int holdi = 0;
3124 double[] bb = new double[n];
3125 int[] indices = new int[n];
3126 for(int i=0; i<n; i++){
3127 bb[i]=aa[i];
3128 indices[i]=i;
3129 }
3130
3131 while(lastIndex != n-1){
3132 index = lastIndex+1;
3133 for(int i=lastIndex+2; i<n; i++){
3134 if(bb[i]<bb[index]){
3135 index=i;
3136 }
3137 }
3138 lastIndex++;
3139 holdb=bb[index];
3140 bb[index]=bb[lastIndex];
3141 bb[lastIndex]=holdb;
3142 holdi=indices[index];
3143 indices[index]=indices[lastIndex];
3144 indices[lastIndex]=holdi;
3145 }
3146 ArrayList<Object> arrayl = new ArrayList<Object>();
3147 arrayl.add(aa);
3148 arrayl.add(bb);
3149 arrayl.add(indices);
3150 return arrayl;
3151 }
3152
3153 // sort elements in an array of doubles into ascending order
3154 // using selection sort method
3155 public static double[] selectionSort(double[] aa){
3156 int index = 0;
3157 int lastIndex = -1;
3158 int n = aa.length;
3159 double hold = 0.0D;
3160 double[] bb = new double[n];
3161 for(int i=0; i<n; i++){
3162 bb[i]=aa[i];
3163 }
3164
3165 while(lastIndex != n-1){
3166 index = lastIndex+1;
3167 for(int i=lastIndex+2; i<n; i++){
3168 if(bb[i]<bb[index]){
3169 index=i;
3170 }
3171 }
3172 lastIndex++;
3173 hold=bb[index];
3174 bb[index]=bb[lastIndex];
3175 bb[lastIndex]=hold;
3176 }
3177 return bb;
3178 }
3179
3180 // sort elements in an array of floats into ascending order
3181 // using selection sort method
3182 public static float[] selectionSort(float[] aa){
3183 int index = 0;
3184 int lastIndex = -1;
3185 int n = aa.length;
3186 float hold = 0.0F;
3187 float[] bb = new float[n];
3188 for(int i=0; i<n; i++){
3189 bb[i]=aa[i];
3190 }
3191
3192 while(lastIndex != n-1){
3193 index = lastIndex+1;
3194 for(int i=lastIndex+2; i<n; i++){
3195 if(bb[i]<bb[index]){
3196 index=i;
3197 }
3198 }
3199 lastIndex++;
3200 hold=bb[index];
3201 bb[index]=bb[lastIndex];
3202 bb[lastIndex]=hold;
3203 }
3204 return bb;
3205 }
3206
3207 // sort elements in an array of ints into ascending order
3208 // using selection sort method
3209 public static int[] selectionSort(int[] aa){
3210 int index = 0;
3211 int lastIndex = -1;
3212 int n = aa.length;
3213 int hold = 0;
3214 int[] bb = new int[n];
3215 for(int i=0; i<n; i++){
3216 bb[i]=aa[i];
3217 }
3218
3219 while(lastIndex != n-1){
3220 index = lastIndex+1;
3221 for(int i=lastIndex+2; i<n; i++){
3222 if(bb[i]<bb[index]){
3223 index=i;
3224 }
3225 }
3226 lastIndex++;
3227 hold=bb[index];
3228 bb[index]=bb[lastIndex];
3229 bb[lastIndex]=hold;
3230 }
3231 return bb;
3232 }
3233
3234 // sort elements in an array of longs into ascending order
3235 // using selection sort method
3236 public static long[] selectionSort(long[] aa){
3237 int index = 0;
3238 int lastIndex = -1;
3239 int n = aa.length;
3240 long hold = 0L;
3241 long[] bb = new long[n];
3242 for(int i=0; i<n; i++){
3243 bb[i]=aa[i];
3244 }
3245
3246 while(lastIndex != n-1){
3247 index = lastIndex+1;
3248 for(int i=lastIndex+2; i<n; i++){
3249 if(bb[i]<bb[index]){
3250 index=i;
3251 }
3252 }
3253 lastIndex++;
3254 hold=bb[index];
3255 bb[index]=bb[lastIndex];
3256 bb[lastIndex]=hold;
3257 }
3258 return bb;
3259 }
3260
3261 // sort elements in an array of doubles into ascending order
3262 // using selection sort method
3263 // aa - the original array - not altered
3264 // bb - the sorted array
3265 // indices - an array of the original indices of the sorted array
3266 public static void selectionSort(double[] aa, double[] bb, int[] indices){
3267 int index = 0;
3268 int lastIndex = -1;
3269 int n = aa.length;
3270 double holdb = 0.0D;
3271 int holdi = 0;
3272 for(int i=0; i<n; i++){
3273 bb[i]=aa[i];
3274 indices[i]=i;
3275 }
3276
3277 while(lastIndex != n-1){
3278 index = lastIndex+1;
3279 for(int i=lastIndex+2; i<n; i++){
3280 if(bb[i]<bb[index]){
3281 index=i;
3282 }
3283 }
3284 lastIndex++;
3285 holdb=bb[index];
3286 bb[index]=bb[lastIndex];
3287 bb[lastIndex]=holdb;
3288 holdi=indices[index];
3289 indices[index]=indices[lastIndex];
3290 indices[lastIndex]=holdi;
3291 }
3292 }
3293
3294 // sort the elements of an array of doubles into ascending order with matching switches in an array of the length
3295 // using selection sort method
3296 // array determining the order is the first argument
3297 // matching array is the second argument
3298 // sorted arrays returned as third and fourth arguments respectively
3299 public static void selectionSort(double[] aa, double[] bb, double[] cc, double[] dd){
3300 int index = 0;
3301 int lastIndex = -1;
3302 int n = aa.length;
3303 int m = bb.length;
3304 if(n!=m)throw new IllegalArgumentException("First argument array, aa, (length = " + n + ") and the second argument array, bb, (length = " + m + ") should be the same length");
3305 int nn = cc.length;
3306 if(nn<n)throw new IllegalArgumentException("The third argument array, cc, (length = " + nn + ") should be at least as long as the first argument array, aa, (length = " + n + ")");
3307 int mm = dd.length;
3308 if(mm<m)throw new IllegalArgumentException("The fourth argument array, dd, (length = " + mm + ") should be at least as long as the second argument array, bb, (length = " + m + ")");
3309
3310 double holdx = 0.0D;
3311 double holdy = 0.0D;
3312
3313
3314 for(int i=0; i<n; i++){
3315 cc[i]=aa[i];
3316 dd[i]=bb[i];
3317 }
3318
3319 while(lastIndex != n-1){
3320 index = lastIndex+1;
3321 for(int i=lastIndex+2; i<n; i++){
3322 if(cc[i]<cc[index]){
3323 index=i;
3324 }
3325 }
3326 lastIndex++;
3327 holdx=cc[index];
3328 cc[index]=cc[lastIndex];
3329 cc[lastIndex]=holdx;
3330 holdy=dd[index];
3331 dd[index]=dd[lastIndex];
3332 dd[lastIndex]=holdy;
3333 }
3334 }
3335
3336 // sort the elements of an array of floats into ascending order with matching switches in an array of the length
3337 // using selection sort method
3338 // array determining the order is the first argument
3339 // matching array is the second argument
3340 // sorted arrays returned as third and fourth arguments respectively
3341 public static void selectionSort(float[] aa, float[] bb, float[] cc, float[] dd){
3342 int index = 0;
3343 int lastIndex = -1;
3344 int n = aa.length;
3345 int m = bb.length;
3346 if(n!=m)throw new IllegalArgumentException("First argument array, aa, (length = " + n + ") and the second argument array, bb, (length = " + m + ") should be the same length");
3347 int nn = cc.length;
3348 if(nn<n)throw new IllegalArgumentException("The third argument array, cc, (length = " + nn + ") should be at least as long as the first argument array, aa, (length = " + n + ")");
3349 int mm = dd.length;
3350 if(mm<m)throw new IllegalArgumentException("The fourth argument array, dd, (length = " + mm + ") should be at least as long as the second argument array, bb, (length = " + m + ")");
3351
3352 float holdx = 0.0F;
3353 float holdy = 0.0F;
3354
3355
3356 for(int i=0; i<n; i++){
3357 cc[i]=aa[i];
3358 dd[i]=bb[i];
3359 }
3360
3361 while(lastIndex != n-1){
3362 index = lastIndex+1;
3363 for(int i=lastIndex+2; i<n; i++){
3364 if(cc[i]<cc[index]){
3365 index=i;
3366 }
3367 }
3368 lastIndex++;
3369 holdx=cc[index];
3370 cc[index]=cc[lastIndex];
3371 cc[lastIndex]=holdx;
3372 holdy=dd[index];
3373 dd[index]=dd[lastIndex];
3374 dd[lastIndex]=holdy;
3375 }
3376 }
3377
3378 // sort the elements of an longs of doubles into ascending order with matching switches in an array of the length
3379 // using selection sort method
3380 // array determining the order is the first argument
3381 // matching array is the second argument
3382 // sorted arrays returned as third and fourth arguments respectively
3383 public static void selectionSort(long[] aa, long[] bb, long[] cc, long[] dd){
3384 int index = 0;
3385 int lastIndex = -1;
3386 int n = aa.length;
3387 int m = bb.length;
3388 if(n!=m)throw new IllegalArgumentException("First argument array, aa, (length = " + n + ") and the second argument array, bb, (length = " + m + ") should be the same length");
3389 int nn = cc.length;
3390 if(nn<n)throw new IllegalArgumentException("The third argument array, cc, (length = " + nn + ") should be at least as long as the first argument array, aa, (length = " + n + ")");
3391 int mm = dd.length;
3392 if(mm<m)throw new IllegalArgumentException("The fourth argument array, dd, (length = " + mm + ") should be at least as long as the second argument array, bb, (length = " + m + ")");
3393
3394 long holdx = 0L;
3395 long holdy = 0L;
3396
3397
3398 for(int i=0; i<n; i++){
3399 cc[i]=aa[i];
3400 dd[i]=bb[i];
3401 }
3402
3403 while(lastIndex != n-1){
3404 index = lastIndex+1;
3405 for(int i=lastIndex+2; i<n; i++){
3406 if(cc[i]<cc[index]){
3407 index=i;
3408 }
3409 }
3410 lastIndex++;
3411 holdx=cc[index];
3412 cc[index]=cc[lastIndex];
3413 cc[lastIndex]=holdx;
3414 holdy=dd[index];
3415 dd[index]=dd[lastIndex];
3416 dd[lastIndex]=holdy;
3417 }
3418 }
3419
3420 // sort the elements of an array of ints into ascending order with matching switches in an array of the length
3421 // using selection sort method
3422 // array determining the order is the first argument
3423 // matching array is the second argument
3424 // sorted arrays returned as third and fourth arguments respectively
3425 public static void selectionSort(int[] aa, int[] bb, int[] cc, int[] dd){
3426 int index = 0;
3427 int lastIndex = -1;
3428 int n = aa.length;
3429 int m = bb.length;
3430 if(n!=m)throw new IllegalArgumentException("First argument array, aa, (length = " + n + ") and the second argument array, bb, (length = " + m + ") should be the same length");
3431 int nn = cc.length;
3432 if(nn<n)throw new IllegalArgumentException("The third argument array, cc, (length = " + nn + ") should be at least as long as the first argument array, aa, (length = " + n + ")");
3433 int mm = dd.length;
3434 if(mm<m)throw new IllegalArgumentException("The fourth argument array, dd, (length = " + mm + ") should be at least as long as the second argument array, bb, (length = " + m + ")");
3435
3436 int holdx = 0;
3437 int holdy = 0;
3438
3439
3440 for(int i=0; i<n; i++){
3441 cc[i]=aa[i];
3442 dd[i]=bb[i];
3443 }
3444
3445 while(lastIndex != n-1){
3446 index = lastIndex+1;
3447 for(int i=lastIndex+2; i<n; i++){
3448 if(cc[i]<cc[index]){
3449 index=i;
3450 }
3451 }
3452 lastIndex++;
3453 holdx=cc[index];
3454 cc[index]=cc[lastIndex];
3455 cc[lastIndex]=holdx;
3456 holdy=dd[index];
3457 dd[index]=dd[lastIndex];
3458 dd[lastIndex]=holdy;
3459 }
3460 }
3461
3462 // sort the elements of an array of doubles into ascending order with matching switches in an array of long of the length
3463 // using selection sort method
3464 // array determining the order is the first argument
3465 // matching array is the second argument
3466 // sorted arrays returned as third and fourth arguments respectively
3467 public static void selectionSort(double[] aa, long[] bb, double[] cc, long[] dd){
3468 int index = 0;
3469 int lastIndex = -1;
3470 int n = aa.length;
3471 int m = bb.length;
3472 if(n!=m)throw new IllegalArgumentException("First argument array, aa, (length = " + n + ") and the second argument array, bb, (length = " + m + ") should be the same length");
3473 int nn = cc.length;
3474 if(nn<n)throw new IllegalArgumentException("The third argument array, cc, (length = " + nn + ") should be at least as long as the first argument array, aa, (length = " + n + ")");
3475 int mm = dd.length;
3476 if(mm<m)throw new IllegalArgumentException("The fourth argument array, dd, (length = " + mm + ") should be at least as long as the second argument array, bb, (length = " + m + ")");
3477
3478 double holdx = 0.0D;
3479 long holdy = 0L;
3480
3481
3482 for(int i=0; i<n; i++){
3483 cc[i]=aa[i];
3484 dd[i]=bb[i];
3485 }
3486
3487 while(lastIndex != n-1){
3488 index = lastIndex+1;
3489 for(int i=lastIndex+2; i<n; i++){
3490 if(cc[i]<cc[index]){
3491 index=i;
3492 }
3493 }
3494 lastIndex++;
3495 holdx=cc[index];
3496 cc[index]=cc[lastIndex];
3497 cc[lastIndex]=holdx;
3498 holdy=dd[index];
3499 dd[index]=dd[lastIndex];
3500 dd[lastIndex]=holdy;
3501 }
3502 }
3503
3504 // sort the elements of an array of long into ascending order with matching switches in an array of double of the length
3505 // using selection sort method
3506 // array determining the order is the first argument
3507 // matching array is the second argument
3508 // sorted arrays returned as third and fourth arguments respectively
3509 public static void selectionSort(long[] aa, double[] bb, long[] cc, double[] dd){
3510 int index = 0;
3511 int lastIndex = -1;
3512 int n = aa.length;
3513 int m = bb.length;
3514 if(n!=m)throw new IllegalArgumentException("First argument array, aa, (length = " + n + ") and the second argument array, bb, (length = " + m + ") should be the same length");
3515 int nn = cc.length;
3516 if(nn<n)throw new IllegalArgumentException("The third argument array, cc, (length = " + nn + ") should be at least as long as the first argument array, aa, (length = " + n + ")");
3517 int mm = dd.length;
3518 if(mm<m)throw new IllegalArgumentException("The fourth argument array, dd, (length = " + mm + ") should be at least as long as the second argument array, bb, (length = " + m + ")");
3519
3520 long holdx = 0L;
3521 double holdy = 0.0D;
3522
3523
3524 for(int i=0; i<n; i++){
3525 cc[i]=aa[i];
3526 dd[i]=bb[i];
3527 }
3528
3529 while(lastIndex != n-1){
3530 index = lastIndex+1;
3531 for(int i=lastIndex+2; i<n; i++){
3532 if(cc[i]<cc[index]){
3533 index=i;
3534 }
3535 }
3536 lastIndex++;
3537 holdx=cc[index];
3538 cc[index]=cc[lastIndex];
3539 cc[lastIndex]=holdx;
3540 holdy=dd[index];
3541 dd[index]=dd[lastIndex];
3542 dd[lastIndex]=holdy;
3543 }
3544 }
3545
3546 // sort the elements of an array of doubles into ascending order with matching switches in an array of int of the length
3547 // using selection sort method
3548 // array determining the order is the first argument
3549 // matching array is the second argument
3550 // sorted arrays returned as third and fourth arguments respectively
3551 public static void selectionSort(double[] aa, int[] bb, double[] cc, int[] dd){
3552 int index = 0;
3553 int lastIndex = -1;
3554 int n = aa.length;
3555 int m = bb.length;
3556 if(n!=m)throw new IllegalArgumentException("First argument array, aa, (length = " + n + ") and the second argument array, bb, (length = " + m + ") should be the same length");
3557 int nn = cc.length;
3558 if(nn<n)throw new IllegalArgumentException("The third argument array, cc, (length = " + nn + ") should be at least as long as the first argument array, aa, (length = " + n + ")");
3559 int mm = dd.length;
3560 if(mm<m)throw new IllegalArgumentException("The fourth argument array, dd, (length = " + mm + ") should be at least as long as the second argument array, bb, (length = " + m + ")");
3561
3562 double holdx = 0.0D;
3563 int holdy = 0;
3564
3565
3566 for(int i=0; i<n; i++){
3567 cc[i]=aa[i];
3568 dd[i]=bb[i];
3569 }
3570
3571 while(lastIndex != n-1){
3572 index = lastIndex+1;
3573 for(int i=lastIndex+2; i<n; i++){
3574 if(cc[i]<cc[index]){
3575 index=i;
3576 }
3577 }
3578 lastIndex++;
3579 holdx=cc[index];
3580 cc[index]=cc[lastIndex];
3581 cc[lastIndex]=holdx;
3582 holdy=dd[index];
3583 dd[index]=dd[lastIndex];
3584 dd[lastIndex]=holdy;
3585 }
3586 }
3587
3588 // sort the elements of an array of int into ascending order with matching switches in an array of double of the length
3589 // using selection sort method
3590 // array determining the order is the first argument
3591 // matching array is the second argument
3592 // sorted arrays returned as third and fourth arguments respectively
3593 public static void selectionSort(int[] aa, double[] bb, int[] cc, double[] dd){
3594 int index = 0;
3595 int lastIndex = -1;
3596 int n = aa.length;
3597 int m = bb.length;
3598 if(n!=m)throw new IllegalArgumentException("First argument array, aa, (length = " + n + ") and the second argument array, bb, (length = " + m + ") should be the same length");
3599 int nn = cc.length;
3600 if(nn<n)throw new IllegalArgumentException("The third argument array, cc, (length = " + nn + ") should be at least as long as the first argument array, aa, (length = " + n + ")");
3601 int mm = dd.length;
3602 if(mm<m)throw new IllegalArgumentException("The fourth argument array, dd, (length = " + mm + ") should be at least as long as the second argument array, bb, (length = " + m + ")");
3603
3604 int holdx = 0;
3605 double holdy = 0.0D;
3606
3607
3608 for(int i=0; i<n; i++){
3609 cc[i]=aa[i];
3610 dd[i]=bb[i];
3611 }
3612
3613 while(lastIndex != n-1){
3614 index = lastIndex+1;
3615 for(int i=lastIndex+2; i<n; i++){
3616 if(cc[i]<cc[index]){
3617 index=i;
3618 }
3619 }
3620 lastIndex++;
3621 holdx=cc[index];
3622 cc[index]=cc[lastIndex];
3623 cc[lastIndex]=holdx;
3624 holdy=dd[index];
3625 dd[index]=dd[lastIndex];
3626 dd[lastIndex]=holdy;
3627 }
3628 }
3629
3630 // sort the elements of an array of long into ascending order with matching switches in an array of int of the length
3631 // using selection sort method
3632 // array determining the order is the first argument
3633 // matching array is the second argument
3634 // sorted arrays returned as third and fourth arguments respectively
3635 public static void selectionSort(long[] aa, int[] bb, long[] cc, int[] dd){
3636 int index = 0;
3637 int lastIndex = -1;
3638 int n = aa.length;
3639 int m = bb.length;
3640 if(n!=m)throw new IllegalArgumentException("First argument array, aa, (length = " + n + ") and the second argument array, bb, (length = " + m + ") should be the same length");
3641 int nn = cc.length;
3642 if(nn<n)throw new IllegalArgumentException("The third argument array, cc, (length = " + nn + ") should be at least as long as the first argument array, aa, (length = " + n + ")");
3643 int mm = dd.length;
3644 if(mm<m)throw new IllegalArgumentException("The fourth argument array, dd, (length = " + mm + ") should be at least as long as the second argument array, bb, (length = " + m + ")");
3645
3646 long holdx = 0L;
3647 int holdy = 0;
3648
3649
3650 for(int i=0; i<n; i++){
3651 cc[i]=aa[i];
3652 dd[i]=bb[i];
3653 }
3654
3655 while(lastIndex != n-1){
3656 index = lastIndex+1;
3657 for(int i=lastIndex+2; i<n; i++){
3658 if(cc[i]<cc[index]){
3659 index=i;
3660 }
3661 }
3662 lastIndex++;
3663 holdx=cc[index];
3664 cc[index]=cc[lastIndex];
3665 cc[lastIndex]=holdx;
3666 holdy=dd[index];
3667 dd[index]=dd[lastIndex];
3668 dd[lastIndex]=holdy;
3669 }
3670 }
3671
3672 // sort the elements of an array of int into ascending order with matching switches in an array of long of the length
3673 // using selection sort method
3674 // array determining the order is the first argument
3675 // matching array is the second argument
3676 // sorted arrays returned as third and fourth arguments respectively
3677 public static void selectionSort(int[] aa, long[] bb, int[] cc, long[] dd){
3678 int index = 0;
3679 int lastIndex = -1;
3680 int n = aa.length;
3681 int m = bb.length;
3682 if(n!=m)throw new IllegalArgumentException("First argument array, aa, (length = " + n + ") and the second argument array, bb, (length = " + m + ") should be the same length");
3683 int nn = cc.length;
3684 if(nn<n)throw new IllegalArgumentException("The third argument array, cc, (length = " + nn + ") should be at least as long as the first argument array, aa, (length = " + n + ")");
3685 int mm = dd.length;
3686 if(mm<m)throw new IllegalArgumentException("The fourth argument array, dd, (length = " + mm + ") should be at least as long as the second argument array, bb, (length = " + m + ")");
3687
3688 int holdx = 0;
3689 long holdy = 0L;
3690
3691
3692 for(int i=0; i<n; i++){
3693 cc[i]=aa[i];
3694 dd[i]=bb[i];
3695 }
3696
3697 while(lastIndex != n-1){
3698 index = lastIndex+1;
3699 for(int i=lastIndex+2; i<n; i++){
3700 if(cc[i]<cc[index]){
3701 index=i;
3702 }
3703 }
3704 lastIndex++;
3705 holdx=cc[index];
3706 cc[index]=cc[lastIndex];
3707 cc[lastIndex]=holdx;
3708 holdy=dd[index];
3709 dd[index]=dd[lastIndex];
3710 dd[lastIndex]=holdy;
3711 }
3712 }
3713
3714
3715 // sort elements in an array of doubles (first argument) into ascending order
3716 // using selection sort method
3717 // returns the sorted array as second argument
3718 // and an array of the indices of the sorted array as the third argument
3719 // same as corresponding selectionSort - retained for backward compatibility
3720 public static void selectSort(double[] aa, double[] bb, int[] indices){
3721 int index = 0;
3722 int lastIndex = -1;
3723 int n = aa.length;
3724 int m = bb.length;
3725 if(m<n)throw new IllegalArgumentException("The second argument array, bb, (length = " + m + ") should be at least as long as the first argument array, aa, (length = " + n + ")");
3726 int k = indices.length;
3727 if(m<n)throw new IllegalArgumentException("The third argument array, indices, (length = " + k + ") should be at least as long as the first argument array, aa, (length = " + n + ")");
3728
3729 double holdb = 0.0D;
3730 int holdi = 0;
3731 for(int i=0; i<n; i++){
3732 bb[i]=aa[i];
3733 indices[i]=i;
3734 }
3735
3736 while(lastIndex != n-1){
3737 index = lastIndex+1;
3738 for(int i=lastIndex+2; i<n; i++){
3739 if(bb[i]<bb[index]){
3740 index=i;
3741 }
3742 }
3743 lastIndex++;
3744 holdb=bb[index];
3745 bb[index]=bb[lastIndex];
3746 bb[lastIndex]=holdb;
3747 holdi=indices[index];
3748 indices[index]=indices[lastIndex];
3749 indices[lastIndex]=holdi;
3750 }
3751 }
3752
3753 // COPY A ONE DIMENSIONAL ARRAY OF double
3754 public static double[] copy(double[] array){
3755 if(array==null)return null;
3756 int n = array.length;
3757 double[] copy = new double[n];
3758 for(int i=0; i<n; i++){
3759 copy[i] = array[i];
3760 }
3761 return copy;
3762 }
3763
3764 // COPY A ONE DIMENSIONAL ARRAY OF float
3765 public static float[] copy(float[] array){
3766 if(array==null)return null;
3767 int n = array.length;
3768 float[] copy = new float[n];
3769 for(int i=0; i<n; i++){
3770 copy[i] = array[i];
3771 }
3772 return copy;
3773 }
3774
3775 // COPY A ONE DIMENSIONAL ARRAY OF int
3776 public static int[] copy(int[] array){
3777 if(array==null)return null;
3778 int n = array.length;
3779 int[] copy = new int[n];
3780 for(int i=0; i<n; i++){
3781 copy[i] = array[i];
3782 }
3783 return copy;
3784 }
3785
3786 // COPY A ONE DIMENSIONAL ARRAY OF long
3787 public static long[] copy(long[] array){
3788 if(array==null)return null;
3789 int n = array.length;
3790 long[] copy = new long[n];
3791 for(int i=0; i<n; i++){
3792 copy[i] = array[i];
3793 }
3794 return copy;
3795 }
3796
3797 // COPY A TWO DIMENSIONAL ARRAY OF double
3798 public static double[][] copy(double[][] array){
3799 if(array==null)return null;
3800 int n = array.length;
3801 double[][] copy = new double[n][];
3802 for(int i=0; i<n; i++){
3803 int m = array[i].length;
3804 copy[i] = new double[m];
3805 for(int j=0; j<m; j++){
3806 copy[i][j] = array[i][j];
3807 }
3808 }
3809 return copy;
3810 }
3811
3812 // COPY A TWO DIMENSIONAL ARRAY OF float
3813 public static float[][] copy(float[][] array){
3814 if(array==null)return null;
3815 int n = array.length;
3816 float[][] copy = new float[n][];
3817 for(int i=0; i<n; i++){
3818 int m = array[i].length;
3819 copy[i] = new float[m];
3820 for(int j=0; j<m; j++){
3821 copy[i][j] = array[i][j];
3822 }
3823 }
3824 return copy;
3825 }
3826
3827 // COPY A TWO DIMENSIONAL ARRAY OF int
3828 public static int[][] copy(int[][] array){
3829 if(array==null)return null;
3830 int n = array.length;
3831 int[][] copy = new int[n][];
3832 for(int i=0; i<n; i++){
3833 int m = array[i].length;
3834 copy[i] = new int[m];
3835 for(int j=0; j<m; j++){
3836 copy[i][j] = array[i][j];
3837 }
3838 }
3839 return copy;
3840 }
3841
3842 // COPY A TWO DIMENSIONAL ARRAY OF long
3843 public static long[][] copy(long[][] array){
3844 if(array==null)return null;
3845 int n = array.length;
3846 long[][] copy = new long[n][];
3847 for(int i=0; i<n; i++){
3848 int m = array[i].length;
3849 copy[i] = new long[m];
3850 for(int j=0; j<m; j++){
3851 copy[i][j] = array[i][j];
3852 }
3853 }
3854 return copy;
3855 }
3856
3857 // COPY A THREE DIMENSIONAL ARRAY OF double
3858 public static double[][][] copy(double[][][] array){
3859 if(array==null)return null;
3860 int n = array.length;
3861 double[][][] copy = new double[n][][];
3862 for(int i=0; i<n; i++){
3863 int m = array[i].length;
3864 copy[i] = new double[m][];
3865 for(int j=0; j<m; j++){
3866 int l = array[i][j].length;
3867 copy[i][j] = new double[l];
3868 for(int k=0; k<l;k++)copy[i][j][k] = array[i][j][k];
3869 }
3870 }
3871 return copy;
3872 }
3873
3874
3875 // COPY A THREE DIMENSIONAL ARRAY OF float
3876 public static float[][][] copy(float[][][] array){
3877 if(array==null)return null;
3878 int n = array.length;
3879 float[][][] copy = new float[n][][];
3880 for(int i=0; i<n; i++){
3881 int m = array[i].length;
3882 copy[i] = new float[m][];
3883 for(int j=0; j<m; j++){
3884 int l = array[i][j].length;
3885 copy[i][j] = new float[l];
3886 for(int k=0; k<l;k++)copy[i][j][k] = array[i][j][k];
3887 }
3888 }
3889 return copy;
3890 }
3891
3892 // COPY A THREE DIMENSIONAL ARRAY OF int
3893 public static int[][][] copy(int[][][] array){
3894 if(array==null)return null;
3895 int n = array.length;
3896 int[][][] copy = new int[n][][];
3897 for(int i=0; i<n; i++){
3898 int m = array[i].length;
3899 copy[i] = new int[m][];
3900 for(int j=0; j<m; j++){
3901 int l = array[i][j].length;
3902 copy[i][j] = new int[l];
3903 for(int k=0; k<l;k++)copy[i][j][k] = array[i][j][k];
3904 }
3905 }
3906 return copy;
3907 }
3908
3909 // COPY A THREE DIMENSIONAL ARRAY OF long
3910 public static long[][][] copy(long[][][] array){
3911 if(array==null)return null;
3912 int n = array.length;
3913 long[][][] copy = new long[n][][];
3914 for(int i=0; i<n; i++){
3915 int m = array[i].length;
3916 copy[i] = new long[m][];
3917 for(int j=0; j<m; j++){
3918 int l = array[i][j].length;
3919 copy[i][j] = new long[l];
3920 for(int k=0; k<l;k++)copy[i][j][k] = array[i][j][k];
3921 }
3922 }
3923 return copy;
3924 }
3925
3926 // COPY A FOUR DIMENSIONAL ARRAY OF double
3927 public static double[][][][] copy(double[][][][] array){
3928 if(array==null)return null;
3929 int n = array.length;
3930 double[][][][] copy = new double[n][][][];
3931 for(int i=0; i<n; i++){
3932 int m = array[i].length;
3933 copy[i] = new double[m][][];
3934 for(int j=0; j<m; j++){
3935 int l = array[i][j].length;
3936 copy[i][j] = new double[l][];
3937 for(int k=0; k<l;k++){
3938 int ll = array[i][j][k].length;
3939 copy[i][j][k] = new double[ll];
3940 for(int kk=0; kk<ll;kk++){
3941 copy[i][j][k][kk] = array[i][j][k][kk];
3942 }
3943 }
3944 }
3945 }
3946 return copy;
3947 }
3948
3949 // COPY A FOUR DIMENSIONAL ARRAY OF float
3950 public static float[][][][] copy(float[][][][] array){
3951 if(array==null)return null;
3952 int n = array.length;
3953 float[][][][] copy = new float[n][][][];
3954 for(int i=0; i<n; i++){
3955 int m = array[i].length;
3956 copy[i] = new float[m][][];
3957 for(int j=0; j<m; j++){
3958 int l = array[i][j].length;
3959 copy[i][j] = new float[l][];
3960 for(int k=0; k<l;k++){
3961 int ll = array[i][j][k].length;
3962 copy[i][j][k] = new float[ll];
3963 for(int kk=0; kk<ll;kk++){
3964 copy[i][j][k][kk] = array[i][j][k][kk];
3965 }
3966 }
3967 }
3968 }
3969 return copy;
3970 }
3971
3972 // COPY A FOUR DIMENSIONAL ARRAY OF int
3973 public static int[][][][] copy(int[][][][] array){
3974 if(array==null)return null;
3975 int n = array.length;
3976 int[][][][] copy = new int[n][][][];
3977 for(int i=0; i<n; i++){
3978 int m = array[i].length;
3979 copy[i] = new int[m][][];
3980 for(int j=0; j<m; j++){
3981 int l = array[i][j].length;
3982 copy[i][j] = new int[l][];
3983 for(int k=0; k<l;k++){
3984 int ll = array[i][j][k].length;
3985 copy[i][j][k] = new int[ll];
3986 for(int kk=0; kk<ll;kk++){
3987 copy[i][j][k][kk] = array[i][j][k][kk];
3988 }
3989 }
3990 }
3991 }
3992 return copy;
3993 }
3994
3995 // COPY A FOUR DIMENSIONAL ARRAY OF long
3996 public static long[][][][] copy(long[][][][] array){
3997 if(array==null)return null;
3998 int n = array.length;
3999 long[][][][] copy = new long[n][][][];
4000 for(int i=0; i<n; i++){
4001 int m = array[i].length;
4002 copy[i] = new long[m][][];
4003 for(int j=0; j<m; j++){
4004 int l = array[i][j].length;
4005 copy[i][j] = new long[l][];
4006 for(int k=0; k<l;k++){
4007 int ll = array[i][j][k].length;
4008 copy[i][j][k] = new long[ll];
4009 for(int kk=0; kk<ll;kk++){
4010 copy[i][j][k][kk] = array[i][j][k][kk];
4011 }
4012 }
4013 }
4014 }
4015 return copy;
4016 }
4017
4018 // COPY A ONE DIMENSIONAL ARRAY OF String
4019 public static String[] copy(String[] array){
4020 if(array==null)return null;
4021 int n = array.length;
4022 String[] copy = new String[n];
4023 for(int i=0; i<n; i++){
4024 copy[i] = array[i];
4025 }
4026 return copy;
4027 }
4028
4029 // COPY A TWO DIMENSIONAL ARRAY OF String
4030 public static String[][] copy(String[][] array){
4031 if(array==null)return null;
4032 int n = array.length;
4033 String[][] copy = new String[n][];
4034 for(int i=0; i<n; i++){
4035 int m = array[i].length;
4036 copy[i] = new String[m];
4037 for(int j=0; j<m; j++){
4038 copy[i][j] = array[i][j];
4039 }
4040 }
4041 return copy;
4042 }
4043
4044 // COPY A THREE DIMENSIONAL ARRAY OF String
4045 public static String[][][] copy(String[][][] array){
4046 if(array==null)return null;
4047 int n = array.length;
4048 String[][][] copy = new String[n][][];
4049 for(int i=0; i<n; i++){
4050 int m = array[i].length;
4051 copy[i] = new String[m][];
4052 for(int j=0; j<m; j++){
4053 int l = array[i][j].length;
4054 copy[i][j] = new String[l];
4055 for(int k=0; k<l;k++)copy[i][j][k] = array[i][j][k];
4056 }
4057 }
4058 return copy;
4059 }
4060
4061 // COPY A FOUR DIMENSIONAL ARRAY OF String
4062 public static String[][][][] copy(String[][][][] array){
4063 if(array==null)return null;
4064 int n = array.length;
4065 String[][][][] copy = new String[n][][][];
4066 for(int i=0; i<n; i++){
4067 int m = array[i].length;
4068 copy[i] = new String[m][][];
4069 for(int j=0; j<m; j++){
4070 int l = array[i][j].length;
4071 copy[i][j] = new String[l][];
4072 for(int k=0; k<l;k++){
4073 int ll = array[i][j][k].length;
4074 copy[i][j][k] = new String[ll];
4075 for(int kk=0; kk<ll;kk++){
4076 copy[i][j][k][kk] = array[i][j][k][kk];
4077 }
4078 }
4079 }
4080 }
4081 return copy;
4082 }
4083
4084
4085
4086
4087 // COPY A ONE DIMENSIONAL ARRAY OF boolean
4088 public static boolean[] copy(boolean[] array){
4089 if(array==null)return null;
4090 int n = array.length;
4091 boolean[] copy = new boolean[n];
4092 for(int i=0; i<n; i++){
4093 copy[i] = array[i];
4094 }
4095 return copy;
4096 }
4097
4098 // COPY A TWO DIMENSIONAL ARRAY OF boolean
4099 public static boolean[][] copy(boolean[][] array){
4100 if(array==null)return null;
4101 int n = array.length;
4102 boolean[][] copy = new boolean[n][];
4103 for(int i=0; i<n; i++){
4104 int m = array[i].length;
4105 copy[i] = new boolean[m];
4106 for(int j=0; j<m; j++){
4107 copy[i][j] = array[i][j];
4108 }
4109 }
4110 return copy;
4111 }
4112
4113 // COPY A THREE DIMENSIONAL ARRAY OF boolean
4114 public static boolean[][][] copy(boolean[][][] array){
4115 if(array==null)return null;
4116 int n = array.length;
4117 boolean[][][] copy = new boolean[n][][];
4118 for(int i=0; i<n; i++){
4119 int m = array[i].length;
4120 copy[i] = new boolean[m][];
4121 for(int j=0; j<m; j++){
4122 int l = array[i][j].length;
4123 copy[i][j] = new boolean[l];
4124 for(int k=0; k<l;k++)copy[i][j][k] = array[i][j][k];
4125 }
4126 }
4127 return copy;
4128 }
4129
4130 // COPY A FOUR DIMENSIONAL ARRAY OF boolean
4131 public static boolean[][][][] copy(boolean[][][][] array){
4132 if(array==null)return null;
4133 int n = array.length;
4134 boolean[][][][] copy = new boolean[n][][][];
4135 for(int i=0; i<n; i++){
4136 int m = array[i].length;
4137 copy[i] = new boolean[m][][];
4138 for(int j=0; j<m; j++){
4139 int l = array[i][j].length;
4140 copy[i][j] = new boolean[l][];
4141 for(int k=0; k<l;k++){
4142 int ll = array[i][j][k].length;
4143 copy[i][j][k] = new boolean[ll];
4144 for(int kk=0; kk<ll;kk++){
4145 copy[i][j][k][kk] = array[i][j][k][kk];
4146 }
4147 }
4148 }
4149 }
4150 return copy;
4151 }
4152
4153
4154
4155 // COPY A ONE DIMENSIONAL ARRAY OF char
4156 public static char[] copy(char[] array){
4157 if(array==null)return null;
4158 int n = array.length;
4159 char[] copy = new char[n];
4160 for(int i=0; i<n; i++){
4161 copy[i] = array[i];
4162 }
4163 return copy;
4164 }
4165
4166 // COPY A TWO DIMENSIONAL ARRAY OF char
4167 public static char[][] copy(char[][] array){
4168 if(array==null)return null;
4169 int n = array.length;
4170 char[][] copy = new char[n][];
4171 for(int i=0; i<n; i++){
4172 int m = array[i].length;
4173 copy[i] = new char[m];
4174 for(int j=0; j<m; j++){
4175 copy[i][j] = array[i][j];
4176 }
4177 }
4178 return copy;
4179 }
4180
4181 // COPY A THREE DIMENSIONAL ARRAY OF char
4182 public static char[][][] copy(char[][][] array){
4183 if(array==null)return null;
4184 int n = array.length;
4185 char[][][] copy = new char[n][][];
4186 for(int i=0; i<n; i++){
4187 int m = array[i].length;
4188 copy[i] = new char[m][];
4189 for(int j=0; j<m; j++){
4190 int l = array[i][j].length;
4191 copy[i][j] = new char[l];
4192 for(int k=0; k<l;k++)copy[i][j][k] = array[i][j][k];
4193 }
4194 }
4195 return copy;
4196 }
4197
4198 // COPY A FOUR DIMENSIONAL ARRAY OF char
4199 public static char[][][][] copy(char[][][][] array){
4200 if(array==null)return null;
4201 int n = array.length;
4202 char[][][][] copy = new char[n][][][];
4203 for(int i=0; i<n; i++){
4204 int m = array[i].length;
4205 copy[i] = new char[m][][];
4206 for(int j=0; j<m; j++){
4207 int l = array[i][j].length;
4208 copy[i][j] = new char[l][];
4209 for(int k=0; k<l;k++){
4210 int ll = array[i][j][k].length;
4211 copy[i][j][k] = new char[ll];
4212 for(int kk=0; kk<ll;kk++){
4213 copy[i][j][k][kk] = array[i][j][k][kk];
4214 }
4215 }
4216 }
4217 }
4218 return copy;
4219 }
4220
4221
4222
4223 // COPY OF AN OBJECT (deprecated - see Conv class)
4224 // Returns a copy of the object
4225 // An exception will be thrown if an attempt to copy a non-serialisable object is made.
4226 // Taken, with minor changes, from { Java Techniques }
4227 // http://javatechniques.com/blog/
4228 public static Object copy(Object obj){
4229 if(obj==null)return null;
4230 return Fmath.copyObject(obj);
4231 }
4232
4233 // COPY OF AN OBJECT (deprecated - see Conv class)
4234 // Returns a copy of the object
4235 // An exception will be thrown if an attempt to copy a non-serialisable object is made.
4236 // Taken, with minor changes, from { Java Techniques }
4237 // http://javatechniques.com/blog/
4238 public static Object copyObject(Object obj){
4239 if(obj==null)return null;
4240 Object objCopy = null;
4241 try {
4242 // Write the object out to a byte array
4243 ByteArrayOutputStream bos = new ByteArrayOutputStream();
4244 ObjectOutputStream oos = new ObjectOutputStream(bos);
4245 oos.writeObject(obj);
4246 oos.flush();
4247 oos.close();
4248 // Make an input stream from the byte array and
4249 // read a copy of the object back in.
4250 ObjectInputStream ois = new ObjectInputStream(
4251 new ByteArrayInputStream(bos.toByteArray()));
4252 objCopy = ois.readObject();
4253 }
4254 catch(IOException e) {
4255 e.printStackTrace();
4256 }
4257 catch(ClassNotFoundException cnfe) {
4258 cnfe.printStackTrace();
4259 }
4260 return objCopy;
4261 }
4262
4263 // UNIT CONVERSIONS (deprecated - see Conv class)
4264
4265 // Converts radians to degrees
4266 public static double radToDeg(double rad){
4267 return rad*180.0D/Math.PI;
4268 }
4269
4270 // Converts degrees to radians
4271 public static double degToRad(double deg){
4272 return deg*Math.PI/180.0D;
4273 }
4274
4275 // Converts frequency (Hz) to radial frequency
4276 public static double frequencyToRadialFrequency(double frequency){
4277 return 2.0D*Math.PI*frequency;
4278 }
4279
4280 // Converts radial frequency to frequency (Hz)
4281 public static double radialFrequencyToFrequency(double radial){
4282 return radial/(2.0D*Math.PI);
4283 }
4284
4285 // Converts electron volts(eV) to corresponding wavelength in nm
4286 public static double evToNm(double ev){
4287 return 1e+9*C_LIGHT/(-ev*Q_ELECTRON/H_PLANCK);
4288 }
4289
4290 // Converts wavelength in nm to matching energy in eV
4291 public static double nmToEv(double nm)
4292 {
4293 return C_LIGHT/(-nm*1e-9)*H_PLANCK/Q_ELECTRON;
4294 }
4295
4296 // Converts moles per litre to percentage weight by volume
4297 public static double molarToPercentWeightByVol(double molar, double molWeight){
4298 return molar*molWeight/10.0D;
4299 }
4300
4301 // Converts percentage weight by volume to moles per litre
4302 public static double percentWeightByVolToMolar(double perCent, double molWeight){
4303 return perCent*10.0D/molWeight;
4304 }
4305
4306 // Converts Celsius to Kelvin
4307 public static double celsiusToKelvin(double cels){
4308 return cels-T_ABS;
4309 }
4310
4311 // Converts Kelvin to Celsius
4312 public static double kelvinToCelsius(double kelv){
4313 return kelv+T_ABS;
4314 }
4315
4316 // Converts Celsius to Fahrenheit
4317 public static double celsiusToFahren(double cels){
4318 return cels*(9.0/5.0)+32.0;
4319 }
4320
4321 // Converts Fahrenheit to Celsius
4322 public static double fahrenToCelsius(double fahr){
4323 return (fahr-32.0)*5.0/9.0;
4324 }
4325
4326 // Converts calories to Joules
4327 public static double calorieToJoule(double cal){
4328 return cal*4.1868;
4329 }
4330
4331 // Converts Joules to calories
4332 public static double jouleToCalorie(double joule){
4333 return joule*0.23884;
4334 }
4335
4336 // Converts grams to ounces
4337 public static double gramToOunce(double gm){
4338 return gm/28.3459;
4339 }
4340
4341 // Converts ounces to grams
4342 public static double ounceToGram(double oz){
4343 return oz*28.3459;
4344 }
4345
4346 // Converts kilograms to pounds
4347 public static double kgToPound(double kg){
4348 return kg/0.4536;
4349 }
4350
4351 // Converts pounds to kilograms
4352 public static double poundToKg(double pds){
4353 return pds*0.4536;
4354 }
4355
4356 // Converts kilograms to tons
4357 public static double kgToTon(double kg){
4358 return kg/1016.05;
4359 }
4360
4361 // Converts tons to kilograms
4362 public static double tonToKg(double tons){
4363 return tons*1016.05;
4364 }
4365
4366 // Converts millimetres to inches
4367 public static double millimetreToInch(double mm){
4368 return mm/25.4;
4369 }
4370
4371 // Converts inches to millimetres
4372 public static double inchToMillimetre(double in){
4373 return in*25.4;
4374 }
4375
4376 // Converts feet to metres
4377 public static double footToMetre(double ft){
4378 return ft*0.3048;
4379 }
4380
4381 // Converts metres to feet
4382 public static double metreToFoot(double metre){
4383 return metre/0.3048;
4384 }
4385
4386 // Converts yards to metres
4387 public static double yardToMetre(double yd){
4388 return yd*0.9144;
4389 }
4390
4391 // Converts metres to yards
4392 public static double metreToYard(double metre){
4393 return metre/0.9144;
4394 }
4395
4396 // Converts miles to kilometres
4397 public static double mileToKm(double mile){
4398 return mile*1.6093;
4399 }
4400
4401 // Converts kilometres to miles
4402 public static double kmToMile(double km){
4403 return km/1.6093;
4404 }
4405
4406 // Converts UK gallons to litres
4407 public static double gallonToLitre(double gall){
4408 return gall*4.546;
4409 }
4410
4411 // Converts litres to UK gallons
4412 public static double litreToGallon(double litre){
4413 return litre/4.546;
4414 }
4415
4416 // Converts UK quarts to litres
4417 public static double quartToLitre(double quart){
4418 return quart*1.137;
4419 }
4420
4421 // Converts litres to UK quarts
4422 public static double litreToQuart(double litre){
4423 return litre/1.137;
4424 }
4425
4426 // Converts UK pints to litres
4427 public static double pintToLitre(double pint){
4428 return pint*0.568;
4429 }
4430
4431 // Converts litres to UK pints
4432 public static double litreToPint(double litre){
4433 return litre/0.568;
4434 }
4435
4436 // Converts UK gallons per mile to litres per kilometre
4437 public static double gallonPerMileToLitrePerKm(double gallPmile){
4438 return gallPmile*2.825;
4439 }
4440
4441 // Converts litres per kilometre to UK gallons per mile
4442 public static double litrePerKmToGallonPerMile(double litrePkm){
4443 return litrePkm/2.825;
4444 }
4445
4446 // Converts miles per UK gallons to kilometres per litre
4447 public static double milePerGallonToKmPerLitre(double milePgall){
4448 return milePgall*0.354;
4449 }
4450
4451 // Converts kilometres per litre to miles per UK gallons
4452 public static double kmPerLitreToMilePerGallon(double kmPlitre){
4453 return kmPlitre/0.354;
4454 }
4455
4456 // Converts UK fluid ounce to American fluid ounce
4457 public static double fluidOunceUKtoUS(double flOzUK){
4458 return flOzUK*0.961;
4459 }
4460
4461 // Converts American fluid ounce to UK fluid ounce
4462 public static double fluidOunceUStoUK(double flOzUS){
4463 return flOzUS*1.041;
4464 }
4465
4466 // Converts UK pint to American liquid pint
4467 public static double pintUKtoUS(double pintUK){
4468 return pintUK*1.201;
4469 }
4470
4471 // Converts American liquid pint to UK pint
4472 public static double pintUStoUK(double pintUS){
4473 return pintUS*0.833;
4474 }
4475
4476 // Converts UK quart to American liquid quart
4477 public static double quartUKtoUS(double quartUK){
4478 return quartUK*1.201;
4479 }
4480
4481 // Converts American liquid quart to UK quart
4482 public static double quartUStoUK(double quartUS){
4483 return quartUS*0.833;
4484 }
4485
4486 // Converts UK gallon to American gallon
4487 public static double gallonUKtoUS(double gallonUK){
4488 return gallonUK*1.201;
4489 }
4490
4491 // Converts American gallon to UK gallon
4492 public static double gallonUStoUK(double gallonUS){
4493 return gallonUS*0.833;
4494 }
4495
4496 // Converts UK pint to American cup
4497 public static double pintUKtoCupUS(double pintUK){
4498 return pintUK/0.417;
4499 }
4500
4501 // Converts American cup to UK pint
4502 public static double cupUStoPintUK(double cupUS){
4503 return cupUS*0.417;
4504 }
4505
4506 // Calculates body mass index (BMI) from height (m) and weight (kg)
4507 public static double calcBMImetric(double height, double weight){
4508 return weight/(height*height);
4509 }
4510
4511 // Calculates body mass index (BMI) from height (ft) and weight (lbs)
4512 public static double calcBMIimperial(double height, double weight){
4513 height = Fmath.footToMetre(height);
4514 weight = Fmath.poundToKg(weight);
4515 return weight/(height*height);
4516 }
4517
4518 // Calculates weight (kg) to give a specified BMI for a given height (m)
4519 public static double calcWeightFromBMImetric(double bmi, double height){
4520 return bmi*height*height;
4521 }
4522
4523 // Calculates weight (lbs) to give a specified BMI for a given height (ft)
4524 public static double calcWeightFromBMIimperial(double bmi, double height){
4525 height = Fmath.footToMetre(height);
4526 double weight = bmi*height*height;
4527 weight = Fmath.kgToPound(weight);
4528 return weight;
4529 }
4530}
4531
Note: See TracBrowser for help on using the repository browser.