1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
/*
* Matrix.java
*
* Created on 22 mars 2007, 16:01
*
*/
package org.umh.math.linear;
/**
* This class models a Matrix object that will be able to
* @author Absil Romain
*/
public class Matrix
{
/**
* The rowspace of the matrix (i.e. the set of its rows view as a set of row
* vectors.
*/
protected Vector[] rowSpace;
/**
* The number of columns of the matrix.
*/
protected int n;
/**
* The number of rows of the matrix.
*/
protected int m;
/**
* Conctructs a new num matrix with m columns and n rows.
* @param m the number of rows of the matrix.
* @param n the number of columns of the matrix.
**/
public Matrix(int m, int n)
{
this.rowSpace = new Vector[m];
for (int i = 0; i < rowSpace.length; i++)
rowSpace[i] = new Vector(n);
this.n = m;
this.m = n;
}
/**
* Constructs a new matrix with the given 2D array.
* @param cofs the coefficients of the matrix.
**/
public Matrix(double[][] cofs)
{
this.n = cofs[0].length;
this.m = cofs.length;
this.rowSpace = new Vector[m];
for (int i = 0; i < m; i++)
rowSpace[i] = new Vector(cofs[i]);
}
/**
* Construcs a new matrix with the rows are the given vectors.
* @param vectors the rows of the matrix.
**/
public Matrix(Vector ... vectors)
{
this.m = vectors.length;
this.n = vectors[0].size();
rowSpace = new Vector[m];
rowSpace[0] = vectors[0];
for (int i = 1; i < m; i++)
{
if(vectors[i].size() != n)
throw new IllegalArgumentException(
"All vectors must be the same dimension");
rowSpace[i] = vectors[i];
}
}
/**
* Returns the line space of the matrix.
* @return the line space of the matrix.
**/
public Vector[] getRowSpace()
{
return rowSpace;
}
/**
* Returns the number of rows of the current matrix.
* @return the number of rows of the current matrix.
**/
public int getRowsNumber()
{
return m;
}
/**
* Returns the number of columns of the current matrix.
* @return the number of columns of the current matrix.
**/
public int getColumnsNumber()
{
return n;
}
/**
* Returns the element (i,j) of the current matrix.
* @param i the row of the element to return.
* @param j the column of the element to return.
* @return the element (i,j) of the current matrix.
**/
public double get(int i, int j)
{
return rowSpace[i].getComp(j);
}
/**
* Returns the row indexed by the given number of the current matrix.
* @return the row indexed by the given number of the current matrix.
* @param i the index of the row you want to be returned.
*/
public Vector getRow(int i)
{
return rowSpace[i];
}
/**
* Returns the column indexed by the given number of the current matrix.
* @return the column indexed by the given number of the current matrix.
* @param i the index of the columns you want to be returned.
*/
public Vector getColumn(int i)
{
Vector vector = new Vector(m);
for (int j = 0; j < m; j++)
vector.setComp(j,rowSpace[j].getComp(i));
return vector;
}
/**
* Sets the element (i,j) to the given value.
* @param i the row of the element to set.
* @param j the column of the element to set.
* @param value the element to set.
**/
public void set(int i, int j, double value)
{
rowSpace[i].setComp(j,value);
}
/**
* Sets the line of the given matrix to the given vector.
* @param i the line to set.
* @param vector the nex value of the line.
**/
public void setRow(int i, Vector vector)
{
rowSpace[i] = vector;
}
/**
* Sets the column of the given matrix to the given vector.
* @param i the column to set.
* @param vector the nex value of the column.
**/
public void setColumn(int i, Vector vector)
{
for (int j = 0; j < m; j++)
rowSpace[j].setComp(i,vector.getComp(j));
}
/**
* Extracts a submatrix from the current matrix at the elements (l,c) and the rank (height, width).
* @param l the index of the row you want the matrix to be extracted.
* @param c the index of the column you want the matrix to be extracted
* @param height the height of the extracted matrix
* @param width the width of the extracted matrix
* @return a submatrix from the current matrix at the elements (l,c) and the rank (height, width).
*/
public Vector[] subMatrix(int l, int c, int height, int width)
{
Vector[] matrix = new Vector[height];
for(int i = 0; i < height; i++)
{
Vector v = new Vector(width);
for(int j = 0; j < width; j++)
v.setComp(j,get(l+i,c+j));
matrix[i] = v;
}
return matrix;
}
/**
* Returns true if the given object and the current matrix are equals,
* returns false otherwise.
* @param other the object you want to know if it is equals to the current matrix.
* @return true if the given object and the current matrix are equals,
* returns false otherwise.
*/
public boolean equals(Object other)
{
if(!(other instanceof Matrix))
return false;
Matrix matrix = (Matrix)other;
for (int i = 0; i < m; i++)
if(!rowSpace[i].equals(matrix.getRow(i)))
return false;
return true;
}
/**
* Returns a copy of the current matrix.
* @return a copy of the current matrix.
*/
public Matrix clone()
{
return new Matrix(rowSpace);
}
/**
* Returns the String representation of the current matrix.
* @return the String representation of the current matrix.
**/
public String toString()
{
String s = "";
for(int i = 0 ; i < m ; i++)
for(int j = 0 ; j < n ; j++)
{
s += get(i,j) + "\t";
if(j == n-1)
s += "\n";
}
return s;
}
/**
* Adds the given matrix to the current matrix and return the result of
* the addition.
* @param matrix the matrix to add.
* @return the result of the addition of the current matrix to the given other matrix.
*/
public Matrix add(Matrix matrix)
{
if(n != matrix.getRowsNumber() || m != matrix.getColumnsNumber())
throw new IllegalArgumentException();
Matrix added = clone();
for (int i = 0; i < m; i++)
added.setRow(i,matrix.getRow(i).add(matrix.getRow(i)));
return added;
}
/**
* Returns the opposite of the current matrix for additive law.
* @return the opposite of the current matrix for additive law.
*/
public Matrix getOpposite()
{
Vector[] oppositeRows = new Vector[m];
for (int i = 0; i < oppositeRows.length; i++)
oppositeRows[i] = rowSpace[i].getOpposite();
return new Matrix(oppositeRows);
}
/**
* Substracts the given matrix to the current matrix.
* @param matrix the matrix to substract
* @return the result of the substraction of the current matrix and the given other matrix.
*/
public Matrix substract(Matrix matrix)
{
if(n != matrix.getRowsNumber() || m != matrix.getColumnsNumber())
throw new IllegalArgumentException();
Matrix added = clone();
for (int i = 0; i < m; i++)
added.setRow(i,matrix.getRow(i).substract(matrix.getRow(i)));
return added;
}
/**
* Multiplies the current matrix by the given scalar, i.e. by a real number.
* @param scalar the number you want the current matrix to be multiplied by.
* @return the result of the multiplication of the current matrix by the given scalar.
*/
public Matrix multByScal(double scalar)
{
Matrix added = clone();
for (int i = 0; i < m; i++)
added.setRow(i,added.getRow(i).multByScal(scalar));
return added;
}
/**
* Multiplies the current matrix by another matrix.
* @param matrix the matrix you want multiply the current matrix by.
* @return the result of the multiplication of the current matrix by the given other matrix.
*/
public Matrix multiply(Matrix matrix)
{
Matrix mult = clone();
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
mult.set(i,j,mult.getRow(i).multiply(matrix.getColumn(j)));
return mult;
}
/**
* Sets the upper part of the matrix triangular and return the factor by
* wich the determinant must be multiplied in case of suare matrix.
* @return the number by wich the determinant of the matrix must be
* multiplied by after the triangularisation.
*/
public double setTriangularSup()
{
int rowIndex = 0;
int columnIndex = 0;
double factor = 1; //factor to return
while(columnIndex < n && rowIndex < m)
{
//if the pivot = 0, swap line
if(get(rowIndex,columnIndex) == 0)
{
int index = indexofFirstDownNonNullPivot(columnIndex);
if(index != -1) //pivot !=0 found
{
swapRow(rowIndex,index);
factor *= -1;
}
else //if no pivot != 0, nothing to do
{
columnIndex++;
factor = 0; //det = 0
continue; //don't make the rest of the loop
}
}
//make null all the elements abose the pivot
// L_i <- L_i - k * L_{pivot != 0} and k choosen to make the
// element i,columnIndex null
double pivot = get(rowIndex, columnIndex);
for(int i = rowIndex + 1; i < m ; i++)
setRow(i,rowSpace[i].substract(
rowSpace[rowIndex].multByScal(get(i,columnIndex)
/ pivot)));
//increment line and column index to continue the algorithm
rowIndex++;
columnIndex++;
}
return factor;
}
public double setTriangularSup(Vector independant)
{
int rowIndex = 0;
int columnIndex = 0;
double factor = 1; //factor to return
while(columnIndex < n && rowIndex < m)
{
//if the pivot = 0, swap line
if(get(rowIndex,columnIndex) == 0)
{
int index = indexofFirstDownNonNullPivot(columnIndex);
if(index != -1) //pivot !=0 found
{
swapRow(rowIndex,index);
factor *= -1;
}
else //if no pivot != 0, nothing to do
{
columnIndex++;
factor = 0; //det = 0
continue; //don't make the rest of the loop
}
}
//make null all the elements abose the pivot
// L_i <- L_i - k * L_{pivot != 0} and k choosen to make the
// element i,columnIndex null
double pivot = get(rowIndex, columnIndex);
for(int i = rowIndex + 1; i < m ; i++)
{
if(independant != null)
independant.setComp(i, independant.getComp(i) -
independant.getComp(rowIndex) *
get(i,columnIndex) / pivot);
setRow(i,rowSpace[i].substract(
rowSpace[rowIndex].multByScal(get(i,columnIndex)
/ pivot)));
}
//increment line and column index to continue the algorithm
rowIndex++;
columnIndex++;
}
return factor;
}
/**
* Sets the lower part of the matrix triangular and return the factor by
* wich the determinant must be multiplied in case of suare matrix.
* @return the number by wich the determinant of the matrix must be multiplied by after the triangularisation.
*/
public double setTriangularInf()
{
int rowIndex = n - 1;
int columnIndex = m - 1;
double factor = 1;
while(rowIndex >= 0 && columnIndex >= 0)
{
//if the pivot = 0, swap line
if(get(rowIndex,columnIndex) == 0)
{
int index = indexofFirstUpNonNullPivot(columnIndex);
if(index != -1) //pivot !=0 found
{
swapRow(rowIndex,index);
factor *= -1;
}
else //if no pivot != 0, nothing to do
{
columnIndex--;
factor = 0; //det = 0
continue; //don't make the rest of the loop
}
}
//make null all the elements abose the pivot
// L_i <- L_i - k * L_{pivot != 0} and k choosen to make the
// element i,columnIndex null
double pivot = get(rowIndex, columnIndex);
for(int i = rowIndex - 1; i >= 0 ; i--)
setRow(i,rowSpace[i].substract(
rowSpace[rowIndex].multByScal(get(i,columnIndex)
/ pivot)));
//increment line and column index to continue the algorithm
rowIndex--;
columnIndex--;
}
return factor;
}
// swaps the line i and the line j
private void swapRow(int i, int j)
{
Vector v = rowSpace[i];
rowSpace[i] = rowSpace[j];
rowSpace[j] = v;
}
//returns the index of the first no null pivot of the matrix at the
//given column, returns -1 if no non null pivots at the given column
private int indexofFirstDownNonNullPivot(int column)
{
Vector vector = getColumn(column);
for (int i = 0; i < m; i++)
if(vector.getComp(i) == 0)
return i;
return -1;
}
private int indexofFirstUpNonNullPivot(int column)
{
Vector vector = getColumn(column);
for (int i = m - 1; i >= 0; i--)
if(vector.getComp(i) == 0)
return i;
return -1;
}
} |
Partager