I am using RcppArmadillo via cxxfunction. I would like to load functions defined as C++ header files. This works with cxxfunction alone, but not when RcppArmadillo is added into the mix. A minimal working example is given below. Any help would be appreciated!
//My header file
#ifndef GUARD_myFunc
#define GUARD_myFunc
#include <Rcpp.h>
NumericMatrix myFunc(NumericMatrix Xin) {
NumericMatrix Xout=Xin;
return Xout;
}
#endif
-
##### My R script
rm(list=ls())
require("inline")
require("Rcpp")
require("RcppArmadillo")
X<-matrix(rnorm(9),3,3)
###works with RcppArmadillo
src1<-'Rcpp::NumericMatrix XR(XS);
arma::mat X(XR.begin(), XR.nrow(), XR.ncol(), false);
return Rcpp::List::create(Rcpp::Named("X")=X);
'
Cfunc1<-cxxfunction(sig=signature(XS="numeric"), body=src1, plugin="RcppArmadillo")
print(X)
print(Cfunc1(X))
###works with myFunc.h
settings <- getPlugin("Rcpp")
settings$env$PKG_CXXFLAGS <- paste("-I", getwd(), sep="")
src2<-'Rcpp::NumericMatrix XR(XS);
Rcpp::NumericMatrix XU=myFunc(XR);
return Rcpp::List::create(Rcpp::Named("X")=XU);
'
Cfunc2<-cxxfunction(sig=signature(XS="numeric"), body=src2, includes=c('#include "myFunc.h"'),settings=settings)
print(X)
print(Cfunc2(X))
###Does not work with RcppArmadillo and myFunc.h
src3<-'Rcpp::NumericMatrix XR(XS);
Rcpp::NumericMatrix XU=myFunc(XR);
arma::mat X(XU.begin(), XU.nrow(), XU.ncol(), false);
return Rcpp::List::create(Rcpp::Named("X")=X);
'
Cfunc3<-cxxfunction(sig=signature(XS="numeric"), body=src3, plugin="RcppArmadillo", includes=c('#include "myFunc.h"'),settings=settings)
print(X)
print(Cfunc3(X))
Related
In an application, I use List to contain some variables (double, arma::mat and other types), and then take the arma::mat component in this list for futher use directly, such as matrix addition. However, some error is thrown.
Below is a toy example to throw the same error as I met:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
arma::mat f(){
arma::mat x = arma::randn(3, 4) ;
List y = List::create( ::Named("a") = arma::randn(3, 4) ) ;
return x - y["a"] ;
}
The error information is
ambiguous overload for 'operator-' (operand types are 'arma::mat'
{aka 'arma::Mat<double>'} and 'Rcpp::Vector<19>::NameProxy' {aka
'Rcpp::internal::generic_name_proxy<19, Rcpp::PreserveStorage>'})
Is there any way to use the y["a"] directly in numerical computation?
You need cast back from the SEXP type you create when adding to an (R or Rcpp, it's the same) List. Correcting that and adding a missing exports tag gives us what is below where I also added the sample invocation (another nice feature).
Code
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::export]]
arma::mat f(){
arma::mat x = arma::randn(3, 4) ;
List y = List::create( ::Named("a") = arma::randn(3, 4) ) ;
return x - Rcpp::as<arma::mat>(y["a"]);
}
/*** R
set.seed(42) # reproducible RNG draws
f()
*/
Output
> Rcpp::sourceCpp("~/git/stackoverflow/71612260/answer.cpp")
>
set.seed(42) # reproducible RNG draws
>
f()
[,1] [,2] [,3] [,4]
[1,] -0.471335 -2.337283 1.205825 0.537811
[2,] 0.119150 1.251941 0.486474 -0.513627
[3,] 3.309860 -0.654003 -0.146678 -0.835439
>
For a vector of strings, return the sum of each string's size.
I tried to use accumulate, together with a lambda function (Is it the best way of calculating what I want in 1-line?)
Codes are written in wandbox (https://wandbox.org/permlink/YAqXGiwxuGVZkDPT)
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> v = {"abc", "def", "ghi"};
size_t totalSize = accumulate(v.begin(), v.end(), [](string s){return s.size();});
cout << totalSize << endl;
return 0;
}
I expect to get a number (9), however, errors are returned:
/opt/wandbox/gcc-head/include/c++/10.0.0/bits/stl_numeric.h:135:39: note: 'std::__cxx11::basic_string' is not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>'
135 | __init = _GLIBCXX_MOVE_IF_20(__init) + *__first;
I want to know how to fix my codes? Thanks.
That's because you do not use std::accumulate properly. Namely, you 1) did not specify the initial value and 2) provided unary predicate instead of a binary. Please check the docs.
The proper way to write what you want would be:
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> v = {"abc", "def", "ghi"};
size_t totalSize = accumulate(v.begin(), v.end(), 0,
[](size_t sum, const std::string& str){ return sum + str.size(); });
cout << totalSize << endl;
return 0;
}
Both issues are fixed in this code:
0 is specified as initial value, because std::accumulate needs to know where to start, and
The lambda now accepts two parameters: accumulated value, and the next element.
Also note how std::string is passed by const ref into the lambda, while you passed it by value, which was leading to string copy on each invocation, which is not cool
I'm using RcppArmadillo in my R package and I would like to use the Rcpp::Nullable in the parameter list.
NumericVector d_snb(NumericVector& x,
Nullable<arma::mat> size_param = R_NilValue,
const int& infinite = 100000, const bool& log_v = false)
This gives an error like:
Error: package or namespace load failed for ‘packagex’ in dyn.load(file, DLLpath = DLLpath, ...): unable to load shared object '/usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so': dlopen(/usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so, 6):
Symbol not found: __Z5d_snbRN4Rcpp6VectorILi14ENS_15PreserveStorageEEENS_8NullableIS2_EES5_S5_RKiRKb
Referenced from: /usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so
Expected in: flat namespace in /usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so
Error: loading failed
Execution halted
The only possible solution right now seems to get the parameter as NumericVector and then get contents and then cast it into Armadillo types.
NumericVector d_snb(NumericVector& x,
Nullable<NumericVector> size_param = R_NilValue ...)
{
...
if(size_param.isNotNull()) {
arma::mat test(NumericVector(size_param));
param_check++;
}
...
}
This gives a warning:
d_snb.cpp:36:21: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
arma::mat test(NumericVector(size_param));
^~~~~~~~~~~~~~~~~~~~~~~~~~~
d_snb.cpp:36:22: note: add a pair of parentheses to declare a variable
arma::mat test(NumericVector(size_param));
^
( )
1 warning generated.
What's the best way to go about this ?
Yes, Rcpp::Nullable<> works only around SEXP-based Rcpp types.
So you have to do the two-step procedure you found. But I think you can do one simpler: arma::mat test = Rcpp::as<arma::mat>(size_param);
Here is a complete example which compiles (but does 'nothing'):
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
void d_snb(Rcpp::NumericVector& x,
Rcpp::Nullable<Rcpp::NumericVector> size_param = R_NilValue) {
if (size_param.isNotNull()) {
arma::vec test = Rcpp::as<arma::vec>(size_param);
test.print("vector");
}
Rcpp::Rcout << x << std::endl;
}
And a quick demo:
R> sourceCpp("/tmp/so44102346.cpp")
R> d_snb(c(1,2,3))
1 2 3
R> d_snb(c(1,2,3), c(4,5,6))
vector
4.0000
5.0000
6.0000
1 2 3
R>
I've been banging my head against a wall trying to understand how to use CGAL's Circular Kernel to calculate the intersection(s) between a line segment (Line_Arc_2) and a Circle (Circle_2). Unfortunately there isn't much in the way of example code for the Circular Kernel, and I'm not finding the reference manual much help.
Here is code that I thought would work, but right now it won't even compile (Mac OS 10.9 using the latest system compiler):
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2)>::type Res;
int main(){
int n = 0;
Circ2 c = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
LineArc2 l = LineArc2( Pt2(0,-2), Pt2(0,2) );
std::vector<Res> result;
CGAL::intersection(c, l, std::back_inserter(result));
return 0;
}
I get an error on the result_of line: "error: no type named 'result_type' in...", and a second error that "no viable overloaded '='" is available for the intersection line.
Also, since this would probably be the follow up question once this is working: how do I actually get at the intersection points that are put in the vector? CGAL's documentation suggests to me "result" should contain pairs of a Circular_arc_point_2 and an unsigned int representing its multiplicity. Is this what I will actually get in this case? More generally, does anyone know a good tutorial for using the Circular Kernel and Spherical Kernel intersection routines?
Thanks!
So it seems that result_of doesn't work here, despite being suggested in the CGAL reference manual for the CircularKernel's intersection function.
Here is a different version that seems to work and can properly handle the output:
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/iterator.h>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> IsectOutput;
using namespace std;
int main(){
int n = 0;
Circ2 c = Circ2(Pt2(1.0,0.0), Pt2(0.0,1.0), Pt2(-1.0, 0.0));
LineArc2 l = LineArc2( Pt2(0.0,-2.0), Pt2(0.0,2.0) );
std::vector<IsectOutput> output;
typedef CGAL::Dispatch_output_iterator< CGAL::cpp11::tuple<IsectOutput>,
CGAL::cpp0x::tuple< std::back_insert_iterator<std::vector<IsectOutput> > > > Dispatcher;
Dispatcher disp = CGAL::dispatch_output<IsectOutput>( std::back_inserter(output) );
CGAL::intersection(l, c, disp);
cout << output.size() << endl;
for( const auto& v : output ){
cout << "Point: (" << CGAL::to_double( v.first.x() ) << ", " << CGAL::to_double( v.first.y() ) << "), Mult: "
<< v.second << std::endl;
}
return 0;
}
result_of is working but the operator you are asking for does not exist, you are missing the output iterator.
However, I agree the doc is misleading. I'll try to fix it.
The following code is working fine:
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef boost::variant<std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> > InterRes;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2,std::back_insert_iterator<std::vector<InterRes> >)>::type Res;
int main(){
Circ2 c = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
LineArc2 l = LineArc2( Pt2(0,-2), Pt2(0,2) );
std::vector<InterRes> result;
CGAL::intersection(c, l, std::back_inserter(result));
return 0;
}
How to populate List (string) items on console window in c++cli.
Are you after the following or do you want to know how to read from console input and store in string (which is then stored in list I assume), this is the first case, how to store strings in list (and output the contents of list):
#include <iostream>
#include <string>
#include <list>
using std::string;
using std::list;
using std::cout;
using std::endl;
list<string> strlist;
strlist.push_back(string("string one"));
strlist.push_back(string("string two"));
strlist.push_back(string("and so on"));
for (list<string>::iterator it = strlist.begin(); it != strlist.end(); ++it)
{
std::cout << (*it) << std::endl;
}
Note: the list holds copy of strings, meaning you end up copying strings when you assign them to the list and when you return them from list. To avoid that you could allocate memory for string and keep only pointers in list:
list<string*> ls;
ls.push_back(new string("string one"));
and so on.
This works as well the above is more verbose to see how to access the list elements, in copy algorithm it all happens behind the scenes:
copy( strlist.begin(), strlist.end(), ostream_iterator<string>( cout, ", " ) );
You can use std::copy in and std::ostream_iterator in to copy elements into the output like so:
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
int main( int, char*[] )
{
using namespace std;
list<string> mylist;
mylist.push_back( "String1" );
mylist.push_back( "String2" );
mylist.push_back( "String3" );
copy( mylist.begin(), mylist.end(), ostream_iterator<string>( cout, ", " ) );
}