1 8 package org.lsmp.djep.vectorJep.values; 9 import org.lsmp.djep.vectorJep.*; 10 11 14 23 public class Matrix extends Number implements MatrixValueI 24 { 25 int rows=0; 27 int cols=0; 28 Object data[][] = null; 29 Dimensions dims; 30 31 private Matrix() {} 32 33 public Matrix(int rows,int cols) 34 { 35 this.rows = rows; 36 this.cols = cols; 37 data = new Object [rows][cols]; 38 dims = Dimensions.valueOf(rows,cols); 39 } 40 41 45 69 72 public String toString() 73 { 74 StringBuffer sb = new StringBuffer (); 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 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 value) 102 { 103 data[i][j] = value; 104 } 105 public Object getEle(int n) 106 { 107 int i = n / cols; 108 int j = n % cols; 109 return data[i][j]; 110 } 111 public Object getEle(int i,int j) 112 { 113 return data[i][j]; 114 } 115 116 public Object [][] getEles() 117 { 118 return data; 119 } 120 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 129 public int intValue() {return ((Number ) data[0][0]).intValue(); } 130 131 public long longValue() {return ((Number ) data[0][0]).longValue(); } 132 133 public float floatValue() { return ((Number ) data[0][0]).floatValue(); } 134 135 public double doubleValue() {return ((Number ) data[0][0]).doubleValue(); } 136 139 public boolean equals(Object 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 153 public int hashCode() { 154 int result = 17; 155 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 |