Skip to content

Commit

Permalink
proposition to create a complex matrix class, with update
Browse files Browse the repository at this point in the history
Signed-off-by: JB-H <jbheyberger@gmail.com>
  • Loading branch information
JB-H committed Dec 15, 2023
1 parent b3dd683 commit d911b63
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public static DenseMatrix getFortescueMatrix() {
// [GA] [ 1 1 1 ] [G0]
// [GB] = [ 1 a² a] * [G1]
// [GC] [ 1 a a²] [G2]
return createComplexMatrix(false).getRealCartesianMatrix();
return createComplexMatrix(false).toRealCartesianMatrix();
}

public static DenseMatrix getFortescueInverseMatrix() {

// [G0] [ 1 1 1 ] [GA]
// [G1] = 1/3 [ 1 a a²] * [GB]
// [G2] [ 1 a² a ] [GC]
return createComplexMatrix(true).getRealCartesianMatrix();
return createComplexMatrix(true).toRealCartesianMatrix();
}

public static ComplexMatrix createComplexMatrix(boolean isInverse) {
Expand Down
79 changes: 38 additions & 41 deletions math/src/main/java/com/powsybl/math/matrix/ComplexMatrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,71 +14,71 @@
*/
public class ComplexMatrix {

private DenseMatrix realPartMatrix;
private DenseMatrix imagPartMatrix;
private final DenseMatrix realPartMatrix;
private final DenseMatrix imagPartMatrix;

public ComplexMatrix(int nbRow, int nbCol) {
this.realPartMatrix = new DenseMatrix(nbRow, nbCol);
this.imagPartMatrix = new DenseMatrix(nbRow, nbCol);
public ComplexMatrix(int rowCount, int columnCount) {
realPartMatrix = new DenseMatrix(rowCount, columnCount);
imagPartMatrix = new DenseMatrix(rowCount, columnCount);
}

// we suppose that the input indices start at 1
public void set(int i, int j, Complex complex) {
this.realPartMatrix.set(i - 1, j - 1, complex.getReal());
this.imagPartMatrix.set(i - 1, j - 1, complex.getImaginary());
realPartMatrix.set(i - 1, j - 1, complex.getReal());
imagPartMatrix.set(i - 1, j - 1, complex.getImaginary());
}

public int getNbRow() {
public int getRowCount() {
return realPartMatrix.getRowCount();
}

public int getNbCol() {
public int getColumnCount() {
return realPartMatrix.getColumnCount();
}

public Complex getTerm(int i, int j) {
return new Complex(realPartMatrix.get(i - 1, j - 1), imagPartMatrix.get(i - 1, j - 1));
}

public static ComplexMatrix complexMatrixIdentity(int nbRow) {
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 static ComplexMatrix getTransposed(ComplexMatrix cm) {
ComplexMatrix complexMatrixTransposed = new ComplexMatrix(cm.getNbCol(), cm.getNbRow());
for (int i = 0; i < cm.getNbCol(); i++) {
for (int j = 0; j < cm.getNbRow(); j++) {
complexMatrixTransposed.realPartMatrix.set(i, j, cm.realPartMatrix.get(j, i));
complexMatrixTransposed.imagPartMatrix.set(i, j, cm.imagPartMatrix.get(j, i));
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 complexMatrixTransposed;
return transposed;
}

public static ComplexMatrix getMatrixScaled(ComplexMatrix cm, Complex factor) {
ComplexMatrix complexMatrixScaled = new ComplexMatrix(cm.getNbRow(), cm.getNbCol());
for (int i = 0; i < cm.getNbRow(); i++) {
for (int j = 0; j < cm.getNbCol(); j++) {
complexMatrixScaled.realPartMatrix.set(i, j, cm.realPartMatrix.get(i, j) * factor.getReal() - cm.imagPartMatrix.get(i, j) * factor.getImaginary());
complexMatrixScaled.imagPartMatrix.set(i, j, cm.imagPartMatrix.get(i, j) * factor.getReal() + cm.realPartMatrix.get(i, j) * factor.getImaginary());
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 complexMatrixScaled;
return scaled;
}

public static ComplexMatrix getMatrixScaled(ComplexMatrix cm, double factor) {
return getMatrixScaled(cm, new Complex(factor, 0.));
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 getRealCartesianMatrix() {
DenseMatrix realMatrix = new DenseMatrix(getNbRow() * 2, getNbCol() * 2);
for (int i = 0; i < getNbRow(); i++) {
for (int j = 0; j < getNbCol(); j++) {
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());
Expand All @@ -90,17 +90,16 @@ public DenseMatrix getRealCartesianMatrix() {
return realMatrix;
}

public static ComplexMatrix getComplexMatrixFromRealCartesian(DenseMatrix realMatrix) {

int nbCol = realMatrix.getColumnCount();
int nbRow = realMatrix.getRowCount();
if (nbCol % 2 != 0 || nbRow % 2 != 0) { // dimensions have to be even
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(nbRow / 2, nbCol / 2);
for (int i = 1; i <= nbRow / 2; i++) {
for (int j = 1; j <= nbCol / 2; j++) {
ComplexMatrix complexMatrix = new ComplexMatrix(rowCount / 2, columnCount / 2);
for (int i = 1; i <= rowCount / 2; i++) {
for (int j = 1; j <= columnCount / 2; j++) {

int rowIndexInCartesian = 2 * (i - 1);
int colIndexInCartesian = 2 * (j - 1);
Expand All @@ -120,9 +119,7 @@ public static ComplexMatrix getComplexMatrixFromRealCartesian(DenseMatrix realMa
throw new MatrixException("Incompatible bloc matrices terms to build a complex matrix from a real cartesian");
}

Complex complexTerm = new Complex(t11, t21);

complexMatrix.set(i, j, complexTerm);
complexMatrix.set(i, j, new Complex(t11, t21));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,30 @@ void complexMatrixTest() {
cm.set(2, 2, new Complex(7., 8.));

// create the real equivalent matrix of complex matrix
DenseMatrix rm = cm.getRealCartesianMatrix();
DenseMatrix rm = cm.toRealCartesianMatrix();

assertEquals(cm.getTerm(1, 1).getReal(), rm.get(0, 0));
assertEquals(cm.getTerm(2, 2).getImaginary(), -rm.get(2, 3));

ComplexMatrix cmFromReal = ComplexMatrix.getComplexMatrixFromRealCartesian(rm);
ComplexMatrix cmFromReal = ComplexMatrix.fromRealCartesian(rm);

for (int i = 1; i < cm.getNbRow(); i++) {
for (int j = 1; j < cm.getNbCol(); j++) {
for (int i = 1; i < cm.getRowCount(); i++) {
for (int j = 1; j < cm.getColumnCount(); j++) {
assertEquals(cm.getTerm(i, j), cmFromReal.getTerm(i, j));
}
}

ComplexMatrix tm = ComplexMatrix.getTransposed(cm);
ComplexMatrix tm = cm.transpose();
assertEquals(cm.getTerm(1, 2), tm.getTerm(2, 1));

ComplexMatrix im = ComplexMatrix.complexMatrixIdentity(2);
ComplexMatrix im = ComplexMatrix.createIdentity(2);
assertEquals(im.getTerm(1, 2), new Complex(0, 0));
assertEquals(im.getTerm(2, 2), new Complex(1, 0));

ComplexMatrix sm = ComplexMatrix.getMatrixScaled(cm, new Complex(2., 1.));
ComplexMatrix sm = cm.scale(new Complex(2., 1.)); //ComplexMatrix.getMatrixScaled(cm, new Complex(2., 1.));
assertEquals(sm.getTerm(1, 1), new Complex(0., 5.));

sm = ComplexMatrix.getMatrixScaled(cm, 3.);
sm = cm.scale(3.);
assertEquals(sm.getTerm(1, 1), new Complex(3., 6.));

}
Expand Down

0 comments on commit d911b63

Please sign in to comment.