-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposition to create a complex matrix class #2835
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b3dd683
proposition to create a complex matrix class
JB-H d911b63
proposition to create a complex matrix class, with update
JB-H 16151da
index of ComplexMatrix starts at 0
JB-H 7915ed9
Merge branch 'main' into ComplexMatrix
geofjamg 711b1a4
Clean
geofjamg 5ded320
Clean
geofjamg 8257692
Clean
geofjamg 5cef63e
Clean
geofjamg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
math/src/main/java/com/powsybl/math/matrix/ComplexMatrix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/** | ||
* Copyright (c) 2022, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.powsybl.math.matrix; | ||
|
||
import org.apache.commons.math3.complex.Complex; | ||
import org.apache.commons.math3.util.FastMath; | ||
|
||
/** | ||
* @author Jean-Baptiste Heyberger {@literal <jbheyberger at gmail.com>} | ||
*/ | ||
public class ComplexMatrix { | ||
|
||
private final DenseMatrix realPartMatrix; | ||
private final DenseMatrix imagPartMatrix; | ||
|
||
public ComplexMatrix(int rowCount, int columnCount) { | ||
realPartMatrix = new DenseMatrix(rowCount, columnCount); | ||
imagPartMatrix = new DenseMatrix(rowCount, columnCount); | ||
} | ||
|
||
public void set(int i, int j, Complex complex) { | ||
realPartMatrix.set(i, j, complex.getReal()); | ||
imagPartMatrix.set(i, j, complex.getImaginary()); | ||
} | ||
|
||
public int getRowCount() { | ||
return realPartMatrix.getRowCount(); | ||
} | ||
|
||
public int getColumnCount() { | ||
return realPartMatrix.getColumnCount(); | ||
} | ||
|
||
public Complex getTerm(int i, int j) { | ||
return new Complex(realPartMatrix.get(i, j), imagPartMatrix.get(i, j)); | ||
} | ||
|
||
public static ComplexMatrix createIdentity(int nbRow) { | ||
ComplexMatrix complexMatrix = new ComplexMatrix(nbRow, nbRow); | ||
for (int i = 0; i < nbRow; i++) { | ||
complexMatrix.realPartMatrix.set(i, i, 1.); | ||
} | ||
return complexMatrix; | ||
} | ||
|
||
public ComplexMatrix transpose() { | ||
ComplexMatrix transposed = new ComplexMatrix(getColumnCount(), getRowCount()); | ||
for (int i = 0; i < getColumnCount(); i++) { | ||
for (int j = 0; j < getRowCount(); j++) { | ||
transposed.realPartMatrix.set(i, j, realPartMatrix.get(j, i)); | ||
transposed.imagPartMatrix.set(i, j, imagPartMatrix.get(j, i)); | ||
} | ||
} | ||
return transposed; | ||
} | ||
|
||
public ComplexMatrix scale(Complex factor) { | ||
ComplexMatrix scaled = new ComplexMatrix(getRowCount(), getColumnCount()); | ||
for (int i = 0; i < getRowCount(); i++) { | ||
for (int j = 0; j < getColumnCount(); j++) { | ||
scaled.realPartMatrix.set(i, j, realPartMatrix.get(i, j) * factor.getReal() - imagPartMatrix.get(i, j) * factor.getImaginary()); | ||
scaled.imagPartMatrix.set(i, j, imagPartMatrix.get(i, j) * factor.getReal() + realPartMatrix.get(i, j) * factor.getImaginary()); | ||
} | ||
} | ||
return scaled; | ||
} | ||
|
||
public ComplexMatrix scale(double factor) { | ||
return scale(new Complex(factor, 0.)); | ||
} | ||
|
||
// utils to switch between complex and real cartesian representation of a complex matrix | ||
public DenseMatrix toRealCartesianMatrix() { | ||
DenseMatrix realMatrix = new DenseMatrix(getRowCount() * 2, getColumnCount() * 2); | ||
for (int i = 0; i < getRowCount(); i++) { | ||
for (int j = 0; j < getColumnCount(); j++) { | ||
Complex complexTerm = new Complex(realPartMatrix.get(i, j), imagPartMatrix.get(i, j)); | ||
realMatrix.add(2 * i, 2 * j, complexTerm.getReal()); | ||
realMatrix.add(2 * i + 1, 2 * j + 1, complexTerm.getReal()); | ||
realMatrix.add(2 * i, 2 * j + 1, -complexTerm.getImaginary()); | ||
realMatrix.add(2 * i + 1, 2 * j, complexTerm.getImaginary()); | ||
} | ||
} | ||
|
||
return realMatrix; | ||
} | ||
|
||
public static ComplexMatrix fromRealCartesian(DenseMatrix realMatrix) { | ||
int columnCount = realMatrix.getColumnCount(); | ||
int rowCount = realMatrix.getRowCount(); | ||
if (columnCount % 2 != 0 || rowCount % 2 != 0) { // dimensions have to be even | ||
throw new MatrixException("Incompatible matrices dimensions to build a complex matrix from a real cartesian"); | ||
} | ||
|
||
ComplexMatrix complexMatrix = new ComplexMatrix(rowCount / 2, columnCount / 2); | ||
for (int i = 0; i < rowCount / 2; i++) { | ||
for (int j = 0; j < columnCount / 2; j++) { | ||
|
||
int rowIndexInCartesian = 2 * i; | ||
int colIndexInCartesian = 2 * j; | ||
|
||
// Before building the complex matrix term, check that the 4x4 cartesian bloc can be transformed into a Complex term | ||
double t11 = realMatrix.get(rowIndexInCartesian, colIndexInCartesian); | ||
double t12 = realMatrix.get(rowIndexInCartesian, colIndexInCartesian + 1); | ||
double t21 = realMatrix.get(rowIndexInCartesian + 1, colIndexInCartesian); | ||
double t22 = realMatrix.get(rowIndexInCartesian + 1, colIndexInCartesian + 1); | ||
|
||
double epsilon = 0.00000001; | ||
if (FastMath.abs(t11 - t22) > epsilon) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two if bloc can be shared, and with the first exception of your method, you can introduce a string variable for exception message. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
throw new MatrixException("Incompatible bloc matrices terms to build a complex matrix from a real cartesian"); | ||
} | ||
|
||
if (FastMath.abs(t12 + t21) > epsilon) { | ||
throw new MatrixException("Incompatible bloc matrices terms to build a complex matrix from a real cartesian"); | ||
} | ||
|
||
complexMatrix.set(i, j, new Complex(t11, t21)); | ||
} | ||
} | ||
|
||
return complexMatrix; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
math/src/test/java/com/powsybl/math/matrix/ComplexMatrixTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Copyright (c) 2022, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.powsybl.math.matrix; | ||
|
||
import org.apache.commons.math3.complex.Complex; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/** | ||
* @author Jean-Baptiste Heyberger {@literal <jbheyberger at gmail.com>} | ||
*/ | ||
public class ComplexMatrixTest { | ||
|
||
@Test | ||
void complexMatrixTest() { | ||
|
||
ComplexMatrix cm = new ComplexMatrix(2, 2); | ||
cm.set(0, 0, new Complex(1., 2.)); | ||
cm.set(0, 1, new Complex(3., 4.)); | ||
cm.set(1, 0, new Complex(5., 6.)); | ||
cm.set(1, 1, new Complex(7., 8.)); | ||
|
||
// create the real equivalent matrix of complex matrix | ||
DenseMatrix rm = cm.toRealCartesianMatrix(); | ||
|
||
assertEquals(cm.getTerm(0, 0).getReal(), rm.get(0, 0)); | ||
assertEquals(cm.getTerm(1, 1).getImaginary(), -rm.get(2, 3)); | ||
|
||
ComplexMatrix cmFromReal = ComplexMatrix.fromRealCartesian(rm); | ||
|
||
for (int i = 0; i < cm.getRowCount(); i++) { | ||
for (int j = 0; j < cm.getColumnCount(); j++) { | ||
assertEquals(cm.getTerm(i, j), cmFromReal.getTerm(i, j)); | ||
} | ||
} | ||
|
||
ComplexMatrix tm = cm.transpose(); | ||
assertEquals(cm.getTerm(0, 1), tm.getTerm(1, 0)); | ||
|
||
ComplexMatrix im = ComplexMatrix.createIdentity(2); | ||
assertEquals(im.getTerm(0, 1), new Complex(0, 0)); | ||
assertEquals(im.getTerm(1, 1), new Complex(1, 0)); | ||
|
||
ComplexMatrix sm = cm.scale(new Complex(2., 1.)); //ComplexMatrix.getMatrixScaled(cm, new Complex(2., 1.)); | ||
assertEquals(sm.getTerm(0, 0), new Complex(0., 5.)); | ||
|
||
sm = cm.scale(3.); | ||
assertEquals(sm.getTerm(0, 0), new Complex(3., 6.)); | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just
inverse
here for the boolean name.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done