- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将一个R函数翻译成Rcpp,一个简单的测试代码如下,但我不知道如何处理默认设置为NULL的参数。
test<- function(t=NULL,tmax=NULL,tmin=NULL){
if(is.null(t)){
yout=(tmax-tmin)*(tmin+tmax)
}else{
yout=2*t
}
return(yout)
}
test(tmax=1:3,tmin=0:2)
// [[Rcpp::export]]
NumericVector cpptest(Rcpp::Nullable<Rcpp::NumericVector> t=R_NilValue,
Rcpp::Nullable<Rcpp::NumericVector> tmax=R_NilValue,
Rcpp::Nullable<Rcpp::NumericVector> tmin=R_NilValue){
int N=0;
if(t.isNotNull()) {
N=t.size(); /* which show a error*/
}else{
N=tmax.size(); /* which show a error*/
}
NumericVector yout=NumericVector(N);
if(t.isNotNull()) {
for(i=0;i<N,i++){
yout[i]=2*t[i]
}
}else{
for(i=0;i<N,i++){
yout[i]=(tmax[i]-tmin[i])*(tmin[i]+tmax[i])
}
}
return(yout)
}
最佳答案
而不是调用,例如.size()
直接在对象上,就像您在这里所做的那样 - N = t.size();
-- 您需要将其转换为基础类型。例如,
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int nullable_size(Nullable<NumericVector> x_ = R_NilValue)
{
if (x_.isNotNull()) {
NumericVector x(x_.get());
return x.size();
}
warning("argument x_ is NULL");
return -1;
}
/*** R
nullable_size(rnorm(5))
# [1] 5
nullable_size(NULL)
# [1] -1
# Warning message:
# In .Primitive(".Call")(<pointer: 0x000000006aa417a0>, x_) :
# argument x_ is NULL
*/
正如 Dirk 所指出的,使用 .get()
这里并不是绝对必要的——使用 NumericVector x(x_);
将调用 Nullable<>::operator SEXP()
并且工作同样出色。
此外,请以后更好地格式化您的代码。
关于Rcpp:如何获取 Rcpp::Nullable NumericVector 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43388698/
因为对这个问题的第一条评论可能是为什么以及为什么可能使这不是一个重复的问题:我有一个具有三个有效状态的值类型变量(如果重要的话是小数类型):has value | null | unspecified
Nullable 的定义是: [SerializableAttribute] public struct Nullable where T : struct, new() 约束where T : st
以下代码拒绝编译: Nullable!(Nullable!int) nni = Nullable!(Nullable!int)(10); 出现此错误消息: Error: inout method nu
Not Nullable 类型转换为 Nullable 类型的基础知识是什么? CLR 内部发生了什么? 值类型是否在内部转换为引用类型? int i = 100; and int ? i = 7?
我在寻找答案时得到的结果是指从 short 到 int 以及从可空到不可空的转换。但是,我无法理解如何将“较大”类型 int? 转换为“较小”类型 short?。 我能想到的唯一方法就是写一个这样的方
我们在契约(Contract)中使用 Swagger。考虑这个简单的响应 DTO public class Result { public int SomeInt { get;set; } }
有一个Nullable结构,还有另一个静态 Nullable具有三个静态方法的类。 我的问题是,为什么static Nullable中的这些静态方法不能?类进入Nullable结构?将它们定义为两种不
我错误地发现了一些让我吃惊的东西。 我有这个方法 public static string PrintDecimal(decimal? input, string NumberFormat = nul
此代码编译: private static void Main(string[] args) { bool? fred = true; if (fred == true)
希望标题已经很清楚了。 我想看一个更改表语句的示例,该语句可以将 Sybase 表中现有的不可为空的列更改为可以为空。 最佳答案 Modifying the NULL default value of
我使用 C# 8 可空引用类型。 我有一个泛型类,它可能接受可为空引用类型作为类型参数。 有没有办法根据泛型类型参数声明不可为空的类型,这些参数可能是可为空的引用类型(甚至是 Nullable 结构)
考虑以下代码: Nullable dt; dt. dt?. . 如何以及为什么? 最佳答案 因为如果 ?. 左侧的对象为 null,则 null 传播的工作方式永远不会执行右侧的对象。因为您知道右
我一直使用 Nullable<>.HasValue因为我喜欢语义。然而,最近我正在研究其他人现有的代码库,他们在其中使用了 Nullable<> != null。专门代替。 是否有理由使用一个而不是另
我对转换方法“.ToString()”有一个普遍的疑问。起初我使用这个语句进行转换: Nullable SomeProperty; string test = SomeProperty.ToStrin
有没有一种方法可以表达两种相关类型(一种具有可空属性,一种不具有),以便您可以在运行时进行检查后将一种强制转换为另一种? 例如 - type Stat = { count: ?number, }
对于 EF 5.0.0、VS 2012 和 .NET 4.5,当我从现有 SQL Server 2012 数据库添加新的 ADO.NET 实体数据模型时,生成的代码不会区分可空和不可空 varchar
我是 PySpark 的新手,正面临一个奇怪的问题。我试图在加载 CSV 数据集时将某些列设置为不可空。我可以使用非常小的数据集 (test.csv) 重现我的案例: col1,col2,col3 1
假设我有以下类(class): public class GenericClass { public T Find() { //return T if found ot
这个问题在这里已经有了答案: How is the boxing/unboxing behavior of Nullable possible? (3 个答案) 关闭 7 年前。 为什么 null可
Firebase 3.6.0 中的警告。 Xcode 8 - Swift 3。 这些是 Firebase 类:- @class FIROptions @class FIRAuthCredential
我是一名优秀的程序员,十分优秀!