Package com.katiejergens.matrixmath
Class Matrix
- java.lang.Object
-
- com.katiejergens.matrixmath.Matrix
-
public class Matrix extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description double[][]
matrix
Holds the values of the matrix
-
Constructor Summary
Constructors Constructor Description Matrix()
Default constructorMatrix(double[][] matrix)
Create a Matrix object from a 2D array.Matrix(int rows, int columns)
Create a Matrix object with the given number of rows and columns.Matrix(int rows, int columns, double value)
Create a Matrix object with the given number of rows and columns and set all spots to the given value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Matrix
augment()
Return an augmented matrix, which is the matrix concatenated to the identity matrix.int
getColumnCount()
Get how many columns.double[][]
getMatrix()
Get the matrix array.int
getRowCount()
Get how many rows.double
getValueAt(int row, int column)
Get the specific value at the given row and column.Matrix
invert()
Inverts a matrix.Matrix
linearCombRows(double scalar, int sourceRow, int destRow)
Adds a scalar multiple of the first row to the second row.Matrix
plus(Matrix matrix)
Add matrices and return the sum.Matrix
rowReduce()
Row reduces a matrix.double[]
scalarTimesRow(double scalar, int row)
Multiplies a scalar times a row.void
setEntry(int row, int column, double value)
Set a specific value at the given row and column.Matrix
switchRows(int firstrow, int secondrow)
Switch two rows of a matrix.Matrix
times(Matrix matrix)
Multiply this matrix to the given matrix and return the product.java.lang.String
toString()
Composes a string representation of the matrix
-
-
-
Constructor Detail
-
Matrix
public Matrix()
Default constructor
-
Matrix
public Matrix(double[][] matrix)
Create a Matrix object from a 2D array.- Parameters:
matrix
- a 2D array of doubles
-
Matrix
public Matrix(int rows, int columns)
Create a Matrix object with the given number of rows and columns. It will automatically initialize with all zeros.- Parameters:
rows
- number of rowscolumns
- number of columns
-
Matrix
public Matrix(int rows, int columns, double value)
Create a Matrix object with the given number of rows and columns and set all spots to the given value.- Parameters:
rows
-columns
-value
-
-
-
Method Detail
-
getMatrix
public double[][] getMatrix()
Get the matrix array.- Returns:
- a 2D double array
-
getRowCount
public int getRowCount()
Get how many rows.- Returns:
- int
-
getColumnCount
public int getColumnCount()
Get how many columns.- Returns:
- int
-
getValueAt
public double getValueAt(int row, int column)
Get the specific value at the given row and column.- Parameters:
row
-column
-- Returns:
- double
-
setEntry
public void setEntry(int row, int column, double value)
Set a specific value at the given row and column.- Parameters:
row
-column
-value
-
-
plus
public Matrix plus(Matrix matrix)
Add matrices and return the sum. Does not change this matrix.- Parameters:
matrix
- Matrix to add- Returns:
- The sum of this matrix and the given matrix.
-
scalarTimesRow
public double[] scalarTimesRow(double scalar, int row)
Multiplies a scalar times a row. For example, trixie.scalarTimesRow(2.0,3) multiplies row three of trixie by two.- Parameters:
scalar
- The value to multiply byrow
- Row number to change- Returns:
- An array/row of the products.
-
times
public Matrix times(Matrix matrix)
Multiply this matrix to the given matrix and return the product. (Does not change this matrix.)- Parameters:
matrix
-- Returns:
- a Matrix
-
switchRows
public Matrix switchRows(int firstrow, int secondrow)
Switch two rows of a matrix. For example, trixie.switchRows(3,5) exchanges rows three and five of the matrix trixie.- Parameters:
firstrow
-secondrow
-- Returns:
- The switched matrix.
-
linearCombRows
public Matrix linearCombRows(double scalar, int sourceRow, int destRow)
Adds a scalar multiple of the first row to the second row. For example, trixie.linearCombRows(.5,3,2) adds .5 times row three to row two.- Parameters:
scalar
-sourceRow
-destRow
-- Returns:
- The new matrix.
-
rowReduce
public Matrix rowReduce()
Row reduces a matrix. For example, trixie.rowreduce() should return the row reduction of trixie. rowreduce should work for all matrices.- Returns:
- The reduced matrix
-
augment
public Matrix augment()
Return an augmented matrix, which is the matrix concatenated to the identity matrix. E.g. 1 0 1 0 0 1 2 1 0 0 1 2 0 0 0 1 The augmented form is: 1 0 1 0 1 0 0 0 0 1 2 1 0 1 0 0 0 0 1 2 0 0 1 0 0 0 0 1 0 0 0 1- Returns:
-
invert
public Matrix invert()
Inverts a matrix. For example, trixie.invert() should return the inverse matrix of trixie. invert should work for square matrices which are invertible.- Returns:
- The inverted matrix
-
toString
public java.lang.String toString()
Composes a string representation of the matrix- Overrides:
toString
in classjava.lang.Object
- Returns:
- A string of the rows x columms.
-
-