KickJava   Java API By Example, From Geeks To Geeks.

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


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 //import JSci.maths.DoubleMatrix;
12
//import JSci.physics.relativity.Rank1Tensor;
13

14 /**
15  * Represents a matrix.
16  *
17  * @author Rich Morris
18  * Created on 07-Jul-2003
19  * @version 2.3.0.2 now extends number
20  * @version 2.3.1.1 Bug with non square matricies fixed.
21  * @since 2.3.2 Added equals method.
22  */

23 public class Matrix extends Number JavaDoc implements MatrixValueI
24 {
25     // want package access to simplify addition of matricies
26
int rows=0;
27     int cols=0;
28     Object JavaDoc data[][] = null;
29     Dimensions dims;
30     
31     private Matrix() {}
32     /** Construct a matrix with given rows and cols. */
33     public Matrix(int rows,int cols)
34     {
35         this.rows = rows;
36         this.cols = cols;
37         data = new Object JavaDoc[rows][cols];
38         dims = Dimensions.valueOf(rows,cols);
39     }
40     
41     /**
42      * Construct a Matrix from a set of row vectors.
43      * @param vecs
44      */

45 /*
46     public Matrix(MVector[] vecs) throws ParseException
47     {
48         if(vecs==null) { throw new ParseException("Tried to create a matrix with null row vectors"); }
49         rows = vecs.length;
50         if(rows==0) { throw new ParseException("Tried to create a matrix with zero row vectors"); }
51         
52         // now check that each vector has the same size.
53         
54         cols = vecs[0].size();
55         for(int i = 1;i<rows;++i)
56             if(cols != vecs[i].size())
57                 throw new ParseException("Each vector must be of the same size");
58     
59         data = new Object[rows][cols];
60         for(int i = 0;i<rows;++i)
61             for(int j=0;j<cols;++j)
62             {
63                 data[i][j]= vecs[i].elementAt(j);
64                 if(data[i][j] == null)
65                     throw new ParseException("Null element in vector");
66             }
67     }
68 */

69     /**
70      * Returns a string rep of matrix. Uses [[a,b],[c,d]] syntax.
71      */

72     public String JavaDoc toString()
73     {
74         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
75         sb.append('[');
76         for(int i = 0;i<rows;++i)
77         {
78             if(i>0) sb.append(',');
79             sb.append('[');
80             for(int j=0;j<cols;++j)
81             {
82                 if(j>0)sb.append(',');
83                 sb.append(data[i][j]);
84             }
85             sb.append(']');
86         }
87         sb.append(']');
88         return sb.toString();
89     }
90     public Dimensions getDim() { return dims; }
91     public int getNumEles() { return rows*cols; }
92     public int getNumRows() { return rows; }
93     public int getNumCols() { return cols; }
94
95     public void setEle(int n,Object JavaDoc value)
96     {
97         int i = n / cols;
98         int j = n % cols;
99         data[i][j] = value;
100     }
101     public void setEle(int i,int j,Object JavaDoc value)
102     {
103         data[i][j] = value;
104     }
105     public Object JavaDoc getEle(int n)
106     {
107         int i = n / cols;
108         int j = n % cols;
109         return data[i][j];
110     }
111     public Object JavaDoc getEle(int i,int j)
112     {
113         return data[i][j];
114     }
115     
116     public Object JavaDoc[][] getEles()
117     {
118         return data;
119     }
120     /** sets the elements to those of the arguments. */
121     public void setEles(MatrixValueI val)
122     {
123         if(!dims.equals(val.getDim())) return;
124         for(int i=0;i<rows;++i)
125             System.arraycopy(((Matrix) val).data[i],0,data[i],0,cols);
126     }
127
128     /** value of ele(1,1). */
129     public int intValue() {return ((Number JavaDoc) data[0][0]).intValue(); }
130     /** value of ele(1,1). */
131     public long longValue() {return ((Number JavaDoc) data[0][0]).longValue(); }
132     /** value of ele(1,1). */
133     public float floatValue() { return ((Number JavaDoc) data[0][0]).floatValue(); }
134     /** value of ele(1,1). */
135     public double doubleValue() {return ((Number JavaDoc) data[0][0]).doubleValue(); }
136     /** Are two matricies equal, element by element
137      * Overrides Object.
138      */

139     public boolean equals(Object JavaDoc obj) {
140         if(!(obj instanceof Matrix)) return false;
141         Matrix mat = (Matrix) obj;
142         if(!mat.getDim().equals(getDim())) return false;
143         for(int i=0;i<rows;++i)
144             for(int j=0;j<cols;++j)
145                 if(!data[i][j].equals(mat.data[i][j])) return false;
146         return true;
147     }
148     
149     /**
150      * Always override hashCode when you override equals.
151      * Efective Java, Joshua Bloch, Sun Press
152      */

153     public int hashCode() {
154         int result = 17;
155 // long xl = Double.doubleToLongBits(this.re);
156
// long yl = Double.doubleToLongBits(this.im);
157
// int xi = (int)(xl^(xl>>32));
158
// int yi = (int)(yl^(yl>>32));
159
for(int i=0;i<rows;++i)
160             for(int j=0;j<cols;++j)
161             result = 37*result+ data[i][j].hashCode();
162         return result;
163     }
164 }
165
Popular Tags