KickJava   Java API By Example, From Geeks To Geeks.

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


1 package JSci.maths.symbolic;
2  
3 import JSci.maths.*;
4 import JSci.maths.groups.*;
5 import JSci.maths.fields.*;
6
7 import java.util.*;
8
9 /** Variables in an Expression. */
10 public class Variable extends Expression {
11     private final String JavaDoc name;
12     private final Object JavaDoc valueSet;
13     private Member value=null;
14
15     /**
16      * @param n the name (symbol) of the variable
17      * @param valueSet the set to which the variable values belong,
18      * e.g. RealField.getInstance(). Note
19      * it is not the Class of the values (odd thing indeed).
20      */

21     public Variable(String JavaDoc n,Object JavaDoc valueSet) {
22     name=n;
23     this.valueSet=valueSet;
24     }
25
26     /** Set the value of the variable.
27      * @param o the value; can be null to unset the variable
28      */

29     public void setValue(Member o) {
30     if (o==null) { value=null; return; }
31     if (valueSet.getClass().isInstance(o.getSet()))
32         value=o;
33     else
34         throw new ClassCastException JavaDoc("Variable "+this+" set to "+o.getSet()+" value ("+valueSet+" required.");
35     }
36
37     /** Get the value of the variable.
38      * @return the value of the variable; null if the variable is unset.
39      */

40     public Member getValue() {
41     return value;
42     }
43
44     public boolean equals(Object JavaDoc o) {
45     if (!(o instanceof Variable)) return false;
46     else return this==o;
47     }
48
49     public String JavaDoc toString() { return name; }
50
51     public Expression differentiate(Variable x) {
52     if (this.equals(x)) return new Constant(((Ring)valueSet).one());
53     else return new Constant(((AbelianGroup)valueSet).zero());
54     }
55
56     public Expression evaluate() {
57     if (value==null) return this;
58     if (value instanceof Expression) return ((Expression)value).evaluate();
59     return new Constant(value);
60     }
61
62     protected int getPriority() {return 20;}
63
64     public Object JavaDoc getSet() { return (AbelianGroup)valueSet; }
65
66 }
67
Popular Tags