Santi Peñate-Vera
Santi Peñate-Vera

Reputation: 1186

Accessing Armadillo sparse complex matrix elements

I have the following function:

double Qi(int i) {
    double val = 0.0;
    for (int j = 0; j < Model.buses.size(); j++)
        val += Sol.V[j] * (Y[i, j].real() * sin(Sol.D[i] - Sol.D[j]) - Y[i, j].imag() * cos(Sol.D[i] - Sol.D[j]));

    return Sol.V[i] * val;
}

The variable Y is a complex sparse matrix from the armadillo library SpValProxy<arma::SpMat<std::complex<double> > >. The problem is that the compiler tells that I cannot access the real or imaginary parts of an specific matrix element.

The error is the following:

error: ‘class arma::SpValProxy > >’ has no member named ‘real’

I have no clue of what to do to access the complex number properties of the complex sparse matrix.

Thanks in advance.

Upvotes: 0

Views: 1466

Answers (2)

hbrerkere
hbrerkere

Reputation: 1605

This will also work, in a more direct manner:

const sp_cx_mat& YY = Y;

// can now access .real() and .imag() directly:
double re = YY(i,j).real();
double im = YY(i,j).imag();

Upvotes: 2

mtall
mtall

Reputation: 3620

SpValProxy is used as an element guard, to capture zero values which are not to be stored in a sparse matrix.

You need to get past the guard like this:

std::complex<double> temp = Y(i,j);

then access the real and imaginary parts of temp. Alernatively, Change Y to be a const reference to a matrix, which should tell Armadillo to bypass the guard directly.

Also, you have a bug: Y[i,j] doesn't do what you think it does. In C++ only one index is used inside a [] expression. Use Y(i,j) instead.

Upvotes: 4

Related Questions