Skip to content

Commit 3129593

Browse files
committed
add transposedHadamard for tensors
1 parent b5f1752 commit 3129593

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,40 @@ public TensorD hadamardTransposed(TensorD B) {
625625
return C;
626626
}
627627

628+
/**
629+
* <code>A<sup>T</sup></code> &SmallCircle; <code>B</code> Hadamard
630+
* multiplication (also known as element-wise product) between this tensor
631+
* ({@code A}) transposed (<code>A<sup>T</sup></code>) and {@code B}. If
632+
* there is a mismatch between the depths of the participating tensors the
633+
* shortest depth is chosen to reduce the operation to a common denominator
634+
* (in which case the excess layers of the longer tensor are left
635+
* untouched).
636+
*
637+
* @param B
638+
* the tensor which is multiplied with this tensor transposed
639+
* @return the result of the Hadamard multiplication
640+
*/
641+
public TensorD transposedHadamard(TensorD B) {
642+
Checks.checkTrans(this, B);
643+
int _rows = B.numRows();
644+
int _cols = B.numColumns();
645+
int _depth = Math.min(this.depth, B.depth);
646+
TensorD C = create(_rows, _cols, _depth);
647+
double[] _a = a;
648+
double[] _b = B.getArrayUnsafe();
649+
double[] _c = C.getArrayUnsafe();
650+
TensorBase btb = (TensorBase) B;
651+
for (int layer = 0; layer < _depth; ++layer) {
652+
for (int col = 0; col < _cols; ++col) {
653+
for (int row = 0; row < _rows; ++row) {
654+
int idx = btb.idx(row, col, layer);
655+
_c[idx] = _b[idx] * _a[idx(col, row, layer)];
656+
}
657+
}
658+
}
659+
return C;
660+
}
661+
628662
/**
629663
* {@code A * B} convenience multiplication. None of the operands are
630664
* mutated. If there is a mismatch between the depths of the participating

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,40 @@ public TensorF hadamardTransposed(TensorF B) {
625625
return C;
626626
}
627627

628+
/**
629+
* <code>A<sup>T</sup></code> &SmallCircle; <code>B</code> Hadamard
630+
* multiplication (also known as element-wise product) between this tensor
631+
* ({@code A}) transposed (<code>A<sup>T</sup></code>) and {@code B}. If
632+
* there is a mismatch between the depths of the participating tensors the
633+
* shortest depth is chosen to reduce the operation to a common denominator
634+
* (in which case the excess layers of the longer tensor are left
635+
* untouched).
636+
*
637+
* @param B
638+
* the tensor which is multiplied with this tensor transposed
639+
* @return the result of the Hadamard multiplication
640+
*/
641+
public TensorF transposedHadamard(TensorF B) {
642+
Checks.checkTrans(this, B);
643+
int _rows = B.numRows();
644+
int _cols = B.numColumns();
645+
int _depth = Math.min(this.depth, B.depth);
646+
TensorF C = create(_rows, _cols, _depth);
647+
float[] _a = a;
648+
float[] _b = B.getArrayUnsafe();
649+
float[] _c = C.getArrayUnsafe();
650+
TensorBase btb = (TensorBase) B;
651+
for (int layer = 0; layer < _depth; ++layer) {
652+
for (int col = 0; col < _cols; ++col) {
653+
for (int row = 0; row < _rows; ++row) {
654+
int idx = btb.idx(row, col, layer);
655+
_c[idx] = _b[idx] * _a[idx(col, row, layer)];
656+
}
657+
}
658+
}
659+
return C;
660+
}
661+
628662
/**
629663
* {@code A * B} convenience multiplication. None of the operands are
630664
* mutated. If there is a mismatch between the depths of the participating

0 commit comments

Comments
 (0)