Skip to content

Commit 75c6c69

Browse files
committed
Hadamard product for real matrices
1 parent 5d9a055 commit 75c6c69

File tree

7 files changed

+100
-4
lines changed

7 files changed

+100
-4
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>net.sourceforge.streamsupport</groupId>
55
<artifactId>jamu</artifactId>
66
<packaging>jar</packaging>
7-
<version>1.3.1-SNAPSHOT-1</version>
7+
<version>1.3.1-SNAPSHOT-2</version>
88
<name>JAMU</name>
99
<description>Java Matrix Utilities built on top of Intel MKL</description>
1010
<url>https://github.com/stefan-zobel/JAMU/</url>
@@ -63,7 +63,7 @@
6363
<goal>jar</goal>
6464
</goals>
6565
<configuration>
66-
<!--doclint>none</doclint-->
66+
<doclint>none</doclint>
6767
</configuration>
6868
</execution>
6969
</executions>

src/main/java/net/jamu/matrix/MatrixD.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019, 2021 Stefan Zobel
2+
* Copyright 2019, 2023 Stefan Zobel
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -603,6 +603,19 @@ public interface MatrixD extends Dimensions, MatrixDConduct {
603603
*/
604604
MatrixD expm();
605605

606+
/**
607+
* Hadamard product {@code C = A} &SmallCircle; {@code B} (also known as
608+
* element-wise product) of this matrix (A) and B.
609+
*
610+
* @param B
611+
* the matrix this matrix is multiplied with
612+
* @param out
613+
* output matrix for the result of the multiplication
614+
* @return {@code out}
615+
* @since 1.3.1
616+
*/
617+
MatrixD hadamard(MatrixD B, MatrixD out);
618+
606619
/**
607620
* Copy into a jagged array.
608621
*

src/main/java/net/jamu/matrix/MatrixDBase.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,22 @@ public MatrixD expm() {
535535
return Expm.expmD(this, normMaxAbs());
536536
}
537537

538+
/**
539+
* {@inheritDoc}
540+
*/
541+
@Override
542+
public MatrixD hadamard(MatrixD B, MatrixD out) {
543+
Checks.checkEqualDimension(this, B);
544+
Checks.checkEqualDimension(this, out);
545+
double[] _a = a;
546+
double[] _b = B.getArrayUnsafe();
547+
double[] _c = out.getArrayUnsafe();
548+
for (int i = 0; i < _a.length; ++i) {
549+
_c[i] = _a[i] * _b[i];
550+
}
551+
return out;
552+
}
553+
538554
/**
539555
* {@inheritDoc}
540556
*/
@@ -859,6 +875,14 @@ public MatrixD transposedTimes(MatrixD B) {
859875
return transAmult(B, create(cols, B.numColumns()));
860876
}
861877

878+
/**
879+
* {@inheritDoc}
880+
*/
881+
@Override
882+
public MatrixD hadamard(MatrixD B) {
883+
return hadamard(B, create(rows, cols));
884+
}
885+
862886
/**
863887
* {@inheritDoc}
864888
*/

src/main/java/net/jamu/matrix/MatrixDConduct.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,17 @@ public interface MatrixDConduct {
329329
*/
330330
MatrixD inverse();
331331

332+
/**
333+
* Hadamard product {@code A} &SmallCircle; {@code B} (also known as
334+
* element-wise product) of this matrix (A) and B.
335+
*
336+
* @param B
337+
* the matrix this matrix is multiplied with
338+
* @return the result of the Hadamard multiplication
339+
* @since 1.3.1
340+
*/
341+
MatrixD hadamard(MatrixD B);
342+
332343
/**
333344
* Reshapes this matrix into a new matrix of dimension {@code rows x cols}
334345
* where the elements in this matrix are read in Fortran-style column-major

src/main/java/net/jamu/matrix/MatrixF.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019, 2021 Stefan Zobel
2+
* Copyright 2019, 2023 Stefan Zobel
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -603,6 +603,19 @@ public interface MatrixF extends Dimensions, MatrixFConduct {
603603
*/
604604
MatrixF expm();
605605

606+
/**
607+
* Hadamard product {@code C = A} &SmallCircle; {@code B} (also known as
608+
* element-wise product) of this matrix (A) and B.
609+
*
610+
* @param B
611+
* the matrix this matrix is multiplied with
612+
* @param out
613+
* output matrix for the result of the multiplication
614+
* @return {@code out}
615+
* @since 1.3.1
616+
*/
617+
MatrixF hadamard(MatrixF B, MatrixF out);
618+
606619
/**
607620
* Copy into a jagged array.
608621
*

src/main/java/net/jamu/matrix/MatrixFBase.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,22 @@ public MatrixF expm() {
535535
return Expm.expmF(this, normMaxAbs());
536536
}
537537

538+
/**
539+
* {@inheritDoc}
540+
*/
541+
@Override
542+
public MatrixF hadamard(MatrixF B, MatrixF out) {
543+
Checks.checkEqualDimension(this, B);
544+
Checks.checkEqualDimension(this, out);
545+
float[] _a = a;
546+
float[] _b = B.getArrayUnsafe();
547+
float[] _c = out.getArrayUnsafe();
548+
for (int i = 0; i < _a.length; ++i) {
549+
_c[i] = _a[i] * _b[i];
550+
}
551+
return out;
552+
}
553+
538554
/**
539555
* {@inheritDoc}
540556
*/
@@ -859,6 +875,14 @@ public MatrixF transposedTimes(MatrixF B) {
859875
return transAmult(B, create(cols, B.numColumns()));
860876
}
861877

878+
/**
879+
* {@inheritDoc}
880+
*/
881+
@Override
882+
public MatrixF hadamard(MatrixF B) {
883+
return hadamard(B, create(rows, cols));
884+
}
885+
862886
/**
863887
* {@inheritDoc}
864888
*/

src/main/java/net/jamu/matrix/MatrixFConduct.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,17 @@ public interface MatrixFConduct {
329329
*/
330330
MatrixF inverse();
331331

332+
/**
333+
* Hadamard product {@code A} &SmallCircle; {@code B} (also known as
334+
* element-wise product) of this matrix (A) and B.
335+
*
336+
* @param B
337+
* the matrix this matrix is multiplied with
338+
* @return the result of the Hadamard multiplication
339+
* @since 1.3.1
340+
*/
341+
MatrixF hadamard(MatrixF B);
342+
332343
/**
333344
* Reshapes this matrix into a new matrix of dimension {@code rows x cols}
334345
* where the elements in this matrix are read in Fortran-style column-major

0 commit comments

Comments
 (0)