[Rcpp-commits] r1996 - in pkg/Rcpp/inst: . include/Rcpp/sugar/functions unitTests
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Aug 13 11:26:49 CEST 2010
Author: romain
Date: 2010-08-13 11:26:47 +0200 (Fri, 13 Aug 2010)
New Revision: 1996
Modified:
pkg/Rcpp/inst/ChangeLog
pkg/Rcpp/inst/include/Rcpp/sugar/functions/complex.h
pkg/Rcpp/inst/unitTests/runit.sugar.R
Log:
new functions for complex : exp, log (more to come)
Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog 2010-08-13 07:06:22 UTC (rev 1995)
+++ pkg/Rcpp/inst/ChangeLog 2010-08-13 09:26:47 UTC (rev 1996)
@@ -3,6 +3,9 @@
* inst/include/Rcpp/sugar/complex.h: simplify complex sugar functions
Re, Im, Conj, Mod
+ * inst/include/Rcpp/sugar/complex.h: new sugar functions operating on
+ complex expressions: exp, log
+
* inst/unitTests/runit.sugar.R: added regression test for complex functions
which did not handle NA properly before
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/complex.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/complex.h 2010-08-13 07:06:22 UTC (rev 1995)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/complex.h 2010-08-13 09:26:47 UTC (rev 1996)
@@ -19,9 +19,15 @@
// You should have received a copy of the GNU General Public License
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
-#ifndef Rcpp__sugar__Mod_h
-#define Rcpp__sugar__Mod_h
+#ifndef Rcpp__sugar__complex_h
+#define Rcpp__sugar__complex_h
+#ifdef HAVE_HYPOT
+# define RCPP_HYPOT ::hypot
+#else
+# define RCPP_HYPOT ::Rf_pythag
+#endif
+
namespace Rcpp{
namespace sugar{
@@ -62,6 +68,20 @@
y.i = -x.i ;
return y ;
}
+ // TODO: this does not use HAVE_C99_COMPLEX as in R, perhaps it should
+ inline Rcomplex complex__exp( Rcomplex x){
+ Rcomplex y ;
+ double expx = ::exp(x.r);
+ y.r = expx * ::cos(x.i);
+ y.i = expx * ::sin(x.i);
+ return y ;
+ }
+ inline Rcomplex complex__log( Rcomplex x){
+ Rcomplex y ;
+ y.i = ::atan2(x.i, x.r);
+ y.r = ::log( RCPP_HYPOT( x.r, x.i ) );
+ return y ;
+ }
} // internal
#define RCPP_SUGAR_COMPLEX(__NAME__,__OUT__) \
@@ -70,7 +90,7 @@
__NAME__( \
const VectorBase<CPLXSXP,NA,T>& t \
){ \
- return sugar::SugarComplex<NA,__OUT__,T, __OUT__ (*)(Rcomplex) >( \
+ return sugar::SugarComplex<NA,__OUT__,T, __OUT__ (*)(Rcomplex) >( \
internal::complex__##__NAME__, t \
) ; \
}
@@ -79,6 +99,8 @@
RCPP_SUGAR_COMPLEX( Im, double )
RCPP_SUGAR_COMPLEX( Mod, double )
RCPP_SUGAR_COMPLEX( Conj, Rcomplex )
+RCPP_SUGAR_COMPLEX( exp, Rcomplex )
+RCPP_SUGAR_COMPLEX( log, Rcomplex )
#undef RCPP_SUGAR_COMPLEX
Modified: pkg/Rcpp/inst/unitTests/runit.sugar.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.R 2010-08-13 07:06:22 UTC (rev 1995)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.R 2010-08-13 09:26:47 UTC (rev 1996)
@@ -476,7 +476,9 @@
_["Re"] = Re( cx ),
_["Im"] = Im( cx ),
_["Conj"] = Conj( cx ),
- _["Mod"] = Mod( cx )
+ _["Mod"] = Mod( cx ),
+ _["exp"] = exp( cx ),
+ _["log"] = log( cx )
) ;
'
),
@@ -1071,7 +1073,9 @@
Re = Re( x ),
Im = Im( x ),
Conj = Conj(x),
- Mod = Mod(x)
+ Mod = Mod(x),
+ exp = exp(x),
+ log = log(x)
)
)
}
More information about the Rcpp-commits
mailing list