Skip to content

Commit

Permalink
Add convertToComplex function to MathUtils
Browse files Browse the repository at this point in the history
Function creates and returns complex matrix based on its real equivalent

Signed-off-by: Georgii Tishenin <georgii.tishenin@eonerc.rwth-aachen.de>
  • Loading branch information
georgii-tishenin committed Jan 16, 2024
1 parent 37ecbca commit 410da27
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dpsim-models/include/dpsim-models/MathUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ namespace CPS {

static MatrixComp returnNonZeroElements(const MatrixComp& mat);

static MatrixComp convertToComplex(const Matrix &realEquivalentMatrix);

static void setMatrixElement(SparseMatrixRow& mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq = 1, Int freqIdx = 0);

static void addToMatrixElement(SparseMatrixRow& mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq = 1, Int freqIdx = 0);
Expand Down
26 changes: 26 additions & 0 deletions dpsim-models/src/MathUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,32 @@ MatrixComp Math::returnNonZeroElements(const MatrixComp &mat)
return nonZeroMatrix;
}

MatrixComp Math::convertToComplex(const Matrix &realEquivalentMatrix)
{
// The size of the complex matrix is half the size of the real matrix
int size = realEquivalentMatrix.rows() / 2;

// Create a complex matrix of the appropriate size
MatrixComp complexMatrix(size, size);

// Iterate over the complex matrix
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
// The real part is in the upper left quadrant of the real matrix
double realPart = realEquivalentMatrix(i, j);

// The imaginary part is in the lower left quadrant of the real matrix
double imagPart = realEquivalentMatrix(i + size, j);

// Assign the complex number to the complex matrix
complexMatrix(i, j) = std::complex<double>(realPart, imagPart);
}
}
return complexMatrix;
}

void Math::setMatrixElement(SparseMatrixRow& mat, Matrix::Index row, Matrix::Index column, Complex value, Int maxFreq, Int freqIdx) {
// Assume square matrix
Eigen::Index harmonicOffset = mat.rows() / maxFreq;
Expand Down

0 comments on commit 410da27

Please sign in to comment.