用C元素智能操作++ [英] Element-wise operations in C++

查看:127
本文介绍了用C元素智能操作++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有preexisting库,可以让我创建一​​个具有以下属性类似数组的对象:

Is there a preexisting library that will let me create array-like objects which have the following properties:


  1. 运行时间尺寸规格(在instantition选择,没有长大以后或缩小)

  2. 符重载执行元素方式操作(即 C = A + B 将导致一个vector C C [I] = A [I] + b [I] 所有 I ,同样为 * - / 等)

  3. 一套好的它作用的elementwise,例如 X =开方(VEC)将元素 X [I] =开方(VEC功能[I])

  4. 提供总结的功能,例如总和(VEC)平均值(VEC)

  5. (可选的)的操作可以被发送到用于处理的GPU。

  1. Run time size specification (chosen at instantition, not grown or shrunk afterwards)
  2. Operators overloaded to perform element wise operations (i.e. c=a+b will result in a vector c with c[i]=a[i]+b[i] for all i, and similarly for *, -, /, etc)
  3. A good set of functions which act elementwise, for example x=sqrt(vec) will have elements x[i]=sqrt(vec[i])
  4. Provide "summarising" functions such as sum(vec), mean(vec) etc
  5. (Optional) Operations can be sent to a GPU for processing.

基本上类似阵列Fortran中工作,与所有隐藏的执行方式。目前我使用矢量从STL和手动重载运营商,但我觉得这可能是一个解决的问题。

Basically something like the way arrays work in Fortran, with all of the implementation hidden. Currently I am using vector from the STL and manually overloading the operators, but I feel like this is probably a solved problem.

推荐答案

在标准库中,每个人都早已被人遗忘的角落尘土飞扬,坐在一类称为的valarray 。看看它,看看它是否适合你的需要。

In the dusty corners of standard library, long forgotten by everyone, sits a class called valarray. Look it up and see if it suits your needs.

从CP preference.com 手册页:

From manual page at cppreference.com:

的std :: valarray的重presenting和操作值数组类。它支持元素明智的数学运算和各种形式的推广下标运算符,切片和间接访问的。

std::valarray is the class for representing and manipulating arrays of values. It supports element-wise mathematical operations and various forms of generalized subscript operators, slicing and indirect access.

一个code片段加以说明:

A code snippet for illustration:

#include <valarray>
#include <algorithm>
#include <iterator>
#include <iostream>

int main()
{
    std::valarray<int> a { 1, 2, 3, 4, 5};
    std::valarray<int> b = a;
    std::valarray<int> c = a + b;
    std::copy(begin(c), end(c),
        std::ostream_iterator<int>(std::cout, " "));
}

输出: 2 4 6 8 10

这篇关于用C元素智能操作++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆