// Copyright (C) 2012 Venelin Mitov
//
// This C++ header file is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This file is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// If you have not received a copy of the GNU General Public License,
// see .
/*
* ArmaAlglibGlue.h
* Provides conversion between arma:: and alglib:: datatypes.
* Created on: Jul 14, 2012
* Author: Venelin Mitov
*/
#ifndef ARMA_ALGLIB_GLUE
#define ARMA_ALGLIB_GLUE
#include "ap.h"
#include "linalg.h"
#include
#include
namespace armaalglib
{
/**
* Creates an instance of type T2 and copies the elements of
* from into it.
* from : object which should be copied into the new instance
* returns the newly created instance
*/
template
T2
inline
as(const T1& from);
/**
* Copies the elements in from to p_to. p_to should point to an
* object of the same mathematical type (i.e. vector or matrix),
* and should have at least as many rows and columns as from.
* from : instance which should be copied
* p_to : pointer where to copy to
*/
template
void
inline
copy(const T1& from, T2* p_to);
namespace internal
{
template
std::size_t
inline
cols(const T_matrix& M);
template
std::size_t
inline
rows(const T_matrix& M);
template
std::size_t
inline
length(const T_vector& V);
template
void
inline
resize(T_matrix& M, std::size_t ncols, std::size_t nrows);
template
void
inline
resize(T_vector& V, std::size_t n);
#define DEF_SIZEFUN(__SIZEFUN__,__CLASS__,__METHOD__) \
template<> \
std::size_t \
__SIZEFUN__(const __CLASS__& X) { \
return X.__METHOD__; \
}
#define DEF_RESIZE_VEC(__CLASS__,__METHOD__) \
template<> \
void \
inline resize<__CLASS__>(__CLASS__& V, std::size_t n) { \
V.__METHOD__(n); \
}
#define DEF_RESIZE_MAT(__CLASS__,__METHOD__) \
template<> \
void \
inline resize<__CLASS__>(__CLASS__& M, std::size_t nrows, size_t ncols) { \
M.__METHOD__(nrows, ncols); \
}
DEF_SIZEFUN(rows, arma::mat, n_rows)
DEF_SIZEFUN(rows, arma::imat, n_rows)
DEF_SIZEFUN(cols, arma::mat, n_cols)
DEF_SIZEFUN(cols, arma::imat, n_cols)
DEF_SIZEFUN(length, arma::vec, n_elem)
DEF_SIZEFUN(length, arma::ivec, n_elem)
DEF_SIZEFUN(rows, alglib::real_2d_array, rows())
DEF_SIZEFUN(rows, alglib::integer_2d_array, rows())
DEF_SIZEFUN(rows, alglib::boolean_2d_array, rows())
DEF_SIZEFUN(cols, alglib::real_2d_array, rows())
DEF_SIZEFUN(cols, alglib::integer_2d_array, rows())
DEF_SIZEFUN(cols, alglib::boolean_2d_array, rows())
DEF_SIZEFUN(length, alglib::real_1d_array, length())
DEF_SIZEFUN(length, alglib::integer_1d_array, length())
DEF_SIZEFUN(length, alglib::boolean_1d_array, length())
DEF_RESIZE_VEC(arma::vec, resize)
DEF_RESIZE_VEC(arma::ivec, resize)
DEF_RESIZE_MAT(arma::mat, resize)
DEF_RESIZE_MAT(arma::imat, resize)
DEF_RESIZE_VEC(alglib::real_1d_array, setlength)
DEF_RESIZE_VEC(alglib::integer_1d_array, setlength)
DEF_RESIZE_VEC(alglib::boolean_1d_array, setlength)
DEF_RESIZE_MAT(alglib::real_2d_array, setlength)
DEF_RESIZE_MAT(alglib::integer_2d_array, setlength)
DEF_RESIZE_MAT(alglib::boolean_2d_array, setlength)
}
#define DEF_COPY_VECTOR(__T1__,__T2__) \
template<> \
void \
inline copy<__T1__, __T2__>(const __T1__& from, __T2__* p_to) { \
for(int i = 0; i < internal::length(from); ++i) \
(*p_to)[i] = from[i]; \
}
#define DEF_COPY_MATRIX(__T1__,__T2__) \
template<> \
void \
inline copy<__T1__, __T2__>(const __T1__& from, __T2__* p_to) { \
for(int i = 0; i < internal::rows(from); ++i) \
for(int j = 0; j < internal::cols(from); ++j) \
(*p_to)(i,j) = from(i,j); \
}
DEF_COPY_VECTOR(alglib::real_1d_array, arma::vec)
DEF_COPY_VECTOR(alglib::integer_1d_array, arma::ivec)
DEF_COPY_VECTOR(alglib::boolean_1d_array, arma::ivec)
DEF_COPY_VECTOR(arma::vec, alglib::real_1d_array)
DEF_COPY_VECTOR(arma::ivec, alglib::integer_1d_array)
DEF_COPY_VECTOR(arma::ivec, alglib::boolean_1d_array)
DEF_COPY_MATRIX(alglib::real_2d_array, arma::mat)
DEF_COPY_MATRIX(alglib::integer_2d_array, arma::imat)
DEF_COPY_MATRIX(alglib::boolean_2d_array, arma::imat)
DEF_COPY_MATRIX(arma::mat, alglib::real_2d_array)
DEF_COPY_MATRIX(arma::imat, alglib::integer_2d_array)
DEF_COPY_MATRIX(arma::imat, alglib::boolean_2d_array)
#define DEF_VECTOR_AS(__T1__,__T2__) \
template<> \
__T2__ \
inline as<__T1__, __T2__>(const __T1__& from) { \
__T2__ v; \
internal::resize(v, internal::length(from)); \
copy<__T1__,__T2__>(from, &v); \
return v; \
}
#define DEF_MATRIX_AS(__T1__,__T2__) \
template<> \
__T2__ \
inline as<__T1__, __T2__>(const __T1__& from) { \
__T2__ m; \
internal::resize(m, internal::rows(from), internal::cols(from)); \
copy<__T1__,__T2__>(from, &m); \
return m; \
}
DEF_VECTOR_AS(alglib::real_1d_array, arma::vec)
DEF_VECTOR_AS(alglib::integer_1d_array, arma::ivec)
DEF_VECTOR_AS(alglib::boolean_1d_array, arma::ivec)
DEF_VECTOR_AS(arma::vec, alglib::real_1d_array)
DEF_VECTOR_AS(arma::ivec, alglib::integer_1d_array)
DEF_VECTOR_AS(arma::ivec, alglib::boolean_1d_array)
DEF_MATRIX_AS(alglib::real_2d_array, arma::mat)
DEF_MATRIX_AS(alglib::integer_2d_array, arma::imat)
DEF_MATRIX_AS(alglib::boolean_2d_array, arma::imat)
DEF_MATRIX_AS(arma::mat, alglib::real_2d_array)
DEF_MATRIX_AS(arma::imat, alglib::integer_2d_array)
DEF_MATRIX_AS(arma::imat, alglib::boolean_2d_array)
}
namespace Rcpp {
template<>
alglib::real_1d_array
as(SEXP v)
{
NumericVector nv(v);
alglib::real_1d_array res;
res.setcontent(nv.size(), nv.begin());
return res;
}
template<>
alglib::real_2d_array
as(SEXP m) {
arma::mat marma = as(m);
return armaalglib::as(marma);
}
template<>
SEXP
wrap(const alglib::real_1d_array &v)
{
return wrap(NumericVector(v.getcontent(), v.getcontent() + v.length()));
}
template<>
SEXP
wrap(const alglib::real_2d_array &m)
{
arma::mat marma;
return wrap(armaalglib::as(m));
// return wrap(marma);
}
}
#endif //ARMA_ALGLIB_GLUE