- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Rcpp 的新手,正在尝试基于 for()
中的负索引执行计算- 使用 RcppArmadillo 循环。
我已经发现 RcppArmadillo 中的负索引不是那么简单,但是可以通过应该保留的元素向量来完成 (as I found here) .当要删除的元素是循环索引时,这对我来说似乎有点困难。我试图在 this answer 中实现最后一种方法,但没有成功。有没有一种简单的方法来指定不包括具有循环索引的元素的元素向量?
所以,我试图在 RcppArmadillo 中为 y[-i]
找到一个等价物。在以下 MWE 中:
# In R:
# Input
n <- 10
y <- 1:20
# Computation
x <- rep(NA, n)
for(i in 1:n){
x[i] <- sum(y[-i])
}
到目前为止我的代码 Rcpp 代码:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
// [[Rcpp::export]]
arma::vec rcpp_sum (arma::vec y, int n){
arma::vec x(n);
arma::vec ind(n);
for (i=0; i<n; i++){
ind[i] = /*no idea...*/
x[i] = sum(y[ind]);
}
return x;
}
任何帮助是极大的赞赏!
最佳答案
对于这样的任务,最好跳过有问题的索引。标准中 C++
我们会检查索引并跳过它。像这样的东西:
// [[Rcpp::export]]
arma::vec rcpp_sum (arma::vec y, int n){
arma::vec x(n);
for (int i = 0; i < n; i++) {
x[i] = 0; // Initialize value
for (int j = 0; j < y.size(); ++j) {
if (i != j) {
x[i] += y[j];
}
}
}
return x;
}
在上面,我们正在远离糖语法。 IMO 在这种情况下没问题,因为替代方案并不太复杂。在我们简化的同时,对
RcppArmadillo
的依赖没有必要,因为我们可以使用纯
Rcpp
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector pure_rcpp_sum (NumericVector y, int n){
NumericVector x(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < y.size(); ++j) {
if (i != j) {
x[i] += y[j];
}
}
}
return x;
}
验证输出:
all.equal(as.vector(rcpp_sum(y, n)), x)
[1] TRUE
all.equal(pure_rcpp_sum(y, n), x)
[1] TRUE
更新
R
中有一个优化的方法。为此特定目的。以上演示了如何解决仅对向量中的值求和而忽略
C++
中的一个值的非常具体的问题。 .这在本质上是教学性的,不一定是此特定任务的最佳方式(如下所示)。
R
代码,我想指出 OP 担心在
C++
中的内部循环中有一个简单的条件语句不应该害怕(就像 base
R
中的情况一样)。据我所知,在 OP 的链接中演示的子集化是 O(n) 并且具有额外逻辑向量的额外开销。我们上面提出的解决方案应该更有效,因为它在没有额外对象的情况下基本上可以做同样的事情。
baseR <- function(y, n) {
mySum <- sum(y)
vapply(1:n, function(x) mySum - y[x], FUN.VALUE = 1)
}
## Here is the OP code for reference
OP <- function(y, n) {
x <- rep(NA, n)
for(i in 1:n) {x[i] <- sum(y[-i])}
x
}
就是这样。它也快如闪电:
huge_y <- rnorm(1e6)
huge_n <- 1e3
system.time(t1 <- baseR(huge_y, huge_n))
user system elapsed
0.003 0.000 0.003
system.time(t2 <- pure_rcpp_sum(huge_y, huge_n))
user system elapsed
2.776 0.003 2.779
system.time(t3 <- OP(huge_y, huge_n))
user system elapsed
9.555 1.248 10.805
all.equal(t1, t2)
[1] TRUE
all.equal(t1, t3)
[1] TRUE
关于loops - RcppArmadillo:for 循环中的负索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63702401/
我有一个非常基本的 MySQL 查询,它从数据库表中读取行并将行值添加或减去定义为 $total_balance 的 PHP 字符串。 例如; $statement_details_query = m
我有 following fiddle ,请注意,如果您使输出的宽度变小,图像将被覆盖并且不会出现滚动条 - 完美。 如果我attempt the same effect on the right ,
这个正则表达式将得到 456。我的问题是为什么它不能是 1-234-56 中的 234 ? 56 是否限定 (?!\d)) 模式,因为它不是单个数字。 (?!\d)) 寻找的起始点在哪里? impor
我需要知道两个子结构之间的内存距离 (&my_type.a - &my_tape.b.c) 结果的类型是什么?我需要将它转换为 (signed int),所以显然它是别的东西。 最佳答案 根据 C11
我遇到了一个扩展异常的异常处理程序类,如下所示: public class AppFileReaderException extends Exception { //Explicit seri
如何可视化负 RGB 值? 根据 OpenCV 文档: CV_8S - 8 位有符号整数 (-128..127) 这是否意味着 -128 表示 0 而 127 表示 255? 如果是,那我们为什么需要
我这里有一段代码给我带来了麻烦: idIndex = panoBuffer.indexOf("\"photo_id\":"); System.out.println(idIndex);
我刚刚练习 Java,对此还很陌生。我只是想创建一个随机数生成器程序来跟踪玩家的获胜、失败、获胜百分比和总获胜金额。该程序的逻辑是,玩家每次 session 有 3 次机会,计算机会生成一个随机数,玩
因此,我们被要求创建一个程序,使用户能够从 1-6 个有关矩阵运算的选项中进行选择。在每个用户的输入中,我们需要检查该输入是否适合要完成的操作(程序应该接受整数或 float ,正数或负数)。如果不满
这是我期望的输出 x |x| 1.2 1.2 -2.3 2.3 3.4 3.4 但我一直收到这个: x |x| 1
假设我有这个: $date1=date_create(date('H:I', strtotime('8:00'))); $date2=date_create(date('H:I', strtotime
如何确定负 FixNum 的无符号解释? # unexpected, true (~0b01111011).to_s(2) == ("-" + (~0b01111011).abs.to_s(2)) #
这是一个用于“邀请您的 friend 加入此群组”脚本的快速 SQL 查询。 我有 2 个表:users 和 group_members。我正在尝试执行一个查询,选择我所有的 friend ——由第一
负 ASCII 值有什么意义? int a = '«'; //a = -85 but as in ASCII table '<<' should be 174 最佳答案 没有负数ASCII值。 ASC
我知道用 PHP 可以做到这一点,但是有没有办法只用 MySQL 来做到这一点? 我有这个数据库: --------------------------------------------------
我在变量中有一个时间戳 $data = (float) -2208988800; 是否可以根据这些数据创建正确的日期?date("d.M.Y", $data) 返回“07.02.2036” 最佳答案
你好我如何将括号格式的负值转换为 double 值。目前我有这个。 Payment.Text = Calc_Payment().ToString("#,##0.00;(#,##0.00)"); 将支付
这是一个小程序。这应该打印 0 或 1,还是它有未定义的行为? #include struct S0 { unsigned f1 : 1; }; struct S0 s; int main (v
运行 lgb.cv 时,我有时会从日志中看到“从分数开始训练”后的负数。想知道这个数字到底是什么意思,单位是什么?是根据参数中指定的指标吗?以下是摘录: [LightGBM] [Info] Total
我正在使用变分自动编码器类型模型,我的损失函数的一部分是均值为 0 和方差为 1 的正态分布与另一个均值和方差由我的模型预测的正态分布之间的 KL 散度。 我用以下方式定义了损失: def kl_lo
我是一名优秀的程序员,十分优秀!