/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package edu.packt.neuralnet.util;
import java.util.HashSet;
import java.util.Set;
/**
*
* @author Administrador
*/
public class Matrix {
private double[][] content;
private int numberOfRows;
private int numberOfColumns;
private Double determinant;
public Matrix(int nRows,int nColumns){
numberOfRows=nRows;
numberOfColumns=nColumns;
content = new double[numberOfRows][numberOfColumns];
}
public Matrix(double[][] matrix){
numberOfRows = matrix.length;
numberOfColumns = matrix[0].length;
content = matrix;
}
public Matrix(double[] vector){
numberOfRows = 1;
numberOfColumns = vector.length;
content = new double[numberOfRows][numberOfColumns];
content[0]=vector;
}
public Matrix(Matrix a){
numberOfRows=a.getNumberOfRows();
numberOfColumns=a.getNumberOfColumns();
content = new double[numberOfRows][numberOfColumns];
for(int i=0;i<numberOfRows;i++){
for(int j=0;j<numberOfColumns;j++){
setValue(i,j,a.getValue(i,j));
}
}
}
public Matrix add(Matrix a){
int nRows = a.getNumberOfRows();
int nColumns = a.getNumberOfColumns();
if(numberOfRows!=a.getNumberOfRows())
throw new IllegalArgumentException("Number of rows of both matrices must match");
if(numberOfColumns!=a.getNumberOfColumns())
throw new IllegalArgumentException("Number of colmns of both matrices must match");
Matrix result = new Matrix(nRows,nColumns);
for(int i=0;i<nRows;i++){
for(int j=0;j<nColumns;j++){
result.setValue(i, j, getValue(i,j)+a.getValue(i, j));
}
}
return result;
}
public static Matrix add(Matrix a,Matrix b){
int nRows = a.getNumberOfRows();
int nColumns = a.getNumberOfColumns();
if(a.numberOfRows!=b.getNumberOfRows())
throw new IllegalArgumentException("Number of rows of both matrices must match");
if(a.numberOfColumns!=b.getNumberOfColumns())
throw new IllegalArgumentException("Number of colmns of both matrices must match");
Matrix result = new Matrix(nRows,nColumns);
for(int i=0;i<nRows;i++){
for(int j=0;j<nColumns;j++){
result.setValue(i, j, a.getValue(i,j)+b.getValue(i, j));
}
}
return result;
}
public Matrix subtract(Matrix a){
int nRows = a.getNumberOfRows();
int nColumns = a.getNumberOfColumns();
if(numberOfRows!=a.getNumberOfRows())
throw new IllegalArgumentException("Number of rows of both matrices must match");
if(numberOfColumns!=a.getNumberOfColumns())
throw new IllegalArgumentException("Number of colmns of both matrices must match");
Matrix result = new Matrix(nRows,nColumns);
for(int i=0;i<nRows;i++){
for(int j=0;j<nColumns;j++){
result.setValue(i, j, getValue(i,j)-a.getValue(i, j));
}
}
return result;
}
public static Matrix subtract(Matrix a,Matrix b){
int nRows = a.getNumberOfRows();
int nColumns = a.getNumberOfColumns();
if(a.numberOfRows!=b.getNumberOfRows())
throw new IllegalArgumentException("Number of rows of both matrices must match");
if(a.numberOfColumns!=b.getNumberOfColumns())
throw new IllegalArgumentException("Number of colmns of both matrices must match");
Matrix result = new Matrix(nRows,nColumns);
for(int i=0;i<nRows;i++){
for(int j=0;j<nColumns;j++){
result.setValue(i, j, a.getValue(i,j)-b.getValue(i, j));
}
}
return result;
}
public Matrix transpose(){
Matrix result = new Matrix(numberOfColumns,numberOfRows);
for(int i=0;i<numberOfRows;i++){
for(int j=0;j<numberOfColumns;j++){
result.setValue(j, i, getValue(i,j));
}
}
return result;
}
public static Matrix transpose(Matrix a){
Matrix result = new Matrix(a.getNumberOfColumns(),a.getNumberOfRows());
for(int i=0;i<a.getNumberOfRows();i++){
for(int j=0;j<a.getNumberOfColumns();j++){
result.setValue(j, i, a.getValue(i,j));
}
}
return result;
}
public Matrix multiply(Matrix a){
Matrix result = new Matrix(getNumberOfRows(),a.getNumberOfColumns());
if(getNumberOfColumns()!=a.getNumberOfRows())
throw new IllegalArgumentException("Number of Columns of first Matrix must match the number of Rows of second Matrix");
for(int i=0;i<getNumberOfRows();i++){
for(int j=0;j<a.getNumberOfColumns();j++){
double value = 0;
for(int k=0;k<a.getNumberOfRows();k++){
value+=getValue(i,k)*a.getValue(k,j);
}
result.setValue(i, j, value);
}
}
return result;
}
public Matrix multiply(double a){
Matrix result = new Matrix(getNumberOfRows(),getNumberOfColumns());
for(int i=0;i<getNumberOfRows();i++){
for(int j=0;j<getNumberOfColumns();j++){
result.setValue(i, j, getValue(i,j)*a);
}
}
return result;
}
public static Matrix multiply(Matrix a,Matrix b){
Matrix result = new Matrix(a.getNumberOfRows(),b.getNumberOfColumns());
if(a.getNumberOfColumns()!=b.getNumberOfRows())
throw new IllegalArgumentException("Number of Columns of first Matrix must match the number of Rows of second Matrix");
for(int i=0;i<a.getNumberOfRows();i++){
for(int j=0;j<b.getNumberOfColumns();j++){
double value = 0;
for(int k=0;k<b.getNumberOfRows();k++){
value+=a.getValue(i,k)*b.getValue(k,j);
}
result.setValue(i, j, value);
}
}
return result;
}
public static Matrix multiply(Matrix a, double b){
Matrix result = new Matrix(a.getNumberOfRows(),a.getNumberOfColumns());
for(int i=0;i<a.getNumberOfRows();i++){
for(int j=0;j<a.getNumberOfColumns();j++){
result.setValue(i, j, a.getValue(i,j)*b);
}
}
return result;
}
public Matrix[] LUdecomposition(){
Matrix[] result = new Matrix[2];
Matrix LU = new Matrix(this);
Matrix L = new Matrix(LU.getNumberOfRows(),LU.getNumberOfColumns());
L.setZeros();
L.setValue(0,0,1.0);
for(int i=1;i<LU.getNumberOfRows();i++){
L.setValue(i,i,1.0);
for(int j=0;j<i;j++){
double multiplier = -LU.getValue(i, j)/LU.getValue(j, j);
LU.sumRowByRow(i, j, multiplier);
L.setValue(i, j, -multiplier);
}
}
Matrix U = new Matrix(LU);
result[0]=L;
result[1]=U;
return result;
}
public void multiplyRow(int row, double multiplier){
if(row>getNumberOfRows())
throw ne