小编dud*_*udu的帖子

运算符重载在R包中停止工作

我有一个基本上是列表的容器类.因为我想支持子集化,所以我重载了子集[运算符(可能实现得很差).

#' Constructor for spectra object
.spectra = function(n_spectrum = 0) {
    object        = vector(mode = "list", n_spectrum)
    class(object) = "spectra"
    return(object)
}

#' Operator overload
#' @export
`[.spectra` = function(x, i) {
    x = unclass(x)
    x = x[i]                  # Using the list's subset function
    class(x) = "spectra"
    return(x)                 # Should return a "spectra" object, not a list
}
Run Code Online (Sandbox Code Playgroud)

现在,这在我的开发环境中(当我正在调试包时)按预期工作.也就是说,如果y_old是一个spectra对象,我做的y_new = y_old[-1],y_new仍然是一个spectra对象.

但是,当我将项目编译为包并安装它时,子集操作符返回一个list而不是一个spectra …

overloading r devtools r-s3

7
推荐指数
1
解决办法
278
查看次数

使用标准库的概率密度函数?

能够使用它std <random>来生成不同概率分布的随机数很好......现在,有没有办法使用标准库计算给定分布及其参数的一组数字的概率?

我知道我可以为我自己的任何发行版编写概率密度和质量函数(参见下面的单个随机变量示例),但如果可以,我宁愿使用标准库.

long double exponential_pdf(long double x, long double rate) {

    if ( x < 0.0 ) {
        return 0.0;
    }
    if ( rate < 0.0 ) {
        return NOT_A_NUMBER;
    }
    auto    pdf = rate * exp( - rate * x);
    return  pdf;
}
Run Code Online (Sandbox Code Playgroud)

c++ boost stl c++11 probability-density

6
推荐指数
1
解决办法
6058
查看次数

使用命名空间不适用于定义?

我无法理解c ++名称空间.请考虑以下示例:

//distr.h

namespace bogus{
    extern const int x;
    extern const int y;
    double made_up_distr(unsigned param);
}
Run Code Online (Sandbox Code Playgroud)

现在如果我将变量定义为下面的cpp,那么编译就好了

//distr.cpp

#include "distr.h"
#include <cmath>

const int bogus::x = 10;   
const int bogus::y = 100;

double bogus::made_up_distr(unsigned param){
    auto pdf = (exp(param) / bogus::x) + bogus::y;
    return pdf;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试简单地引入bogus命名空间并使用它

//broken distr.cpp

#include "distr.h"
#include <cmath>

using namespace bogus;

const int x = 10;
const int y = 100;

double made_up_distr(unsigned param){
    auto pdf = (exp(param) / x) + y; …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces

4
推荐指数
1
解决办法
464
查看次数

标签 统计

c++ ×2

boost ×1

c++11 ×1

devtools ×1

namespaces ×1

overloading ×1

probability-density ×1

r ×1

r-s3 ×1

stl ×1