如何找到 Sparse Matrix/Vector A 的行 [i] 和列 [j]?

How to find row [i] and column [j] of Sparse Matrix/Vector A?

我收到以下错误;在其他两个部分使用 A[i][j]== 0A[i][j] 错误的原因。我在代码中使用了三次来检查 A 矩阵的行 [i] 和列 [j] 是否为零。但是,由于 A 矩阵很大,包含很多零,所以我将其定义为稀疏矩阵。 但是,对于稀疏矩阵A中的行[i]和列[j]如何find/look?

到目前为止,我已经尝试了 A.coeff[i][j] == 0 但没有成功。

Type 'std::tuple_element<0, std::tuple<Eigen::SparseMatrix<double, 0>, std::vector<double>, std::vector<double>>>::type' (aka 'Eigen::SparseMatrix<double, 0>') does not provide a subscript operator

#include <iostream>
#include <fstream>
#include <ostream>
#include <tuple>
#include <vector>
#include <array>
#include "gurobi_c++.h"
#include </home/user/snap/eigen-3.4.0/Eigen/Eigen>
#include </home/user/snap/eigen-3.4.0/Eigen/Sparse>

using TripletVector = std::vector<Eigen::Triplet<double>>;
using SparseMatrix  = Eigen::SparseMatrix<double>;
using Vector        = std::vector<double>;

std::tuple<SparseMatrix, Vector, Vector> read_Abc(GRBModel& model) {
    // number of variables, number of constraints, number of nonzeros in A
    std::size_t n       = model.get(GRB_IntAttr_NumVars);
    std::size_t m       = model.get(GRB_IntAttr_NumConstrs);
    std::size_t num_nnz = model.get(GRB_IntAttr_NumNZs);

    // allocate space
    TripletVector triplet(num_nnz);
    Vector b(m);
    Vector c(n);

    // Read the objective coefficients
    auto obj_expr = model.getObjective().getLinExpr();
    for (std::size_t j = 0; j < n; ++j) {
        c[j] = obj_expr.getCoeff(j);
    }

    // Read the coefficient matrix A and the rhs vector b
    std::size_t k = 0;
    for (std::size_t i = 0; i < m; ++i) {
        auto con      = model.getConstr(i);
        auto row      = model.getRow(con);
        for (std::size_t r = 0; r < row.size(); ++r) {
            auto col_idx = row.getVar(r).index();
            auto coeff   = row.getCoeff(r);
            triplet[k++] = Eigen::Triplet<double>(i, col_idx, coeff);
        }
        // read the constraint rhs
        b[i] = con.get(GRB_DoubleAttr_RHS);
    }

    // Construct the sparse matrix.
    SparseMatrix A(m, n);
    A.setFromTriplets(triplet.begin(), triplet.end());

    return {A, b, c};
}

int main(int argc, char* argv[]) {

    std::ofstream myfile; //Defining file name
    myfile.open("ScalingPrint.txt"); //Opening the file

    GRBEnv env     = GRBEnv();
    GRBModel model = GRBModel(env,"/home/user/CLionProjects/Scaling/example.lp" );
    auto [A, b, c] = read_Abc(model);
    std::cout<<"Writing to File Started!"<<std::endl;
    //Declarations and Initializing of Variables
    int m = 3;     //Declaring Size of matrix A as above
    int n = 4;
    double row_max[m];     //Declaring One dimensional scaling matrices for rows like int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    double row_multi[m];
    double col_max[m];     //Declaring One dimensional scaling matrices for columns
    double col_multi[m];
    double max = 0; //Maximum value in the row
    int cnt = 0;    //Counter
    bool exitLoop = false;  //Boolean for zero row control

   for (int i = 0; i < m; i++) {
        row_max[i] = 0.0;   //Creating [m][1] zero matrices
        row_multi[i] = 0.0; //Creating [n][1] zeros matrices
        for (int j = 0; j < n; j++) {
            if (A[i][j] == 0.0) {         //Finding if a row contains at least one nonzero element
                cnt = cnt + 1;
                if (cnt == n) //Since we are only checking rows if cnt==n then there is a row that is full of zeros
                {
                    exitLoop = true;
                    break; //Break the for loop if all rows are zero
                }
            }
            if (!exitLoop) { //Scaling Procedure Started for Matrices whose rows have at least one non-zero element
                if (abs(A[i][j]) > max) { //Absolute value of the element in the row
                    max = A[i][j];
                    row_max[i] = max; //Adding the biggest abs value to row_max (1D matrix)
                }
            }
        }
        //Calculation of specific row scaling factor
        row_multi[i] = 1.0 / row_max[i]; //1.0 Written for fractional Division
    }


    return 0;
}

A.coeff[i][j] == 0是语法错误,因为coeff是成员函数的名字。

要读取一个元素你需要调用 coeff: A.coeff(i, j).