01: package JSci.maths.symbolic;
02:
03: import JSci.maths.*;
04: import JSci.maths.groups.*;
05: import JSci.maths.fields.*;
06:
07: import java.util.*;
08:
09: /** Variables in an Expression. */
10: public class Variable extends Expression {
11: private final String name;
12: private final Object 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 n, Object 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) {
31: value = null;
32: return;
33: }
34: if (valueSet.getClass().isInstance(o.getSet()))
35: value = o;
36: else
37: throw new ClassCastException("Variable " + this
38: + " set to " + o.getSet() + " value (" + valueSet
39: + " required.");
40: }
41:
42: /** Get the value of the variable.
43: * @return the value of the variable; null if the variable is unset.
44: */
45: public Member getValue() {
46: return value;
47: }
48:
49: public boolean equals(Object o) {
50: if (!(o instanceof Variable))
51: return false;
52: else
53: return this == o;
54: }
55:
56: public String toString() {
57: return name;
58: }
59:
60: public Expression differentiate(Variable x) {
61: if (this .equals(x))
62: return new Constant(((Ring) valueSet).one());
63: else
64: return new Constant(((AbelianGroup) valueSet).zero());
65: }
66:
67: public Expression evaluate() {
68: if (value == null)
69: return this ;
70: if (value instanceof Expression)
71: return ((Expression) value).evaluate();
72: return new Constant(value);
73: }
74:
75: protected int getPriority() {
76: return 20;
77: }
78:
79: public Object getSet() {
80: return (AbelianGroup) valueSet;
81: }
82:
83: }
|