[Rquantlib-commits] r244 - in pkg/RQuantLib: R inst src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jun 12 19:56:10 CEST 2010


Author: edd
Date: 2010-06-12 19:56:10 +0200 (Sat, 12 Jun 2010)
New Revision: 244

Modified:
   pkg/RQuantLib/R/calendars.R
   pkg/RQuantLib/inst/ChangeLog
   pkg/RQuantLib/src/calendars.cpp
Log:
more code simplifications mostly getting rid of RcppParams


Modified: pkg/RQuantLib/R/calendars.R
===================================================================
--- pkg/RQuantLib/R/calendars.R	2010-06-12 15:58:27 UTC (rev 243)
+++ pkg/RQuantLib/R/calendars.R	2010-06-12 17:56:10 UTC (rev 244)
@@ -1,6 +1,7 @@
 ## RQuantLib -- R interface to the QuantLib libraries
 ##
-## Copyright (C) 2002 - 2009 Dirk Eddelbuettel <edd at debian.org>
+## Copyright (C) 2002 - 2009 Dirk Eddelbuettel
+## Copyright (C) 2010        Dirk Eddelbuettel and Khanh Nguyen
 ##
 ## $Id$
 ##
@@ -24,8 +25,7 @@
   stopifnot(is.character(calendar))
   stopifnot(class(dates)=="Date")
   val <- .Call("QL_isBusinessDay",
-               list(calendar=calendar),
-               dates,
+               calendar, dates,
                PACKAGE="RQuantLib")
   val <- as.logical(val[[1]])
   names(val) <- dates
@@ -36,64 +36,61 @@
   stopifnot(is.character(calendar))
   stopifnot(class(dates)=="Date")
   val <- .Call("QL_isHoliday",
-               list(calendar=calendar),
-               dates,
+               calendar, dates,
                PACKAGE="RQuantLib")
   val <- as.logical(val[[1]])
   names(val) <- dates
   val
 }
+
 isWeekend <- function(calendar="TARGET", dates=Sys.Date()) {
   stopifnot(is.character(calendar))
   stopifnot(class(dates)=="Date")
   val <- .Call("QL_isWeekend",
-               list(calendar=calendar),
-               dates,
+               calendar, dates,
                PACKAGE="RQuantLib")
   val <- as.logical(val[[1]])
   names(val) <- dates
   val
 }
+
 isEndOfMonth <- function(calendar="TARGET", dates=Sys.Date()) {
   stopifnot(is.character(calendar))
   stopifnot(class(dates)=="Date")
   val <- .Call("QL_isEndOfMonth",
-               list(calendar=calendar),
-               dates,
+               calendar, dates,
                PACKAGE="RQuantLib")
   val <- as.logical(val[[1]])
   names(val) <- dates
   val
 }
+
 endOfMonth <- function(calendar="TARGET", dates=Sys.Date()) {
   stopifnot(is.character(calendar))
   stopifnot(class(dates)=="Date")
   val <- .Call("QL_endOfMonth",
-               list(calendar=calendar),
-               dates,
+               calendar, dates,
                PACKAGE="RQuantLib")
   val <- val[[1]]
   names(val) <- dates
   val
 }
+
 adjust <- function(calendar="TARGET", dates=Sys.Date(), bdc = 0 ) {
   stopifnot(is.character(calendar))
   stopifnot(class(dates)=="Date")
   val <- .Call("QL_adjust",
-               list(calendar=calendar,
-                    bdc = as.double(bdc)),
-               dates,
+               calendar, as.double(bdc), dates,
                PACKAGE="RQuantLib")
   val <- val[[1]]
   names(val) <- dates
   val
 }
 
-
 advance <- function(calendar="TARGET", dates=Sys.Date(),
                     n, timeUnit, # call 1
                     period,      # call 2
-                   bdc = 0, emr = 0) {
+                    bdc = 0, emr = 0) {
   stopifnot(is.character(calendar))
   stopifnot(class(dates)=="Date")
   call1 <- missing(period) & !missing(n) & !missing(timeUnit)  ## or was it && ?
@@ -102,8 +99,8 @@
   val <- NULL
   if (call1)
     val <- .Call("QL_advance1",
-                 list(calendar=calendar,
-                      amount = as.double(n),
+                 calendar,
+                 list(amount = as.double(n),
                       unit = as.double(timeUnit),
                       bdc = as.double(bdc),
                       emr = as.double(emr)),
@@ -111,8 +108,8 @@
                  PACKAGE="RQuantLib")
   if (call2)
     val <- .Call("QL_advance2",
-                 list(calendar=calendar,
-                      period = as.double(period),
+                 calendar,
+                 list(period = as.double(period),
                       bdc = as.double(bdc),
                       emr = as.double(emr)),
                  dates,
@@ -122,7 +119,6 @@
   val
 }
 
-
 businessDaysBetween  <- function(calendar="TARGET",
                                  from=Sys.Date(),
                                  to = Sys.Date() + 5,
@@ -133,18 +129,15 @@
   stopifnot(class(from)=="Date")
   stopifnot(class(to)=="Date")
   val <- .Call("QL_businessDaysBetween",
-               list(calendar=calendar,
-                    includeFirst = as.double(includeFirst),
-                    includeLast = as.double(includeLast)
-                    ),
+               calendar,
+               list(includeFirst = as.double(includeFirst),
+                    includeLast = as.double(includeLast)),
                from, to,
                PACKAGE="RQuantLib")
   val <- val[[1]]
   val
 }
 
-
-
 holidayList <- function(calendar="TARGET",
                         from=Sys.Date(),
                         to=Sys.Date() + 5,
@@ -153,10 +146,8 @@
   stopifnot(class(from)=="Date")
   stopifnot(class(to)=="Date")
   val <- .Call("QL_holidayList",
-               list(calendar=calendar,
-                    includeWeekends=as.double(includeWeekends),
-                    from=from, to=to
-                    ),               
+               calendar,
+               list(includeWeekends=as.double(includeWeekends), from, to),
                PACKAGE="RQuantLib")
   val
 }

Modified: pkg/RQuantLib/inst/ChangeLog
===================================================================
--- pkg/RQuantLib/inst/ChangeLog	2010-06-12 15:58:27 UTC (rev 243)
+++ pkg/RQuantLib/inst/ChangeLog	2010-06-12 17:56:10 UTC (rev 244)
@@ -1,5 +1,7 @@
 2010-06-12  Dirk Eddelbuettel  <edd at debian.org>
 
+	* src/calendars.cpp: More code simplification using "new" Rcpp API
+
 	* src/utils.cpp: Add Brazil + South Korea to getCalendar()
 	* src/calendars.cpp: Move getCalendar() into this file
 

Modified: pkg/RQuantLib/src/calendars.cpp
===================================================================
--- pkg/RQuantLib/src/calendars.cpp	2010-06-12 15:58:27 UTC (rev 243)
+++ pkg/RQuantLib/src/calendars.cpp	2010-06-12 17:56:10 UTC (rev 244)
@@ -25,9 +25,7 @@
 
 #include "rquantlib.hpp"
 
-Calendar* getCalendar(SEXP calParameters){
-    RcppParams rparam(calParameters);
-    std::string    calstr = rparam.getStringValue("calendar");
+Calendar* getCalendar(const std::string &calstr) {
     Calendar* pcal = NULL;
 
     if (calstr == "TARGET") { 		// generic calendar 
@@ -86,10 +84,10 @@
     return pcal;
 }
 
-RcppExport SEXP QL_isBusinessDay(SEXP calParameters, SEXP dateSexp){
+RcppExport SEXP QL_isBusinessDay(SEXP calSexp, SEXP dateSexp){
 
     try {
-        Calendar *pcal = getCalendar(calParameters);
+        Calendar *pcal = getCalendar(Rcpp::as<std::string>(calSexp));
 
         RcppDateVector dates  = RcppDateVector(dateSexp);
 
@@ -113,11 +111,10 @@
     return R_NilValue;
 }
 
+RcppExport SEXP QL_isHoliday(SEXP calSexp, SEXP dateSexp){
 
-RcppExport SEXP QL_isHoliday(SEXP calParameters, SEXP dateSexp){
-
     try {
-        Calendar* pcal = getCalendar(calParameters);
+        Calendar* pcal = getCalendar(Rcpp::as<std::string>(calSexp));
 
         RcppDateVector dates  = RcppDateVector(dateSexp);
         int n = dates.size();
@@ -142,10 +139,10 @@
 
 
 
-RcppExport SEXP QL_isWeekend(SEXP calParameters, SEXP dateSexp){
+RcppExport SEXP QL_isWeekend(SEXP calSexp, SEXP dateSexp){
 
     try {
-        Calendar* pcal = getCalendar(calParameters);
+        Calendar* pcal = getCalendar(Rcpp::as<std::string>(calSexp));
 
         RcppDateVector dates  = RcppDateVector(dateSexp);
         int n = dates.size();
@@ -169,10 +166,10 @@
 }
 
 
-RcppExport SEXP QL_isEndOfMonth(SEXP calParameters, SEXP dateSexp){
+RcppExport SEXP QL_isEndOfMonth(SEXP calSexp, SEXP dateSexp){
 
     try {
-        Calendar* pcal = getCalendar(calParameters);
+        Calendar* pcal = getCalendar(Rcpp::as<std::string>(calSexp));
 
         RcppDateVector dates  = RcppDateVector(dateSexp);
         int n = dates.size();
@@ -196,10 +193,10 @@
 }
 
 
-RcppExport SEXP QL_endOfMonth(SEXP calParameters, SEXP dateSexp){
+RcppExport SEXP QL_endOfMonth(SEXP calSexp, SEXP dateSexp){
 
     try {
-        Calendar* pcal = getCalendar(calParameters);
+        Calendar* pcal = getCalendar(Rcpp::as<std::string>(calSexp));
 
         RcppDateVector dates  = RcppDateVector(dateSexp);
         int n = dates.size();
@@ -225,13 +222,11 @@
 
 
 
-RcppExport SEXP QL_adjust(SEXP calParameters, SEXP dateSexp){
+RcppExport SEXP QL_adjust(SEXP calSexp, SEXP bdcSEXP, SEXP dateSexp){
 
     try {
-        Calendar* pcal = getCalendar(calParameters);
-        RcppParams rparam(calParameters);        
-        BusinessDayConvention bdc = getBusinessDayConvention(rparam.getDoubleValue("bdc"));
-
+        Calendar* pcal = getCalendar(Rcpp::as<std::string>(calSexp));
+        BusinessDayConvention bdc = getBusinessDayConvention( Rcpp::as<double>(bdcSEXP) );
         RcppDateVector dates  = RcppDateVector(dateSexp);
         int n = dates.size();
         std::vector<QuantLib::Date> adjusted(n);
@@ -256,18 +251,16 @@
     return R_NilValue;
 }
 
+RcppExport SEXP QL_advance1(SEXP calSexp, SEXP params, SEXP dateSexp){
 
-RcppExport SEXP QL_advance1(SEXP calParameters, SEXP dateSexp){
-
     try {
-        Calendar* pcal = getCalendar(calParameters);
-        RcppParams rparam(calParameters);        
-        BusinessDayConvention bdc = getBusinessDayConvention(rparam.getDoubleValue("bdc"));
-        double emr = rparam.getDoubleValue("emr");
-        double amount = rparam.getDoubleValue("amount");
-        double unit = rparam.getDoubleValue("unit");
+        Calendar* pcal = getCalendar(Rcpp::as<std::string>(calSexp));
+        Rcpp::List rparam(params);        
+        BusinessDayConvention bdc = getBusinessDayConvention(rparam["bdc"]);
+        double emr = rparam["emr"];
+        double amount = rparam["amount"];
+        double unit = rparam["unit"];
 
-
         RcppDateVector dates  = RcppDateVector(dateSexp);
         int n = dates.size();
         std::vector<QuantLib::Date> advance(n);
@@ -293,16 +286,15 @@
 }
 
 
-RcppExport SEXP QL_advance2(SEXP calParameters, SEXP dateSexp){
+RcppExport SEXP QL_advance2(SEXP calSexp, SEXP param, SEXP dateSexp){
 
     try {
-        Calendar* pcal = getCalendar(calParameters);
-        RcppParams rparam(calParameters);        
-        BusinessDayConvention bdc = getBusinessDayConvention(rparam.getDoubleValue("bdc"));
-        double emr = rparam.getDoubleValue("emr");
-        double period = rparam.getDoubleValue("period");
+        Calendar* pcal = getCalendar(Rcpp::as<std::string>(calSexp));
+        Rcpp::List rparam(param);        
+        BusinessDayConvention bdc = getBusinessDayConvention(rparam["bdc"]);
+        double emr = rparam["emr"];
+        double period = rparam["period"];
 
-
         RcppDateVector dates  = RcppDateVector(dateSexp);
         int n = dates.size();
         std::vector<QuantLib::Date> advance(n);
@@ -328,14 +320,14 @@
     return R_NilValue;
 }
 
-RcppExport SEXP QL_businessDaysBetween(SEXP calParameters, 
+RcppExport SEXP QL_businessDaysBetween(SEXP calSexp, SEXP params,
                                        SEXP from, SEXP to){
 
     try {
-        Calendar* pcal = getCalendar(calParameters);
-        RcppParams rparam(calParameters);
-        double ifirst = rparam.getDoubleValue("includeFirst");
-        double ilast = rparam.getDoubleValue("includeLast");
+        Calendar* pcal = getCalendar(Rcpp::as<std::string>(calSexp));
+        Rcpp::List rparam(params);
+        double ifirst = rparam["includeFirst"];
+        double ilast = rparam["includeLast"];
 
         RcppDateVector dates1  = RcppDateVector(from);
         RcppDateVector dates2  = RcppDateVector(to);
@@ -364,18 +356,15 @@
 }
 
 
-RcppExport SEXP QL_holidayList(SEXP calParameters, 
-                                 SEXP from, 
-                                 SEXP to){
+RcppExport SEXP QL_holidayList(SEXP calSexp, SEXP params) {
 
     try {
-        Calendar* pcal = getCalendar(calParameters);
-        RcppParams rparam(calParameters);
-        double iw = rparam.getDoubleValue("includeWeekends");
+        Calendar* pcal = getCalendar(Rcpp::as<std::string>(calSexp));
+        Rcpp::List rparam(params);
+        double iw = rparam["includeWeekends"];
+        RcppDate d1 = RcppDate( (int) rparam["from"] );
+        RcppDate d2 = RcppDate( (int) rparam["to"] );
 
-        RcppDate d1 = rparam.getDateValue("from");
-        RcppDate d2 = rparam.getDateValue("to");
-
         std::vector<QuantLib::Date> 
             holidays = QuantLib::Calendar::holidayList(*pcal,
                                                        Date(dateFromR(d1)), 



More information about the Rquantlib-commits mailing list