Skip to content

Commit

Permalink
Primitive-D
Browse files Browse the repository at this point in the history
  • Loading branch information
apete committed Feb 11, 2024
1 parent 6c60e9d commit ef80ad2
Show file tree
Hide file tree
Showing 60 changed files with 1,324 additions and 817 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Added / Changed / Deprecated / Fixed / Removed / Security
#### org.ojalgo.structure

- Refactored the builder/factory interfaces to better support creating immutable 1D, 2D or AnyD structures. This has implications for most ojAlgo data structures. There are deprecations in all factory classes, but everything that worked before still works (I believe).
- The `Structure*D`, `Access*D`, `Mutate*D` and `Factory*D` interfaces have all been refactored to make working with `int` indices (rather than `long`) the primary alternative. Huge data structures, that require `long` indices, are still suported – no change in this regard. In reality only a few implementations actually allowed to create such large instances. This change is to allow all the other classes to be implemented using `int` and not bother (so much) with `long`. Users of the various classes in ojAlgo should see no difference. If you have created your own classes implementing some of ojAlgo's interfaces you will probably need to implement some additional methods in those classes.

## [53.3.0] – 2024-02-04

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/ojalgo/array/Array2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,16 @@ public N get(final long row, final long col) {
return myDelegate.get(Structure2D.index(myRowsCount, row, col));
}

@Override
public int getColDim() {
return Math.toIntExact(myColumnsCount);
}

@Override
public int getRowDim() {
return Math.toIntExact(myRowsCount);
}

@Override
public int hashCode() {
int prime = 31;
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/org/ojalgo/array/ArrayAnyD.java
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ public void set(final long[] reference, final short value) {

@Override
public long[] shape() {
return myStructure;
return myStructure.clone();
}

@Override
Expand All @@ -755,6 +755,16 @@ public short shortValue(final long... ref) {
return myDelegate.shortValue(StructureAnyD.index(myStructure, ref));
}

@Override
public int size() {
return myDelegate.size();
}

@Override
public int size(final int dimension) {
return Math.toIntExact(this.count(dimension));
}

@Override
public Array1D<N> sliceRange(final long first, final long limit) {
return myDelegate.wrapInArray1D().sliceRange(first, limit);
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/ojalgo/array/OffHeapArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ long getCapacityLimit() {
public static final DenseArray.Factory<Double> Z016 = new Factory(MathType.Z016, OffHeapZ016::new);
public static final DenseArray.Factory<Double> Z032 = new Factory(MathType.Z032, OffHeapZ032::new);
public static final DenseArray.Factory<Double> Z064 = new Factory(MathType.Z064, OffHeapZ064::new);

/**
* @deprecated Use {@link #R032} instead
*/
Expand Down Expand Up @@ -199,6 +198,11 @@ public void modifyOne(final long index, final UnaryFunction<Double> modifier) {
this.set(index, modifier.invoke(this.doubleValue(index)));
}

@Override
public final int size() {
return Math.toIntExact(myCount);
}

@Override
public void visitOne(final long index, final VoidFunction<Double> visitor) {
visitor.accept(this.doubleValue(index));
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/ojalgo/array/SegmentedArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ public void set(final long index, final float value) {
mySegments[(int) (index >> myIndexBits)].set(index & myIndexMask, value);
}

@Override
public int size() {
return Math.toIntExact(this.count());
}

@Override
public void visitOne(final long index, final VoidFunction<N> visitor) {
if (this.isPrimitive()) {
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/org/ojalgo/array/SparseArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,11 @@ public void set(final long index, final float value) {
this.update(index, internalIndex, value, false);
}

@Override
public int size() {
return Math.toIntExact(myCount);
}

public void supplyNonZerosTo(final Mutate1D consumer) {
if (this.isPrimitive()) {
for (int n = 0; n < myActualLength; n++) {
Expand Down Expand Up @@ -889,11 +894,6 @@ Access1D<N> getValues(final long fromIncl, final long toExcl) {

return new Access1D<>() {

@Override
public long count() {
return limit - first;
}

@Override
public double doubleValue(final int index) {
return myValues.doubleValue(first + index);
Expand All @@ -909,6 +909,11 @@ public N get(final long index) {
return myValues.get(first + index);
}

@Override
public int size() {
return limit - first;
}

};
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/ojalgo/data/DataBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ public Double get(final long row, final long col) {
return Double.valueOf(this.doubleValue(row, col));
}

@Override
public int getColDim() {
return myData.getColDim();
}

@Override
public int getRowDim() {
return myData.getRowDim();
}

public boolean isFull() {
return this.remaining() == 0;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/ojalgo/equation/Equation.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ public void setRHS(final double rhs) {
myRHS = rhs;
}

@Override
public int size() {
return myBody.size();
}

@Override
public String toString() {
return index + ": " + myBody.toString() + " = " + myRHS;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/ojalgo/matrix/BasicMatrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ public N get(final long row, final long col) {
return this.store().get(row, col);
}

@Override
public int getColDim() {
return mySupplier.getColDim();
}

/**
* Matrix condition (2-norm)
*
Expand Down Expand Up @@ -386,6 +391,11 @@ public int getRank() {
return this.getRankProvider().getRank();
}

@Override
public int getRowDim() {
return mySupplier.getRowDim();
}

/**
* The sum of the diagonal elements.
*
Expand Down Expand Up @@ -648,6 +658,11 @@ public M signum() {
return this.newInstance(this.store().signum());
}

@Override
public int size() {
return mySupplier.size();
}

/**
* <p>
* This method solves a system of linear equations: [this][X]=[rhs]. A combination of columns in [this]
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/ojalgo/matrix/Mutator2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,16 @@ public N get(final long row, final long col) {
}
}

@Override
public int getColDim() {
return myDelegate.getColDim();
}

@Override
public int getRowDim() {
return myDelegate.getRowDim();
}

@Override
public void modifyAll(final UnaryFunction<N> modifier) {
if (!mySafe) {
Expand Down Expand Up @@ -571,6 +581,11 @@ public void set(final long row, final long col, final Comparable<?> value) {
myDelegate.set(row, col, value);
}

@Override
public int size() {
return myDelegate.size();
}

@Override
public void supplyTo(final TransformableRegion<N> receiver) {
myDelegate.supplyTo(receiver);
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/org/ojalgo/matrix/decomposition/Eigenvalue.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public Eigenpair(final ComplexNumber aValue, final Access1D<ComplexNumber> aVect
vector = aVector;
}

@Override
public int compareTo(final Eigenpair other) {
return DESCENDING_NORM.compare(value, other.value);
}
Expand Down Expand Up @@ -129,17 +130,20 @@ default Eigenvalue<N> make(final boolean hermitian) {
default Eigenvalue<N> make(final int dimension, final boolean hermitian) {
return this.make(new Structure2D() {

public long countColumns() {
@Override
public int getColDim() {
return dimension;
}

public long countRows() {
@Override
public int getRowDim() {
return dimension;
}

}, hermitian);
}

@Override
default Eigenvalue<N> make(final Structure2D typical) {
if (typical instanceof MatrixStore) {
return this.make(typical, ((MatrixStore<?>) typical).isHermitian());
Expand Down Expand Up @@ -442,6 +446,7 @@ default Eigenpair getEigenpair(final int index) {
*
* @see org.ojalgo.matrix.Provider2D.Eigenpairs#getEigenpairs()
*/
@Override
default List<Eigenpair> getEigenpairs() {

List<Eigenpair> retVal = new ArrayList<>();
Expand Down Expand Up @@ -569,8 +574,10 @@ default MatrixStore<ComplexNumber> getEigenvectors() {
*
* @return true if they are ordered
*/
@Override
boolean isOrdered();

@Override
default MatrixStore<N> reconstruct() {
final MatrixStore<N> mtrxV = this.getV();
MatrixStore<N> mtrxD = this.getD();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ default D make(final int numberOfRows, final int numberOfColumns) {
return this.make(new Structure2D() {

@Override
public long countColumns() {
public int getColDim() {
return numberOfColumns;
}

@Override
public long countRows() {
public int getRowDim() {
return numberOfRows;
}
});
Expand Down Expand Up @@ -420,13 +420,13 @@ interface Values<N extends Comparable<N>> extends Ordered<N> {
Structure2D TYPICAL = new Structure2D() {

@Override
public long countColumns() {
return 50L;
public int getColDim() {
return 50;
}

@Override
public long countRows() {
return 50L;
public int getRowDim() {
return 50;
}

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,12 @@ static void toDiagonal(final double[] s, final double[] e, final RotateRight q1R
private final Structure2D myInputStructure = new Structure2D() {

@Override
public long countColumns() {
public int getColDim() {
return myTransposed ? myBidiagonal.getRowDim() : myBidiagonal.getColDim();
}

@Override
public long countRows() {
public int getRowDim() {
return myTransposed ? myBidiagonal.getColDim() : myBidiagonal.getRowDim();
}
};
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/ojalgo/matrix/store/ColumnsSupplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public long countRows() {
return this.getCurrent().elements();
}

@Override
public int getColDim() {
return 1;
}

@Override
public int getRowDim() {
return myBase.getRowDim();
}

@Override
public ElementView1D<N, ?> nonzeros() {
return this.getCurrent().nonzeros();
Expand Down Expand Up @@ -111,6 +121,14 @@ public N get(final int row, final int col) {
return ColumnsSupplier.this.get(row, columns[col]);
}

public int getColDim() {
return columns.length;
}

public int getRowDim() {
return ColumnsSupplier.this.getRowDim();
}

public Factory<N, ?> physical() {
return ColumnsSupplier.this.physical();
}
Expand All @@ -133,6 +151,7 @@ public void supplyTo(final TransformableRegion<N> receiver) {
public String toString() {
return Access2D.toString(this);
}

};
}

Expand Down Expand Up @@ -161,10 +180,20 @@ public N get(final int row, final int col) {
return myColumns.get(col).get(row);
}

@Override
public int getColDim() {
return myColumns.size();
}

public SparseArray<N> getColumn(final int index) {
return myColumns.get(index);
}

@Override
public int getRowDim() {
return myRowsCount;
}

@Override
public Factory<N, ?> physical() {
return myPhysicalStoreFactory;
Expand Down
Loading

0 comments on commit ef80ad2

Please sign in to comment.