[R] complex values in C
Iris Simmons
|kw@|mmo @end|ng |rom gm@||@com
Sat Mar 14 03:46:02 CET 2026
I'm not sure if I know exactly what you're saying, so sorry if this
doesn't answer your question.
Everywhere you write double *, replace it with Rcomplex *
Replace REALSXP with CPLXSXP
Replace REAL() with COMPLEX()
When getting / / setting the real and imaginary parts, use x.r and x.i
and x.r = u and x.i = v
On Fri, Mar 13, 2026 at 10:25 PM Therneau, Terry M., Ph.D. via R-help
<r-help using r-project.org> wrote:
>
> I've often used the following paradym in C code to define a list object returned to R
>
> static const char *outnames[]= {"dvec", "A", "Ainv", "P", ""};
> SEXP rval, stemp;
> double *dvec, *A, *Ainv;
> int *P;
>
>
> PROTECT(rval = mkNamed(VECSXP, outnames));
> stemp= SET_VECTOR_ELT(rval, 0, allocVector(REALSXP, nc));
> dvec= REAL(stemp);
> stemp = SET_VECTOR_ELT(rval, 1, allocMatrix(REALSXP, nc, nc));
> A = REAL(stemp);
>
> Then the code puts values into dvec[i] and etc. and finally returns rval.
>
> I'm missing how to do this when returning a complex value. I've looked through the
> "Writing R extension" manual and have come up dry. What am I missing?
>
> Terry T.
>
>
> --
> Terry M Therneau, PhD
> Department of Quantitative Health Sciences
> Mayo Clinic
> therneau using mayo.edu
>
> "TERR-ree THUR-noh"
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list