Skip to content
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

Constify OcMatrix #3148

Merged
merged 10 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/ivoc/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,10 @@

static double m_setval(void* v) {
Matrix* m = (Matrix*) v;
int i, j;
double val, *pval;
i = (int) chkarg(1, 0, m->nrow() - 1);
j = (int) chkarg(2, 0, m->ncol() - 1);
val = *getarg(3);
pval = m->mep(i, j);
*pval = val;
int i = (int) chkarg(1, 0, m->nrow() - 1);
int j = (int) chkarg(2, 0, m->ncol() - 1);
double val = *getarg(3);
m->coeff(i, j) = val;
return val;
}

Expand Down Expand Up @@ -171,7 +168,7 @@
m->resize(nrow, ncol);
for (i = 0; i < nrow; ++i)
for (j = 0; j < ncol; ++j) {
*(m->mep(i, j)) = hoc_scan(f);
m->coeff(i, j) = hoc_scan(f);
}
return 0.;
}
Expand Down Expand Up @@ -602,7 +599,7 @@
int k;
for (k = 0, i = 0; i < nrow; ++i) {
for (j = 0; j < ncol; ++j) {
*(m->mep(i, j)) = *getarg(++k);
m->coeff(i, j) = *getarg(++k);
}
}
return temp_objvar(m);
Expand Down Expand Up @@ -641,7 +638,7 @@
double* ve = vector_vec(vout);
for (j = 0; j < ncol; ++j)
for (i = 0; i < nrow; ++i) {
*(m->mep(i, j)) = ve[k++];
m->coeff(i, j) = ve[k++];

Check warning on line 641 in src/ivoc/matrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/matrix.cpp#L641

Added line #L641 was not covered by tests
}
return temp_objvar(m);
}
Expand Down
85 changes: 55 additions & 30 deletions src/ivoc/ocmatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,35 @@
}
}

void OcMatrix::unimp() {
hoc_execerror("Matrix method not implemented for this type matrix", 0);
void OcMatrix::unimp() const {
hoc_execerror("Matrix method not implemented for this type matrix", nullptr);

Check warning on line 38 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L37-L38

Added lines #L37 - L38 were not covered by tests
}

void OcMatrix::nonzeros(std::vector<int>& m, std::vector<int>& n) {
void OcMatrix::nonzeros(std::vector<int>& m, std::vector<int>& n) const {

Check warning on line 41 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L41

Added line #L41 was not covered by tests
m.clear();
n.clear();
for (int i = 0; i < nrow(); i++) {
for (int j = 0; j < ncol(); j++) {
if (getval(i, j) != 0) {
if (getval(i, j) != 0.) {

Check warning on line 46 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L46

Added line #L46 was not covered by tests
m.push_back(i);
n.push_back(j);
}
}
}
}

std::vector<std::pair<int, int>> OcMatrix::nonzeros() const {
alkino marked this conversation as resolved.
Show resolved Hide resolved
std::vector<std::pair<int, int>> nzs;
for (int i = 0; i < nrow(); i++) {
for (int j = 0; j < ncol(); j++) {
if (getval(i, j) != 0.) {
nzs.emplace_back(i, j);

Check warning on line 59 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L54-L59

Added lines #L54 - L59 were not covered by tests
}
}
}
return nzs;
}

Check warning on line 64 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L63-L64

Added lines #L63 - L64 were not covered by tests

OcFullMatrix* OcMatrix::full() {
if (type_ != MFULL) { // could clone one maybe
hoc_execerror("Matrix is not a FULL matrix (type 1)", 0);
Expand All @@ -68,13 +80,13 @@
double* OcFullMatrix::mep(int i, int j) {
return &m_(i, j);
}
double OcFullMatrix::getval(int i, int j) {
double OcFullMatrix::getval(int i, int j) const {
return m_(i, j);
}
int OcFullMatrix::nrow() {
int OcFullMatrix::nrow() const {
return m_.rows();
}
int OcFullMatrix::ncol() {
int OcFullMatrix::ncol() const {
return m_.cols();
}

Expand All @@ -84,29 +96,29 @@
m_.conservativeResizeLike(v);
}

void OcFullMatrix::mulv(Vect* vin, Vect* vout) {
void OcFullMatrix::mulv(Vect* vin, Vect* vout) const {

Check warning on line 99 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L99

Added line #L99 was not covered by tests
auto v1 = Vect2VEC(vin);
auto v2 = Vect2VEC(vout);
v2 = m_ * v1;
}

void OcFullMatrix::mulm(Matrix* in, Matrix* out) {
void OcFullMatrix::mulm(Matrix* in, Matrix* out) const {
out->full()->m_ = m_ * in->full()->m_;
}

void OcFullMatrix::muls(double s, Matrix* out) {
void OcFullMatrix::muls(double s, Matrix* out) const {

Check warning on line 109 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L109

Added line #L109 was not covered by tests
out->full()->m_ = s * m_;
}

void OcFullMatrix::add(Matrix* in, Matrix* out) {
void OcFullMatrix::add(Matrix* in, Matrix* out) const {
out->full()->m_ = m_ + in->full()->m_;
}

void OcFullMatrix::copy(Matrix* out) {
void OcFullMatrix::copy(Matrix* out) const {
out->full()->m_ = m_;
}

void OcFullMatrix::bcopy(Matrix* out, int i0, int j0, int n0, int m0, int i1, int j1) {
void OcFullMatrix::bcopy(Matrix* out, int i0, int j0, int n0, int m0, int i1, int j1) const {

Check warning on line 121 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L121

Added line #L121 was not covered by tests
out->full()->m_.block(i1, j1, n0, m0) = m_.block(i0, j0, n0, m0);
}

Expand All @@ -119,14 +131,14 @@
}

// As only symmetric matrix are accepted, eigenvalues are not complex
void OcFullMatrix::symmeigen(Matrix* mout, Vect* vout) {
void OcFullMatrix::symmeigen(Matrix* mout, Vect* vout) const {
auto v1 = Vect2VEC(vout);
Eigen::EigenSolver<Eigen::MatrixXd> es(m_);
v1 = es.eigenvalues().real();
mout->full()->m_ = es.eigenvectors().real();
}

void OcFullMatrix::svd1(Matrix* u, Matrix* v, Vect* d) {
void OcFullMatrix::svd1(Matrix* u, Matrix* v, Vect* d) const {
auto v1 = Vect2VEC(d);
Eigen::JacobiSVD<Eigen::MatrixXd> svd(m_, Eigen::ComputeFullU | Eigen::ComputeFullV);
v1 = svd.singularValues();
Expand All @@ -138,17 +150,17 @@
}
}

void OcFullMatrix::getrow(int k, Vect* out) {
void OcFullMatrix::getrow(int k, Vect* out) const {
auto v1 = Vect2VEC(out);
v1 = m_.row(k);
}

void OcFullMatrix::getcol(int k, Vect* out) {
void OcFullMatrix::getcol(int k, Vect* out) const {
auto v1 = Vect2VEC(out);
v1 = m_.col(k);
}

void OcFullMatrix::getdiag(int k, Vect* out) {
void OcFullMatrix::getdiag(int k, Vect* out) const {

Check warning on line 163 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L163

Added line #L163 was not covered by tests
auto vout = m_.diagonal(k);
if (k >= 0) {
for (int i = 0, j = k; i < nrow() && j < ncol(); ++i, ++j) {
Expand Down Expand Up @@ -205,15 +217,15 @@
m_.setIdentity();
}

void OcFullMatrix::exp(Matrix* out) {
void OcFullMatrix::exp(Matrix* out) const {

Check warning on line 220 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L220

Added line #L220 was not covered by tests
out->full()->m_ = m_.exp();
}

void OcFullMatrix::pow(int i, Matrix* out) {
void OcFullMatrix::pow(int i, Matrix* out) const {

Check warning on line 224 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L224

Added line #L224 was not covered by tests
out->full()->m_ = m_.pow(i).eval();
}

void OcFullMatrix::inverse(Matrix* out) {
void OcFullMatrix::inverse(Matrix* out) const {

Check warning on line 228 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L228

Added line #L228 was not covered by tests
out->full()->m_ = m_.inverse();
}

Expand All @@ -226,7 +238,7 @@
v2 = lu_->solve(v1);
}

double OcFullMatrix::det(int* e) {
double OcFullMatrix::det(int* e) const {

Check warning on line 241 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L241

Added line #L241 was not covered by tests
*e = 0;
double m = m_.determinant();
if (m) {
Expand Down Expand Up @@ -260,19 +272,19 @@
}
}

double OcSparseMatrix::getval(int i, int j) {
double OcSparseMatrix::getval(int i, int j) const {
return m_.coeff(i, j);
}

int OcSparseMatrix::nrow() {
int OcSparseMatrix::nrow() const {
return m_.rows();
}

int OcSparseMatrix::ncol() {
int OcSparseMatrix::ncol() const {
return m_.cols();
}

void OcSparseMatrix::mulv(Vect* vin, Vect* vout) {
void OcSparseMatrix::mulv(Vect* vin, Vect* vout) const {
auto v1 = Vect2VEC(vin);
auto v2 = Vect2VEC(vout);
v2 = m_ * v1;
Expand Down Expand Up @@ -330,7 +342,7 @@
}
}

void OcSparseMatrix::ident(void) {
void OcSparseMatrix::ident() {

Check warning on line 345 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L345

Added line #L345 was not covered by tests
m_.setIdentity();
}

Expand All @@ -348,15 +360,15 @@
}
}

int OcSparseMatrix::sprowlen(int i) {
int OcSparseMatrix::sprowlen(int i) const {

Check warning on line 363 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L363

Added line #L363 was not covered by tests
int acc = 0;
for (decltype(m_)::InnerIterator it(m_, i); it; ++it) {
acc += 1;
}
return acc;
}

double OcSparseMatrix::spgetrowval(int i, int jindx, int* j) {
double OcSparseMatrix::spgetrowval(int i, int jindx, int* j) const {

Check warning on line 371 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L371

Added line #L371 was not covered by tests
int acc = 0;
for (decltype(m_)::InnerIterator it(m_, i); it; ++it) {
if (acc == jindx) {
Expand All @@ -368,13 +380,26 @@
return 0;
}

void OcSparseMatrix::nonzeros(std::vector<int>& m, std::vector<int>& n) {
void OcSparseMatrix::nonzeros(std::vector<int>& m, std::vector<int>& n) const {

Check warning on line 383 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L383

Added line #L383 was not covered by tests
m.clear();
n.clear();
m.reserve(m_.nonZeros());
n.reserve(m_.nonZeros());

Check warning on line 387 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L386-L387

Added lines #L386 - L387 were not covered by tests
for (int k = 0; k < m_.outerSize(); ++k) {
for (decltype(m_)::InnerIterator it(m_, k); it; ++it) {
m.push_back(it.row());
n.push_back(it.col());
}
}
}

std::vector<std::pair<int, int>> OcSparseMatrix::nonzeros() const {
std::vector<std::pair<int, int>> nzs;
nzs.reserve(m_.nonZeros());
for (int k = 0; k < m_.outerSize(); ++k) {
for (decltype(m_)::InnerIterator it(m_, k); it; ++it) {
nzs.emplace_back(it.row(), it.col());
}
}
return nzs;
}

Check warning on line 405 in src/ivoc/ocmatrix.cpp

View check run for this annotation

Codecov / codecov/patch

src/ivoc/ocmatrix.cpp#L405

Added line #L405 was not covered by tests
Loading
Loading