2017-07-30 61 views
0

我傳遞一個Python代碼C++哪裏找到Python表達式所示:選擇犰狳子矩陣具有非連續的索引

J11 = dS_dVa[array([pvpq]).T, pvpq].real 

這裏,J11dS_dVa是稀疏矩陣,pvpq是陣列索引可以是任何生長順序(即1,2,5,7,9)

看我已經推斷下面的文檔here

arma::Row<int> pvpq(calc->pqpv); 

arma::sp_mat J11 = arma::real(dS_dVa.submat(pvpq, pvpq)); 

其中calc->pqpvstd::vector<int>類型。

但是GCC編譯器說:

engine.h:2436: error: no matching function for call to ‘arma::SpMat<std::complex<double> >::submat(arma::Row<int>&, arma::Row<int>&)’ 
     arma::sp_mat J11 = arma::real(dS_dVa.submat(pvpq, pvpq)); 
                  ^

我該如何解決這個問題?

它是否告訴我稀疏矩陣沒有submat方法?

回答

0

過了一段時間,我做了我自己的功能。它使用內部的CSC結構。

/** 
    * @brief sp_submatrix Function to extract columns and rows from a sparse matrix 
    * @param A Sparse matrix pointer 
    * @param rows vector of the rown indices to keep (must be sorted) 
    * @param cols vector of the clumn indices to keep (must be sorted) 
    * @return Sparse matrix of the indicated indices 
    */ 
    arma::sp_mat sp_submatrix(arma::sp_mat *A, std::vector<std::size_t> *rows, std::vector<std::size_t> *cols) { 

     std::size_t n_rows = rows->size(); 
     std::size_t n_cols = cols->size(); 

     bool found = false; 
     std::size_t n = 0; 
     std::size_t p = 0; 
     std::size_t found_idx = 0; 

     arma::vec new_val(A->n_nonzero); 
     arma::uvec new_row_ind(A->n_nonzero); 
     arma::uvec new_col_ptr(n_cols + 1); 

     new_col_ptr(p) = 0; 

     for (auto const& j: *cols) { // for every column in the cols vector 

      for (std::size_t k = A->col_ptrs[j]; k < A->col_ptrs[j + 1]; k++) { // k is the index of the "values" and "row_indices" that corresponds to the column j 

       // search row_ind[k] in rows 
       found = false; 
       found_idx = 0; 
       while (!found && found_idx < n_rows) { 
        if (A->row_indices[k] == rows->at(found_idx)) 
         found = true; 
        found_idx++; 
       } 

       // store the values if the row was found in rows 
       if (found) { // if the row index is in the designated rows...     
        new_val(n) = A->values[k]; // store the value 
        new_row_ind(n) = found_idx - 1; // store the index where the original index was found inside "rows" 
        n++; 
       } 
      } 

      p++; 
      new_col_ptr(p) = n; 
     } 
     new_col_ptr(p) = n ; 

     // reshape the vectors to the actual number of elements 
     new_val.reshape(n, 1); 
     new_row_ind.reshape(n, 1); 

     return arma::sp_mat(new_row_ind, new_col_ptr, new_val, n_rows, n_cols); 
    } 
+0

建議使它成爲麪糊 –

2

犰狳只支持連續形式的子矩陣視圖。請參閱sp_mat文檔中的注意事項部分。