Revision 6694

View differences:

trunk/libraries/libIverUtiles/src-test/com/iver/utiles/TestMathExtension.java
1
package com.iver.utiles;
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 *  Generalitat Valenciana
23
 *   Conselleria d'Infraestructures i Transport
24
 *   Av. Blasco Ib??ez, 50
25
 *   46010 VALENCIA
26
 *   SPAIN
27
 *
28
 *      +34 963862235
29
 *   gvsig@gva.es
30
 *      www.gvsig.gva.es
31
 *
32
 *    or
33
 *
34
 *   IVER T.I. S.A
35
 *   Salamanca 50
36
 *   46005 Valencia
37
 *   Spain
38
 *
39
 *   +34 963163400
40
 *   dac@iver.es
41
 */
42

  
43
/**
44
 * Tests for the MathExtension class
45
 * 
46
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
47
 */
48
public class TestMathExtension {
49
	/**
50
	 * Test method for the TestMathExtension class
51
	 * 
52
	 * @param args
53
	 */
54
	public static void main(String[] args)
55
	{
56
		// Test 1
57
		System.out.println("Test 1 -> log2(1)");
58
		showResultState(0, MathExtension.log2(1), 1);
59

  
60
		// Test 2
61
		System.out.println("Test 2 -> log2(16)");
62
		showResultState(4, MathExtension.log2(16), 2);
63

  
64
		// Test 3
65
		System.out.println("Test 3 -> log2Integer(15)");
66
		showResultState(3, MathExtension.log2Integer(15), 3);
67
		
68
		// Test 4
69
		System.out.println("Test 4 -> log2Integer(64)");
70
		showResultState(6, MathExtension.log2Integer(64), 4);
71
		
72
		// Test 5
73
		System.out.println("Test 5 -> exp2Integer(10)");
74
		showResultState(1024, MathExtension.exp2Integer(10), 5);
75
		
76
		// Test 6
77
		System.out.println("Test 6 -> exp2(0)");
78
		showResultState(1, MathExtension.exp2(0), 6);
79

  
80
		// Test 7
81
		System.out.println("Test 7 -> exp2Integer(3.5)");
82
		showResultState(11, MathExtension.exp2Integer(3.5), 7);
83
		
84
		// Test 8
85
		System.out.println("Test 8 -> logX(e2, e6)");
86
		showResultState(3, MathExtension.logX(Math.pow(Math.E, 2), Math.pow(Math.E, 6)), 8);
87
		
88
		// Test 9
89
		System.out.println("Test 9 -> logXInteger(5, 137.43)");
90
		showResultState(3, MathExtension.logXInteger(5, 137.43), 9);
91

  
92
		// Test 10
93
		System.out.println("Test 10 -> expXInteger(3.4, 2.1)");
94
		showResultState(13, MathExtension.expXInteger(3.4, 2.1), 10);
95
		
96
		// There might be more tests
97
		
98
	}
99
	
100
	/**
101
	 * Indicates if the result of the current test has been successfull or not
102
	 * 
103
	 * @param correct The correct result
104
	 * @param result The result of the operation
105
	 * @param testNumber The numer of the current test
106
	 */
107
	private static void showResultState(double correct, double result, int testNumber) {
108
		if (correct == result)
109
			System.out.println("Test " + testNumber + " OK    (Result: " + result + ")");
110
		else
111
			System.out.println("Test " + testNumber + " FAILED    (Result: " + result + ")");
112
	}
113
}
0 114

  
trunk/libraries/libIverUtiles/src/com/iver/utiles/MathExtension.java
1
package com.iver.utiles;
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 *  Generalitat Valenciana
23
 *   Conselleria d'Infraestructures i Transport
24
 *   Av. Blasco Ib??ez, 50
25
 *   46010 VALENCIA
26
 *   SPAIN
27
 *
28
 *      +34 963862235
29
 *   gvsig@gva.es
30
 *      www.gvsig.gva.es
31
 *
32
 *    or
33
 *
34
 *   IVER T.I. S.A
35
 *   Salamanca 50
36
 *   46005 Valencia
37
 *   Spain
38
 *
39
 *   +34 963163400
40
 *   dac@iver.es
41
 */
42

  
43
/**
44
 * Some more mathematical functions that 'java.lang.Math' doesn't have
45
 * 
46
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
47
 */
48
public final class MathExtension {
49
	
50
	/**
51
	 * 2-base logarithm of a
52
	 * @param a
53
	 * @return
54
	 */
55
	public static double log2(double a) {
56
		return (Math.log(a) / Math.log(2));
57
	}
58
	
59
	/**
60
	 * 2-base logarithm of a, but the result doesn't have decimals (approximated to the integer number most near by below)
61
	 * @param a
62
	 * @return
63
	 */
64
	public static int log2Integer(double a) {
65
		return (int) Math.floor(log2(a));
66
	}
67
	
68
	/**
69
	 * 2 raised to the power a
70
	 * @param a
71
	 * @return
72
	 */
73
	public static double exp2(double a) {
74
		return (Math.exp(a * Math.log(2)));
75
	}
76
	
77
	/**
78
	 * 2 raised to the power a, but the result doesn't have decimals (approximated to the integer number most near by below)
79
	 * @param a
80
	 * @return
81
	 */
82
	public static int exp2Integer(double a) {
83
		return (int) Math.floor(exp2(a));
84
	}
85

  
86
	
87
	/**
88
	 * X-base logarithm of a
89
	 * @param a
90
	 * @return
91
	 */
92
	public static double logX(double x_base, double a) {
93
		return (Math.log(a) / Math.log(x_base));
94
	}
95
	
96
	/**
97
	 * X-base logarithm of a, but the result doesn't have decimals (approximated to the integer number most near by below)
98
	 * @param a
99
	 * @return
100
	 */
101
	public static int logXInteger(double x_base, double a) {
102
		return (int) Math.floor(logX(x_base, a));
103
	}
104

  
105
	
106
	/**
107
	 * X raised to the power a
108
	 * @param a
109
	 * @return
110
	 */
111
	public static double expX(double x, double a) {
112
		return (Math.exp(a * Math.log(x)));
113
	}
114
	
115
	/**
116
	 * X raised to the power a, but the result doesn't have decimals (approximated to the integer number most near by below)
117
	 * @param a
118
	 * @return
119
	 */
120
	public static int expXInteger(double x, double a) {
121
		return (int) Math.floor(expX(x, a));
122
	}
123
}
0 124

  

Also available in: Unified diff