KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > vectorJep > Dimensions


1 /* @author rich
2  * Created on 25-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.vectorJep;
9
10
11 /**
12  * A class to represent a set of dimensions.
13  * Might be 1 for 0-dimensional numbers.
14  * [3] for a 3D vector
15  * [3,3] for a matrix
16  * @author rich
17  * Created on 25-Oct-2003
18  */

19 public class Dimensions
20 {
21     private int dims[];
22     public static final Dimensions ONE = new Dimensions(1);
23     public static final Dimensions TWO = new Dimensions(2);
24     public static final Dimensions THREE = new Dimensions(3);
25     
26     private Dimensions() {};
27     /** Sets the dimension to a single number. Implies its a Scaler or MVector. */
28     private Dimensions(int d) { dims = new int[] {d}; }
29     /** Use this method for matricies. */
30     private Dimensions(int d1,int d2) { dims = new int[] {d1,d2}; }
31     /** Construct a dimension set from an array. */
32     private Dimensions(int d[])
33     {
34         dims = new int[d.length];
35         System.arraycopy(d,0,dims,0,d.length);
36     }
37     /**
38      * Factory method returns a Dimension for vector of given length.
39      * For 1,2,3 returns ONE,TWO or THREE otherwise create a new object.
40      */

41     public static Dimensions valueOf(int d)
42     {
43         switch(d) {
44             case 1: return Dimensions.ONE;
45             case 2: return Dimensions.TWO;
46             case 3: return Dimensions.THREE;
47             default:
48                 return new Dimensions(d);
49         }
50     }
51     /** returns dimensions for a matrix. **/
52     public static Dimensions valueOf(int rows,int cols)
53     {
54         return new Dimensions(rows,cols);
55     }
56     /** return a dimension [d,inDim[0],...,inDim[n]] */
57     public static Dimensions valueOf(int d,Dimensions inDim)
58     {
59         Dimensions res = new Dimensions();
60         res.dims = new int[inDim.rank()+1];
61         res.dims[0]=d;
62         for(int i=0;i<inDim.rank();++i)
63             res.dims[i+1]=inDim.dims[i];
64         return res;
65     }
66
67     /** return a dimension [inDim[0],...,inDim[n],d] */
68     public static Dimensions valueOf(Dimensions inDim,int d)
69     {
70         Dimensions res = new Dimensions();
71         res.dims = new int[inDim.rank()+1];
72         for(int i=0;i<inDim.rank();++i)
73             res.dims[i]=inDim.dims[i];
74         res.dims[inDim.rank()+1]=d;
75         return res;
76     }
77
78     /** returns a dimensions with given dimensions. */
79     public static Dimensions valueOf(int dims[])
80     {
81         if(dims.length == 1) return valueOf(dims[0]);
82         if(dims.length == 2) return valueOf(dims[0],dims[1]);
83         return new Dimensions(dims);
84     }
85
86     /** get the first dimension, 1 for numbers,
87         or the length of a vector.
88         for a matrix [[1,2,3],[4,5,6]] first dim is number of rows eg 2 */

89     public int getFirstDim() { return dims[0]; }
90     /** get the last dimension, 1 for numbers,
91         or the length of a vector.
92         for a matrix [[1,2,3],[4,5,6]] last dim is number of cols eg 3
93      */

94     public int getLastDim() { return dims[dims.length-1]; }
95     public int getIthDim(int i) { return dims[i]; }
96     
97     /** Is it 0D, ie a simple number. **/
98     public boolean is0D() { return dims.length == 1 && dims[0] == 1; }
99     /** Is it 1D, ie a vector [1,2,3]. **/
100     public boolean is1D() { return dims.length == 1 && dims[0] != 1; }
101     /** Is it 2D, ie a matrix [[1,2,3],[4,5,6]]. **/
102     public boolean is2D() { return dims.length == 2; }
103     /**
104      * The total number of elements.
105      * Produce of all the dimensions.
106      */

107     public int numEles()
108     {
109         int res=1;
110         for(int i=0;i<dims.length;++i) res *= dims[i];
111         return res;
112     }
113     /** rank of dimensions 0 for numbers, 1 for vectors, 2 for matricies */
114     public int rank()
115     {
116         if(is0D()) return 0;
117         return dims.length;
118     }
119     /** A string repsesentation.
120      * Either 1,n,[m,n],[l,m,n] etc.
121      */

122     public String JavaDoc toString()
123     {
124         if(is0D()) return String.valueOf(dims[0]);
125         if(is1D()) return String.valueOf(dims[0]);
126         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("["+dims[0]);
127         for(int i=1;i<dims.length;++i)
128             sb.append(","+dims[i]);
129         sb.append("]");
130         return sb.toString();
131     }
132
133     /** Two dimensions are equal if the element of dims are the same. */
134     public boolean equals(Dimensions dims2)
135     {
136         if(dims2 == null) return false;
137         if( dims.length != dims2.dims.length) return false;
138         for(int i=0;i<dims.length;++i)
139         { if(dims[i] != dims2.dims[i]) return false;}
140         return true;
141     }
142
143     /** apparently your should always override hashcode when you
144      * operride equals (Effective Java, Bloch).
145      */

146     public int hashcode()
147     {
148         int res =17;
149         for(int i=0;i<dims.length;++i)
150             res = 37*res + dims[i];
151         return res;
152     }
153 }
154
Popular Tags