typemaps in R

View: New views
4 Messages — Rating Filter:   Alert me  

typemaps in R

by Bastian Angermann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

the typemaps for mapping unsigned int to an appropriate type in
R seem to be broken.
I am trying to wrap a class with constructor
Erdos_Renyi_Network(unsigned int ng, double af,double prob);
Calling network <- Erdos_Renyi_Network(ng=10,af=0.5,p=0.1) results in:
Error in Erdos_Renyi_Network(ng = 10, af = 0.5, p = 0.1) :
  REAL() can only be applied to a 'numeric', not a 'integer'


Reviewing the output generated by SWIG reveals that the unsigned int argument is cast into an "integer":
  ng = as(ng, "integer");
but the C++ code calls:
        arg1 = static_cast< unsigned int >(REAL(ng)[0]);
for the same argument.

Could this be fixed by changing line 45,46 in rtypes.swg to
%typemap(scoercein) unsigned int, unsigned int *, unsigned int &
  %{  $input = as($input, "numeric");     %} ?
This removes be cast to "integer" done in R and things seem to work, but I don't know if I am introducing other bugs by this "fix".

Any comments would be appreciated,
Bastian

I am using the SVN revision 10421 of SWIG and R version 2.7.0.
--
GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen!
Jetzt dabei sein: http://www.shortview.de/?mc=sv_ext_mf@gmx

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: typemaps in R

by Soeren Sonnenburg-9 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 2008-05-06 at 00:50 +0200, Bastian Angermann wrote:
> Hi,
>
> the typemaps for mapping unsigned int to an appropriate type in
> R seem to be broken.

Yeah well R does not have anything except bool, integer, double - so
this mapping is quite arbitrary / IMHO unclear what should be done.

> I am trying to wrap a class with constructor
> Erdos_Renyi_Network(unsigned int ng, double af,double prob);
> Calling network <- Erdos_Renyi_Network(ng=10,af=0.5,p=0.1) results in:
> Error in Erdos_Renyi_Network(ng = 10, af = 0.5, p = 0.1) :
>   REAL() can only be applied to a 'numeric', not a 'integer'

BTW, would it work when you used an int instead of an unsigned int?

> Reviewing the output generated by SWIG reveals that the unsigned int argument is cast into an "integer":
>   ng = as(ng, "integer");
> but the C++ code calls:
> arg1 = static_cast< unsigned int >(REAL(ng)[0]);
> for the same argument.
>
> Could this be fixed by changing line 45,46 in rtypes.swg to
> %typemap(scoercein) unsigned int, unsigned int *, unsigned int &
>   %{  $input = as($input, "numeric");     %} ?
> This removes be cast to "integer" done in R and things seem to work, but I don't know if I am introducing other bugs by this "fix".

In my eyes this cast is broken anyway... just consider $input is a
matrix, doing as($input, ... ) will turn it into a vector. I tend to say
that that the code doing the REAL(ng)[0] should be changed to generate a
INTEGER(ng)[0] and that this scoercein typemap shouldn't do anythin,
i.e. %{ %} .

Soeren

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: typemaps in R

by Bastian Angermann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 06 May 2008 05:18:04 +0200 Soeren Sonnenburg wrote:

> On Tue, 2008-05-06 at 00:50 +0200, Bastian Angermann wrote:
> > Hi,
> >
> > the typemaps for mapping unsigned int to an appropriate type in
> > R seem to be broken.
>
> Yeah well R does not have anything except bool, integer, double - so
> this mapping is quite arbitrary / IMHO unclear what should be done.
>
> > I am trying to wrap a class with constructor
> > Erdos_Renyi_Network(unsigned int ng, double af,double prob);
> > Calling network <- Erdos_Renyi_Network(ng=10,af=0.5,p=0.1) results in:
> > Error in Erdos_Renyi_Network(ng = 10, af = 0.5, p = 0.1) :
> >   REAL() can only be applied to a 'numeric', not a 'integer'
>
> BTW, would it work when you used an int instead of an unsigned int?

Yes it is working for int. In R we have the typemap
%typemap(scoercein) int, int *, int &
  %{  $input = as($input, "integer");     %}
and on the C side of things
INTEGER(ng)[0]

> > Could this be fixed by changing line 45,46 in rtypes.swg to
> > %typemap(scoercein) unsigned int, unsigned int *, unsigned int &
> >   %{  $input = as($input, "numeric");     %} ?
> > This removes be cast to "integer" done in R and things seem to work,
> > but I don't know if I am introducing other bugs by this "fix".
>
> In my eyes this cast is broken anyway... just consider $input is a
> matrix, doing as($input, ... ) will turn it into a vector. I tend to say
> that that the code doing the REAL(ng)[0] should be changed to generate a
> INTEGER(ng)[0] and that this scoercein typemap shouldn't do anythin,
> i.e. %{ %} .
>

I don't think this will solve all problems, when going in the other direction (C->R) starting with an unsigned int => 2^31 a cast to "integer" will not have the desired result.

Bastian
--
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: typemaps in R

by Soeren Sonnenburg-9 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 2008-05-06 at 23:17 +0200, Bastian Angermann wrote:

> On Tue, 06 May 2008 05:18:04 +0200 Soeren Sonnenburg wrote:
>
> > On Tue, 2008-05-06 at 00:50 +0200, Bastian Angermann wrote:
> > > Hi,
> > >
> > > the typemaps for mapping unsigned int to an appropriate type in
> > > R seem to be broken.
> >
> > Yeah well R does not have anything except bool, integer, double - so
> > this mapping is quite arbitrary / IMHO unclear what should be done.
> >
> > > I am trying to wrap a class with constructor
> > > Erdos_Renyi_Network(unsigned int ng, double af,double prob);
> > > Calling network <- Erdos_Renyi_Network(ng=10,af=0.5,p=0.1) results in:
> > > Error in Erdos_Renyi_Network(ng = 10, af = 0.5, p = 0.1) :
> > >   REAL() can only be applied to a 'numeric', not a 'integer'
> >
> > BTW, would it work when you used an int instead of an unsigned int?
>
> Yes it is working for int. In R we have the typemap
> %typemap(scoercein) int, int *, int &
>   %{  $input = as($input, "integer");     %}
> and on the C side of things
> INTEGER(ng)[0]

yeah, which is also wrong if you intended to pass a matrix of ints. on
the other hand it gives you this convenience that if you erroneously
passed a REAL that it is on the fly converted to ints ... I admittedly
don't like that...

> > > Could this be fixed by changing line 45,46 in rtypes.swg to
> > > %typemap(scoercein) unsigned int, unsigned int *, unsigned int &
> > >   %{  $input = as($input, "numeric");     %} ?
> > > This removes be cast to "integer" done in R and things seem to work,
> > > but I don't know if I am introducing other bugs by this "fix".
> >
> > In my eyes this cast is broken anyway... just consider $input is a
> > matrix, doing as($input, ... ) will turn it into a vector. I tend to say
> > that that the code doing the REAL(ng)[0] should be changed to generate a
> > INTEGER(ng)[0] and that this scoercein typemap shouldn't do anythin,
> > i.e. %{ %} .
> >
>
> I don't think this will solve all problems, when going in the other
> direction (C->R) starting with an unsigned int => 2^31 a cast to
> "integer" will not have the desired result.

True, but there is no right way as R simply does not support unsigned
ints (what do you do with negative floats and what do you do when they
are real floats). IMHO the real fix would be to remove support for
unsigned ints completely and requiring people to use ints/doubles. And
if you really intend to use unsigned ints you should create a typemap
yourself and specifiy that mapping yourself.

Soeren.

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user