From 24e7cb50fb4efde0df9ac05050d201f577842e19 Mon Sep 17 00:00:00 2001 From: stefan-zobel Date: Wed, 6 Mar 2024 19:52:27 +0100 Subject: [PATCH 01/11] switch to Java 8 --- pom.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 74a139d..9e60e18 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ net.sourceforge.streamsupport jamu jar - 1.4.1 + 1.4.2-SNAPSHOT JAMU Java Matrix Utilities built on top of Intel MKL https://github.com/stefan-zobel/JAMU/ @@ -46,16 +46,16 @@ maven-compiler-plugin - 3.8.1 + 3.12.1 - 1.7 - 1.7 + 1.8 + 1.8 org.apache.maven.plugins maven-javadoc-plugin - 3.2.0 + 3.5.0 attach-javadocs From 13f265ff329e3b0b414802a73e53737387f944f3 Mon Sep 17 00:00:00 2001 From: stefan-zobel Date: Wed, 6 Mar 2024 22:24:02 +0100 Subject: [PATCH 02/11] work around javadoc html5 entity issue --- pom.xml | 3 --- src/main/java/net/jamu/matrix/ComplexMatrixD.java | 2 +- src/main/java/net/jamu/matrix/ComplexMatrixDConduct.java | 2 +- src/main/java/net/jamu/matrix/ComplexMatrixF.java | 2 +- src/main/java/net/jamu/matrix/ComplexMatrixFConduct.java | 2 +- src/main/java/net/jamu/matrix/MatrixD.java | 2 +- src/main/java/net/jamu/matrix/MatrixDConduct.java | 6 +++--- src/main/java/net/jamu/matrix/MatrixF.java | 2 +- src/main/java/net/jamu/matrix/MatrixFConduct.java | 6 +++--- src/main/java/net/jamu/matrix/TensorD.java | 8 ++++---- src/main/java/net/jamu/matrix/TensorF.java | 8 ++++---- 11 files changed, 20 insertions(+), 23 deletions(-) diff --git a/pom.xml b/pom.xml index 9e60e18..1c18d2f 100644 --- a/pom.xml +++ b/pom.xml @@ -62,9 +62,6 @@ jar - - none - diff --git a/src/main/java/net/jamu/matrix/ComplexMatrixD.java b/src/main/java/net/jamu/matrix/ComplexMatrixD.java index 739b3db..b57236c 100644 --- a/src/main/java/net/jamu/matrix/ComplexMatrixD.java +++ b/src/main/java/net/jamu/matrix/ComplexMatrixD.java @@ -638,7 +638,7 @@ public interface ComplexMatrixD extends MatrixDimensions, ComplexMatrixDConduct ComplexMatrixD expm(); /** - * Hadamard product {@code C = A} ∘ {@code B} (also known as + * Hadamard product {@code C = A} \u2218 {@code B} (also known as * element-wise product) of this matrix (A) and B. * * @param B diff --git a/src/main/java/net/jamu/matrix/ComplexMatrixDConduct.java b/src/main/java/net/jamu/matrix/ComplexMatrixDConduct.java index e380ad1..171f93d 100644 --- a/src/main/java/net/jamu/matrix/ComplexMatrixDConduct.java +++ b/src/main/java/net/jamu/matrix/ComplexMatrixDConduct.java @@ -339,7 +339,7 @@ public interface ComplexMatrixDConduct { ComplexMatrixD inverse(); /** - * Hadamard product {@code A} ∘ {@code B} (also known as + * Hadamard product {@code A} \u2218 {@code B} (also known as * element-wise product) of this matrix (A) and B. * * @param B diff --git a/src/main/java/net/jamu/matrix/ComplexMatrixF.java b/src/main/java/net/jamu/matrix/ComplexMatrixF.java index 2ff328e..1475a55 100644 --- a/src/main/java/net/jamu/matrix/ComplexMatrixF.java +++ b/src/main/java/net/jamu/matrix/ComplexMatrixF.java @@ -638,7 +638,7 @@ public interface ComplexMatrixF extends MatrixDimensions, ComplexMatrixFConduct ComplexMatrixF expm(); /** - * Hadamard product {@code C = A} ∘ {@code B} (also known as + * Hadamard product {@code C = A} \u2218 {@code B} (also known as * element-wise product) of this matrix (A) and B. * * @param B diff --git a/src/main/java/net/jamu/matrix/ComplexMatrixFConduct.java b/src/main/java/net/jamu/matrix/ComplexMatrixFConduct.java index c4080c2..cd68567 100644 --- a/src/main/java/net/jamu/matrix/ComplexMatrixFConduct.java +++ b/src/main/java/net/jamu/matrix/ComplexMatrixFConduct.java @@ -339,7 +339,7 @@ public interface ComplexMatrixFConduct { ComplexMatrixF inverse(); /** - * Hadamard product {@code A} ∘ {@code B} (also known as + * Hadamard product {@code A} \u2218 {@code B} (also known as * element-wise product) of this matrix (A) and B. * * @param B diff --git a/src/main/java/net/jamu/matrix/MatrixD.java b/src/main/java/net/jamu/matrix/MatrixD.java index 4d89161..31d834b 100644 --- a/src/main/java/net/jamu/matrix/MatrixD.java +++ b/src/main/java/net/jamu/matrix/MatrixD.java @@ -604,7 +604,7 @@ public interface MatrixD extends MatrixDimensions, MatrixDConduct { MatrixD expm(); /** - * Hadamard product {@code C = A} ∘ {@code B} (also known as + * Hadamard product {@code C = A} \u2218 {@code B} (also known as * element-wise product) of this matrix (A) and B. * * @param B diff --git a/src/main/java/net/jamu/matrix/MatrixDConduct.java b/src/main/java/net/jamu/matrix/MatrixDConduct.java index a337211..4d7ce04 100644 --- a/src/main/java/net/jamu/matrix/MatrixDConduct.java +++ b/src/main/java/net/jamu/matrix/MatrixDConduct.java @@ -330,7 +330,7 @@ public interface MatrixDConduct { MatrixD inverse(); /** - * Hadamard product {@code A} ∘ {@code B} (also known as + * Hadamard product {@code A} \u2218 {@code B} (also known as * element-wise product) of this matrix (A) and B. * * @param B @@ -341,7 +341,7 @@ public interface MatrixDConduct { MatrixD hadamard(MatrixD B); /** - * ABT Hadamard + * A \u2218 BT Hadamard * multiplication (also known as element-wise product) between this matrix * ({@code A}) and the transpose of {@code B} (BT). * @@ -353,7 +353,7 @@ public interface MatrixDConduct { MatrixD hadamardTransposed(MatrixD B); /** - * ATB Hadamard + * AT \u2218 B Hadamard * multiplication (also known as element-wise product) between this matrix * ({@code A}) transposed (AT) and {@code B}. * diff --git a/src/main/java/net/jamu/matrix/MatrixF.java b/src/main/java/net/jamu/matrix/MatrixF.java index 1aa7faf..70d44c4 100644 --- a/src/main/java/net/jamu/matrix/MatrixF.java +++ b/src/main/java/net/jamu/matrix/MatrixF.java @@ -604,7 +604,7 @@ public interface MatrixF extends MatrixDimensions, MatrixFConduct { MatrixF expm(); /** - * Hadamard product {@code C = A} ∘ {@code B} (also known as + * Hadamard product {@code C = A} \u2218 {@code B} (also known as * element-wise product) of this matrix (A) and B. * * @param B diff --git a/src/main/java/net/jamu/matrix/MatrixFConduct.java b/src/main/java/net/jamu/matrix/MatrixFConduct.java index a781731..67a07bc 100644 --- a/src/main/java/net/jamu/matrix/MatrixFConduct.java +++ b/src/main/java/net/jamu/matrix/MatrixFConduct.java @@ -330,7 +330,7 @@ public interface MatrixFConduct { MatrixF inverse(); /** - * Hadamard product {@code A} ∘ {@code B} (also known as + * Hadamard product {@code A} \u2218 {@code B} (also known as * element-wise product) of this matrix (A) and B. * * @param B @@ -341,7 +341,7 @@ public interface MatrixFConduct { MatrixF hadamard(MatrixF B); /** - * ABT Hadamard + * A \u2218 BT Hadamard * multiplication (also known as element-wise product) between this matrix * ({@code A}) and the transpose of {@code B} (BT). * @@ -353,7 +353,7 @@ public interface MatrixFConduct { MatrixF hadamardTransposed(MatrixF B); /** - * ATB Hadamard + * AT \u2218 B Hadamard * multiplication (also known as element-wise product) between this matrix * ({@code A}) transposed (AT) and {@code B}. * diff --git a/src/main/java/net/jamu/matrix/TensorD.java b/src/main/java/net/jamu/matrix/TensorD.java index c1d65bb..1b83a69 100644 --- a/src/main/java/net/jamu/matrix/TensorD.java +++ b/src/main/java/net/jamu/matrix/TensorD.java @@ -549,7 +549,7 @@ public TensorD mult(TensorD B, TensorD C) { } /** - * Hadamard product {@code C = A} ∘ {@code B} (also known as + * Hadamard product {@code C = A} \u2218 {@code B} (also known as * element-wise product) of this tensor (A) and B. If there is a mismatch * between the depths of the participating tensors the shortest depth is * chosen to reduce the operation to a common denominator (in which case the @@ -576,7 +576,7 @@ public TensorD hadamard(TensorD B, TensorD out) { } /** - * Hadamard product {@code A} ∘ {@code B} (also known as + * Hadamard product {@code A} \u2218 {@code B} (also known as * element-wise product) of this tensor (A) and B. If there is a mismatch * between the depths of the participating tensors the shortest depth is * chosen to reduce the operation to a common denominator (in which case the @@ -592,7 +592,7 @@ public TensorD hadamard(TensorD B) { } /** - * ABT Hadamard + * A \u2218 BT Hadamard * multiplication (also known as element-wise product) between this tensor * ({@code A}) and the transpose of {@code B} (BT). * If there is a mismatch between the depths of the participating tensors @@ -626,7 +626,7 @@ public TensorD hadamardTransposed(TensorD B) { } /** - * ATB Hadamard + * AT \u2218 B Hadamard * multiplication (also known as element-wise product) between this tensor * ({@code A}) transposed (AT) and {@code B}. If * there is a mismatch between the depths of the participating tensors the diff --git a/src/main/java/net/jamu/matrix/TensorF.java b/src/main/java/net/jamu/matrix/TensorF.java index e2578b3..48d03db 100644 --- a/src/main/java/net/jamu/matrix/TensorF.java +++ b/src/main/java/net/jamu/matrix/TensorF.java @@ -549,7 +549,7 @@ public TensorF mult(TensorF B, TensorF C) { } /** - * Hadamard product {@code C = A} ∘ {@code B} (also known as + * Hadamard product {@code C = A} \u2218 {@code B} (also known as * element-wise product) of this tensor (A) and B. If there is a mismatch * between the depths of the participating tensors the shortest depth is * chosen to reduce the operation to a common denominator (in which case the @@ -576,7 +576,7 @@ public TensorF hadamard(TensorF B, TensorF out) { } /** - * Hadamard product {@code A} ∘ {@code B} (also known as + * Hadamard product {@code A} \u2218 {@code B} (also known as * element-wise product) of this tensor (A) and B. If there is a mismatch * between the depths of the participating tensors the shortest depth is * chosen to reduce the operation to a common denominator (in which case the @@ -592,7 +592,7 @@ public TensorF hadamard(TensorF B) { } /** - * ABT Hadamard + * A \u2218 BT Hadamard * multiplication (also known as element-wise product) between this tensor * ({@code A}) and the transpose of {@code B} (BT). * If there is a mismatch between the depths of the participating tensors @@ -626,7 +626,7 @@ public TensorF hadamardTransposed(TensorF B) { } /** - * ATB Hadamard + * AT \u2218 B Hadamard * multiplication (also known as element-wise product) between this tensor * ({@code A}) transposed (AT) and {@code B}. If * there is a mismatch between the depths of the participating tensors the From facfc9f169dd53875d30ff2c59b1e0b248fd57c1 Mon Sep 17 00:00:00 2001 From: stefan-zobel Date: Wed, 6 Mar 2024 22:47:08 +0100 Subject: [PATCH 03/11] add DFunction / FFunction --- src/main/java/net/jamu/matrix/DFunction.java | 32 ++++++++++++++++++++ src/main/java/net/jamu/matrix/FFunction.java | 32 ++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 src/main/java/net/jamu/matrix/DFunction.java create mode 100644 src/main/java/net/jamu/matrix/FFunction.java diff --git a/src/main/java/net/jamu/matrix/DFunction.java b/src/main/java/net/jamu/matrix/DFunction.java new file mode 100644 index 0000000..f0b24e6 --- /dev/null +++ b/src/main/java/net/jamu/matrix/DFunction.java @@ -0,0 +1,32 @@ +/* + * Copyright 2018 Stefan Zobel + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.jamu.matrix; + +/** + * Represents a function that accepts one {@code double} argument and produces a + * {@code double} result. + */ +@FunctionalInterface +public interface DFunction { + /** + * Applies this function to the given argument. + * + * @param x + * the function argument + * @return the function result + */ + double apply(double x); +} diff --git a/src/main/java/net/jamu/matrix/FFunction.java b/src/main/java/net/jamu/matrix/FFunction.java new file mode 100644 index 0000000..5fc70b7 --- /dev/null +++ b/src/main/java/net/jamu/matrix/FFunction.java @@ -0,0 +1,32 @@ +/* + * Copyright 2018 Stefan Zobel + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.jamu.matrix; + +/** + * Represents a function that accepts one {@code float} argument and produces a + * {@code float} result. + */ +@FunctionalInterface +public interface FFunction { + /** + * Applies this function to the given argument. + * + * @param x + * the function argument + * @return the function result + */ + float apply(float x); +} From 33ee8a1efaee85d90675124abe8615c40e513dd5 Mon Sep 17 00:00:00 2001 From: stefan-zobel Date: Wed, 6 Mar 2024 22:59:28 +0100 Subject: [PATCH 04/11] add `mapInplace` to TensorD/F --- src/main/java/net/jamu/matrix/TensorD.java | 16 ++++++++++++++++ src/main/java/net/jamu/matrix/TensorF.java | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/main/java/net/jamu/matrix/TensorD.java b/src/main/java/net/jamu/matrix/TensorD.java index 1b83a69..8543b47 100644 --- a/src/main/java/net/jamu/matrix/TensorD.java +++ b/src/main/java/net/jamu/matrix/TensorD.java @@ -737,6 +737,22 @@ public TensorD scaleInplace(double alpha) { return this; } + /** + * {@code A = f(A)} where the scalar function {@code f} is applied to each + * element of {@code A}. + * + * @param f + * the scalar function to apply to each element of this tensor + * @return this tensor (mutated) + */ + public TensorD mapInplace(DFunction f) { + double[] _a = a; + for (int i = 0; i < _a.length; ++i) { + _a[i] = f.apply(_a[i]); + } + return this; + } + /** * Clamps all elements in this tensor into the range {@code [min, max]}. * diff --git a/src/main/java/net/jamu/matrix/TensorF.java b/src/main/java/net/jamu/matrix/TensorF.java index 48d03db..3e0a63d 100644 --- a/src/main/java/net/jamu/matrix/TensorF.java +++ b/src/main/java/net/jamu/matrix/TensorF.java @@ -737,6 +737,22 @@ public TensorF scaleInplace(float alpha) { return this; } + /** + * {@code A = f(A)} where the scalar function {@code f} is applied to each + * element of {@code A}. + * + * @param f + * the scalar function to apply to each element of this tensor + * @return this tensor (mutated) + */ + public TensorF mapInplace(FFunction f) { + float[] _a = a; + for (int i = 0; i < _a.length; ++i) { + _a[i] = f.apply(_a[i]); + } + return this; + } + /** * Clamps all elements in this tensor into the range {@code [min, max]}. * From bff3d63b0d18a6dd1668ac5e4ad5b382314d0acf Mon Sep 17 00:00:00 2001 From: stefan-zobel Date: Wed, 6 Mar 2024 23:10:27 +0100 Subject: [PATCH 05/11] add `map` to TensorD/F --- src/main/java/net/jamu/matrix/TensorD.java | 15 ++++++++++++++- src/main/java/net/jamu/matrix/TensorF.java | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/jamu/matrix/TensorD.java b/src/main/java/net/jamu/matrix/TensorD.java index 8543b47..f83b739 100644 --- a/src/main/java/net/jamu/matrix/TensorD.java +++ b/src/main/java/net/jamu/matrix/TensorD.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 Stefan Zobel + * Copyright 2023, 2024 Stefan Zobel * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -753,6 +753,19 @@ public TensorD mapInplace(DFunction f) { return this; } + /** + * Returns {@code f(A)} where the scalar function {@code f} is applied to + * each element on a copy of {@code A}. + * + * @param f + * the scalar function to apply to each element on a copy of this + * tensor + * @return a copy of this tensor where f has been applied to each element + */ + public TensorD map(DFunction f) { + return new TensorD(this).mapInplace(f); + } + /** * Clamps all elements in this tensor into the range {@code [min, max]}. * diff --git a/src/main/java/net/jamu/matrix/TensorF.java b/src/main/java/net/jamu/matrix/TensorF.java index 3e0a63d..6630c54 100644 --- a/src/main/java/net/jamu/matrix/TensorF.java +++ b/src/main/java/net/jamu/matrix/TensorF.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 Stefan Zobel + * Copyright 2023, 2024 Stefan Zobel * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -753,6 +753,19 @@ public TensorF mapInplace(FFunction f) { return this; } + /** + * Returns {@code f(A)} where the scalar function {@code f} is applied to + * each element on a copy of {@code A}. + * + * @param f + * the scalar function to apply to each element on a copy of this + * tensor + * @return a copy of this tensor where f has been applied to each element + */ + public TensorF map(FFunction f) { + return new TensorF(this).mapInplace(f); + } + /** * Clamps all elements in this tensor into the range {@code [min, max]}. * From 514a12c441558524e500896186c7edcee9c1c793 Mon Sep 17 00:00:00 2001 From: stefan-zobel Date: Thu, 7 Mar 2024 18:44:00 +0100 Subject: [PATCH 06/11] add missing `@since` Javadoc tags --- src/main/java/net/jamu/matrix/DFunction.java | 2 ++ src/main/java/net/jamu/matrix/FFunction.java | 2 ++ src/main/java/net/jamu/matrix/TensorD.java | 4 ++++ src/main/java/net/jamu/matrix/TensorF.java | 4 ++++ 4 files changed, 12 insertions(+) diff --git a/src/main/java/net/jamu/matrix/DFunction.java b/src/main/java/net/jamu/matrix/DFunction.java index f0b24e6..cda35d1 100644 --- a/src/main/java/net/jamu/matrix/DFunction.java +++ b/src/main/java/net/jamu/matrix/DFunction.java @@ -18,6 +18,8 @@ /** * Represents a function that accepts one {@code double} argument and produces a * {@code double} result. + * + * @since 1.4.2 */ @FunctionalInterface public interface DFunction { diff --git a/src/main/java/net/jamu/matrix/FFunction.java b/src/main/java/net/jamu/matrix/FFunction.java index 5fc70b7..923ba25 100644 --- a/src/main/java/net/jamu/matrix/FFunction.java +++ b/src/main/java/net/jamu/matrix/FFunction.java @@ -18,6 +18,8 @@ /** * Represents a function that accepts one {@code float} argument and produces a * {@code float} result. + * + * @since 1.4.2 */ @FunctionalInterface public interface FFunction { diff --git a/src/main/java/net/jamu/matrix/TensorD.java b/src/main/java/net/jamu/matrix/TensorD.java index f83b739..14faf5b 100644 --- a/src/main/java/net/jamu/matrix/TensorD.java +++ b/src/main/java/net/jamu/matrix/TensorD.java @@ -744,6 +744,8 @@ public TensorD scaleInplace(double alpha) { * @param f * the scalar function to apply to each element of this tensor * @return this tensor (mutated) + * + * @since 1.4.2 */ public TensorD mapInplace(DFunction f) { double[] _a = a; @@ -761,6 +763,8 @@ public TensorD mapInplace(DFunction f) { * the scalar function to apply to each element on a copy of this * tensor * @return a copy of this tensor where f has been applied to each element + * + * @since 1.4.2 */ public TensorD map(DFunction f) { return new TensorD(this).mapInplace(f); diff --git a/src/main/java/net/jamu/matrix/TensorF.java b/src/main/java/net/jamu/matrix/TensorF.java index 6630c54..ef7be64 100644 --- a/src/main/java/net/jamu/matrix/TensorF.java +++ b/src/main/java/net/jamu/matrix/TensorF.java @@ -744,6 +744,8 @@ public TensorF scaleInplace(float alpha) { * @param f * the scalar function to apply to each element of this tensor * @return this tensor (mutated) + * + * @since 1.4.2 */ public TensorF mapInplace(FFunction f) { float[] _a = a; @@ -761,6 +763,8 @@ public TensorF mapInplace(FFunction f) { * the scalar function to apply to each element on a copy of this * tensor * @return a copy of this tensor where f has been applied to each element + * + * @since 1.4.2 */ public TensorF map(FFunction f) { return new TensorF(this).mapInplace(f); From 3db26121a7c9c567434f8b7c7cf005c441ea0248 Mon Sep 17 00:00:00 2001 From: stefan-zobel Date: Thu, 7 Mar 2024 19:49:38 +0100 Subject: [PATCH 07/11] add `mapInplace` to MatrixD/MatrixF --- src/main/java/net/jamu/matrix/MatrixD.java | 13 ++++++++++++- src/main/java/net/jamu/matrix/MatrixDBase.java | 14 +++++++++++++- src/main/java/net/jamu/matrix/MatrixF.java | 13 ++++++++++++- src/main/java/net/jamu/matrix/MatrixFBase.java | 14 +++++++++++++- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/jamu/matrix/MatrixD.java b/src/main/java/net/jamu/matrix/MatrixD.java index 31d834b..7a7996e 100644 --- a/src/main/java/net/jamu/matrix/MatrixD.java +++ b/src/main/java/net/jamu/matrix/MatrixD.java @@ -1,5 +1,5 @@ /* - * Copyright 2019, 2023 Stefan Zobel + * Copyright 2019, 2024 Stefan Zobel * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -683,6 +683,17 @@ public interface MatrixD extends MatrixDimensions, MatrixDConduct { */ MatrixD clampInplace(double min, double max); + /** + * {@code A = f(A)} where the scalar function {@code f} is applied to each + * element of {@code A}. + * + * @param f + * the scalar function to apply to each element of this matrix + * @return this matrix (mutated) + * @since 1.4.2 + */ + MatrixD mapInplace(DFunction f); + /** * Set all elements |xij| ≤ k * 2-53 * ({@code k} times the machine epsilon for doubles) to {@code 0.0} where diff --git a/src/main/java/net/jamu/matrix/MatrixDBase.java b/src/main/java/net/jamu/matrix/MatrixDBase.java index e4ec1e9..6e6210c 100644 --- a/src/main/java/net/jamu/matrix/MatrixDBase.java +++ b/src/main/java/net/jamu/matrix/MatrixDBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2019, 2023 Stefan Zobel + * Copyright 2019, 2024 Stefan Zobel * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -657,6 +657,18 @@ public MatrixD clampInplace(double min, double max) { return this; } + /** + * {@inheritDoc} + */ + @Override + public MatrixD mapInplace(DFunction f) { + double[] _a = a; + for (int i = 0; i < _a.length; ++i) { + _a[i] = f.apply(_a[i]); + } + return this; + } + /** * {@inheritDoc} */ diff --git a/src/main/java/net/jamu/matrix/MatrixF.java b/src/main/java/net/jamu/matrix/MatrixF.java index 70d44c4..6b5617c 100644 --- a/src/main/java/net/jamu/matrix/MatrixF.java +++ b/src/main/java/net/jamu/matrix/MatrixF.java @@ -1,5 +1,5 @@ /* - * Copyright 2019, 2023 Stefan Zobel + * Copyright 2019, 2024 Stefan Zobel * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -683,6 +683,17 @@ public interface MatrixF extends MatrixDimensions, MatrixFConduct { */ MatrixF clampInplace(float min, float max); + /** + * {@code A = f(A)} where the scalar function {@code f} is applied to each + * element of {@code A}. + * + * @param f + * the scalar function to apply to each element of this matrix + * @return this matrix (mutated) + * @since 1.4.2 + */ + MatrixF mapInplace(FFunction f); + /** * Set all elements |xij| ≤ k * 2-24 * ({@code k} times the machine epsilon for floats) to {@code 0.0f} where diff --git a/src/main/java/net/jamu/matrix/MatrixFBase.java b/src/main/java/net/jamu/matrix/MatrixFBase.java index 0695293..2735e49 100644 --- a/src/main/java/net/jamu/matrix/MatrixFBase.java +++ b/src/main/java/net/jamu/matrix/MatrixFBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2019, 2023 Stefan Zobel + * Copyright 2019, 2024 Stefan Zobel * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -657,6 +657,18 @@ public MatrixF clampInplace(float min, float max) { return this; } + /** + * {@inheritDoc} + */ + @Override + public MatrixF mapInplace(FFunction f) { + float[] _a = a; + for (int i = 0; i < _a.length; ++i) { + _a[i] = f.apply(_a[i]); + } + return this; + } + /** * {@inheritDoc} */ From 4beb165019f5686f4fa7f9bcac32d39d39555ab0 Mon Sep 17 00:00:00 2001 From: stefan-zobel Date: Thu, 7 Mar 2024 20:13:34 +0100 Subject: [PATCH 08/11] add `map` to MatrixD/MatrixF --- src/main/java/net/jamu/matrix/MatrixDBase.java | 8 ++++++++ src/main/java/net/jamu/matrix/MatrixDConduct.java | 14 +++++++++++++- src/main/java/net/jamu/matrix/MatrixFBase.java | 8 ++++++++ src/main/java/net/jamu/matrix/MatrixFConduct.java | 14 +++++++++++++- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/jamu/matrix/MatrixDBase.java b/src/main/java/net/jamu/matrix/MatrixDBase.java index 6e6210c..39b5424 100644 --- a/src/main/java/net/jamu/matrix/MatrixDBase.java +++ b/src/main/java/net/jamu/matrix/MatrixDBase.java @@ -1039,6 +1039,14 @@ public MatrixD abs() { return m; } + /** + * {@inheritDoc} + */ + @Override + public MatrixD map(DFunction f) { + return copy().mapInplace(f); + } + /** * {@inheritDoc} */ diff --git a/src/main/java/net/jamu/matrix/MatrixDConduct.java b/src/main/java/net/jamu/matrix/MatrixDConduct.java index 4d7ce04..7214650 100644 --- a/src/main/java/net/jamu/matrix/MatrixDConduct.java +++ b/src/main/java/net/jamu/matrix/MatrixDConduct.java @@ -1,5 +1,5 @@ /* - * Copyright 2020, 2023 Stefan Zobel + * Copyright 2020, 2024 Stefan Zobel * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -364,6 +364,18 @@ public interface MatrixDConduct { */ MatrixD transposedHadamard(MatrixD B); + /** + * Returns {@code f(A)} where the scalar function {@code f} is applied to + * each element on a copy of {@code A}. + * + * @param f + * the scalar function to apply to each element on a copy of this + * matrix + * @return a copy of this matrix where f has been applied to each element + * @since 1.4.2 + */ + MatrixD map(DFunction f); + /** * Reshapes this matrix into a new matrix of dimension {@code rows x cols} * where the elements in this matrix are read in Fortran-style column-major diff --git a/src/main/java/net/jamu/matrix/MatrixFBase.java b/src/main/java/net/jamu/matrix/MatrixFBase.java index 2735e49..53f3cf3 100644 --- a/src/main/java/net/jamu/matrix/MatrixFBase.java +++ b/src/main/java/net/jamu/matrix/MatrixFBase.java @@ -1039,6 +1039,14 @@ public MatrixF abs() { return m; } + /** + * {@inheritDoc} + */ + @Override + public MatrixF map(FFunction f) { + return copy().mapInplace(f); + } + /** * {@inheritDoc} */ diff --git a/src/main/java/net/jamu/matrix/MatrixFConduct.java b/src/main/java/net/jamu/matrix/MatrixFConduct.java index 67a07bc..a37eaa1 100644 --- a/src/main/java/net/jamu/matrix/MatrixFConduct.java +++ b/src/main/java/net/jamu/matrix/MatrixFConduct.java @@ -1,5 +1,5 @@ /* - * Copyright 2020, 2023 Stefan Zobel + * Copyright 2020, 2024 Stefan Zobel * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -364,6 +364,18 @@ public interface MatrixFConduct { */ MatrixF transposedHadamard(MatrixF B); + /** + * Returns {@code f(A)} where the scalar function {@code f} is applied to + * each element on a copy of {@code A}. + * + * @param f + * the scalar function to apply to each element on a copy of this + * matrix + * @return a copy of this matrix where f has been applied to each element + * @since 1.4.2 + */ + MatrixF map(FFunction f); + /** * Reshapes this matrix into a new matrix of dimension {@code rows x cols} * where the elements in this matrix are read in Fortran-style column-major From 8082e1e2f1764ba74625f2d3b072631c10ec90c9 Mon Sep 17 00:00:00 2001 From: stefan-zobel Date: Fri, 8 Mar 2024 04:26:47 +0100 Subject: [PATCH 09/11] Javadoc formatting --- src/main/java/net/jamu/matrix/TensorD.java | 2 -- src/main/java/net/jamu/matrix/TensorF.java | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/main/java/net/jamu/matrix/TensorD.java b/src/main/java/net/jamu/matrix/TensorD.java index 14faf5b..761a95d 100644 --- a/src/main/java/net/jamu/matrix/TensorD.java +++ b/src/main/java/net/jamu/matrix/TensorD.java @@ -744,7 +744,6 @@ public TensorD scaleInplace(double alpha) { * @param f * the scalar function to apply to each element of this tensor * @return this tensor (mutated) - * * @since 1.4.2 */ public TensorD mapInplace(DFunction f) { @@ -763,7 +762,6 @@ public TensorD mapInplace(DFunction f) { * the scalar function to apply to each element on a copy of this * tensor * @return a copy of this tensor where f has been applied to each element - * * @since 1.4.2 */ public TensorD map(DFunction f) { diff --git a/src/main/java/net/jamu/matrix/TensorF.java b/src/main/java/net/jamu/matrix/TensorF.java index ef7be64..c393379 100644 --- a/src/main/java/net/jamu/matrix/TensorF.java +++ b/src/main/java/net/jamu/matrix/TensorF.java @@ -744,7 +744,6 @@ public TensorF scaleInplace(float alpha) { * @param f * the scalar function to apply to each element of this tensor * @return this tensor (mutated) - * * @since 1.4.2 */ public TensorF mapInplace(FFunction f) { @@ -763,7 +762,6 @@ public TensorF mapInplace(FFunction f) { * the scalar function to apply to each element on a copy of this * tensor * @return a copy of this tensor where f has been applied to each element - * * @since 1.4.2 */ public TensorF map(FFunction f) { From bff6c48eec5dd257a12acf1fd661b9bcc466c78a Mon Sep 17 00:00:00 2001 From: stefan-zobel Date: Fri, 8 Mar 2024 21:17:49 +0100 Subject: [PATCH 10/11] add `rowsAverage` for MatrixD/F --- src/main/java/net/jamu/matrix/Matrices.java | 30 ++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/jamu/matrix/Matrices.java b/src/main/java/net/jamu/matrix/Matrices.java index b3c0f4a..7360758 100644 --- a/src/main/java/net/jamu/matrix/Matrices.java +++ b/src/main/java/net/jamu/matrix/Matrices.java @@ -1,5 +1,5 @@ /* - * Copyright 2019, 2023 Stefan Zobel + * Copyright 2019, 2024 Stefan Zobel * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -3499,6 +3499,34 @@ public static ComplexMatrixF sumRows(ComplexMatrixF A) { return s; } + /** + * Returns a {@code 1 x A.numColumns()} matrix that contains the average of + * the row sums of matrix {@code A}. + * + * @param A + * the matrix whose average of the rows sums should be computed + * @return a new matrix of dimension {@code 1 x A.numColumns()} that + * contains the average of the row sums of matrix {@code A} + * @since 1.4.2 + */ + public static MatrixD rowsAverage(MatrixD A) { + return sumRows(A).scaleInplace(A.numRows()); + } + + /** + * Returns a {@code 1 x A.numColumns()} matrix that contains the average of + * the row sums of matrix {@code A}. + * + * @param A + * the matrix whose average of the rows sums should be computed + * @return a new matrix of dimension {@code 1 x A.numColumns()} that + * contains the average of the row sums of matrix {@code A} + * @since 1.4.2 + */ + public static MatrixF rowsAverage(MatrixF A) { + return sumRows(A).scaleInplace(A.numRows()); + } + /** * Returns a {@code A.numRows() x 1} matrix that contains the column sums of * matrix {@code A}. From d5885314649250c44227c769647d11e5baff9dda Mon Sep 17 00:00:00 2001 From: stefan-zobel Date: Fri, 8 Mar 2024 21:26:53 +0100 Subject: [PATCH 11/11] add `colsAverage` for MatrixD/F --- src/main/java/net/jamu/matrix/Matrices.java | 32 +++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/jamu/matrix/Matrices.java b/src/main/java/net/jamu/matrix/Matrices.java index 7360758..8fe9a75 100644 --- a/src/main/java/net/jamu/matrix/Matrices.java +++ b/src/main/java/net/jamu/matrix/Matrices.java @@ -3504,7 +3504,7 @@ public static ComplexMatrixF sumRows(ComplexMatrixF A) { * the row sums of matrix {@code A}. * * @param A - * the matrix whose average of the rows sums should be computed + * the matrix whose average of the row sums should be computed * @return a new matrix of dimension {@code 1 x A.numColumns()} that * contains the average of the row sums of matrix {@code A} * @since 1.4.2 @@ -3518,7 +3518,7 @@ public static MatrixD rowsAverage(MatrixD A) { * the row sums of matrix {@code A}. * * @param A - * the matrix whose average of the rows sums should be computed + * the matrix whose average of the row sums should be computed * @return a new matrix of dimension {@code 1 x A.numColumns()} that * contains the average of the row sums of matrix {@code A} * @since 1.4.2 @@ -3645,6 +3645,34 @@ public static ComplexMatrixF sumColumns(ComplexMatrixF A) { return s; } + /** + * Returns a {@code A.numRows() x 1} matrix that contains the average of the + * column sums of matrix {@code A}. + * + * @param A + * the matrix whose average of column sums should be computed + * @return a new matrix of dimension {@code A.numRows() x 1} that contains + * the average of the column sums of matrix {@code A} + * @since 1.4.2 + */ + public static MatrixD colsAverage(MatrixD A) { + return sumColumns(A).scaleInplace(A.numColumns()); + } + + /** + * Returns a {@code A.numRows() x 1} matrix that contains the average of the + * column sums of matrix {@code A}. + * + * @param A + * the matrix whose average of column sums should be computed + * @return a new matrix of dimension {@code A.numRows() x 1} that contains + * the average of the column sums of matrix {@code A} + * @since 1.4.2 + */ + public static MatrixF colsAverage(MatrixF A) { + return sumColumns(A).scaleInplace(A.numColumns()); + } + private static boolean checkApproxEqualArgs(MatrixDimensions A, MatrixDimensions B, double relTol, double absTol) { if (relTol < 0.0 || Double.isNaN(relTol) || Double.isInfinite(relTol)) { throw new IllegalArgumentException("illegal relTol : " + relTol);