[Rd] C Interface

2010-06-18 Thread michael meyer
Greetings,

I am trying to call simple C-code from R.
I am on Windows XP with RTools installed.

The C-function is

#include 
#include 
#include 
#include 

// prevent name mangling
extern "C" {

SEXP __cdecl test(SEXP s){

  SEXP result;
  PROTECT(result = NEW_NUMERIC(1));
  double* ptr=NUMERIC_POINTER(result);
  double t = *REAL(s);
  double u = t-floor(t)-0.5;
  if(u>0) *ptr=-1+4*u; else *ptr=-1-4*u;
  Rprintf("The value is %f", *ptr);
  UNPROTECT(1);
  return result;
}

};

It is compiled with

R CMD SHLIB OrthoFunctions.c

with flag

MAKEFLAGS="CC=g++"


However when I call this code from R with

test <- function(t){
  .Call("test",t)
}
dyn.load("./OrthoFunctions.dll")
test(0)
dyn.unload("./OrthoFunctions.dll")

then R crashes.

If I compile with the default flags (no extern "C", no __cdecl) I get an
error message about an undefined reference to "__gxx_personality_v0":

C:\...>R CMD SHLIB OrthoFunctions.c
C:/Programme/R/R-2.10.1/etc/Makeconf:151: warning: overriding commands for
target `.c.o'
C:/Programme/R/R-2.10.1/etc/Makeconf:142: warning: ignoring old commands for
target `.c.o'
C:/Programme/R/R-2.10.1/etc/Makeconf:159: warning: overriding commands for
target `.c.d'
C:/Programme/R/R-2.10.1/etc/Makeconf:144: warning: ignoring old commands for
target `.c.d'
C:/Programme/R/R-2.10.1/etc/Makeconf:169: warning: overriding commands for
target `.m.o'
C:/Programme/R/R-2.10.1/etc/Makeconf:162: warning: ignoring old commands for
target `.m.o'
g++ -I"C:/Programme/R/R-2.10.1/include"-O2 -Wall  -c
OrthoFunctions.c -o OrthoFunctions.o
gcc -shared -s -o OrthoFunctions.dll tmp.def OrthoFunctions.o
-LC:/Programme/R/R-2.10.1/bin -lR
OrthoFunctions.o:OrthoFunctions.c:(.eh_frame+0x11): undefined reference to
`__gxx_personality_v0'
collect2: ld returned 1 exit status



I have a vague idea of the issue of calling conventions and was hoping that
the __cdecl
specifier would force the appropriate convention.
I also have Cygwin installed as part of the Python(x,y) distribution but I
am assuming that
R CMD SHLIB source.c
calls the right compiler.

What could the problem be?

Many thanks,


Michael

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] C Interface

2010-06-20 Thread michael meyer
Thanks for all replies.
I'll use inlining until I have figured out how to build a proper package.

Michael

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Call for suggestions

2010-07-04 Thread michael meyer
Greetings,

If this is not the appropriate place to post this question please let me
know where
to post it.

I have a package under development which fits models of the form
$$
f(t)=\sum_i B_iG_i(t,\omega)
$$
depending on a parameter vector $\omega$ of arbitrary dimension to
data (one dimensional time series) in the general framework of the

data = deterministic signal + Gaussian noise

in the spirit of
Bretthorst, G. Larry, 1988, "Bayesian Spectrum Analysis and Parameter
Estimation,"
Lecture Notes in Statistics, vol. 48, Springer-Verlag, New York.
The basic parametric model
$$
G_i(t,\omega)=cos(\omega_i t), sin(\omega_i t)
$$
corresponds to classical spectral analysis, however the model can (at least
in principle)
be completely general. The problem is that the models cannot be defined by
the user but
have to be hard coded (in C++ since the computations are substantial).

I plan to include the ability to modify each model by the action of further
parameters as:

time changes: t -> t+omega, t -> omega*t, t -> t^omega
model function change: G(t) -> sign(G(t))*|G(t)|^omega

I plan to include models that can be generated by these actions from trig
functions,
some piecewise linear functions, monomials, and exponential function.
My question is: what further parametric models are of sufficiently general
interest to be
included?


Many thanks,

Michael Meyer

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] inconsistent behaviour of c(...)

2019-07-19 Thread Michael Meyer via R-devel



Greetings,

Running R 3.5.0 under Windows 7

typeof(c(1,"2")) yields "character" as expected. But in

d.f <- data.frame(C=c(1,"2"))

typeof(d.f$C) yields "integer".

Is this a bug?

Michael Meyer

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] behaviour and documentation of qr.solve

2019-08-14 Thread Michael Meyer via R-devel


Greetings,

In my opinion the documentation or behaviour of qr.solve, qr.coef, qr.resid, 
and qr.fitted is not easily comprehensible and unfortunate.
We all know that a linear system Ax=b can have 0, one or infinitely many 
solutions. To treat all these cases uniformly we can rephrase the problem
as 
x = argmin_u||Au-b||,   


where ||.|| denotes the Euclidean norm. There is then exactly one natural and 
distinguished solution x, the minimizer x which is itself of minimal
Euclidean Norm. So if we want to return only one solution, I think we can agree 
that this should be it.

In fact this very solution can be computed from the QR-decomposition of either 
A (overdetermined system) or t(A) (underdetermined system).
I tried qr.solve on the underdetermined system Ax=b with  

b <- c(3,5,7)   
and
A <- rbind(
c(1,1,1,1,1),
c(1,2,2,2,2),
c(1,2,3,3,3)
)
The system has infinitely many solutions. The minimal norm solution is 
x=c(1,0,2/3,2/3,2/3).
But qr.solve(A,b) yielded the solution x=(1,0,2,0,0) which is destinguished 
only by being sparse and I do not think qr.solve
tries to compute the sparsest solution. So what does qr.solve do in case of an 
underdetermined system? 
It is not documented.

Then I tried to figure out what qr.coef, qr.resid, and qr.fitted do. I had to 
do actual experiments to figure out that it seems to solve the
problem x=argmin_u||Au-y|| with
   qr.coef(A,y) = x but which x 
when there are infinitely many?
   qr.fitted(A,y) = Ax
   qr.resid(A,y) = y-Ax 
but this is certainly not evident from the language in the documentation which 
conflates qr(x,...) with solving the system Ax=b then states:

"The functions qr.coef, qr.resid, and qr.fitted return the coefficients, 
residuals and fitted values obtained 
  when fitting y to the matrix with QR decomposition qr."

Since when do we call solving a system of equations "fitting the right hand 
side to the matrix ..." or call the solution x "the coefficients"
(which more usually are the elements of A) or introduce the "fitted values" 
with no definition?
Moreover the language does not fit the underdetermined case Ax=y, where we need 
the QR-decomposition of t(A) and not of A  to compute the
minimizer x = argmin_u||Au-y|| which is itself of minimal norm.

Or, maybe this is not at all what these functions are doing.
But then, what is it and should this not be evident from the documentation?

Sincerely,

Michael Meyer

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel