Skip to content

This is a Java library for computational Linear Algebra. It is a work in progress.

Notifications You must be signed in to change notification settings

amukh1/Java-Linear

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Java-Linear - Amukh1

This is a Java library for computational Linear Algebra. It is a work in progress.

Features

  • Vectors
  • Matrices
  • Vector operations
  • Matrix operations
  • Matrix-Vector operations
  • Matrix output
  • Vector output
  • Spaces (Soon!)
  • Space operations (Soon!)
  • Subspaces (Soon!)

Usage

Defining Matrices and Vectors

Matrices

Constant elements:

// 2 x 2 Matrix by rows:
double[][] rows = {{1, 2} {3, 4}};
new Matrix(rows);

Non-constant elements:

// n x m Matrix by rows:
double[][] rows = new double[n][m];
for(int i = 0; i < n; i++) {
    // fill n arrays with m elements
}
new Matrix(rows);

Vectors

Constant components/elements:

// Vector in R^2:
double[] elements = {1, 2};
new Vector(elements);

Non-constant components/elements:

// Vector in R^n:
double[] elements = new double[n];
for(int i = 0; i < n; i++) /* generate n elements */;
new Vector(elements);

Vector operations:

Addition:

// Add v and w:
Vector v = new Vector(vE);
Vector w = new Vector(wE);

v.add(w);
w.add(v);

Scalar multiplication:

// Scale v by a factor of x:
Vector v = new Vector(vE);

v.scale((double) x);

Subtraction:

// Substract w from v:
Vector v = new Vector(vE);
Vector w = new Vector(wE);

v.add(w.scale(-1));

Dot Product:

// Dot Product between v and w:
Vector v = new Vector(vE);
Vector w = new Vector(wE);

v.dot(w);
w.dot(v);

Cross Product (Soon!):

// Cross Product between v and w:
Vector v = new Vector(vE);
Vector w = new Vector(wE);

v.cross(w);
w.cross(v);

Matrix operations

Addition:

// Matrix A + B:
Matrix A = new Matrix(AE);
Matrix B = new Matrix(BE);

A.add(B);
B.add(A);

Scalar multiplication (Soon!):

// Matrix xA:
Matrix A = new Matrix(AE);

A.scale((double) x);

Multiplication:

// Matrix A x B:
Matrix A = new Matrix(AE);
Matrix B = new Matrix(BE);

A.mul(B);
B.mul(A);

Transposition:

// Matrix A^T:
Matrix A = new Matrix(AE);

A.transpose();

Matrix-Vector operations:

Linear Maps/Matrix transformations

Matrix A = new Matrix(AE);
Vector x = new Vector(xE);

A.transform(x);

Matrix output:

Matrix A = new Matrix(AE);

A.printRows();

Vector output:

Vector x = new Vector(xE);

x.printVec();

Final Example:

public class Main {
    public static void main(String[] args) {
        double[][] mr = {{1.0, 3.0, 5.0}, {2.0, 4.0, 6.0}};
        Matrix m = new Matrix(mr);

        double[][] identity = {{1, 0}, {0, 1}};
        Matrix I = new Matrix(identity);

        double[] vr = {1, 2, 3};
        Vector v = new Vector(vr);

        double[] v2s = {1, 2};
        Vector v2 = new Vector(v2s);

        m.printRows();
        v.printVec();

        v.add(v).scale(2).printVec();

        System.out.println(v.dot(v));

        m.add(m).printRows();

        m.transpose().printRows();

        I.transform(v2).printVec();

        I.mul(I).printRows();
    }
}
[ 1.0 3.0 5.0 
2.0 4.0 6.0 ]

[ 1.0 2.0 3.0 ]

[ 4.0 8.0 12.0 ]

14.0

[ 2.0 6.0 10.0 
4.0 8.0 12.0 ]

[ 1.0 2.0 
3.0 4.0 
5.0 6.0 ]

[ 1.0 2.0 ]

[ 1.0 0.0 
0.0 1.0 ]

About

This is a Java library for computational Linear Algebra. It is a work in progress.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages