KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > lib > math > MathFunction


1 package com.teamkonzept.lib.math;
2
3 public class MathFunction extends UnaryOperator{
4     
5     final static int LOG = 0; // requests extra parameter
6
final static int ABS = 1;
7     final static int ACOS = 2;
8     final static int ASIN = 3;
9     final static int ATAN = 4;
10     final static int CEIL = 5;
11     final static int COSH = 6;
12     final static int COS = 7;
13     final static int COTAN = 8;
14     final static int EXP = 9;
15     final static int FAC = 10;
16     final static int FLOOR = 11;
17     final static int FPART = 12;
18     final static int LN = 13;
19     final static int ROUND = 14;
20     final static int SFAC = 15;
21     final static int SINH = 16;
22     final static int SIN = 17;
23     final static int SQRT = 18;
24     final static int TANH = 19;
25     final static int TAN = 20;
26     
27     int type;
28     
29     // default base for log function
30
double extraOperand = 10.0;
31
32     public MathFunction(int type, int parenlevel, int position)
33     throws UnsupportedOperatorException{
34     super(parenlevel, getPriority(type, position), position);
35     this.type = type;
36     }
37     public MathFunction(int type, int parenlevel, int position,
38             Double JavaDoc extraOperand){
39     super(parenlevel, MATH_FUNCTION_PRIORITY, position);
40     this.type = type;
41     this.extraOperand = extraOperand.doubleValue();
42     }
43
44     /**
45      * evaluates this function to the operand
46      * returns the result of type Double
47      */

48     public Object JavaDoc evaluate(Object JavaDoc operand) throws BadOperandTypeException{
49     if ( !(operand instanceof Double JavaDoc) )
50         throw new BadOperandTypeException(operand, "Double");
51     double d = ((Double JavaDoc)operand).doubleValue();
52     switch ( type ){
53     case LOG:
54         return new Double JavaDoc(Math.log(d) / Math.log(extraOperand));
55     case ABS:
56         return new Double JavaDoc(Math.abs(d));
57     case ACOS:
58         return new Double JavaDoc(Math.acos(d));
59     case ASIN:
60         return new Double JavaDoc(Math.asin(d));
61     case ATAN:
62         return new Double JavaDoc(Math.atan(d));
63     case CEIL:
64         return new Double JavaDoc(Math.ceil(d));
65     case COSH:
66         return new Double JavaDoc((Math.exp(d) + Math.exp(0.0 - d)) / 2.0);
67     case COS:
68         return new Double JavaDoc(Math.cos(d));
69     case COTAN:
70         return new Double JavaDoc(1.0 / Math.tan(d));
71     case EXP:
72         return new Double JavaDoc(Math.exp(d));
73     case FAC:
74         if ( Math.ceil(d) != d )
75         throw new BadOperandTypeException(operand, "Integer");
76         return new Double JavaDoc(computeFaculty((int)d));
77     case FLOOR:
78         return new Double JavaDoc(Math.floor(d));
79     case FPART:
80         return new Double JavaDoc(d - Math.floor(d));
81     case LN:
82         return new Double JavaDoc(Math.log(d));
83     case ROUND:
84         return new Double JavaDoc(Math.round(d));
85     case SFAC:
86         if ( Math.ceil(d) != d )
87         throw new BadOperandTypeException(operand, "Integer");
88         return new Double JavaDoc(computeSemiFaculty((int)d));
89     case SINH:
90         return new Double JavaDoc((Math.exp(d) - Math.exp(0.0 - d)) / 2.0);
91     case SIN:
92         return new Double JavaDoc(Math.sin(d));
93     case SQRT:
94         return new Double JavaDoc(Math.sqrt(d));
95     case TANH:
96         double d1 = Math.exp(d);
97         double d2 = Math.exp(0.0 - d);
98         return new Double JavaDoc( (d1 - d2) / (d1 + d2) );
99     case TAN:
100         return new Double JavaDoc(Math.tan(d));
101     }
102     return null;
103     }
104
105     static int getPriority(int op, int position)
106     throws UnsupportedOperatorException{
107     if ( op < 0 || op > 20 )
108         throw new UnsupportedOperatorException(MathFunction.class,
109                            op, position);
110     return MATH_FUNCTION_PRIORITY;
111     }
112
113     static long computeFaculty(int value){
114     long res = 1;
115     for ( int i = 2; i <= value; i++ )
116         res = res * i;
117     return res;
118     }
119     
120     static long computeSemiFaculty(int value){
121     long res = 1;
122     for ( int i = value; i > 1; i = i - 2 )
123         res = res * i;
124     return res;
125     }
126 }
127
Popular Tags