如何创建一个接受 xexpression 或容器的函数?

问题描述

我对 xtensor 类型有点迷茫。我想创建一个可以接受表达式或容器的函数。我该怎么做?

即:

auto multbytwo(WHATGOESHERE x) {
    return x * 2;
}

xt::xtensor<double,2> a = whatever;
auto b = a + 3.0;
auto c = multbytwo(b);
// now c should NOT be a container,it should be an un-evaluated xexpression.

...就像我说的,如果它的参数是一个已计算的容器或一个“未计算的”xexpression,我希望 multbytwo 函数能够正常工作。甚至有可能吗?如果参数是表达式,我想将没有求值的表达式传递给临时表达式。我的理解是,如果 WHATEVER 是 xtensor 那么它将评估表达式,我不想要那样。

解决方法

每个表达式类型 Exarrayxfunction 等)都继承自 xexpression<E>。因此,您可以使 multbytwo 成为接受这种类型的模板函数,并在使用它之前向下转换参数:

template <class E>
auto multbytwo(const xt::xexpression<E>& x)
{
    return x.derived_cast() * 2;
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...