Skip to content

Commit

Permalink
Hadamard product for complex matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-zobel committed Jul 21, 2023
1 parent 75c6c69 commit e6eea0e
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/main/java/net/jamu/matrix/ComplexMatrixD.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020, 2021 Stefan Zobel
* Copyright 2020, 2023 Stefan Zobel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -637,6 +637,19 @@ public interface ComplexMatrixD extends Dimensions, ComplexMatrixDConduct {
*/
ComplexMatrixD expm();

/**
* Hadamard product {@code C = A} ∘ {@code B} (also known as
* element-wise product) of this matrix (A) and B.
*
* @param B
* the matrix this matrix is multiplied with
* @param out
* output matrix for the result of the multiplication
* @return {@code out}
* @since 1.3.1
*/
ComplexMatrixD hadamard(ComplexMatrixD B, ComplexMatrixD out);

/**
* Computes the singular value decomposition of this matrix.
*
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/net/jamu/matrix/ComplexMatrixDBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,28 @@ public ComplexMatrixD expm() {
return Expm.expmComplexD(this, normMaxAbs());
}

/**
* {@inheritDoc}
*/
@Override
public ComplexMatrixD hadamard(ComplexMatrixD B, ComplexMatrixD out) {
Checks.checkEqualDimension(this, B);
Checks.checkEqualDimension(this, out);
double[] _a = a;
double[] _b = B.getArrayUnsafe();
double[] _c = out.getArrayUnsafe();
ZdImpl a = new ZdImpl(0.0);
ZdImpl b = new ZdImpl(0.0);
for (int i = 0; i < _a.length; i += 2) {
a.set(_a[i], _a[i + 1]);
b.set(_b[i], _b[i + 1]);
a.mul(b);
_c[i] = a.re();
_c[i + 1] = a.im();
}
return out;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -940,6 +962,14 @@ public ComplexMatrixD conjugateTransposedTimes(ComplexMatrixD B) {
return conjTransAmult(B, create(cols, B.numColumns()));
}

/**
* {@inheritDoc}
*/
@Override
public ComplexMatrixD hadamard(ComplexMatrixD B) {
return hadamard(B, create(rows, cols));
}

/**
* {@inheritDoc}
*/
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/jamu/matrix/ComplexMatrixDConduct.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ public interface ComplexMatrixDConduct {
*/
ComplexMatrixD inverse();

/**
* Hadamard product {@code A} &SmallCircle; {@code B} (also known as
* element-wise product) of this matrix (A) and B.
*
* @param B
* the matrix this matrix is multiplied with
* @return the result of the Hadamard multiplication
* @since 1.3.1
*/
ComplexMatrixD hadamard(ComplexMatrixD B);

/**
* 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
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/net/jamu/matrix/ComplexMatrixF.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020, 2021 Stefan Zobel
* Copyright 2020, 2023 Stefan Zobel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -637,6 +637,19 @@ public interface ComplexMatrixF extends Dimensions, ComplexMatrixFConduct {
*/
ComplexMatrixF expm();

/**
* Hadamard product {@code C = A} &SmallCircle; {@code B} (also known as
* element-wise product) of this matrix (A) and B.
*
* @param B
* the matrix this matrix is multiplied with
* @param out
* output matrix for the result of the multiplication
* @return {@code out}
* @since 1.3.1
*/
ComplexMatrixF hadamard(ComplexMatrixF B, ComplexMatrixF out);

/**
* Computes the singular value decomposition of this matrix.
*
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/net/jamu/matrix/ComplexMatrixFBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,28 @@ public ComplexMatrixF expm() {
return Expm.expmComplexF(this, normMaxAbs());
}

/**
* {@inheritDoc}
*/
@Override
public ComplexMatrixF hadamard(ComplexMatrixF B, ComplexMatrixF out) {
Checks.checkEqualDimension(this, B);
Checks.checkEqualDimension(this, out);
float[] _a = a;
float[] _b = B.getArrayUnsafe();
float[] _c = out.getArrayUnsafe();
ZfImpl a = new ZfImpl(0.0f);
ZfImpl b = new ZfImpl(0.0f);
for (int i = 0; i < _a.length; i += 2) {
a.set(_a[i], _a[i + 1]);
b.set(_b[i], _b[i + 1]);
a.mul(b);
_c[i] = a.re();
_c[i + 1] = a.im();
}
return out;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -939,6 +961,14 @@ public ComplexMatrixF conjugateTransposedTimes(ComplexMatrixF B) {
return conjTransAmult(B, create(cols, B.numColumns()));
}

/**
* {@inheritDoc}
*/
@Override
public ComplexMatrixF hadamard(ComplexMatrixF B) {
return hadamard(B, create(rows,cols));
}

/**
* {@inheritDoc}
*/
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/jamu/matrix/ComplexMatrixFConduct.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ public interface ComplexMatrixFConduct {
*/
ComplexMatrixF inverse();

/**
* Hadamard product {@code A} &SmallCircle; {@code B} (also known as
* element-wise product) of this matrix (A) and B.
*
* @param B
* the matrix this matrix is multiplied with
* @return the result of the Hadamard multiplication
* @since 1.3.1
*/
ComplexMatrixF hadamard(ComplexMatrixF B);

/**
* 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
Expand Down

0 comments on commit e6eea0e

Please sign in to comment.