如何求稀疏矩阵/向量A的行[i]和列[j]?
我收到以下错误;错误 A[i][j]== 0
和其他两个部分中使用 A[i][j]
的原因。我在代码中使用了三次这个来检查 A 矩阵的行 [i] 和列 [j] 是否等于 0。然而,由于A矩阵太大并且包含很多零,所以我将其定义为稀疏矩阵。
但是,如何在稀疏矩阵 A 中查找/寻找行 [i] 和列 [j] 呢?
到目前为止,我已经尝试过 A.coeff[i][j] == 0
但没有成功。
类型 'std::tuple_element<0、std::tuple
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
A.coeff[i][j] == 0
是一个语法错误,因为coeff
是成员函数的名称。要读取元素,您需要调用
coeff
:A.coeff(i, j)
。A.coeff[i][j] == 0
is a syntax error, becausecoeff
is the name of a member function.To read an element you need to call
coeff
:A.coeff(i, j)
.