KickJava   Java API By Example, From Geeks To Geeks.

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


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
10 import org.lsmp.djep.vectorJep.*;
11
12 /**
13  * A Vector of elements.
14  * @author Rich Morris
15  * Created on 07-Jul-2003
16  * @version 1.3.0.2 now extends number
17  */

18
19 public class MVector extends Number JavaDoc implements MatrixValueI
20 {
21     private Object JavaDoc data[] = null;
22     private Dimensions dim;
23     //DoubleMatrix jsciMat;
24

25     private MVector() {}
26     /** constructs a vector of a given size. **/
27     public MVector(int size)
28     {
29         data = new Object JavaDoc[size];
30         dim = Dimensions.valueOf(size);
31     }
32     
33     /**
34      * Construct a Vector from a set of elements.
35      * @param vecs
36      */

37 /*
38     public MVector(Object[] eles) throws ParseException
39     {
40         if(eles==null) { throw new ParseException("Tried to create an MVector with null elements"); }
41         size = eles.length;
42         if(size==0) { throw new ParseException("Tried to create an Mvector with zero elements"); }
43         
44         // now check that each vector has the same size.
45         
46         data = eles;
47     }
48 */

49     public String JavaDoc toString()
50     {
51         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
52         sb.append('[');
53         for(int i = 0;i<data.length;++i)
54         {
55                 if(i>0) sb.append(',');
56                 sb.append(data[i]);
57         }
58         sb.append(']');
59         return sb.toString();
60     }
61
62     public Dimensions getDim() { return dim; }
63     public int getNumEles() { return data.length; }
64     public void setEle(int i,Object JavaDoc value) { data[i] = value; }
65     public Object JavaDoc getEle(int i) { return data[i]; }
66     /** sets the elements to those of the arguments. */
67     public void setEles(MatrixValueI val)
68     {
69         if(!dim.equals(val.getDim())) return;
70         System.arraycopy(((MVector) val).data,0,data,0,getNumEles());
71     }
72
73     /** value of constant ele(1). */
74     public int intValue() {return ((Number JavaDoc) data[0]).intValue(); }
75     /** value of constant ele(1). */
76     public long longValue() {return ((Number JavaDoc) data[0]).longValue(); }
77     /** value of constant ele(1). */
78     public float floatValue() { return ((Number JavaDoc) data[0]).floatValue(); }
79     /** value of constant ele(1). */
80     public double doubleValue() {return ((Number JavaDoc) data[0]).doubleValue(); }
81
82     public boolean equals(Object JavaDoc obj) {
83         if(!(obj instanceof MVector)) return false;
84         MVector vec = (MVector) obj;
85         if(!vec.getDim().equals(getDim())) return false;
86         for(int i=0;i<data.length;++i)
87                 if(!data[i].equals(vec.data[i])) return false;
88         return true;
89     }
90
91     /**
92      * Always override hashCode when you override equals.
93      * Efective Java, Joshua Bloch, Sun Press
94      */

95     public int hashCode() {
96         int result = 17;
97         for(int i=0;i<data.length;++i)
98             result = 37*result+ data[i].hashCode();
99         return result;
100     }
101     
102 }
103
Popular Tags