- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
任何人都可以解释以下行为吗?
当声明一个新的NumericMatrix
时,y
,作为原始矩阵,x
,乘以一个标量,c
,标量/矩阵乘法的顺序很重要。如果我将左侧的标量与右侧的矩阵相乘(例如 NumericMatrix y = c * x;
),则会出现奇怪的行为。原始矩阵 x
已更改!
但是,如果我将原始矩阵放在左边并在右边乘以标量(例如 NumericMatrix y = x * c;
),x
保持不变。
这似乎不会影响其他数据类型。我已经使用 int
和 NumericVector
进行了测试。
NumericMatrix
时出现问题#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericMatrix testfun(NumericMatrix x) {
NumericMatrix y(x.rows(), x.cols());
y = x * 2;
std::cout << x; // x is unmodified
y = 2 * x;
std::cout << x; // x is now modified
return x;
}
/*** R
x <- matrix(2, nrow = 3, ncol = 3)
print(x)
y <- testfun(x = x)
print(y)
print(x)
*/
输出如下。
> x <- matrix(2, nrow = 3, ncol = 3)
> print(x)
[,1] [,2] [,3]
[1,] 2 2 2
[2,] 2 2 2
[3,] 2 2 2
> y <- testfun(x = x)
2.00000 2.00000 2.00000
2.00000 2.00000 2.00000
2.00000 2.00000 2.00000
4.00000 4.00000 4.00000
4.00000 4.00000 4.00000
4.00000 4.00000 4.00000
> print(y)
[,1] [,2] [,3]
[1,] 4 4 4
[2,] 4 4 4
[3,] 4 4 4
> print(x)
[,1] [,2] [,3]
[1,] 4 4 4
[2,] 4 4 4
[3,] 4 4 4
> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.1
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.6.1 tools_3.6.1 RcppArmadillo_0.9.800.1.0
[4] Rcpp_1.0.2 RcppProgress_0.4.1 packrat_0.5.0
[7] RcppParallel_4.4.4
.
最佳答案
问题太长了,隐藏了它的意义。我们不需要第二个和第三个例子。我们只需要这段代码:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericMatrix testfun(NumericMatrix x) {
NumericMatrix y(x.rows(), x.cols());
y = x * 2;
std::cout << x; // x is unmodified
y = 2 * x; // contrast with x * 2
std::cout << x; // x is now modified
return x;
}
/*** R
print(x <- matrix(2, nrow = 2, ncol = 2))
print(y <- testfun(x = x))
print(x)
*/
R> Rcpp::sourceCpp("~/git/stackoverflow/59515517/question.cpp")
R> print(x <- matrix(2, nrow = 2, ncol = 2))
[,1] [,2]
[1,] 2 2
[2,] 2 2
R> print(y <- testfun(x = x))
2.00000 2.00000
2.00000 2.00000
4.00000 4.00000
4.00000 4.00000
[,1] [,2]
[1,] 4 4
[2,] 4 4
R> print(x)
[,1] [,2]
[1,] 4 4
[2,] 4 4
R>
x * 2
的行为不同于 2 * x
。后者有副作用。这可能是一个错误,
用 Rcpp 做矩阵代数真的没有意义。实现是初步的和不完整的。如果您想做“数学”,请使用 RcppArmadillo 或 RcppEigen。
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::mat testfun(arma::mat x) {
arma::mat y(x.n_rows, x.n_cols);
y = x * 2;
std::cout << x; // x is unmodified
y = 2 * x; // contrast with x * 2
std::cout << x; // x is now modified
return x;
}
/*** R
print(x <- matrix(2, nrow = 2, ncol = 2))
print(y <- testfun(x = x))
print(x)
*/
R> Rcpp::sourceCpp("~/git/stackoverflow/59515517/answer.cpp")
R> print(x <- matrix(2, nrow = 2, ncol = 2))
[,1] [,2]
[1,] 2 2
[2,] 2 2
R> print(y <- testfun(x = x))
2.0000 2.0000
2.0000 2.0000
2.0000 2.0000
2.0000 2.0000
[,1] [,2]
[1,] 2 2
[2,] 2 2
R> print(x)
[,1] [,2]
[1,] 2 2
[2,] 2 2
R>
我会看看能否解决您在此处发现的 Rcpp 错误。
编辑:这是一个错误,我还不太明白,但我在代码来源的问题中添加了一些内容:https://github.com/RcppCore/Rcpp/issues/365
编辑 2: 修复现在在 master 中。感谢 KK 的 PR,感谢在评论中暗示这背后可能是什么的其他人,特别是感谢 Ralf 让我们所有人都尝试修复。
关于rcpp - Rcpp NumericMatrix 在左/右乘以标量时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59515517/
我想将函数参数中的默认值设置为 Rcpp::Function 参数。 只是简单的赋值,Rcpp::Function func = mean , 不可能。它返回错误:no viable conversi
我正在处理需要逐元素矩阵乘法的代码。我试图在 Rcpp 中实现这一点,因为代码需要一些昂贵的循环。我对 Rcpp 还很陌生,可能会遗漏一些东西,但我无法使逐元素矩阵乘法工作。 // [[Rcpp::e
在 C++ 中,我们可以声明一个变量作为引用。 int a = 10; int& b = a; 如果我们设置 b=15 , a 也会改变。 我想在 Rcpp 中做类似的事情。 List X = obj
我正在阅读很棒的 Rcpp vignette关于使用 Rcpp 模块公开 C++ 类和函数。在这种情况下,是否可以创建一个 Rcpp 函数,该函数具有一个类型为 Uniform 的类作为参数之一,并且
我在 R 中有一个命名列表: l = list(a=1, b=2) 我想在 Rcpp 中使用这个列表,并迭代值和名称。理想情况下,它可能类似于(为简洁起见使用 C++11 格式): void prin
这个问题在这里已经有了答案: Rcpp - sourceCpp - undefined symbol (2 个答案) 关闭 4 年前。 我现有的 C 代码由三个文件组成:头文件(“.h”文件)、库文
我目前正在为类作业编写模拟退火算法(“解决”背包问题),并想在 Rcpp 中完成(我必须使用 R,而 Rcpp 更快)。 Rcpp 一直给我以下错误 invalid static_cast from
根据我的理解,在 Rcpp 和 C++ 之间转换 vector 会创建新 vector ,如下所示。我的理解对吗? 将 Rcpp vector 转换为 C++ vector 时,我们使用 Rcpp::
我想将参数的默认值设置为 NULL在Rcpp如果参数不是NULL,则函数并根据参数进行一些计算.这种代码的一个例子是 #include using namespace Rcpp; // [[Rcpp
任何人都可以解释以下行为吗? 当声明一个新的NumericMatrix时,y,作为原始矩阵,x,乘以一个标量,c,标量/矩阵乘法的顺序很重要。如果我将左侧的标量与右侧的矩阵相乘(例如 NumericM
有一种方法可以使用 NA 值初始化数值向量,例如。 NumericVector x(10,NumericVector::get_na()) 有没有类似的方法可以将矩阵初始化为 NA 值? 最佳答案 这
这可能是一个非常简单的问题,但我不知道哪里出了问题。 我有一个传递给 Rcpp 函数的列表,该列表的第一个元素是一个 data.frame。 我如何获取该 data.frame? bar = list
我正在尝试开发一个使用 Sundials 的 R 包用于求解微分方程的 C 库。为了不让用户安装库,我将库的源代码放在我的包中。 我已将库中的所有头文件放入 /inst/include/sundial
我正在研究一个同时使用 Rcpp::IntegerVector (行/列指针)和模板化 std::vector 的 Rcpp 稀疏矩阵类。基本原理是,在极大的稀疏矩阵中深度复制整数指针 vector
我想将一个R函数翻译成Rcpp,一个简单的测试代码如下,但我不知道如何处理默认设置为NULL的参数。 test t=R_NilValue, Rcpp
我想将一个R函数翻译成Rcpp,一个简单的测试代码如下,但我不知道如何处理默认设置为NULL的参数。 test t=R_NilValue, Rcpp
我想公开一个 C++ 类和一个将该类的对象作为 R 参数的函数。我必须遵循简化的示例。我使用创建了一个包 Rscript -e 'Rcpp::Rcpp.package.skeleton("soq")'
我想用 Rcpp 编写一个 C++ 函数,它使用 hypred 包中的 C 函数,它在 CRAN here 上. 我读了using C function from other package in R
[我在别处将其草拟为评论,但决定创建一个适当的问题...] 在 Rcpp 中使用数据帧时,就代码结构而言,目前被认为是“最佳实践”的是什么?从 R 到 C++ 代码的输入数据帧“传输”非常容易,但是如
我正在尝试使用 Rcpp::CharacterMatrix 并将每一行转换为 Rcpp::List 中它自己的元素。 但是,我为此编写的函数有一个奇怪的行为,即列表的每个条目都对应于矩阵的最后一行。为
我是一名优秀的程序员,十分优秀!