RcppArmadillo 逻辑矩阵操作
RcppArmadillo LogicalMatrix operations
我想在 RcppArmadillo 中做一些逻辑矩阵乘法,但我遇到了一些问题。例如,在 R 中,可以在以下代码中这样做:
times = c(1,2,3)
ti = c(times,4)
lst = c(4,5,6)
st = matrix(lst,nrow=1) %*% outer(times,ti,"<")
结果:
> st
[,1] [,2] [,3] [,4]
[1,] 0 4 9 15
这里matrix(lst,nrow=1)
是一个1×3的矩阵,outer(times,ti,"<")
是一个3×4的逻辑矩阵:
> matrix(lst,nrow=1)
[,1] [,2] [,3]
[1,] 4 5 6
> outer(times,ti,"<")
[,1] [,2] [,3] [,4]
[1,] FALSE TRUE TRUE TRUE
[2,] FALSE FALSE TRUE TRUE
[3,] FALSE FALSE FALSE TRUE
RcppArmadillo 版本如下:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::export(".vm")]]
arma::mat vm_mult(const arma::vec lhs,
const arma::umat rhs)
{
return lhs.t() * rhs;
}
// [[Rcpp::export]]
NumericMatrix ty(NumericVector times, NumericVector ti,NumericVector lst){
LogicalMatrix m = outer(times,ti,std::less<double>());
NumericMatrix st = vm_mult(lst,m);
return st;
}
vm_mult
是向量矩阵乘法,我把矩阵定义为umat
类型,即Mat<unsigned int>
。尝试通过 sourceCpp 运行 时出现以下错误:
error: conversion from 'LogicalMatrix' (aka 'Matrix<10>') to 'arma::umat' (aka 'Mat<unsigned int>') is ambiguous
NumericMatrix st = vm_mult(mag,m);
^
我也把type改成了const arma::Mat<unsigned char> rhs
,出现了类似的错误:
error: conversion from 'LogicalMatrix' (aka 'Matrix<10>') to 'arma::Mat<unsigned char>' is ambiguous
NumericMatrix st = vm_mult(mag,m);
^
我查看了Armadillo 库的文档,似乎没有专门定义逻辑矩阵。
所以除了将逻辑矩阵转换为 1,0 整数矩阵我该怎么办
好的,我明白了!原来需要使用 as<arma::umat>
.
将 LogicalMatrix 从 Rcpp 传递到 arma::umat
以下代码应该可以正常工作。
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::export]]
arma::mat ty(NumericVector times, NumericVector ti,NumericVector mag){
LogicalMatrix m = outer(times,ti,std::less<double>());
arma::umat rhs = as<arma::umat>(m);
arma::vec lhs = as<arma::vec>(mag);
arma::mat st = lhs.t() * rhs;
return st;
}
结果:
> sourceCpp('vm.cpp')
> ty(times,ti,lst)
[,1] [,2] [,3] [,4]
[1,] 0 4 9 15
我想在 RcppArmadillo 中做一些逻辑矩阵乘法,但我遇到了一些问题。例如,在 R 中,可以在以下代码中这样做:
times = c(1,2,3)
ti = c(times,4)
lst = c(4,5,6)
st = matrix(lst,nrow=1) %*% outer(times,ti,"<")
结果:
> st
[,1] [,2] [,3] [,4]
[1,] 0 4 9 15
这里matrix(lst,nrow=1)
是一个1×3的矩阵,outer(times,ti,"<")
是一个3×4的逻辑矩阵:
> matrix(lst,nrow=1)
[,1] [,2] [,3]
[1,] 4 5 6
> outer(times,ti,"<")
[,1] [,2] [,3] [,4]
[1,] FALSE TRUE TRUE TRUE
[2,] FALSE FALSE TRUE TRUE
[3,] FALSE FALSE FALSE TRUE
RcppArmadillo 版本如下:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::export(".vm")]]
arma::mat vm_mult(const arma::vec lhs,
const arma::umat rhs)
{
return lhs.t() * rhs;
}
// [[Rcpp::export]]
NumericMatrix ty(NumericVector times, NumericVector ti,NumericVector lst){
LogicalMatrix m = outer(times,ti,std::less<double>());
NumericMatrix st = vm_mult(lst,m);
return st;
}
vm_mult
是向量矩阵乘法,我把矩阵定义为umat
类型,即Mat<unsigned int>
。尝试通过 sourceCpp 运行 时出现以下错误:
error: conversion from 'LogicalMatrix' (aka 'Matrix<10>') to 'arma::umat' (aka 'Mat<unsigned int>') is ambiguous
NumericMatrix st = vm_mult(mag,m);
^
我也把type改成了const arma::Mat<unsigned char> rhs
,出现了类似的错误:
error: conversion from 'LogicalMatrix' (aka 'Matrix<10>') to 'arma::Mat<unsigned char>' is ambiguous
NumericMatrix st = vm_mult(mag,m);
^
我查看了Armadillo 库的文档,似乎没有专门定义逻辑矩阵。
所以除了将逻辑矩阵转换为 1,0 整数矩阵我该怎么办
好的,我明白了!原来需要使用 as<arma::umat>
.
以下代码应该可以正常工作。
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::export]]
arma::mat ty(NumericVector times, NumericVector ti,NumericVector mag){
LogicalMatrix m = outer(times,ti,std::less<double>());
arma::umat rhs = as<arma::umat>(m);
arma::vec lhs = as<arma::vec>(mag);
arma::mat st = lhs.t() * rhs;
return st;
}
结果:
> sourceCpp('vm.cpp')
> ty(times,ti,lst)
[,1] [,2] [,3] [,4]
[1,] 0 4 9 15