001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.mandarax.lib.math;
019:
020: import org.mandarax.kernel.Function;
021: import org.mandarax.kernel.Predicate;
022:
023: /**
024: * Class serving predicates and functions needed for double arithmetic.
025: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
026: * @version 3.4 <7 March 05>
027: * @since 1.4 (in 1.3 in the renamed package org.mandarax.math)
028: */
029: public class DoubleArithmetic {
030:
031: // binary functions
032: public static final Function MAX = new DoubleArithmetic.Max();
033: public static final Function MIN = new DoubleArithmetic.Min();
034: public static final Function PLUS = new DoubleArithmetic.Plus();
035: public static final Function MINUS = new DoubleArithmetic.Minus();
036: public static final Function TIMES = new DoubleArithmetic.Times();
037: public static final Function DIV = new DoubleArithmetic.Div();
038: public static final Function AVG = new DoubleArithmetic.Avg();
039: public static final Function POW = new DoubleArithmetic.Pow();
040:
041: // unary functions
042: public static final Function SQRT = new DoubleArithmetic.Sqrt();
043: public static final Function ABS = new DoubleArithmetic.Abs();
044: public static final Function FLOOR = new DoubleArithmetic.Floor();
045: public static final Function CEIL = new DoubleArithmetic.Ceil();
046: public static final Function ROUND = new DoubleArithmetic.Round();
047:
048: // binary predicates
049: public static final Predicate LESS_THAN = new DoubleArithmetic.LessThan();
050: public static final Predicate GREATER_THAN = new DoubleArithmetic.GreaterThan();
051: public static final Predicate EQUAL = new DoubleArithmetic.Equal();
052: public static final Predicate NOT_EQUAL = new DoubleArithmetic.NotEqual();
053: public static final Predicate LESS_THAN_OR_EQUAL = new DoubleArithmetic.LessThanOrEqual();
054: public static final Predicate GREATER_THAN_OR_EQUAL = new DoubleArithmetic.GreaterThanOrEqual();
055:
056: // ternary predicates
057: public static final Predicate INCLUSIVE_BETWEEN = new DoubleArithmetic.InclusiveBetween();
058: public static final Predicate EXCLUSIVE_BETWEEN = new DoubleArithmetic.ExclusiveBetween();
059:
060: // all functions and predicates
061: public static final Function[] ALL_FUNCTIONS = { MAX, MIN, PLUS,
062: MINUS, TIMES, DIV, AVG, POW, SQRT, ABS, FLOOR, CEIL, ROUND };
063: public static final Predicate[] ALL_PREDICATES = { EQUAL,
064: NOT_EQUAL, LESS_THAN, GREATER_THAN, LESS_THAN_OR_EQUAL,
065: GREATER_THAN_OR_EQUAL, INCLUSIVE_BETWEEN, EXCLUSIVE_BETWEEN };
066:
067: // class representing the max function
068: public static class Max extends BinaryDoubleArithmeticFunction {
069:
070: public String getName() {
071: return "max";
072: }
073:
074: public double compute(double d1, double d2) {
075: return Math.max(d1, d2);
076: }
077: }
078:
079: // class representing the min function
080: public static class Min extends BinaryDoubleArithmeticFunction {
081:
082: public String getName() {
083: return "min";
084: }
085:
086: public double compute(double d1, double d2) {
087: return Math.min(d1, d2);
088: }
089: }
090:
091: // class representing the plus function
092: public static class Plus extends BinaryDoubleArithmeticFunction {
093:
094: public String getName() {
095: return "+";
096: }
097:
098: public double compute(double d1, double d2) {
099: return d1 + d2;
100: }
101: }
102:
103: // class representing the times function
104: public static class Times extends BinaryDoubleArithmeticFunction {
105:
106: public String getName() {
107: return "*";
108: }
109:
110: public double compute(double d1, double d2) {
111: return d1 * d2;
112: }
113: }
114:
115: // class representing the minus function
116: public static class Minus extends BinaryDoubleArithmeticFunction {
117:
118: public String getName() {
119: return "-";
120: }
121:
122: public double compute(double d1, double d2) {
123: return d1 - d2;
124: }
125: }
126:
127: // class representing the divide by function
128: public static class Div extends BinaryDoubleArithmeticFunction {
129:
130: public String getName() {
131: return "/";
132: }
133:
134: public double compute(double d1, double d2) {
135: return d1 / d2;
136: }
137: }
138:
139: // class representing the average function
140: public static class Avg extends BinaryDoubleArithmeticFunction {
141:
142: public String getName() {
143: return "avg";
144: }
145:
146: public double compute(double d1, double d2) {
147: return (d1 + d2) / 2;
148: }
149: }
150:
151: // class representing the to the power of function
152: public static class Pow extends BinaryDoubleArithmeticFunction {
153:
154: public String getName() {
155: return "pow";
156: }
157:
158: public double compute(double d1, double d2) {
159: return Math.pow(d1, d2);
160: }
161: }
162:
163: // class representing the square root function
164: public static class Sqrt extends UnaryDoubleArithmeticFunction {
165:
166: public String getName() {
167: return "sqrt";
168: }
169:
170: public double compute(double d) {
171: return Math.sqrt(d);
172: }
173: }
174:
175: // class representing the floor function
176: public static class Floor extends UnaryDoubleArithmeticFunction {
177:
178: public String getName() {
179: return "floor";
180: }
181:
182: public double compute(double d) {
183: return Math.floor(d);
184: }
185: }
186:
187: // class representing the ceil function
188: public static class Ceil extends UnaryDoubleArithmeticFunction {
189:
190: public String getName() {
191: return "ceil";
192: }
193:
194: public double compute(double d) {
195: return Math.ceil(d);
196: }
197: }
198:
199: // class representing the round function
200: public static class Round extends UnaryDoubleArithmeticFunction {
201:
202: public String getName() {
203: return "round";
204: }
205:
206: public double compute(double d) {
207: return Math.round(d);
208: }
209: }
210:
211: // class representing the abs function
212: public static class Abs extends UnaryDoubleArithmeticFunction {
213:
214: public String getName() {
215: return "abs";
216: }
217:
218: public double compute(double d) {
219: return Math.abs(d);
220: }
221: }
222:
223: // class representing the equals predicate
224: public static class Equal extends BinaryDoubleArithmeticPredicate {
225:
226: public String getName() {
227: return "=";
228: }
229:
230: public boolean evaluate(double d1, double d2) {
231: return d1 == d2;
232: }
233: }
234:
235: // class representing the equals not predicate
236: public static class NotEqual extends
237: BinaryDoubleArithmeticPredicate {
238:
239: public String getName() {
240: return "!=";
241: }
242:
243: public boolean evaluate(double d1, double d2) {
244: return d1 != d2;
245: }
246: }
247:
248: // class representing the greater than predicate
249: public static class GreaterThan extends
250: BinaryDoubleArithmeticPredicate {
251:
252: public String getName() {
253: return ">";
254: }
255:
256: public boolean evaluate(double d1, double d2) {
257: return d1 > d2;
258: }
259: }
260:
261: // class representing the less than predicate
262: public static class LessThan extends
263: BinaryDoubleArithmeticPredicate {
264:
265: public String getName() {
266: return "<";
267: }
268:
269: public boolean evaluate(double d1, double d2) {
270: return d1 < d2;
271: }
272: }
273:
274: // class representing the less than or equals predicate
275: public static class LessThanOrEqual extends
276: BinaryDoubleArithmeticPredicate {
277:
278: public String getName() {
279: return "=<";
280: }
281:
282: public boolean evaluate(double d1, double d2) {
283: return (d1 < d2) || (d1 == d2);
284: }
285: }
286:
287: // class representing the greater than or equals predicate
288: public static class GreaterThanOrEqual extends
289: BinaryDoubleArithmeticPredicate {
290:
291: public String getName() {
292: return "=>";
293: }
294:
295: public boolean evaluate(double d1, double d2) {
296: return (d1 > d2) || (d1 == d2);
297: }
298: }
299:
300: // class representing the exclusive between predicate
301: public static class ExclusiveBetween extends
302: TernaryDoubleArithmeticPredicate {
303:
304: public String getName() {
305: return "between (excl.)";
306: }
307:
308: public boolean evaluate(double d1, double d2, double d3) {
309: return (d1 < d2) && (d2 < d3);
310: }
311: }
312:
313: // class representing the inclusive between predicate
314: public static class InclusiveBetween extends
315: TernaryDoubleArithmeticPredicate {
316:
317: public String getName() {
318: return "between (incl.)";
319: }
320:
321: public boolean evaluate(double d1, double d2, double d3) {
322: return (d1 <= d2) && (d2 <= d3);
323: }
324: }
325: }
|