Creating sparse matrix of type "dgCMatrix" directly

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating sparse matrix of type "dgCMatrix" directly

Søren Højsgaard-2
I want to create a sparse matrix of type "dgCMatrix" using the Matrix package (and the matrix must be of this type even if other more compact representations may exist). I do

> library(Matrix)
> m1<-Matrix(rep(1,4),nrow=2,ncol=2,sparse=T)
> m1
2 x 2 sparse Matrix of class "dsCMatrix"
       
[1,] 1 1
[2,] 1 1

To convert m1, I do
> as(m1, "dgCMatrix")
2 x 2 sparse Matrix of class "dgCMatrix"
       
[1,] 1 1
[2,] 1 1

Is it possible to construct a dgCMatrix "directly" i.e. without going through the additional as() step above?

Best regards
Søren

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Creating sparse matrix of type "dgCMatrix" directly

Douglas Bates-2
On Sat, Jul 28, 2012 at 7:26 AM, Søren Højsgaard <[hidden email]> wrote:

> I want to create a sparse matrix of type "dgCMatrix" using the Matrix package (and the matrix must be of this type even if other more compact representations may exist). I do
>
>> library(Matrix)
>> m1<-Matrix(rep(1,4),nrow=2,ncol=2,sparse=T)
>> m1
> 2 x 2 sparse Matrix of class "dsCMatrix"
>
> [1,] 1 1
> [2,] 1 1
>
> To convert m1, I do
>> as(m1, "dgCMatrix")
> 2 x 2 sparse Matrix of class "dgCMatrix"
>
> [1,] 1 1
> [2,] 1 1
>
> Is it possible to construct a dgCMatrix "directly" i.e. without going through the additional as() step above?

The Matrix function is a high-level function that is designed to
produce a compact and informative representation of its arguments.
That's why it checks for symmetry, triangularity, etc.

To decide how to bypass these checks it would be useful to know what
you are starting with.  Will it be a dense matrix or a triplet
representation or ...?

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Creating sparse matrix of type "dgCMatrix" directly

Søren Højsgaard-2


-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Douglas Bates
Sent: 28. juli 2012 20:36
To: Søren Højsgaard
Cc: [hidden email]
Subject: Re: [R] Creating sparse matrix of type "dgCMatrix" directly

On Sat, Jul 28, 2012 at 7:26 AM, Søren Højsgaard <[hidden email]> wrote:

> I want to create a sparse matrix of type "dgCMatrix" using the Matrix
> package (and the matrix must be of this type even if other more
> compact representations may exist). I do
>
>> library(Matrix)
>> m1<-Matrix(rep(1,4),nrow=2,ncol=2,sparse=T)
>> m1
> 2 x 2 sparse Matrix of class "dsCMatrix"
>
> [1,] 1 1
> [2,] 1 1
>
> To convert m1, I do
>> as(m1, "dgCMatrix")
> 2 x 2 sparse Matrix of class "dgCMatrix"
>
> [1,] 1 1
> [2,] 1 1
>
> Is it possible to construct a dgCMatrix "directly" i.e. without going through the additional as() step above?

The Matrix function is a high-level function that is designed to produce a compact and informative representation of its arguments.
That's why it checks for symmetry, triangularity, etc.

To decide how to bypass these checks it would be useful to know what you are starting with.  Will it be a dense matrix or a triplet representation or ...?

[>>] Dear Doug,
[>>] I use sparse matrices to represent graphs so the matrices contain zeros and non-zeros (in most cases ones). I need the matrices to be of the dgCMatrix type because these matrices can be used as sparse matrices in the RcppEigen package. For now I have created my own dgCMatrix() function doing what I've sketched above, but there is - as you also indicate - an overhead in the checks in the Matrix() function, and also an overhead in using as().
Regards
Søren

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.