Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove norm methods from matrix classes. To compute the norm of a ten… #95

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/main/java/org/flag4j/core/MatrixComparisonsMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,4 @@ public interface MatrixComparisonsMixin<T> {
* @return True if this matrix is the identity matrix. Otherwise, returns false.
*/
boolean isI();


/**
* Checks if matrices are inverses of each other.
* @param B Second matrix.
* @return True if matrix B is an inverse of this matrix. Otherwise, returns false. Otherwise, returns false.
*/
boolean isInv(T B);
}
10 changes: 0 additions & 10 deletions src/main/java/org/flag4j/core/MatrixOperationsMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.flag4j.dense.CVector;
import org.flag4j.dense.Matrix;
import org.flag4j.dense.Vector;
import org.flag4j.linalg.decompositions.svd.SVD;
import org.flag4j.sparse.CooCMatrix;
import org.flag4j.sparse.CooCVector;
import org.flag4j.sparse.CooMatrix;
Expand Down Expand Up @@ -1058,15 +1057,6 @@ default W stack(CooCVector b, int axis) {
X tr();


/**
* Computes the condition number of this matrix using {@link SVD SVD}.
* Specifically, the condition number is computed as the maximum singular value divided by the minimum singular
* value of this matrix.
*
* @return The condition number of this matrix (Assuming Frobenius norm).
*/
double cond();

/**
* Extracts the diagonal elements of this matrix and returns them as a vector.
* @return A vector containing the diagonal entries of this matrix.
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/org/flag4j/core/MatrixPropertiesMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,6 @@ default boolean isInvertible() {
}


/**
* Computes the L<sub>p, q</sub> norm of this matrix.
* @param p P value in the L<sub>p, q</sub> norm.
* @param q Q value in the L<sub>p, q</sub> norm.
* @return The L<sub>p, q</sub> norm of this matrix.
*/
double norm(double p, double q);


/**
* Computes the max norm of a matrix.
* @return The max norm of this matrix.
*/
double maxNorm();


/**
* Computes the rank of this matrix (i.e. the dimension of the column space of this matrix).
* Note that here, rank is <b>NOT</b> the same as a tensor rank.
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/org/flag4j/core/TensorPropertiesMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,4 @@ interface TensorPropertiesMixin {
* entry (in row-major ordering) are returned.
*/
int[] argMax();


/**
* Computes the 2-norm of this tensor. This is equivalent to {@link #norm(double) norm(2)}.
* @return the 2-norm of this tensor.
*/
double norm();


/**
* Computes the p-norm of this tensor.
* @param p The p value in the p-norm. <br>
* - If p is inf, then this method computes the maximum/infinite norm.
* @return The p-norm of this tensor.
* @throws IllegalArgumentException If p is less than 1.
*/
double norm(double p);


/**
* Computes the maximum/infinite norm of this tensor.
* @return The maximum/infinite norm of this tensor.
*/
double infNorm();
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,6 @@ public int[] argMax() {
}


@Override
public double infNorm() {
return AggregateReal.maxAbs(entries);
}


@Override
public boolean isPos() {
return RealProperties.isPos(entries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,6 @@ public double maxAbs() {
}


@Override
public double infNorm() {
return AggregateComplex.maxAbs(entries);
}


@Override
public boolean isZeros() {
return ComplexProperties.isZeros(entries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,6 @@ public double maxAbs() {
}


@Override
public double infNorm() {
return AggregateReal.maxAbs(entries);
}


@Override
public boolean isPos() {
return RealProperties.isPos(entries);
Expand Down
145 changes: 8 additions & 137 deletions src/main/java/org/flag4j/dense/CMatrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@
import org.flag4j.core.dense_base.ComplexDenseTensorBase;
import org.flag4j.core.dense_base.DenseMatrixMixin;
import org.flag4j.io.PrintOptions;
import org.flag4j.linalg.Invert;
import org.flag4j.linalg.MatrixNorms;
import org.flag4j.linalg.decompositions.svd.ComplexSVD;
import org.flag4j.operations.MatrixMultiplyDispatcher;
import org.flag4j.operations.TransposeDispatcher;
import org.flag4j.operations.common.complex.ComplexOperations;
import org.flag4j.operations.common.real.RealProperties;
import org.flag4j.operations.dense.complex.*;
import org.flag4j.operations.dense.complex.ComplexDenseDeterminant;
import org.flag4j.operations.dense.complex.ComplexDenseEquals;
import org.flag4j.operations.dense.complex.ComplexDenseProperties;
import org.flag4j.operations.dense.complex.ComplexDenseSetOperations;
import org.flag4j.operations.dense.real_complex.RealComplexDenseElemDiv;
import org.flag4j.operations.dense.real_complex.RealComplexDenseElemMult;
import org.flag4j.operations.dense.real_complex.RealComplexDenseEquals;
Expand Down Expand Up @@ -530,26 +533,6 @@ public boolean isCloseToI() {
}


/**
* Checks if matrices are inverses of each other.
*
* @param B Second matrix.
* @return True if matrix B is an inverse (approximately) of this matrix. Otherwise, returns false. Otherwise, returns false.
*/
@Override
public boolean isInv(CMatrix B) {
boolean result;

if(!this.isSquare() || !B.isSquare() || !shape.equals(B.shape)) {
result = false;
} else {
result = this.mult(B).isCloseToI();
}

return result;
}


/**
* Flattens a matrix to have a single row. To flatten matrix to a single column, see {@link #flatten(int)}.
*
Expand Down Expand Up @@ -3350,48 +3333,6 @@ public CNumber tr() {
}


/**
* Computes the condition number of this matrix using the 2-norm.
* Specifically, the condition number is computed as the norm of this matrix multiplied by the norm
* of the inverse of this matrix.
*
* @return The condition number of this matrix (Assuming 2-norm). This value may be
* {@link Double#POSITIVE_INFINITY infinite}.
*/
@Override
public double cond() {
return cond(2);
}


/**
* Computes the condition number of this matrix using a specified norm. The condition number of a matrix is defined
* as the norm of a matrix multiplied by the norm of the inverse of the matrix.
* @param p Specifies the order of the norm to be used when computing the condition number.
* Common {@code p} values include:<br>
* - {@code p} = {@link Double#POSITIVE_INFINITY}, {@link #infNorm()}.<br>
* - {@code p} = 2, The standard matrix 2-norm (the largest singular value).<br>
* - {@code p} = -2, The Smallest singular value.<br>
* - {@code p} = 1, Maximum absolute row sum.<br>
* @return The condition number of this matrix using the specified norm. This value may be
* {@link Double#POSITIVE_INFINITY infinite}.
*/
// TODO Pull up to matrix mixin
public double cond(double p) {
double cond;

if(p==2 || p==-2) {
// Compute the singular value decomposition of the matrix.
Vector s = new ComplexSVD(false).decompose(this).getS().getDiag();
cond = p==2 ? s.max()/s.min() : s.min()/s.max();
} else {
cond = norm(p)*Invert.inv(this).norm(p);
}

return cond;
}


/**
* Extracts the diagonal elements of this matrix and returns them as a vector.
* @return A vector containing the diagonal entries of this matrix.
Expand Down Expand Up @@ -3589,30 +3530,6 @@ public boolean isSingular() {
}


/**
* Computes the L<sub>p, q</sub> norm of this matrix.
*
* @param p P value in the L<sub>p, q</sub> norm.
* @param q Q value in the L<sub>p, q</sub> norm.
* @return The L<sub>p, q</sub> norm of this matrix.
*/
@Override
public double norm(double p, double q) {
return ComplexDenseOperations.matrixNormLpq(entries, shape, p, q);
}


/**
* Computes the max norm of a matrix.
*
* @return The max norm of this matrix.
*/
@Override
public double maxNorm() {
return ComplexDenseOperations.matrixMaxNorm(entries);
}


/**
* Computes the rank of this matrix (i.e. the dimension of the column space of this matrix).
* Note that here, rank is <b>NOT</b> the same as a tensor rank.
Expand All @@ -3624,7 +3541,9 @@ public int matrixRank() {
Matrix S = new ComplexSVD(false).decompose(this).getS();
int stopIdx = Math.min(numRows, numCols);

double tol = 2.0*Math.max(numRows, numCols)* Flag4jConstants.EPS_F64*norm(); // Tolerance for determining if a singular value should be considered zero.
double tol = 2.0*Math.max(numRows, numCols)* Flag4jConstants.EPS_F64*MatrixNorms.norm(this); // Tolerance for determining if a
// singular
// value should be considered zero.
int rank = 0;

for(int i=0; i<stopIdx; i++) {
Expand Down Expand Up @@ -3715,54 +3634,6 @@ public CNumber get(int... indices) {
}


/**
* Computes the 2-norm of this tensor. This is equivalent to {@link #norm(double) norm(2)}.
*
* @return the 2-norm of this tensor.
*/
@Override
public double norm() {
return ComplexDenseOperations.matrixNormL2(entries, shape);
}


/**
* Computes the p-norm of this tensor.
*
* @param p The p value in the p-norm. <br>
* - If p is inf, then this method computes the maximum/infinite norm.
* @return The p-norm of this tensor.
* @throws IllegalArgumentException If p is less than 1.
*/
@Override
public double norm(double p) {
double norm;

if(Double.isInfinite(p)) {
if(p > 0) {
norm = maxNorm();
} else {
norm = minAbs();
}
} else {
norm = ComplexDenseOperations.matrixNormLp(entries, shape, p);
}

return norm;
}


/**
* Computes the maximum/infinite norm of this tensor.
*
* @return The maximum/infinite norm of this tensor.
*/
@Override
public double infNorm() {
return ComplexDenseOperations.matrixInfNorm(entries, shape);
}


/**
* Gets row of matrix formatted as a human-readable String. Helper method for {@link #toString} method.
* @param i Index of row to get.
Expand Down
40 changes: 2 additions & 38 deletions src/main/java/org/flag4j/dense/CVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.flag4j.core.dense_base.ComplexDenseTensorBase;
import org.flag4j.core.dense_base.DenseVectorMixin;
import org.flag4j.io.PrintOptions;
import org.flag4j.operations.common.real.VectorNorms;
import org.flag4j.linalg.VectorNorms;
import org.flag4j.operations.dense.complex.ComplexDenseVectorOperations;
import org.flag4j.operations.dense.real_complex.RealComplexDenseElemDiv;
import org.flag4j.operations.dense.real_complex.RealComplexDenseElemMult;
Expand Down Expand Up @@ -365,42 +365,6 @@ protected Vector makeRealTensor(Shape shape, double[] entries) {
}


/**
* Computes the 2-norm of this tensor. This is equivalent to {@link #norm(double) norm(2)}.
*
* @return the 2-norm of this tensor.
*/
@Override
public double norm() {
return VectorNorms.norm(entries);
}


/**
* Computes the p-norm of this tensor. Warning, if p is large in absolute value, overflow errors may occur.
*
* @param p The p value in the p-norm. <br>
* - If p is {@link Double#POSITIVE_INFINITY}, then this method computes the maximum/infinite norm.<br>
* - If p is {@link Double#NEGATIVE_INFINITY}, then this method computes the minimum norm.
* @return The p-norm of this tensor.
*/
@Override
public double norm(double p) {
return VectorNorms.norm(entries, p);
}


/**
* Computes the maximum/infinite norm of this tensor.
*
* @return The maximum/infinite norm of this tensor.
*/
@Override
public double infNorm() {
return maxAbs();
}


/**
* Extends a vector a specified number of times to a matrix.
*
Expand Down Expand Up @@ -891,7 +855,7 @@ public CNumber inner(CooVector b) {
*/
@Override
public CVector normalize() {
double norm = this.norm();
double norm = VectorNorms.norm(this);
return norm==0 ? new CVector(size) : this.div(norm);
}

Expand Down
Loading
Loading