type conversion

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

type conversion

sustik
Hi All,

I am confused by a type conversion happening against my intent.

In an R script I allocate a matrix X, and I pass it to a C function
by using

tmp -< .C(..., as.double(X),...).

I use as.double() because I read that it makes sure that the
parameter passing is correct.

I return the matrix from the R script using:

return (list(..., X = tmp$X, ...))

The returned value is not a matrix but a flat vector.

I must not understand something fundamental that is happening
here.

Should I remove the as.double() from the .C() call?

Here is what I am trying to do: allocate a matrix (with doubles)
that is modified by the .C() call and return it from the script.

I am also not sure how to make sure that the allocated matrix
is made up of double values.  If I use diag(n) to initialize
it will that do the trick?

Thanks!
-Matyas

        [[alternative HTML version deleted]]

______________________________________________
[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: type conversion

Petr Savicky
On Sun, Mar 04, 2012 at 03:18:33PM -0600, Matyas Sustik wrote:

> Hi All,
>
> I am confused by a type conversion happening against my intent.
>
> In an R script I allocate a matrix X, and I pass it to a C function
> by using
>
> tmp -< .C(..., as.double(X),...).
>
> I use as.double() because I read that it makes sure that the
> parameter passing is correct.
>
> I return the matrix from the R script using:
>
> return (list(..., X = tmp$X, ...))
>
> The returned value is not a matrix but a flat vector.
>
> I must not understand something fundamental that is happening
> here.

Hi.

No, this is correct. See section 5.2 Interface functions .C and
.Fortran in R-exts.pdf for the list of C types. There is int * and
double *, which are vectors. Your R code should restore the dim
attribute after the return from C code, for example using matrix()
with appropriate nrow and ncol parameters.

If you want to pass really a matrix, use .Call, which is more
powerful.

Hope this helps.

Petr Savicky.

______________________________________________
[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: type conversion

sustik
In reply to this post by sustik



-------- Original message --------
Subject: type conversion
Date: Sun Mar 04 15:18:33 CST 2012
From: Matyas Sustik <[hidden email]>
To: [hidden email]

I got it.  Just documenting it on the list.
I need do: 
dim(tmp$X) <- c(n, k)
return (list(..., X = tmp$X, ...))

-Matyas

        [[alternative HTML version deleted]]


______________________________________________
[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: type conversion

Prof Brian Ripley
In reply to this post by Petr Savicky
On 04/03/2012 22:26, Petr Savicky wrote:

> On Sun, Mar 04, 2012 at 03:18:33PM -0600, Matyas Sustik wrote:
>> Hi All,
>>
>> I am confused by a type conversion happening against my intent.
>>
>> In an R script I allocate a matrix X, and I pass it to a C function
>> by using
>>
>> tmp -<  .C(..., as.double(X),...).
>>
>> I use as.double() because I read that it makes sure that the
>> parameter passing is correct.
>>
>> I return the matrix from the R script using:
>>
>> return (list(..., X = tmp$X, ...))
>>
>> The returned value is not a matrix but a flat vector.
>>
>> I must not understand something fundamental that is happening
>> here.
>
> Hi.
>
> No, this is correct. See section 5.2 Interface functions .C and
> .Fortran in R-exts.pdf for the list of C types. There is int * and
> double *, which are vectors. Your R code should restore the dim
> attribute after the return from C code, for example using matrix()
> with appropriate nrow and ncol parameters.

'No, this is not correct'.  If you want to use a double matrix in .C or
.Fortran, use

storage.mode(X) <- "double"

There are quite a few examples in R's own code, e.g. lm.fit.

> If you want to pass really a matrix, use .Call, which is more
> powerful.

But handling conversion in .Call is a lot trickier (especially if you
want to dispatch methods), so e.g. det() uses storage.mode at R level
before .Call.

> Hope this helps.
>
> Petr Savicky.
>
> ______________________________________________
> [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.


--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[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: type conversion

Petr Savicky
On Mon, Mar 05, 2012 at 09:29:51AM +0000, Prof Brian Ripley wrote:

> On 04/03/2012 22:26, Petr Savicky wrote:
> >On Sun, Mar 04, 2012 at 03:18:33PM -0600, Matyas Sustik wrote:
> >>Hi All,
> >>
> >>I am confused by a type conversion happening against my intent.
> >>
> >>In an R script I allocate a matrix X, and I pass it to a C function
> >>by using
> >>
> >>tmp -<  .C(..., as.double(X),...).
> >>
> >>I use as.double() because I read that it makes sure that the
> >>parameter passing is correct.
> >>
> >>I return the matrix from the R script using:
> >>
> >>return (list(..., X = tmp$X, ...))
> >>
> >>The returned value is not a matrix but a flat vector.
> >>
> >>I must not understand something fundamental that is happening
> >>here.
> >
> >Hi.
> >
> >No, this is correct. See section 5.2 Interface functions .C and
> >.Fortran in R-exts.pdf for the list of C types. There is int * and
> >double *, which are vectors. Your R code should restore the dim
> >attribute after the return from C code, for example using matrix()
> >with appropriate nrow and ncol parameters.
>
> 'No, this is not correct'.  If you want to use a double matrix in .C or
> .Fortran, use
>
> storage.mode(X) <- "double"
>
> There are quite a few examples in R's own code, e.g. lm.fit.

Thank you for correction. I was not aware of this.

  storage.mode(X) <- "double"
  tmp <- .C(..., X=X,...)

guarantees the correct type and keeps the dim attribute
in tmp$X.

Petr Savicky.

______________________________________________
[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.