logo
down
shadow

boost ublas: rotate 2d vector


boost ublas: rotate 2d vector

Content Index :

boost ublas: rotate 2d vector
Tag : cpp , By : user186831
Date : January 02 2021, 06:48 AM


Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Boost uBLAS matrix/vector product


Tag : cpp , By : ┬Áilad
Date : March 29 2020, 07:55 AM
it helps some times I haven't looked that much at Boost uBLAS, but Eigen sure is nice, and has good performance as well.

How to copy a boost::numeric::ublas::vector to a matrix?


Tag : cpp , By : Cube_Zombie
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , What am I doing wrong here? , Did this not work?
std::copy(v.begin(), v.end(), m.begin1());
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <algorithm>

int main()
{
    boost::numeric::ublas::vector<int> v(10);
    boost::numeric::ublas::matrix<int> m(10,10);  //using v.size() also works
    std::copy(v.begin(), v.end(), m.begin1());
    return 0;
}

Convert Rcpp Numeric Vector into boost:ublas:vector


Tag : cpp , By : Habikki
Date : March 29 2020, 07:55 AM
may help you . First off, sorry for the delay. Hopefully, what I've done makes up for it.
With that being said, let's do this!
#include <RcppCommon.h>

// Flags for C++ compiler

// [[Rcpp::depends(BH)]]
// [[Rcpp::plugins("cpp11")]]

// Third party library includes that provide the template class of ublas
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/matrix.hpp>

// Provide Forward Declarations
namespace Rcpp {

   namespace traits{

   // Setup non-intrusive extension via template specialization for
   // 'ublas' class boost::numeric::ublas

   // Support for wrap
   template <typename T> SEXP wrap(const boost::numeric::ublas::vector<T> & obj);

   // Support for as<T>
   template <typename T> class Exporter< boost::numeric::ublas::vector<T> >;

  }
}
// >> Place <Rcpp.h> AFTER the forward declaration!!!! <<

#include <Rcpp.h>


// >> Place Definitions of Forward Declarations AFTER <Rcpp.h>!!!! <<
// Define template specializations for as<> and wrap

namespace Rcpp {

namespace traits{

// Defined wrap case
template <typename T> SEXP wrap(const boost::numeric::ublas::vector<T> & obj){
  const int RTYPE = Rcpp::traits::r_sexptype_traits<T>::rtype ;

  return Rcpp::Vector< RTYPE >(obj.begin(), obj.end());
};


// Defined as< > case
template<typename T>
class Exporter< boost::numeric::ublas::vector<T> > {
  typedef typename boost::numeric::ublas::vector<T> OUT ;

  // Convert the type to a valid rtype. 
  const static int RTYPE = Rcpp::traits::r_sexptype_traits< T >::rtype ;
  Rcpp::Vector<RTYPE> vec;

public:
  Exporter(SEXP x) : vec(x) {
    if (TYPEOF(x) != RTYPE)
      throw std::invalid_argument("Wrong R type for mapped 1D array");
  }
  OUT get() {

    // Need to figure out a way to perhaps do a pointer pass?
    OUT x(vec.size());

    std::copy(vec.begin(), vec.end(), x.begin()); // have to copy data

    return x;
  }
} ;


}
}
// Here we define a shortcut to the boost ublas class to enable multiple ublas types via a template.
// ublas::vector<T> => ublas::vector<double>, ... , ublas::vector<int>
namespace ublas = ::boost::numeric::ublas;
// [[Rcpp::export]]
void containment_test(Rcpp::NumericVector x1) {

  Rcpp::Rcout << "Converting from Rcpp::NumericVector to ublas::vector<double>" << std::endl;

  ublas::vector<double> x = Rcpp::as< ublas::vector<double> >(x1); // initialize the vector to all zero

  Rcpp::Rcout << "Running output test with ublas::vector<double>" << std::endl;

  for (unsigned i = 0; i < x.size (); ++ i)
    Rcpp::Rcout  << x(i) << std::endl;

  Rcpp::Rcout << "Converting from ublas::vector<double> to Rcpp::NumericVector" << std::endl;

  Rcpp::NumericVector test = Rcpp::wrap(x);

  Rcpp::Rcout << "Running output test with Rcpp::NumericVector" << std::endl;

  for (unsigned i = 0; i < test.size (); ++ i)
    Rcpp::Rcout  << test(i) << std::endl;

}
containment_test(c(1,2,3,4))
Converting from Rcpp::NumericVector to ublas::vector<double>
Running output test with ublas::vector<double>
1
2
3
4
Converting from ublas::vector<double> to Rcpp::NumericVector
Running output test with Rcpp::NumericVector
1
2
3
4
// [[Rcpp::export]]
ublas::vector<double> automagic_ublas_rcpp(ublas::vector<double> x1) {
  return x1;
}
automagic_ublas_rcpp(c(1,2,3.2,1.2))
[1] 1.0 2.0 3.2 1.2
// -------------- Stage 1: Forward Declarations with `RcppCommon.h`

#include <RcppCommon.h>

// Flags for C++ compiler
// [[Rcpp::depends(BH)]]
// [[Rcpp::plugins("cpp11")]]

// Third party library includes that provide the template class of ublas
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/matrix.hpp>


// Here we use ublas_vec to enable multiple ublas types via a template.
// ublas::vector<T> => ublas::vector<double>, ... , ublas::vector<int>
namespace ublas = ::boost::numeric::ublas;


// Provide Forward Declarations
namespace Rcpp {

  namespace traits{

    // Setup non-intrusive extension via template specialization for
    // 'ublas' class boost::numeric::ublas

    // Support for wrap
    template <typename T> SEXP wrap(const boost::numeric::ublas::vector<T> & obj);

    // Support for as<T>
    template <typename T> class Exporter< boost::numeric::ublas::vector<T> >;

  }
}

// -------------- Stage 2: Including Rcpp.h

// >> Place <Rcpp.h> AFTER the forward declaration!!!! <<

#include <Rcpp.h>


// >> Place Definitions of Forward Declarations AFTER <Rcpp.h>!!!! <<


// -------------- Stage 3: Implementation of Declarations

// Define template specializations for as<> and wrap

namespace Rcpp {

  namespace traits{

    // Defined wrap case
    template <typename T> SEXP wrap(const boost::numeric::ublas::vector<T> & obj){
      const int RTYPE = Rcpp::traits::r_sexptype_traits<T>::rtype ;

      return Rcpp::Vector< RTYPE >(obj.begin(), obj.end());
    };


    // Defined as< > case
    template<typename T>
    class Exporter< boost::numeric::ublas::vector<T> > {
      typedef typename boost::numeric::ublas::vector<T> OUT ;

      // Convert the type to a valid rtype. 
      const static int RTYPE = ::Rcpp::traits::r_sexptype_traits< T >::rtype ;
      Rcpp::Vector<RTYPE> vec;

    public:
      Exporter(SEXP x) : vec(x) {
        if (TYPEOF(x) != RTYPE)
          throw std::invalid_argument("Wrong R type for mapped 1D array");
      }
      OUT get() {

        // Need to figure out a way to perhaps do a pointer pass?
        OUT x(vec.size());

        std::copy(vec.begin(), vec.end(), x.begin()); // have to copy data

        return x;
      }
    } ;


  }
}

// -------------- Stage 4: Tests

// [[Rcpp::export]]
ublas::vector<double> automagic_ublas_rcpp(ublas::vector<double> x1) {
  return x1;
}


// [[Rcpp::export]]
void containment_test(Rcpp::NumericVector x1) {

  Rcpp::Rcout << "Converting from Rcpp::NumericVector to ublas::vector<double>" << std::endl;

  ublas::vector<double> x = Rcpp::as< ublas::vector<double> >(x1); // initialize the vector to all zero

  Rcpp::Rcout << "Running output test with ublas::vector<double>" << std::endl;

  for (unsigned i = 0; i < x.size (); ++ i)
    Rcpp::Rcout  << x(i) << std::endl;

  Rcpp::Rcout << "Converting from ublas::vector<double> to Rcpp::NumericVector" << std::endl;

  Rcpp::NumericVector test = Rcpp::wrap(x);

  Rcpp::Rcout << "Running output test with Rcpp::NumericVector" << std::endl;

  for (unsigned i = 0; i < test.size (); ++ i)
    Rcpp::Rcout  << test(i) << std::endl;

}

Operator overloading Boost ublas Vector


Tag : cpp , By : Mikael
Date : March 29 2020, 07:55 AM

typedef a vector and boost::numeric::ublas::vector of fixed size


Tag : cpp , By : hsdfhksh
Date : March 29 2020, 07:55 AM
wish helps you Neither std::vector nor (at the time of writing) boost::numeric::ublas::vector are designed to be a fixed size. There's no template parameter that specifies the size of the container.
So no, a fixed sized typedef makes no sense for these containers.
Related Posts Related QUESTIONS :
  • Avoid allocating in constructor or preserve simplicity (and RAII?)
  • Can C++ raise an error when std array initialization is too small?
  • Reference to end is ambiguous
  • Problem with basic usage of std::enable_if
  • How to print out a decimal number as octal number using setiosflags in C++
  • Open Visual Studio with solution and specific file and line
  • Enum value based on template type
  • Is there any way to swap nodes in std::list?
  • How to copy QString into wchar_t buffer
  • Make the compiler generate an empty default function for an std::function
  • Insert or push_back to end of a std::vector?
  • Best practice for const temporary types
  • Include CSV File in Complied Binary
  • Issue with binding non static function to callback
  • How can read from some files and write content of them with different form into files with same names
  • Why does auto deduce this variable as double and not float?
  • no instance of overloaded function "std::make_unique" matches the argument list, but works with unique_ptr con
  • How to see array size from a pointer in c++
  • Error taking address of temporary in Eclipse
  • Using an iterator to go through a vector and modify the contents
  • Are extern extern "C", and extern "C" extern, allowed?
  • Can't solve C2660 and C2065 Errors
  • C referencing C++ extern
  • How to write the definition of a derived class in c++?
  • Why when I include <cmath> I need to use the namespace std too?
  • How to assign a 32-bit unsigned integer to a bit field containing 32 bits
  • Why does the same class being defined in multiple .cpp files not cause a linker multiple definition error?
  • C++ 11db error when trying to quit the program in xcode. beginner level
  • Add content of a vector into a Capnproto map object
  • Recursively Pass Template Template To a Template Template Function
  • Swap rows in a 2D array with std::swap. How does it work?
  • Is there any situation in which an object's storage might change during its lifetime?
  • clang++ always generates empty profraw coverage reports
  • Do memory leaks persist after program completion if the OS does not clear it?
  • How to link library using cmake
  • How to use getters and setters without generating a copy?
  • Generating multiple amounts of the same sprite broken
  • function in c++ why my compiler didn't recognize the error()
  • Relationship between copy(...) and copy(seq, ...)
  • Are <cmath> functions required to be `noexcept` in C++17?
  • How to find a struct list item
  • How can you handle DLL versions when referencing C++ DLL's over COM from VBScript with CreateObject?
  • Do not understand how c++ set works
  • Actual build date in C++
  • How to link to already compiled external shared libraries in RCPP using Makevars?
  • combination of enable_if + std::less + sizeof... makes MSVC fail
  • Can you call the destructor without calling the constructor?
  • How do I prevent a function from freeing memory of a local variable?
  • Why am I getting an exception with a push involved with a shared pointer?
  • Resizing an array by pointer
  • avoiding dynamic_cast without increasing coupling
  • I cannot solve the else part in my if-else ladder?
  • Inherit from arbitrary class in c++?
  • LNK2019 unresolved external symbol from MSVCRTD.lib
  • Do I need to free wchar memory allocated when using 'new' or does delete[] also free it?
  • Object creation with varying buffer size
  • Is there a way to save a variable in an std::string?
  • template lambda vs functor with template operator()
  • How to release boost::interprocess::named_mutex when the process crashes
  • How do I implement the Interface Segregation Principle using smart pointers in C++?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com