Skip to content

Instantly share code, notes, and snippets.

@rossGardiner
Created March 10, 2015 19:43
Show Gist options
  • Save rossGardiner/1114f37fc368d04896e7 to your computer and use it in GitHub Desktop.
Save rossGardiner/1114f37fc368d04896e7 to your computer and use it in GitHub Desktop.
public class Matrix
{
private int rows;
private int columns;
private double[][]theMatrix;
/**
* Constructor for class Matrix
* @param setRows
* @param setColumns
*/
public Matrix (int setRows, int setColumns)
{
this.rows = setRows;
this.columns = setColumns;
this.theMatrix = this.createMatrix();
}
/**
* Creates an array according to this.rows and this.columns
* @return
*/
protected double[][] createMatrix()
{
return new double[this.getRows()][this.getColumns()];
}
public double[][] getMatrix()
{
return this.theMatrix;
}
public int getRows()
{
return this.rows;
}
public int getColumns()
{
return this.columns;
}
/**
* sets a value within the matrix
* @param posX
* @param posY
* @param val
*/
public void setAMatrixValue( int posX, int posY, double val)
{
this.theMatrix[posX][posY] = val;
}
public void setMatrix(double[][] mat)
{
this.theMatrix = mat;
}
/**
* returns a value within the matrix
* @param Xpos
* @param Ypos
* @return
*/
public double getAMatrixValue(int Xpos, int Ypos)
{
return this.theMatrix[Xpos][Ypos];
}
/**
* multiplies all components in the matrix by a factor
* @param aFactor
*/
public void multiplyMatrix(double aFactor)
{
for (int i = 0; i < this.getRows(); i++)
{
for (int j = 0; j <this.getColumns(); j++)
{
this.theMatrix[i][j] = this.getAMatrixValue(i, j) * aFactor;
}
}
}
/**
* multiplies a matrix by a factor
* overloads multiplyMatrix(double)
* in Matrix class
* @param factor
* @param mat
* @return
*/
public double[][] multiplyMatrix(double factor, double[][] mat)
{
for(int i = 0; i < this.getRows(); i++)
{
for (int j =0; j < this.getColumns(); j++)
mat[i][j] = (mat[i][j] * factor);
}
return mat;
}
/**
* prints a component of the matrix
* @param Xpos
* @param Ypos
*/
public void printAVal(int Xpos, int Ypos)
{
System.out.println(this.getAMatrixValue(Xpos, Ypos));
}
/**
* prints all components of the matrix
* @param aMatrix
*/
public void matrixToString(double[][] aMatrix)
{
for (int i = 0; i < this.getRows(); i++)
{
for (int j =0; j <this.getColumns(); j++)
{
System.out.println(aMatrix[i][j]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment