Developed by one person (Ryuu Mitsuki) π₯
JMatrix is a lightweight Java library that provides essential matrix operations like addition, subtraction, multiplication, and determinant calculation. Designed with simplicity in mind, it is perfect for educational purposes and small-scale projects involving linear algebra. The library also supports custom matrix sizes and efficient handling of matrix operations for both square and rectangular matrices.
Important
This project was intended for educational purposes only. It is not recommended for use in large-scale projects.
JMatrix provides following basic matrix operations:
In addition to the fundamental matrix operations, JMatrix also includes matrix type checkers, allowing users to identify certain characteristics of matrices:
isDiagonal
- Check whether the matrix is diagonal.isSquare
- Check whether the matrix is square.isLowerTriangular
- Check whether the matrix is lower triangular.isUpperTriangular
- Check whether the matrix is upper triangular.isPermutationMatrix
- Check whether the matrix is permutation matrix.
What is Matrix?
Matrices are rectangular arrays of numbers or symbols, arranged in rows and columns.
They are widely used in various fields, including mathematics, physics, computer science, and engineering.
Matrices provide a concise and organized way to represent and manipulate data.Refer to π JMatrix Wiki, if want to know about matrix with simplified informations.
If you are interested in obtaining the latest stable version of the project, please check the latest version. You can download the archived package containing compiled classes from there.
For improved stability and better usability, we highly recommend downloading the archived package that also includes the source files (jmatrix-<VERSION>_with_sources.jar
).
This package contains all the necessary documentation about classes, methods, and other aspects related to JMatrix, making it easier to explore and understand the library APIs.
Warning
Currently, there is an issue with pre-build processes when using Make on Windows systems. The issue was that it failed while trying to create child processes to configure the build requirements.
For better functionality, we recommend using Maven instead. Because using Make is an alternative way for flexibility on UNIX systems.
To use JMatrix in your project, you will need the following prerequisites:
If you plan to build the JMatrix project, please ensure you have the following prerequisites:
- Java (min. JDK 11, recommended is JDK 17 and later).
- Python (min. version 3.7, recommended is 3.11 and later).
- Latest version of Make or Maven.
Important
If you choose to build the project using Maven, you don't need Python.
Sadly, we're having a problem with pre-build processes on Windows systems. It is recommended to use Maven instead if you're using Windows to build the project, ensuring better functionality.
For more detailed instructions on building the project, you can refer to the πGetting Started page.
Once you have the necessary prerequisites, you can start exploring and using JMatrix in your projects. The documentation included in the archived package will guide you through the classes, methods, and functionalities offered by the library.
There are five constructors available for constructing matrices in the JMatrix library. Each constructor serves a specific purpose, providing users with flexibility and ease of use.
Note
If you are unfamiliar with matrices or need a refresher, you can check the πIntroduction to Matrix page to gain a basic understanding before delving into matrix constructors.
public Matrix();
This constructor does not require any arguments and constructs a Matrix with null entries, resulting in a null matrix. A null matrix cannot perform any operations until it is initialized with valid elements. For example:
// Create a null entries matrix
Matrix m = new Matrix();
Note
Do not confuse the null matrix with the zero matrix. A null matrix has null entries, whereas the zero matrix contains all elements as zeros.
Examples
$$\begin{bmatrix} 0.0 & 0.0 & 0.0 \\\ 0.0 & 0.0 & 0.0 \end{bmatrix}_{2 \times 3}$$ null
Yes, that is a null matrix. It has none or null entries inside the matrix. The output above is the result of this code below:
Matrix nullM = new Matrix(); System.out.println( (nullM.getEntries() == null) ? nullM.getEntries() : nullM.toString());Also if you use either the
display()
orprettyDisplay()
method from null matrix, this output will be printed.<null_matrix>
public Matrix(int rows, int cols);
With this constructor, you can create a zero matrix with ease by providing two arguments: the number of rows and columns. A zero matrix contains all elements as zeros. For example:
// Create null matrix with size 3x4
Matrix m = new Matrix(3, 4);
The code above constructs a new zero matrix with size
public Matrix(int rows, int cols, double val);
This constructor is similar to Matrix(int, int)
but with an additional argument that sets the value for all elements of the constructed matrix. For example:
// Create a new matrix with size 4x4 and set all elements to 5.0
Matrix m = new Matrix(4, 4, 5);
The constructed matrix will looks like this:
public Matrix(double[][] arr);
This constructor is highly recommended for constructing a new matrix. You can declare the entries first and then convert them into a Matrix object whenever needed.
Note
Please note, this constructor only accepts two-dimensional array with type of double
.
For example:
// Declare and initialize entries "a"
double[][] a = {
{ 1, 2, 3 },
{ 4, 5, 6 }
};
// Convert to Matrix
Matrix m = new Matrix(a);
Alternatively, you can directly create a new matrix using this code:
// Create new matrix
Matrix m = new Matrix(new double[][] {
{ 1, 2, 3 },
{ 4, 5, 6 }
});
public static Matrix identity(int n);
This constructor creates a new identity matrix with a size of
Please avoid using for-loop
statements. Instead, consider using
// Create new identity matrix with size 5x5
Matrix mI = Matrix.identity(5);
The matrix will looks like this:
The JMatrix library provides several basic matrix operations that allow users to perform common matrix calculations with ease. These operations include:
For more detailed information about each matrix operation, you can refer to the π JMatrix Wiki.
public void sum(Matrix m);
public static Matrix sum(Matrix m, Matrix n);
public void sum(double[][] a);
public static double[][] sum(double[][] a, double[][] b);
π Wiki: Matrix Addition
In matrix addition, two matrices with the same dimensions are added together element-wise. Each element of the resulting matrix is the sum of the corresponding elements from the two input matrices.
Important
Before performing matrix addition, ensure that the two matrices have the same dimensions (in other words, square matrix).
Example code:
// Construct new matrices
Matrix m = new Matrix(new double[][] {
{ 6, 7, 0, 1 },
{ 2, 6, 1, 8 }
});
Matrix n = new Matrix(new double[][] {
{ 1, 9, 3, -4 },
{ 7, 1, -1, 5 }
});
// Perform addition for both matrices and
// create a new matrix as the resultant matrix
Matrix k = Matrix.sum(m, n);
Result:
In the example above, two matrices Matrix.sum(m, n)
method is used to add both matrices element-wise, and the resulting matrix
public void sub(Matrix m);
public static Matrix sub(Matrix m, Matrix n);
public void sub(double[][] a);
public static double[][] sub(double[][] a, double[][] b);
π Wiki: Matrix Subtraction
Matrix subtraction involves subtracting corresponding elements of one matrix from another.
Important
Before performing matrix subtraction, ensure that the two matrices have the same dimensions (in other words, square matrix).
Example code:
// Construct new matrices
Matrix m = new Matrix(new double[][] {
{ 1, -5, 8, -2, 3 },
{ 2, 12, -2, 7, 0 },
{ 6, 7, 9, 1, -5 }
});
Matrix n = new Matrix(new double[][] {
{ 4, 6, 8, -3, 9 },
{ -10, 6, 8, 1, 1 },
{ 6, -7, 2, 3, 5 }
});
// Perform subtraction for both matrices and
// create a new matrix as the resultant matrix
Matrix k = Matrix.sub(m, n);
Result:
In the example above, two matrices Matrix.sub(m, n)
method is used to subtract
public void mult(double x);
public static Matrix mult(Matrix m, double x);
π Wiki: Scalar Multiplication
Scalar multiplication involves multiplying all elements of a matrix by a scalar value. The resulting matrix will have each of its elements multiplied by the given scalar value.
Note
The resulting matrix's sizes of scalar multiplication will be always the same with the sizes of the operand matrix.
Example code:
// Construct new matrix
Matrix m = new Matrix(new double[][] {
{ 9, 6, 4 },
{ 2, 1, 5 }
});
// Perform scalar multiplication with the scalar equal to 5
// and create a new matrix as the resultant matrix
Matrix s = Matrix.mult(m, 5);
Result:
In the example above, a matrix Matrix.mult(m, 5)
method is used to multiply each element of matrix
public void mult(Matrix m);
public static Matrix mult(Matrix m, Matrix n);
public void mult(double[][] a);
public static double[][] mult(double[][] a, double[][] b);
π Wiki: Matrix Multiplication
Matrix multiplication involves multiplying two matrices together following a specific rule.
Important
Before performing matrix multiplication, ensure the number of columns in the first matrix must be equal to the number of rows in the second matrix.
Example code:
// Create and construct new matrices
Matrix m = new Matrix(new double[][] {
{ 2, 2, 2, 2 },
{ 2, 2, 2, 2 }
});
Matrix n = new Matrix(new double[][] {
{ 4, 4 },
{ 4, 4 },
{ 4, 4 },
{ 4, 4 }
});
// Operate matrix multiplication for both matrices
// and create a new matrix as the resultant matrix
Matrix mn = Matrix.mult(m, n);
Result:
In the example above, two matrices Matrix.mult(m, n)
method is used to perform matrix multiplication between matrices
public void transpose();
public static Matrix transpose(Matrix m);
public static double[][] transpose(double[][] a);
π Wiki: Matrix Transposition
Matrix transposition involves swapping the rows and columns of a matrix. The resulting matrix will have its rows and columns interchanged.
Note
π‘ Tip: Repeating this operation to the transposed matrix will reset their indices position to the original position.
Example code:
// Create and construct new matrix
Matrix m = new Matrix (new double[][] {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 }
});
// Transpose the matrix and create a new matrix
// to store the transposed matrix
Matrix mT = Matrix.transpose(m);
Result:
In the example above, a matrix Matrix.transpose(m)
method is used to transpose matrix
JMatrix is authored and maintained by Ryuu Mitsuki.
Please feel free to contribute to this project if you know of a problematic algorithm based on linear algebra concepts or if you wish to make any existing algorithm better. Any contributions is very appreciated and greatly helped me.
JMatrix is licensed under the Apache License 2.0. This license permits you to use, modify, distribute, and sublicense the software, subject to certain conditions.
You are free to use JMatrix for both commercial and non-commercial purposes. If you modify the software, you must clearly indicate the changes you made. Any contributions you make to the project are also subject to the same license terms.
The Apache License 2.0 allows you to distribute derivative works, but you must include the full text of the license in your distribution. Additionally, you are responsible for ensuring that any downstream recipients of the software are aware of its licensing terms.
For more details about the permissions, limitations, and conditions under which JMatrix is licensed, please refer to the LICENSE file provided with the project. The LICENSE file contains the complete text of "The License", ensuring full transparency and clarity regarding the terms of use for the software.
By using JMatrix, you agree to comply with the terms of the Apache License 2.0 and understand your rights and responsibilities as a user of this open-source software.