Volesti包中dgCMatrix适配Hpolytope的可行方案求助
First, let's break down and resolve the two distinct issues in your error message:
1. Fix the b vector type
The error confirms your b is a dgCMatrix, but the Hpolytope class requires b to be a numeric vector. If you loaded b as a sparse matrix (e.g., a 40k×1 dgCMatrix), convert it to a numeric vector with:
b <- as.numeric(b_sparse)
This will immediately resolve the second part of your error.
2. Make dgCMatrix compatible with Hpolytope's A slot
The core problem is that Volesti's Hpolytope expects A to be a base R matrix or a class extending it—but dgCMatrix inherits from the Matrix package's virtual Matrix class, not base matrix. Here are three practical workarounds:
Option 1: Register dgCMatrix as extending base matrix (quick global fix)
Use setOldClass to inform R that dgCMatrix is a subclass of base matrix. This is a simple hack but affects your entire R session, so use it cautiously:
library(Matrix) setOldClass(c("dgCMatrix", "matrix"))
After running this, try creating your Hpolytope again. The validObject check should now recognize dgCMatrix as an extension of matrix.
Option 2: Manually add matrix class to your sparse matrix
Temporarily modify the class attribute of your dgCMatrix to include matrix. This is a lightweight workaround but may cause unexpected behavior in non-Volesti functions:
class(A_sparse) <- c("dgCMatrix", "matrix")
Proceed to create the Hpolytope with this modified object.
Option 3: Use the development version of Volesti
The stable release may lack sparse matrix support, but the GitHub development version has added compatibility with Matrix package classes. Install it with:
devtools::install_github("GeomScale/Volesti")
After installation, check if dgCMatrix is accepted directly by Hpolytope—this is the most sustainable long-term solution if it works for your use case.
Notes
- Test all workarounds with a small subset of your data first to avoid memory crashes.
- If using Option 1 or 2, restrict their use to your Volesti workflow, as some base R matrix functions may not behave correctly with modified
dgCMatrixobjects.
内容的提问来源于stack exchange,提问作者Alešek

