Skip to content

A lightweight Java library for basic introduction to matrix and linear algebra concepts.

License

Notifications You must be signed in to change notification settings

mitsuki31/jmatrix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

JMatrix

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:

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:

  • Java (min. JDK 11, recommended is JDK 17 and later)
  • Git Bash (optional, Windows only)

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

Zero Matrix

$$\begin{bmatrix} 0.0 & 0.0 & 0.0 \\\ 0.0 & 0.0 & 0.0 \end{bmatrix}_{2 \times 3}$$

Null Matrix

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() or prettyDisplay() 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 $3 \times 4$. The matrix will looks like this:

$$\begin{bmatrix} 0.0 & 0.0 & 0.0 & 0.0 \\\ 0.0 & 0.0 & 0.0 & 0.0 \\\ 0.0 & 0.0 & 0.0 & 0.0 \end{bmatrix}$$
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:

$$\begin{bmatrix} 5.0 & 5.0 & 5.0 & 5.0 \\\ 5.0 & 5.0 & 5.0 & 5.0 \\\ 5.0 & 5.0 & 5.0 & 5.0 \\\ 5.0 & 5.0 & 5.0 & 5.0 \end{bmatrix}$$
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 $n \times n$ (where $n$ is a non-floating number). An identity matrix is a square matrix with ones on the main diagonal and zeros elsewhere, often denoted as $I$.

Please avoid using $I$ or $i$ as variable names for matrices in code, as $i$ is commonly used in for-loop statements. Instead, consider using $mI$ or a similar alternative. For instance:

// Create new identity matrix with size 5x5
Matrix mI = Matrix.identity(5);

The matrix will looks like this:

$$\begin{bmatrix} 1.0 & 0.0 & 0.0 & 0.0 & 0.0 \\\ 0.0 & 1.0 & 0.0 & 0.0 & 0.0 \\\ 0.0 & 0.0 & 1.0 & 0.0 & 0.0 \\\ 0.0 & 0.0 & 0.0 & 1.0 & 0.0 \\\ 0.0 & 0.0 & 0.0 & 0.0 & 1.0 \end{bmatrix}$$

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:

$$\mathbf{k} = \begin{bmatrix} 7.0 & 16.0 & 3.0 & -3.0 \\\ 9.0 & 7.0 & 0.0 & 13.0 \end{bmatrix}$$

In the example above, two matrices $m$ and $n$ are created. The Matrix.sum(m, n) method is used to add both matrices element-wise, and the resulting matrix $k$ is computed and stored. The output matrix $k$ is the sum of matrices $m$ and $n$.

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:

$$\mathbf{k} = \begin{bmatrix} -3.0 & -11.0 & 0.0 & 1.0 & -6.0 \\\ 12.0 & 6.0 & -10.0 & 6.0 & -1.0 \\\ 0.0 & 14.0 & 7.0 & -2.0 & -10.0 \end{bmatrix}$$

In the example above, two matrices $m$ and $n$ are created. The Matrix.sub(m, n) method is used to subtract $n$ from $m$ element-wise, and the resulting matrix $k$ is computed and stored. The output matrix $k$ is the difference between matrices $m$ and $n$.

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:

$$\mathbf{s} = \begin{bmatrix} 45.0 & 30.0 & 20.0 \\\ 10.0 & 5.0 & 25.0 \end{bmatrix}$$

In the example above, a matrix $m$ is created. The Matrix.mult(m, 5) method is used to multiply each element of matrix $m$ by the scalar value $5$, resulting in a new matrix $s$.

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:

$$\mathbf{mn} = \begin{bmatrix} 32.0 & 32.0 \\\ 32.0 & 32.0 \end{bmatrix}$$

In the example above, two matrices $m$ and $n$ are created. The Matrix.mult(m, n) method is used to perform matrix multiplication between matrices $m$ and $n$, resulting in a new matrix $mn$.

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:

$$\mathbf{mT} = \begin{bmatrix} 1.0 & 5.0 \\\ 2.0 & 6.0 \\\ 3.0 & 7.0 \\\ 4.0 & 8.0 \end{bmatrix}$$

In the example above, a matrix $m$ is created. The Matrix.transpose(m) method is used to transpose matrix $m$, resulting in a new matrix $mT$ with the rows and columns interchanged.

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.