Skip to content

Commit e6eea0e

Browse files
committed
Hadamard product for complex matrices
1 parent 75c6c69 commit e6eea0e

File tree

6 files changed

+110
-2
lines changed

6 files changed

+110
-2
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020, 2021 Stefan Zobel
2+
* Copyright 2020, 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.
@@ -637,6 +637,19 @@ public interface ComplexMatrixD extends Dimensions, ComplexMatrixDConduct {
637637
*/
638638
ComplexMatrixD expm();
639639

640+
/**
641+
* Hadamard product {@code C = A} ∘ {@code B} (also known as
642+
* element-wise product) of this matrix (A) and B.
643+
*
644+
* @param B
645+
* the matrix this matrix is multiplied with
646+
* @param out
647+
* output matrix for the result of the multiplication
648+
* @return {@code out}
649+
* @since 1.3.1
650+
*/
651+
ComplexMatrixD hadamard(ComplexMatrixD B, ComplexMatrixD out);
652+
640653
/**
641654
* Computes the singular value decomposition of this matrix.
642655
*

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,28 @@ public ComplexMatrixD expm() {
597597
return Expm.expmComplexD(this, normMaxAbs());
598598
}
599599

600+
/**
601+
* {@inheritDoc}
602+
*/
603+
@Override
604+
public ComplexMatrixD hadamard(ComplexMatrixD B, ComplexMatrixD out) {
605+
Checks.checkEqualDimension(this, B);
606+
Checks.checkEqualDimension(this, out);
607+
double[] _a = a;
608+
double[] _b = B.getArrayUnsafe();
609+
double[] _c = out.getArrayUnsafe();
610+
ZdImpl a = new ZdImpl(0.0);
611+
ZdImpl b = new ZdImpl(0.0);
612+
for (int i = 0; i < _a.length; i += 2) {
613+
a.set(_a[i], _a[i + 1]);
614+
b.set(_b[i], _b[i + 1]);
615+
a.mul(b);
616+
_c[i] = a.re();
617+
_c[i + 1] = a.im();
618+
}
619+
return out;
620+
}
621+
600622
/**
601623
* {@inheritDoc}
602624
*/
@@ -940,6 +962,14 @@ public ComplexMatrixD conjugateTransposedTimes(ComplexMatrixD B) {
940962
return conjTransAmult(B, create(cols, B.numColumns()));
941963
}
942964

965+
/**
966+
* {@inheritDoc}
967+
*/
968+
@Override
969+
public ComplexMatrixD hadamard(ComplexMatrixD B) {
970+
return hadamard(B, create(rows, cols));
971+
}
972+
943973
/**
944974
* {@inheritDoc}
945975
*/

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,17 @@ public interface ComplexMatrixDConduct {
338338
*/
339339
ComplexMatrixD inverse();
340340

341+
/**
342+
* Hadamard product {@code A} &SmallCircle; {@code B} (also known as
343+
* element-wise product) of this matrix (A) and B.
344+
*
345+
* @param B
346+
* the matrix this matrix is multiplied with
347+
* @return the result of the Hadamard multiplication
348+
* @since 1.3.1
349+
*/
350+
ComplexMatrixD hadamard(ComplexMatrixD B);
351+
341352
/**
342353
* Reshapes this matrix into a new matrix of dimension {@code rows x cols}
343354
* where the elements in this matrix are read in Fortran-style column-major

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020, 2021 Stefan Zobel
2+
* Copyright 2020, 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.
@@ -637,6 +637,19 @@ public interface ComplexMatrixF extends Dimensions, ComplexMatrixFConduct {
637637
*/
638638
ComplexMatrixF expm();
639639

640+
/**
641+
* Hadamard product {@code C = A} &SmallCircle; {@code B} (also known as
642+
* element-wise product) of this matrix (A) and B.
643+
*
644+
* @param B
645+
* the matrix this matrix is multiplied with
646+
* @param out
647+
* output matrix for the result of the multiplication
648+
* @return {@code out}
649+
* @since 1.3.1
650+
*/
651+
ComplexMatrixF hadamard(ComplexMatrixF B, ComplexMatrixF out);
652+
640653
/**
641654
* Computes the singular value decomposition of this matrix.
642655
*

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,28 @@ public ComplexMatrixF expm() {
597597
return Expm.expmComplexF(this, normMaxAbs());
598598
}
599599

600+
/**
601+
* {@inheritDoc}
602+
*/
603+
@Override
604+
public ComplexMatrixF hadamard(ComplexMatrixF B, ComplexMatrixF out) {
605+
Checks.checkEqualDimension(this, B);
606+
Checks.checkEqualDimension(this, out);
607+
float[] _a = a;
608+
float[] _b = B.getArrayUnsafe();
609+
float[] _c = out.getArrayUnsafe();
610+
ZfImpl a = new ZfImpl(0.0f);
611+
ZfImpl b = new ZfImpl(0.0f);
612+
for (int i = 0; i < _a.length; i += 2) {
613+
a.set(_a[i], _a[i + 1]);
614+
b.set(_b[i], _b[i + 1]);
615+
a.mul(b);
616+
_c[i] = a.re();
617+
_c[i + 1] = a.im();
618+
}
619+
return out;
620+
}
621+
600622
/**
601623
* {@inheritDoc}
602624
*/
@@ -939,6 +961,14 @@ public ComplexMatrixF conjugateTransposedTimes(ComplexMatrixF B) {
939961
return conjTransAmult(B, create(cols, B.numColumns()));
940962
}
941963

964+
/**
965+
* {@inheritDoc}
966+
*/
967+
@Override
968+
public ComplexMatrixF hadamard(ComplexMatrixF B) {
969+
return hadamard(B, create(rows,cols));
970+
}
971+
942972
/**
943973
* {@inheritDoc}
944974
*/

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,17 @@ public interface ComplexMatrixFConduct {
338338
*/
339339
ComplexMatrixF inverse();
340340

341+
/**
342+
* Hadamard product {@code A} &SmallCircle; {@code B} (also known as
343+
* element-wise product) of this matrix (A) and B.
344+
*
345+
* @param B
346+
* the matrix this matrix is multiplied with
347+
* @return the result of the Hadamard multiplication
348+
* @since 1.3.1
349+
*/
350+
ComplexMatrixF hadamard(ComplexMatrixF B);
351+
341352
/**
342353
* Reshapes this matrix into a new matrix of dimension {@code rows x cols}
343354
* where the elements in this matrix are read in Fortran-style column-major

0 commit comments

Comments
 (0)