KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > symbolic > Function


1 package JSci.maths.symbolic;
2  
3 import JSci.maths.*;
4 import JSci.maths.fields.*;
5 import JSci.maths.groups.*;
6
7 import java.util.*;
8
9 /** A function like sin(x), exp(x) or sqrt(x). <br>
10 This will be substituted by <code>Evaluation</code>.
11 */

12 public class Function extends Expression {
13
14     public static final String JavaDoc SIN = "sin";
15     public static final String JavaDoc COS = "cos";
16     public static final String JavaDoc TAN = "tan";
17     public static final String JavaDoc ASIN = "asin";
18     public static final String JavaDoc ACOS = "acos";
19     public static final String JavaDoc ATAN = "atan";
20     public static final String JavaDoc SINH = "sinh";
21     public static final String JavaDoc COSH = "cosh";
22     public static final String JavaDoc TANH = "tanh";
23     public static final String JavaDoc ASINH = "asinh";
24     public static final String JavaDoc ACOSH = "acosh";
25     public static final String JavaDoc ATANH = "atanh";
26     public static final String JavaDoc EXP = "exp";
27     public static final String JavaDoc LOG = "log";
28     public static final String JavaDoc SQRT = "sqrt";
29
30     private final String JavaDoc type;
31     private final Expression arg;
32
33     /**
34     * @param n the name (type) of the function; for example, Function.SIN
35     * @param a the argument
36     */

37     public Function(String JavaDoc n,Expression a) {
38     type=n;
39     arg=a;
40     }
41
42     public String JavaDoc toString() { return type+"("+arg+")"; }
43
44     public int getPriority() {return 15;}
45
46     public Expression differentiate(Variable x) {
47     Expression d=null;
48     if (type.equals(SIN)) d=new Function(COS,arg);
49     if (type.equals(COS)) d=Expression.negative(new Function(SIN,arg));
50     if (type.equals(TAN)) d=Expression.power(new Function(COS,arg),-2);
51     if (type.equals(ASIN)) d=Expression.inverse(new Function(SQRT,Expression.sum( ((Ring)getSet()).one(), Expression.negative(Expression.power(arg,2)) )));
52     if (type.equals(ACOS)) d=Expression.inverse(new Function(SQRT,Expression.sum( ((Ring)getSet()).one(), Expression.negative(Expression.power(arg,2)) ) ));
53     if (type.equals(ATAN)) d=Expression.inverse(Expression.sum( ((Ring)getSet()).one(), Expression.power(arg,2) ));
54     if (type.equals(SINH)) d=new Function(COSH,arg);
55     if (type.equals(COSH)) d=new Function(SINH,arg);
56     if (type.equals(TANH)) d=Expression.power(new Function(COSH,arg),-2);
57     if (type.equals(ASINH)) d=Expression.inverse(new Function(SQRT,Expression.sum(((Ring)getSet()).one(),Expression.power(arg,2))));
58         if (type.equals(ACOSH)) d=Expression.inverse(new Function(SQRT,Expression.sum(((Ring)getSet()).one().negate(),Expression.power(arg,2))));
59     if (type.equals(ATANH)) d=Expression.inverse(new Function(SQRT,Expression.sum(((Ring)getSet()).one(),Expression.power(arg,2).negate())));
60     if (type.equals(EXP)) d=new Function(EXP,arg);
61     if (type.equals(LOG)) d=Expression.inverse(arg);
62     if (type.equals(SQRT)) d=Expression.inverse(Expression.product((Ring.Member)((Ring)getSet()).one().add(((Ring)getSet()).one()),new Function(SQRT,arg)));
63
64     if (d==null)
65         throw new IllegalArgumentException JavaDoc("Unknown Function type in derivative()");
66     return Expression.product(d,arg.differentiate(x));
67     }
68
69     public boolean equals(Object JavaDoc o) {
70     if (!Function.class.isInstance(o)) return false;
71     Function f = (Function)o;
72     return (type.equals(f.type) && arg.equals(f.arg));
73     }
74
75     public Object JavaDoc getSet() { return arg.getSet(); }
76     
77     public Expression evaluate() {
78     Expression sarg = arg.evaluate();
79     
80     if (sarg instanceof Constant) {
81         if (((Constant)sarg).getValue() instanceof Complex) {
82         Complex a = (Complex)((Constant)sarg).getValue();
83         if (type.equals(SIN)) return new Constant(Complex.sin(a));
84         if (type.equals(COS)) return new Constant(Complex.cos(a));
85         if (type.equals(TAN)) return new Constant(Complex.tan(a));
86         if (type.equals(ASIN)) return new Constant(Complex.asin(a));
87         if (type.equals(ACOS)) return new Constant(Complex.acos(a));
88         if (type.equals(ATAN)) return new Constant(Complex.atan(a));
89         if (type.equals(SIN)) return new Constant(Complex.sinh(a));
90         if (type.equals(COS)) return new Constant(Complex.cosh(a));
91         if (type.equals(TAN)) return new Constant(Complex.tanh(a));
92         if (type.equals(ASIN)) return new Constant(Complex.asinh(a));
93         if (type.equals(ACOS)) return new Constant(Complex.acosh(a));
94         if (type.equals(ATAN)) return new Constant(Complex.atanh(a));
95         if (type.equals(EXP)) return new Constant(Complex.exp(a));
96         if (type.equals(LOG)) return new Constant(Complex.log(a));
97         if (type.equals(SQRT)) return new Constant(a.sqrt());
98         throw new IllegalArgumentException JavaDoc("Unknown Function type in evaluate()");
99         }
100         if (((Constant)sarg).getValue() instanceof MathDouble) {
101         MathDouble a = (MathDouble)((Constant)sarg).getValue();
102         if (type.equals(SIN)) return new Constant(MathDouble.sin(a));
103         if (type.equals(COS)) return new Constant(MathDouble.cos(a));
104         if (type.equals(TAN)) return new Constant(MathDouble.tan(a));
105         if (type.equals(ASIN)) return new Constant(MathDouble.asin(a));
106         if (type.equals(ACOS)) return new Constant(MathDouble.acos(a));
107         if (type.equals(ATAN)) return new Constant(MathDouble.atan(a));
108         if (type.equals(SIN)) return new Constant(MathDouble.sinh(a));
109         if (type.equals(COS)) return new Constant(MathDouble.cosh(a));
110         if (type.equals(TAN)) return new Constant(MathDouble.tanh(a));
111         if (type.equals(ASIN)) return new Constant(MathDouble.asinh(a));
112         if (type.equals(ACOS)) return new Constant(MathDouble.acosh(a));
113         if (type.equals(ATAN)) return new Constant(MathDouble.atanh(a));
114         if (type.equals(EXP)) return new Constant(MathDouble.exp(a));
115         if (type.equals(LOG)) return new Constant(MathDouble.log(a));
116         if (type.equals(SQRT)) return new Constant(new MathDouble(Math.sqrt(a.value())));
117         throw new IllegalArgumentException JavaDoc("Unknown Function type in evaluate()");
118         }
119         throw new IllegalArgumentException JavaDoc("Function argument is "+sarg.getSet()+" ; must be Complex or MathDouble");
120     }
121
122     if (sarg instanceof Function) {
123         Function f = (Function)sarg;
124         if (type.equals(SIN) && f.type.equals(ASIN)) return f.arg;
125         if (type.equals(COS) && f.type.equals(ACOS)) return f.arg;
126         if (type.equals(TAN) && f.type.equals(ATAN)) return f.arg;
127         if (type.equals(ASIN) && f.type.equals(SIN)) return f.arg;
128         if (type.equals(ACOS) && f.type.equals(COS)) return f.arg;
129         if (type.equals(ATAN) && f.type.equals(TAN)) return f.arg;
130         if (type.equals(SINH) && f.type.equals(ASINH)) return f.arg;
131         if (type.equals(COSH) && f.type.equals(ACOSH)) return f.arg;
132         if (type.equals(TANH) && f.type.equals(ATANH)) return f.arg;
133         if (type.equals(ASINH) && f.type.equals(SINH)) return f.arg;
134         if (type.equals(ACOSH) && f.type.equals(COSH)) return f.arg;
135         if (type.equals(ATANH) && f.type.equals(TANH)) return f.arg;
136         if (type.equals(EXP) && f.type.equals(LOG)) return f.arg;
137         if (type.equals(LOG) && f.type.equals(EXP)) return f.arg;
138     }
139
140     return new Function(type,sarg);
141
142     }
143
144 }
145
146
147
148
Popular Tags