Skip to content

Commit

Permalink
Tensor inheritance (#115)
Browse files Browse the repository at this point in the history
* Updated inheritance of tensors.

- All ring tensors now inherit from their equivalent semiring tensors.
- All field tensors now inherit from their equivalent ring tensors.
- Added tests for Complex64 numbers.

* Added Tests

- Removed redundant transpose classes.

* Fixed Javadoc.

* Remove duplicated code.

* Added tests.

- Fixed bugs that caused failing tests.

* Added tests.

- Fixed bugs that caused failing tests.

* Added tests.

- Fixed bugs which failing tests.

* Fix for norms.

- Updated and fixed matrix and vector norms
- Changes to norms are reflected in the Condition.java for computing condition numbers.

* Added balancing decomposition

* Small fixes.
  • Loading branch information
jacobdwatters authored Jan 25, 2025
1 parent 0c046d4 commit 4cd1881
Show file tree
Hide file tree
Showing 400 changed files with 11,447 additions and 16,772 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<h1 title="Class RealDenseMatrixMultTranspose" class="title">Class RealDenseMatrixMultTranspose</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">org.flag4j.linalg.ops.dense.real.RealDenseMatrixMultTranspose</div>
<div class="inheritance">org.flag4j.linalg.ops.dense.real.RealDenseMatMultTranspose</div>
</div>
<section class="class-description" id="class-description">
<div class="horizontal-scroll">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<h1 title="Class RealDenseMatrixMultiplication" class="title">Class RealDenseMatrixMultiplication</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">org.flag4j.linalg.ops.dense.real.RealDenseMatrixMultiplication</div>
<div class="inheritance">org.flag4j.linalg.ops.dense.real.RealDenseMatMult</div>
</div>
<section class="class-description" id="class-description">
<div class="horizontal-scroll">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<h1 title="Class DenseCsrFieldMatMult" class="title">Class DenseCsrFieldMatMult</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">org.flag4j.linalg.ops.dense_sparse.csr.field_ops.DenseCsrFieldMatMult</div>
<div class="inheritance">org.flag4j.linalg.ops.dense_sparse.csr.semiring_ops.DenseCsrSemiringMatMult</div>
</div>
<section class="class-description" id="class-description">
<div class="horizontal-scroll">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<h1 title="Class CooFieldNorms" class="title">Class CooFieldNorms</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">org.flag4j.linalg.ops.sparse.coo.field_ops.CooFieldNorms</div>
<div class="inheritance">org.flag4j.linalg.ops.sparse.coo.ring_ops.CooRingNorms</div>
</div>
<section class="class-description" id="class-description">
<div class="horizontal-scroll">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<h1 title="Class RealSparseMatrixGetSet" class="title">Class RealSparseMatrixGetSet</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">org.flag4j.linalg.ops.sparse.coo.real.RealSparseMatrixGetSet</div>
<div class="inheritance">org.flag4j.linalg.ops.sparse.coo.real.RealCooMatrixGetSet</div>
</div>
<section class="class-description" id="class-description">
<div class="horizontal-scroll">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<h1 title="Class CsrFieldEquals" class="title">Class CsrFieldEquals</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">org.flag4j.linalg.ops.sparse.csr.field_ops.CsrFieldEquals</div>
<div class="inheritance">org.flag4j.linalg.ops.sparse.csr.ring_ops.CsrRingProperties</div>
</div>
<section class="class-description" id="class-description">
<div class="horizontal-scroll">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<h1 title="Class RealDenseMatrixMultTranspose" class="title">Class RealDenseMatrixMultTranspose</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">org.flag4j.linalg.ops.dense.real.RealDenseMatrixMultTranspose</div>
<div class="inheritance">org.flag4j.linalg.ops.dense.real.RealDenseMatMultTranspose</div>
</div>
<section class="class-description" id="class-description">
<div class="horizontal-scroll">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<h1 title="Class RealDenseMatrixMultiplication" class="title">Class RealDenseMatrixMultiplication</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">org.flag4j.linalg.ops.dense.real.RealDenseMatrixMultiplication</div>
<div class="inheritance">org.flag4j.linalg.ops.dense.real.RealDenseMatMult</div>
</div>
<section class="class-description" id="class-description">
<div class="horizontal-scroll">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<h1 title="Class DenseCsrFieldMatMult" class="title">Class DenseCsrFieldMatMult</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">org.flag4j.linalg.ops.dense_sparse.csr.field_ops.DenseCsrFieldMatMult</div>
<div class="inheritance">org.flag4j.linalg.ops.dense_sparse.csr.semiring_ops.DenseCsrSemiringMatMult</div>
</div>
<section class="class-description" id="class-description">
<div class="horizontal-scroll">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<h1 title="Class CooFieldNorms" class="title">Class CooFieldNorms</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">org.flag4j.linalg.ops.sparse.coo.field_ops.CooFieldNorms</div>
<div class="inheritance">org.flag4j.linalg.ops.sparse.coo.ring_ops.CooRingNorms</div>
</div>
<section class="class-description" id="class-description">
<div class="horizontal-scroll">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
<h1 title="Class RealSparseMatrixGetSet" class="title">Class RealSparseMatrixGetSet</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">org.flag4j.linalg.ops.sparse.coo.real.RealSparseMatrixGetSet</div>
<div class="inheritance">org.flag4j.linalg.ops.sparse.coo.real.RealCooMatrixGetSet</div>
</div>
<section class="class-description" id="class-description">
<div class="horizontal-scroll">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<h1 title="Class CsrFieldEquals" class="title">Class CsrFieldEquals</h1>
</div>
<div class="inheritance" title="Inheritance Tree"><a href="https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/Object.html" title="class or interface in java.lang" class="external-link">java.lang.Object</a>
<div class="inheritance">org.flag4j.linalg.ops.sparse.csr.field_ops.CsrFieldEquals</div>
<div class="inheritance">org.flag4j.linalg.ops.sparse.csr.ring_ops.CsrRingProperties</div>
</div>
<section class="class-description" id="class-description">
<div class="horizontal-scroll">
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
module flag4j {
requires java.logging;
requires java.desktop;

// Abstract algebra stuff.
exports org.flag4j.algebraic_structures;
Expand Down Expand Up @@ -67,14 +68,12 @@
exports org.flag4j.linalg.ops.sparse;

exports org.flag4j.linalg.ops.sparse.coo;
exports org.flag4j.linalg.ops.sparse.coo.field_ops;
exports org.flag4j.linalg.ops.sparse.coo.real;
exports org.flag4j.linalg.ops.sparse.coo.real_complex;
exports org.flag4j.linalg.ops.sparse.coo.ring_ops;
exports org.flag4j.linalg.ops.sparse.coo.semiring_ops;

exports org.flag4j.linalg.ops.sparse.csr;
exports org.flag4j.linalg.ops.sparse.csr.field_ops;
exports org.flag4j.linalg.ops.sparse.csr.real;
exports org.flag4j.linalg.ops.sparse.csr.real_complex;
// ------------------------------------------------------------------------
Expand All @@ -85,4 +84,7 @@
// Utilities
exports org.flag4j.util;
exports org.flag4j.util.exceptions;
exports org.flag4j.linalg.ops.sparse.csr.ring_ops;
exports org.flag4j.linalg.ops.dense_sparse.csr.semiring_ops;
exports org.flag4j.linalg.decompositions.balance;
}
7 changes: 4 additions & 3 deletions src/main/java/org/flag4j/algebraic_structures/Complex128.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2024. Jacob Watters
* Copyright (c) 2024-2025. Jacob Watters
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -350,7 +350,7 @@ public Complex128 mult(Complex128 b) {
*/
@Override
public boolean isZero() {
return equals(ZERO);
return re == 0.0 && im == 0.0;
}


Expand All @@ -363,7 +363,7 @@ public boolean isZero() {
*/
@Override
public boolean isOne() {
return equals(ONE);
return re == 1.0 && im == 0.0;
}


Expand Down Expand Up @@ -398,6 +398,7 @@ public Complex128 getOne() {
* @param b Second element in product.
* @return The product of this field element and {@code b}.
*/
@Override
public Complex128 mult(double b) {
return new Complex128(re*b, im*b);
}
Expand Down
21 changes: 9 additions & 12 deletions src/main/java/org/flag4j/algebraic_structures/Complex64.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ public Complex64(String num) {
}


/**
* Checks if this complex has zero imaginary part and real part equal to a double.
* @return True if {@code this.re == b && this.im == 0}. False otherwise.
*/
public boolean equals(float b) {
return this.re == b && this.im == 0;
}


/**
* Checks if an object is equal to this Field element.
* @param b Object to compare to this Field element.
Expand Down Expand Up @@ -502,18 +511,6 @@ public Complex64 conj() {
}


/**
* Compute a raised to the power of b. This method wraps {@link Math#pow(double, double)}
* and returns a {@link Complex64}.
* @param a The base.
* @param b The exponent.
* @return a raised to the power of b.
*/
public static Complex64 pow(float a, float b) {
return new Complex64((float) Math.pow(a, b));
}


/**
* Compute a raised to the power of b.
* and returns a {@link Complex64}.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/flag4j/algebraic_structures/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ default T mult(double b) {
*/
default T div(double b) {
throw new UnsupportedOperationException("Division with primitive doubles is not supported for this field: "
+ getClass() + ".");
+ getClass().getName() + ".");
}


Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/flag4j/algebraic_structures/Ring.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public interface Ring<T extends Ring<T>> extends Semiring<T> {
* @param b Second ring element in difference.
* @return The difference of this ring element and {@code b}.
*/
public T sub(T b);
T sub(T b);


/**
Expand All @@ -115,7 +115,7 @@ public interface Ring<T extends Ring<T>> extends Semiring<T> {
*
* @return The additive inverse for this ring element.
*/
public T addInv();
T addInv();


/**
Expand All @@ -133,8 +133,9 @@ default double abs() {
* Computes the magnitude of this ring element.
* @return The magnitude of this ring element.
*/
default public double mag() {
throw new UnsupportedOperationException("Magnitude/absolute value is not defined for this ring: " + getClass() + ".");
default double mag() {
throw new UnsupportedOperationException("Magnitude/absolute value is not defined for this algebraic object: "
+ getClass().getName() + ".");
}


Expand All @@ -143,7 +144,8 @@ default public double mag() {
* @return The conjugation of this ring's element.
* @implNote The default implementation of this method simply returns this rings element.
*/
public default T conj() {
throw new UnsupportedOperationException("Magnitude/absolute value is not defined for this ring: " + getClass() + ".");
default T conj() {
throw new UnsupportedOperationException("Conjugation is not defined for this algebraic object: "
+ getClass().getName() + ".");
}
}
42 changes: 0 additions & 42 deletions src/main/java/org/flag4j/arrays/SmartMatrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import org.flag4j.algebraic_structures.Complex128;
import org.flag4j.algebraic_structures.Field;
import org.flag4j.algebraic_structures.RealFloat64;
import org.flag4j.algebraic_structures.Semiring;
import org.flag4j.arrays.backend.MatrixMixin;
import org.flag4j.arrays.backend.smart_visitors.*;
Expand Down Expand Up @@ -394,45 +393,4 @@ public int hashCode() {
public String toString() {
return "SmartMatrix type: " + this.matrix.getClass().getSimpleName() + "\n" + matrix.toString();
}


// TODO: TESTING
public static void main(String[] args) {
var realDense = new Matrix(new double[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
});

var complexDense = new CMatrix(new Complex128[][]{
{new Complex128(1.46, 12.6), new Complex128(-2, 1),new Complex128(0, 4)},
{new Complex128(2), new Complex128(2.1, 3),new Complex128(-1, 3)},
{new Complex128(1, 1), new Complex128(12, 5),new Complex128(0, -2)}
});

var fieldDense = new FieldMatrix<>(new Complex128[][]{
{new Complex128(1.46, 12.6), new Complex128(-2, 1),new Complex128(0, 4)},
{new Complex128(2), new Complex128(2.1, 3),new Complex128(-1, 3)},
{new Complex128(1, 1), new Complex128(12, 5),new Complex128(0, -2)}
});

var fieldDense2 = new FieldMatrix<>(new RealFloat64[][]{
{new RealFloat64(1.5566), new RealFloat64(-9.3), new RealFloat64(0)},
{new RealFloat64(1), new RealFloat64(-2), new RealFloat64(400.1)},
{new RealFloat64(2), new RealFloat64(0.84), new RealFloat64(8e9)}
});

SmartMatrix a = new SmartMatrix(realDense);
SmartMatrix b = new SmartMatrix(complexDense);
SmartMatrix c = new SmartMatrix(fieldDense);
SmartMatrix d = new SmartMatrix(fieldDense2);

double trace = a.tr(Double.class);

System.out.println("a:\n" + a + "\n");
System.out.println("b:\n" + b + "\n");
System.out.println("a + b:\n" + a.add(b) + "\n");
System.out.println("c + c:\n" + d.add(d) + "\n");
System.out.println();
}
}
Loading

0 comments on commit 4cd1881

Please sign in to comment.