corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * CVS identifier:
3 *
4 * $Id: MathUtil.java,v 1.15 2001/09/14 08:48:51 grosbois Exp $
5 *
6 * Class: MathUtil
7 *
8 * Description: Utility mathematical methods
9 *
10 *
11 *
12 * COPYRIGHT:
13 *
14 * This software module was originally developed by Raphaël Grosbois and
15 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
16 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
17 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
18 * Centre France S.A) in the course of development of the JPEG2000
19 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
20 * software module is an implementation of a part of the JPEG 2000
21 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
22 * Systems AB and Canon Research Centre France S.A (collectively JJ2000
23 * Partners) agree not to assert against ISO/IEC and users of the JPEG
24 * 2000 Standard (Users) any of their rights under the copyright, not
25 * including other intellectual property rights, for this software module
26 * with respect to the usage by ISO/IEC and Users of this software module
27 * or modifications thereof for use in hardware or software products
28 * claiming conformance to the JPEG 2000 Standard. Those intending to use
29 * this software module in hardware or software products are advised that
30 * their use may infringe existing patents. The original developers of
31 * this software module, JJ2000 Partners and ISO/IEC assume no liability
32 * for use of this software module or modifications thereof. No license
33 * or right to this software module is granted for non JPEG 2000 Standard
34 * conforming products. JJ2000 Partners have full right to use this
35 * software module for his/her own purpose, assign or donate this
36 * software module to any third party and to inhibit third parties from
37 * using this software module for non JPEG 2000 Standard conforming
38 * products. This copyright notice must be included in all copies or
39 * derivative works of this software module.
40 *
41 * Copyright (c) 1999/2000 JJ2000 Partners.
42 * */
43 using System;
44 namespace CSJ2K.j2k.util
45 {
46  
47 /// <summary> This class contains a collection of utility methods fro mathematical
48 /// operations. All methods are static.
49 ///
50 /// </summary>
51 public class MathUtil
52 {
53  
54 /// <summary> Method that calculates the floor of the log, base 2, of 'x'. The
55 /// calculation is performed in integer arithmetic, therefore, it is exact.
56 ///
57 /// </summary>
58 /// <param name="x">The value to calculate log2 on.
59 ///
60 /// </param>
61 /// <returns> floor(log(x)/log(2)), calculated in an exact way.
62 ///
63 /// </returns>
64 public static int log2(int x)
65 {
66 int y, v;
67 // No log of 0 or negative
68 if (x <= 0)
69 {
70 throw new System.ArgumentException("" + x + " <= 0");
71 }
72 // Calculate log2 (it's actually floor log2)
73 v = x;
74 y = - 1;
75 while (v > 0)
76 {
77 v >>= 1;
78 y++;
79 }
80 return y;
81 }
82  
83 /// <summary> Method that calculates the Least Common Multiple (LCM) of two strictly
84 /// positive integer numbers.
85 ///
86 /// </summary>
87 /// <param name="x1">First number
88 ///
89 /// </param>
90 /// <param name="x2">Second number
91 ///
92 /// </param>
93 public static int lcm(int x1, int x2)
94 {
95 if (x1 <= 0 || x2 <= 0)
96 {
97 throw new System.ArgumentException("Cannot compute the least " + "common multiple of two " + "numbers if one, at least," + "is negative.");
98 }
99 int max, min;
100 if (x1 > x2)
101 {
102 max = x1;
103 min = x2;
104 }
105 else
106 {
107 max = x2;
108 min = x1;
109 }
110 for (int i = 1; i <= min; i++)
111 {
112 if ((max * i) % min == 0)
113 {
114 return i * max;
115 }
116 }
117 throw new System.ApplicationException("Cannot find the least common multiple of numbers " + x1 + " and " + x2);
118 }
119  
120 /// <summary> Method that calculates the Least Common Multiple (LCM) of several
121 /// positive integer numbers.
122 ///
123 /// </summary>
124 /// <param name="x">Array containing the numbers.
125 ///
126 /// </param>
127 public static int lcm(int[] x)
128 {
129 if (x.Length < 2)
130 {
131 throw new System.ApplicationException("Do not use this method if there are less than" + " two numbers.");
132 }
133 int tmp = lcm(x[x.Length - 1], x[x.Length - 2]);
134 for (int i = x.Length - 3; i >= 0; i--)
135 {
136 if (x[i] <= 0)
137 {
138 throw new System.ArgumentException("Cannot compute the least " + "common multiple of " + "several numbers where " + "one, at least," + "is negative.");
139 }
140 tmp = lcm(tmp, x[i]);
141 }
142 return tmp;
143 }
144  
145 /// <summary> Method that calculates the Greatest Common Divisor (GCD) of two
146 /// positive integer numbers.
147 ///
148 /// </summary>
149 public static int gcd(int x1, int x2)
150 {
151 if (x1 < 0 || x2 < 0)
152 {
153 throw new System.ArgumentException("Cannot compute the GCD " + "if one integer is negative.");
154 }
155 int a, b, g, z;
156  
157 if (x1 > x2)
158 {
159 a = x1;
160 b = x2;
161 }
162 else
163 {
164 a = x2;
165 b = x1;
166 }
167  
168 if (b == 0)
169 return 0;
170  
171 g = b;
172  
173 while (g != 0)
174 {
175 z = a % g;
176 a = g;
177 g = z;
178 }
179 return a;
180 }
181  
182 /// <summary> Method that calculates the Greatest Common Divisor (GCD) of several
183 /// positive integer numbers.
184 ///
185 /// </summary>
186 /// <param name="x">Array containing the numbers.
187 ///
188 /// </param>
189 public static int gcd(int[] x)
190 {
191 if (x.Length < 2)
192 {
193 throw new System.ApplicationException("Do not use this method if there are less than" + " two numbers.");
194 }
195 int tmp = gcd(x[x.Length - 1], x[x.Length - 2]);
196 for (int i = x.Length - 3; i >= 0; i--)
197 {
198 if (x[i] < 0)
199 {
200 throw new System.ArgumentException("Cannot compute the least " + "common multiple of " + "several numbers where " + "one, at least," + "is negative.");
201 }
202 tmp = gcd(tmp, x[i]);
203 }
204 return tmp;
205 }
206 }
207 }