KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > vectorJep > values > Tensor


1 /* @author rich
2  * Created on 07-Jul-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.vectorJep.values;
9 import org.lsmp.djep.vectorJep.*;
10
11 /**
12  * Represents tensor (generalisation of Matrix/Vector).
13  * @author Rich Morris
14  * Created on 07-Jul-2003
15  * @version 1.3.0.2 now extends number
16  */

17 public class Tensor extends Number JavaDoc implements MatrixValueI {
18     private Object JavaDoc values[]=null;
19     private Dimensions dims;
20     //DoubleMatrix jsciMat;
21

22     private Tensor() {}
23
24     /** Creates a Tensor with the given dimension. **/
25     public Tensor(Dimensions dims)
26     {
27         values = new Object JavaDoc[dims.numEles()];
28         this.dims = dims;
29     }
30
31     /** Creates a Tensor with same dimension as the arguments. **/
32     public Tensor(Tensor t)
33     {
34         values = new Object JavaDoc[t.getDim().numEles()];
35         this.dims = t.getDim();
36     }
37     
38     /** Creates a tensor with dimensions [len,dims[0],...,dims[n]] **/
39     public Tensor(int len,Dimensions dims)
40     {
41         values = new Object JavaDoc[len*dims.numEles()];
42         this.dims = Dimensions.valueOf(len,dims);
43     }
44
45     public Dimensions getDim() { return dims; }
46     public int getNumEles() { return values.length; }
47     public void setEle(int i,Object JavaDoc value) { values[i]=value; }
48     public Object JavaDoc getEle(int i) { return values[i]; }
49     /** sets the elements to those of the arguments. */
50     public void setEles(MatrixValueI val)
51     {
52         if(!dims.equals(val.getDim())) return;
53         System.arraycopy(((Tensor) val).values,0,values,0,getNumEles());
54     }
55
56     /** Factory method to return a new Vector, Matrix or Tensor
57      * with the given dimensions.
58      */

59     public static MatrixValueI getInstance(Dimensions dims)
60     {
61         switch(dims.rank())
62         {
63             case 0: return new Scaler();
64             case 1: return new MVector(dims.getFirstDim());
65             case 2: return new Matrix(dims.getFirstDim(),dims.getLastDim());
66             default:
67                     return new Tensor(dims);
68         }
69     }
70     
71     public static MatrixValueI getInstance(int rows,Dimensions dims)
72     {
73         switch(dims.rank())
74         {
75             case 0: return new MVector(rows);
76             case 1: return new Matrix(rows,dims.getFirstDim());
77             default:
78                     return new Tensor(Dimensions.valueOf(rows,dims));
79         }
80     }
81
82     private int curEle =0;
83     /** Recursive procedure to print the tensor with lots of brackets. **/
84     protected void bufferAppend(StringBuffer JavaDoc sb,int currank)
85     {
86         sb.append("[");
87         if(currank+1 >= dims.rank())
88         {
89             // bottom of tree
90
for(int i=0;i<dims.getIthDim(currank);++i)
91             {
92                 if(i!=0) sb.append(",");
93                 sb.append(getEle(curEle++));
94             }
95         }
96         else
97         {
98             // not bottom of tree
99
for(int i=0;i<dims.getIthDim(currank);++i)
100             {
101                 if(i!=0) sb.append(",");
102                 bufferAppend(sb,currank+1);
103             }
104         }
105         sb.append("]");
106     }
107     /**
108      * Returns a string rep of tensor. Uses [[a,b],[c,d]] syntax.
109      */

110     public String JavaDoc toString()
111     {
112         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
113         curEle = 0;
114         bufferAppend(sb,0);
115         return sb.toString();
116     }
117
118     /** value of constant ele(1). */
119     public int intValue() {return ((Number JavaDoc) values[0]).intValue(); }
120     /** value of constant ele(1). */
121     public long longValue() {return ((Number JavaDoc) values[0]).longValue(); }
122     /** value of constant ele(1). */
123     public float floatValue() { return ((Number JavaDoc) values[0]).floatValue(); }
124     /** value of constant ele(1). */
125     public double doubleValue() {return ((Number JavaDoc) values[0]).doubleValue(); }
126
127     public boolean equals(Object JavaDoc obj) {
128         if(!(obj instanceof Tensor)) return false;
129         Tensor tens = (Tensor) obj;
130         if(!tens.getDim().equals(getDim())) return false;
131         for(int i=0;i<values.length;++i)
132                 if(!values[i].equals(tens.values[i])) return false;
133         return true;
134     }
135
136     /**
137      * Always override hashCode when you override equals.
138      * Efective Java, Joshua Bloch, Sun Press
139      */

140     public int hashCode() {
141         int result = 17;
142         for(int i=0;i<values.length;++i)
143             result = 37*result+ values[i].hashCode();
144         return result;
145     }
146 }
147
Popular Tags