Skip to content

Commit 87ead56

Browse files
authored
Merge pull request #5 from stefan-zobel/1.4.4-SNAPSHOT
Merge branch 1.4.4-Snapshot
2 parents 9fb93d4 + 677fa15 commit 87ead56

16 files changed

+422
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Matrices in *JAMU* are internally backed by 1-dimensional Java arrays in column-
3030
<dependency>
3131
<groupId>net.sourceforge.streamsupport</groupId>
3232
<artifactId>jamu</artifactId>
33-
<version>1.4.3</version>
33+
<version>1.4.4-SNAPSHOT</version>
3434
</dependency>
3535
```
3636

pom.xml

Lines changed: 1 addition & 1 deletion
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.4.3</version>
7+
<version>1.4.4</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>

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ static void checkSameRows(Dimensions A, Dimensions B) {
179179

180180
static void checkSameCols(Dimensions A, Dimensions B) {
181181
if (A.numColumns() != B.numColumns()) {
182-
throw new IndexOutOfBoundsException(
183-
"A.numColumns() != B.numColumns() (" + A.numColumns() + " != " + B.numColumns() + ")");
182+
throw getSameColsException(A, B);
184183
}
185184
}
186185

@@ -318,6 +317,11 @@ static void throwInconsistentRowLengths(int cols, int rowIdx, int rowLength) {
318317
+ " has length " + rowLength + ")");
319318
}
320319

320+
static IndexOutOfBoundsException getSameColsException(Dimensions A, Dimensions B) {
321+
return new IndexOutOfBoundsException(
322+
"A.numColumns() != B.numColumns() (" + A.numColumns() + " != " + B.numColumns() + ")");
323+
}
324+
321325
private Checks() {
322326
throw new AssertionError();
323327
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020, 2023 Stefan Zobel
2+
* Copyright 2020, 2024 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.
@@ -385,6 +385,22 @@ public interface ComplexMatrixD extends MatrixDimensions, ComplexMatrixDConduct
385385
*/
386386
ComplexMatrixD setInplace(ComplexMatrixD other);
387387

388+
/**
389+
* Overwrite the content of the column indexed by {@code colIdx} with the
390+
* content of the column vector {@code colVector} where {@code colVector}
391+
* must have dimension {@code this.numRows() x 1}.
392+
*
393+
* @param colIdx
394+
* index of the column that will be overwritten by the content of
395+
* {@code colVector}
396+
* @param colVector
397+
* a column vector that has the same number of rows as this
398+
* matrix
399+
* @return this matrix (mutated)
400+
* @since 1.4.4
401+
*/
402+
ComplexMatrixD setColumnInplace(int colIdx, ComplexMatrixD colVector);
403+
388404
/**
389405
* Let {@code this} be a m-by-n matrix and let {@code B} be a j-by-k matrix.
390406
* Set the entries on and above the main diagonal in {@code this} matrix

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020, 2023 Stefan Zobel
2+
* Copyright 2020, 2024 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.
@@ -435,6 +435,16 @@ public ComplexMatrixD submatrix(int r0, int c0, int r1, int c1, ComplexMatrixD B
435435
return B;
436436
}
437437

438+
/**
439+
* {@inheritDoc}
440+
*/
441+
@Override
442+
public ComplexMatrixD setColumnInplace(int colIdx, ComplexMatrixD colVector) {
443+
checkIndex(0, colIdx);
444+
Checks.checkCommensurateColVector(this, colVector);
445+
return setSubmatrixInplace(0, colIdx, colVector, 0, 0, colVector.endRow(), 0);
446+
}
447+
438448
/**
439449
* {@inheritDoc}
440450
*/

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020, 2023 Stefan Zobel
2+
* Copyright 2020, 2024 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.
@@ -385,6 +385,22 @@ public interface ComplexMatrixF extends MatrixDimensions, ComplexMatrixFConduct
385385
*/
386386
ComplexMatrixF setInplace(ComplexMatrixF other);
387387

388+
/**
389+
* Overwrite the content of the column indexed by {@code colIdx} with the
390+
* content of the column vector {@code colVector} where {@code colVector}
391+
* must have dimension {@code this.numRows() x 1}.
392+
*
393+
* @param colIdx
394+
* index of the column that will be overwritten by the content of
395+
* {@code colVector}
396+
* @param colVector
397+
* a column vector that has the same number of rows as this
398+
* matrix
399+
* @return this matrix (mutated)
400+
* @since 1.4.4
401+
*/
402+
ComplexMatrixF setColumnInplace(int colIdx, ComplexMatrixF colVector);
403+
388404
/**
389405
* Let {@code this} be a m-by-n matrix and let {@code B} be a j-by-k matrix.
390406
* Set the entries on and above the main diagonal in {@code this} matrix

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020, 2023 Stefan Zobel
2+
* Copyright 2020, 2024 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.
@@ -435,6 +435,16 @@ public ComplexMatrixF submatrix(int r0, int c0, int r1, int c1, ComplexMatrixF B
435435
return B;
436436
}
437437

438+
/**
439+
* {@inheritDoc}
440+
*/
441+
@Override
442+
public ComplexMatrixF setColumnInplace(int colIdx, ComplexMatrixF colVector) {
443+
checkIndex(0, colIdx);
444+
Checks.checkCommensurateColVector(this, colVector);
445+
return setSubmatrixInplace(0, colIdx, colVector, 0, 0, colVector.endRow(), 0);
446+
}
447+
438448
/**
439449
* {@inheritDoc}
440450
*/

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,68 @@ public static ComplexMatrixF identityComplexF(int n) {
408408
return m;
409409
}
410410

411+
/**
412+
* Create a {@code (1, 1)} MatrixD matrix holding the given {@code scalar}.
413+
*
414+
* @param scalar
415+
* the scalar value in the created {@code 1 x 1} matrix
416+
* @return a scalar MatrixD matrix of dimension {@code (1, 1)} with the
417+
* given {@code scalar} value
418+
* @since 1.4.4
419+
*/
420+
public static MatrixD scalarD(double scalar) {
421+
return new SimpleMatrixD(1, 1, new double[] { scalar });
422+
}
423+
424+
/**
425+
* Create a {@code (1, 1)} MatrixF matrix holding the given {@code scalar}.
426+
*
427+
* @param scalar
428+
* the scalar value in the created {@code 1 x 1} matrix
429+
* @return a scalar MatrixF matrix of dimension {@code (1, 1)} with the
430+
* given {@code scalar} value
431+
* @since 1.4.4
432+
*/
433+
public static MatrixF scalarF(float scalar) {
434+
return new SimpleMatrixF(1, 1, new float[] { scalar });
435+
}
436+
437+
/**
438+
* Create a {@code (1, 1)} ComplexMatrixD matrix holding the given complex
439+
* scalar {@code (reVal, imVal)}.
440+
*
441+
* @param reVal
442+
* real part of the complex scalar in the created {@code 1 x 1}
443+
* matrix
444+
* @param imVal
445+
* imaginary part of the complex scalar in the created
446+
* {@code 1 x 1} matrix
447+
* @return a scalar ComplexMatrixD matrix of dimension {@code (1, 1)} with
448+
* the given complex scalar {@code (reVal, imVal)} value
449+
* @since 1.4.4
450+
*/
451+
public static ComplexMatrixD scalarComplexD(double reVal, double imVal) {
452+
return new SimpleComplexMatrixD(1, 1, new double[] { reVal, imVal });
453+
}
454+
455+
/**
456+
* Create a {@code (1, 1)} ComplexMatrixF matrix holding the given complex
457+
* scalar {@code (reVal, imVal)}.
458+
*
459+
* @param reVal
460+
* real part of the complex scalar in the created {@code 1 x 1}
461+
* matrix
462+
* @param imVal
463+
* imaginary part of the complex scalar in the created
464+
* {@code 1 x 1} matrix
465+
* @return a scalar ComplexMatrixF matrix of dimension {@code (1, 1)} with
466+
* the given complex scalar {@code (reVal, imVal)} value
467+
* @since 1.4.4
468+
*/
469+
public static ComplexMatrixF scalarComplexF(float reVal, float imVal) {
470+
return new SimpleComplexMatrixF(1, 1, new float[] { reVal, imVal });
471+
}
472+
411473
/**
412474
* Create a quadratic diagonal matrix whose main diagonal contains the
413475
* entries provided in the {@code diagonal} array.

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,22 @@ public interface MatrixD extends MatrixDimensions, MatrixDConduct {
339339
*/
340340
MatrixD setInplace(MatrixD other);
341341

342+
/**
343+
* Overwrite the content of the column indexed by {@code colIdx} with the
344+
* content of the column vector {@code colVector} where {@code colVector}
345+
* must have dimension {@code this.numRows() x 1}.
346+
*
347+
* @param colIdx
348+
* index of the column that will be overwritten by the content of
349+
* {@code colVector}
350+
* @param colVector
351+
* a column vector that has the same number of rows as this
352+
* matrix
353+
* @return this matrix (mutated)
354+
* @since 1.4.4
355+
*/
356+
MatrixD setColumnInplace(int colIdx, MatrixD colVector);
357+
342358
/**
343359
* Let {@code this} be a m-by-n matrix and let {@code B} be a j-by-k matrix.
344360
* Set the entries on and above the main diagonal in {@code this} matrix
@@ -694,6 +710,26 @@ public interface MatrixD extends MatrixDimensions, MatrixDConduct {
694710
*/
695711
MatrixD mapInplace(DFunction f);
696712

713+
/**
714+
* If {@code B} is a column vector with the same number of rows as this
715+
* matrix a "stretched" version of {@code B} with a compatible number of
716+
* columns (where the "additional" columns are simple copies of the original
717+
* {@code B} column vector) gets added to this matrix inplace. If
718+
* {@code B}'s dimension is the same as the dimension of this matrix this
719+
* operation behaves exactly like {@link #addInplace(MatrixD)}. Any other
720+
* dimension of {@code B} is treated as a mismatch and results in an
721+
* IndexOutOfBoundsException.
722+
*
723+
* @param B
724+
* a column vector with dimension {@code (this.numRows() x 1)}
725+
* @return this matrix (mutated)
726+
* @throws IndexOutOfBoundsException
727+
* if the dimension of {@code B} doesn't match in the sense
728+
* described above
729+
* @since 1.4.4
730+
*/
731+
MatrixD addBroadcastedVectorInplace(MatrixD B);
732+
697733
/**
698734
* Set all elements <code>|x<sub>ij</sub>| &le; k * 2<sup>-53</sup></code>
699735
* ({@code k} times the machine epsilon for doubles) to {@code 0.0} where

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,31 @@ public MatrixD add(double alpha, MatrixD B, MatrixD C) {
172172
return C;
173173
}
174174

175+
/**
176+
* {@inheritDoc}
177+
*/
178+
@Override
179+
public MatrixD addBroadcastedVectorInplace(MatrixD B) {
180+
Checks.checkSameRows(this, B);
181+
if (this.numColumns() == B.numColumns()) {
182+
return addInplace(B);
183+
}
184+
if (B.numColumns() == 1) {
185+
double[] _a = a;
186+
double[] _b = B.getArrayUnsafe();
187+
int cols_ = cols;
188+
int rows_ = rows;
189+
for (int col = 0; col < cols_; ++col) {
190+
for (int row = 0; row < rows_; ++row) {
191+
_a[idx(row, col)] += _b[row];
192+
}
193+
}
194+
return this;
195+
}
196+
// incompatible dimensions
197+
throw Checks.getSameColsException(this, B);
198+
}
199+
175200
/**
176201
* {@inheritDoc}
177202
*/
@@ -394,6 +419,16 @@ public MatrixD submatrix(int r0, int c0, int r1, int c1, MatrixD B, int rb, int
394419
return B;
395420
}
396421

422+
/**
423+
* {@inheritDoc}
424+
*/
425+
@Override
426+
public MatrixD setColumnInplace(int colIdx, MatrixD colVector) {
427+
checkIndex(0, colIdx);
428+
Checks.checkCommensurateColVector(this, colVector);
429+
return setSubmatrixInplace(0, colIdx, colVector, 0, 0, colVector.endRow(), 0);
430+
}
431+
397432
/**
398433
* {@inheritDoc}
399434
*/
@@ -1047,6 +1082,14 @@ public MatrixD map(DFunction f) {
10471082
return copy().mapInplace(f);
10481083
}
10491084

1085+
/**
1086+
* {@inheritDoc}
1087+
*/
1088+
@Override
1089+
public MatrixD plusBroadcastedVector(MatrixD B) {
1090+
return copy().addBroadcastedVectorInplace(B);
1091+
}
1092+
10501093
/**
10511094
* {@inheritDoc}
10521095
*/

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,27 @@ public interface MatrixDConduct {
376376
*/
377377
MatrixD map(DFunction f);
378378

379+
/**
380+
* If {@code B} is a column vector with the same number of rows as this
381+
* matrix a "stretched" version of {@code B} with a compatible number of
382+
* columns (where the "additional" columns are simple copies of the original
383+
* {@code B} column vector) gets added to a copy of this matrix. If
384+
* {@code B}'s dimension is the same as the dimension of this matrix this
385+
* operation behaves exactly like {@link #plus(MatrixD)}. Any other
386+
* dimension of {@code B} is treated as a mismatch and results in an
387+
* IndexOutOfBoundsException.
388+
*
389+
* @param B
390+
* a column vector with dimension {@code (this.numRows() x 1)}
391+
* @return a copy of this matrix where the column vector {@code B} has been
392+
* added as described above
393+
* @throws IndexOutOfBoundsException
394+
* if the dimension of {@code B} doesn't match in the sense
395+
* described above
396+
* @since 1.4.4
397+
*/
398+
MatrixD plusBroadcastedVector(MatrixD B);
399+
379400
/**
380401
* Reshapes this matrix into a new matrix of dimension {@code rows x cols}
381402
* where the elements in this matrix are read in Fortran-style column-major

0 commit comments

Comments
 (0)