- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道是否有一种使用 NumericMatrix 和 NumericVector 类计算矩阵乘法的方法。我想知道是否有任何简单的方法
帮助我避免以下循环进行此计算。我只想计算 X%*%beta。
// assume X and beta are initialized and X is of dimension (nsites, p),
// beta is a NumericVector with p elements.
for(int j = 0; j < nsites; j++)
{
temp = 0;
for(int l = 0; l < p; l++) temp = temp + X(j,l) * beta[l];
}
最佳答案
根据 Dirk 的评论,这里有几个案例通过重载 *
演示 Armadillo 库的矩阵乘法。运算符(operator):
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export(".mm")]]
arma::mat mm_mult(const arma::mat& lhs,
const arma::mat& rhs)
{
return lhs * rhs;
}
// [[Rcpp::export(".vm")]]
arma::mat vm_mult(const arma::vec& lhs,
const arma::mat& rhs)
{
return lhs.t() * rhs;
}
// [[Rcpp::export(".mv")]]
arma::mat mv_mult(const arma::mat& lhs,
const arma::vec& rhs)
{
return lhs * rhs;
}
// [[Rcpp::export(".vv")]]
arma::mat vv_mult(const arma::vec& lhs,
const arma::vec& rhs)
{
return lhs.t() * rhs;
}
`%a*%` <- function(x,y) {
if (is.matrix(x) && is.matrix(y)) {
return(.mm(x,y))
} else if (!is.matrix(x) && is.matrix(y)) {
return(.vm(x,y))
} else if (is.matrix(x) && !is.matrix(y)) {
return(.mv(x,y))
} else {
return(.vv(x,y))
}
}
##
mx <- matrix(1,nrow=3,ncol=3)
vx <- rep(1,3)
my <- matrix(.5,nrow=3,ncol=3)
vy <- rep(.5,3)
%*%
相比功能:
R> mx %a*% my
[,1] [,2] [,3]
[1,] 1.5 1.5 1.5
[2,] 1.5 1.5 1.5
[3,] 1.5 1.5 1.5
R> mx %*% my
[,1] [,2] [,3]
[1,] 1.5 1.5 1.5
[2,] 1.5 1.5 1.5
[3,] 1.5 1.5 1.5
##
R> vx %a*% my
[,1] [,2] [,3]
[1,] 1.5 1.5 1.5
R> vx %*% my
[,1] [,2] [,3]
[1,] 1.5 1.5 1.5
##
R> mx %a*% vy
[,1]
[1,] 1.5
[2,] 1.5
[3,] 1.5
R> mx %*% vy
[,1]
[1,] 1.5
[2,] 1.5
[3,] 1.5
##
R> vx %a*% vy
[,1]
[1,] 1.5
R> vx %*% vy
[,1]
[1,] 1.5
关于matrix-multiplication - 在 Rcpp 中使用 NumericMatrix 和 NumericVector 进行矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28465766/
对于NumericVector ,我可以将一个较小的 NumericVector 进行子集化通过使用 IntegerVector包含要子集化的位置。 例如假设x<-c(1,2,2,3,4,5) , i
A recently asked question让我相信 Rcpp 的 * 语法糖没有按预期工作。在链接的问题中,用户试图将矩阵乘以标量。 R代码 这是我们在 Rcpp 中尝试实现的目标,但目前在普
我一直在测试 Rcpp 和 RcppArmadillo 来计算大矩阵的汇总统计数据。这比基础 R colMeans 或 Armadillo 在大约 400 万行、45 列上要快得多(快 5 或 10
任何人都可以解释以下行为吗? 当声明一个新的NumericMatrix时,y,作为原始矩阵,x,乘以一个标量,c,标量/矩阵乘法的顺序很重要。如果我将左侧的标量与右侧的矩阵相乘(例如 NumericM
我正在学习在我的工作中使用 RcppParallel,并试图安装一个使用 Rcpp.package.skeleton() 制作的简单包。该软件包包含三个源文件,即 Rcpp 的 HelloWorld
我学习 Rcpp 类/数据结构时的一个新手问题:是否有一个成员函数可以为 Rcpp::NumericMatrix 类的对象删除行/列? (或其他类型的 type **Matrix -- 我假设它是一个
这就是我现在做的 library(Rcpp) A A A B C X 1 0 0 Y 0 2 0 Z 0 0 3 > scaleMatrix(A, 2) > A A B C X 1 0 0
我想使用 Rcpp 折叠转置 NumericMatrix 的行。例如: library("data.table") library("Rcpp") dt1 string_collapse(con
我正在开发一个需要一些非常快的矩阵乘法的包,所以希望使用 RcppEigen .出于各种原因,尽管与多维数组的需要有关,但我需要将类 Eigen::MatrixXd 的创建对象转换为类 Rcpp::N
我必须明智地将矩阵条目与数字进行比较,因此我尝试定义一个 Cxx 函数,例如 src = t; return result; } ' cppFunction(src) 但是会抛出一些异常。是什么原
关注此question ,我试图了解如何有效地更新 Rccp::NumericMatrix 数据类型的子集。 我有以下场景: Rcpp::NumericMatrix m of 5 x 5 需要更新几行
有没有办法分配一个长度为 n 的 Rcpp List,其中 List 的每个元素都将填充一个 NumericMatrix,但每个 NumericMatrix 的大小可以改变? 我有一个使用 std::
我可以选择矩阵的所有行和矩阵的一系列列,如下所示: library(Rcpp) cppFunction(' NumericMatrix subset(NumericMatrix x){ retur
我发现如果没有漂亮的 我会迷失方向和 Rcpp 及其相关包提供的用于不同对象类型之间转换的命令。 我有一个点矩阵,其中的行表示二维笛卡尔空间中的点: pointsMatrix #includ
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
#include #include extern "C" { #include "cheader.h" } using namespace Rcpp; // [[Rcpp::export]]
我想知道是否有一种使用 NumericMatrix 和 NumericVector 类计算矩阵乘法的方法。我想知道是否有任何简单的方法 帮助我避免以下循环进行此计算。我只想计算 X%*%beta。 /
我有一个 std::vector>我想将其转换为 Rcpp::DataFrame或 Rcpp::NumericMatrix . 我目前的解决方案看起来像这样,但远非理想;它产生一个数字列表。 Rcpp
我是一名优秀的程序员,十分优秀!