KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > matrixJep > MatrixVariable


1 /* @author rich
2  * Created on 26-Oct-2003
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.matrixJep;
9 import org.nfunk.jep.*;
10 import org.lsmp.djep.vectorJep.*;
11 import org.lsmp.djep.vectorJep.values.*;
12 import org.lsmp.djep.xjep.*;
13 import org.lsmp.djep.djep.*;
14 import java.util.*;
15 /**
16  * Holds all info about a variable.
17  * Has a name, an equation, a dimension (or sent of dimensions if matrix or tensor)
18  * and also a set of {@link MatrixPartialDerivative MatrixPartialDerivative}.
19  * The derivatives are stored in a hashtable index by
20  * the sorted names of derivatives.
21  * i.e. d^2f/dxdy, and d^2f/dydx will both be indexed by {"x","y"}.
22  * df/dx is indexed by {"x"}, d^2f/dx^2 is index by {"x","x"}.
23  * Partial derivatives are calculated as required by the
24  * findDerivative method.
25  * @author Rich Morris
26  * Created on 26-Oct-2003
27  * @since 2.3.2 Added a setValue method overriding
28  */

29 public class MatrixVariable extends DVariable implements MatrixVariableI {
30
31     private Dimensions dims;
32     private MatrixValueI mvalue = null;
33
34     
35 // private VariableInfo(String name,MatrixNodeI eqn,Dimensions dims)
36
// {
37
// this.name = name; this.equation = eqn; this.dims = dims;
38
// value = Tensor.getInstance(dims);
39
// this.validValue=false;
40
// }
41

42     protected PartialDerivative createDerivative(String JavaDoc derivnames[],Node eqn)
43     {
44         return new MatrixPartialDerivative(this,derivnames,eqn);
45     }
46
47     /**
48      * The constructor is package private. Variables should be created
49      * using the VariableTable.find(Sting name) method.
50      */

51     MatrixVariable(String JavaDoc name)
52     {
53         super(name);
54         this.dims = Dimensions.ONE;
55         this.mvalue = new Scaler();
56         setValidValue(false);
57     }
58
59     MatrixVariable(String JavaDoc name,Object JavaDoc value)
60     {
61         super(name);
62         if(value instanceof MatrixValueI)
63             this.mvalue = (MatrixValueI) value;
64         else
65         {
66             this.mvalue = new Scaler();
67             this.mvalue.setEle(0,value);
68         }
69         this.dims = mvalue.getDim();
70         setValidValue(true);
71     }
72
73     public Dimensions getDimensions() { return dims; }
74     public void setDimensions(Dimensions dims) {
75         this.dims = dims;
76         this.mvalue=Tensor.getInstance(dims);
77         this.invalidateAll();
78     }
79
80     /** returns the value, uses the Scaler type. */
81     public MatrixValueI getMValue() { return mvalue; }
82
83     /** returns the value, unpacks Scalers so they just return its elements. */
84     public Object JavaDoc getValue() {
85         if(mvalue instanceof Scaler)
86         return mvalue.getEle(0);
87         else
88             return mvalue;
89     }
90
91     /**
92      * Sets the value of this variable.
93      * Needed when using marco functions in matrix calculations.
94      * TODO might be better to change macro function behaviour.
95      */

96     protected boolean setValueRaw(Object JavaDoc val) {
97         if(val instanceof MatrixValueI)
98         {
99             mvalue = (MatrixValueI) val;
100             this.dims = mvalue.getDim();
101         }
102         else
103             mvalue.setEle(0,val);
104         return true;
105     }
106      
107     public void setMValue(MatrixValueI val) {
108         if(this.isConstant()) return;
109         mvalue.setEles(val);
110         setValidValue(true);
111         setChanged();
112         notifyObservers();
113     }
114     
115 // public void setMValue(VectorMatrixTensorI value)
116
// { this.mvalue = value; this.invalidateAll(); }
117

118     public void print(PrintVisitor bpv)
119     {
120         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(name);
121         sb.append(": ");
122         if(dims!=null) sb.append("dim "+dims.toString());
123         if(hasValidValue() && mvalue != null) sb.append(" val "+getMValue() );
124         else sb.append(" null value");
125         sb.append(" ");
126         if(this.getEquation()!=null)
127             sb.append("eqn "+bpv.toString(this.getEquation()));
128         else
129             sb.append("no equation");
130         sb.append("\n");
131         for(java.util.Enumeration JavaDoc e = this.derivatives.elements(); e.hasMoreElements(); )
132         {
133             MatrixPartialDerivative var = (MatrixPartialDerivative) e.nextElement();
134             sb.append("\t"+var.toString()+": ");
135             if(var.hasValidValue()) sb.append(" val "+var.getMValue() );
136             else sb.append(" null value");
137             sb.append(" ");
138             sb.append(bpv.toString(var.getEquation()));
139             sb.append("\n");
140         }
141         System.out.print(sb.toString());
142     }
143     
144
145 }
146
Popular Tags