简体   繁体   English

通过向量下标访问 Fortran 数组,cpp 等效

[英]Fortran array access via vector subscripts, cpp equivalent

I am wondering whether there is a cpp equivalent to accessing array locations in fortran via indexes stored in other arrays我想知道是否有一个 cpp 相当于通过存储在其他数组中的索引访问 fortran 中的数组位置

I am novice to cpp but experienced in oop fortran.我是 cpp 的新手,但对 oop fortran 有经验。 I am thinking about leaving fortran behind for the much better support of oop in recent cpp (oop in fortran is probably at the stage of year 2000 cpp).我正在考虑放弃 fortran,以便在最近的 cpp 中更好地支持 oop(fortran 中的 oop 可能处于 2000 cpp 的阶段)。

However, my applications are heavily geared towards linear algebra.但是,我的应用程序主要面向线性代数。 Contrarily to cpp, fortran has a lot of compiler built in support for this.与 cpp 不同的是,fortran 有很多内置的编译器对此提供支持。 But I would happily load libraries in cpp for gaining elaborate oop support.但是我很乐意在 cpp 中加载库以获得精心设计的 oop 支持。

But if the below construct is missing in cpp that would be really annoying.但是,如果 cpp 中缺少以下构造,那将非常烦人。

As I haven't found anything related yet I would appreciate if some experienced cpp programmer could comment.由于我还没有找到任何相关的内容,如果有经验丰富的 cpp 程序员可以发表评论,我将不胜感激。

An assignment to a 1D array location in fortan using a cascade of vector subscripts can be a complex as this:使用向量下标级联对 fortan 中的一维数组位置进行分配可能很复杂,如下所示:

iv1(ivcr(val(i,j)))=1

where iv1 is a 1D integer vector, ivcr is a 1D integer vector, val is a 2D integer array and i and j are scalars.其中 iv1 是一维整数向量,ivcr 是一维整数向量,val 是一个二维整数数组,i 和 j 是标量。 I am wondering whether I could write this in a similar compact form in cpp.我想知道我是否可以在 cpp 中以类似的紧凑形式编写它。

A only slightly more complex example would be:一个稍微复杂一点的例子是:

iv1(ivcr(val(i:j,j)))=1

which will fill a section in iv1 with "1".这将用“1”填充 iv1 中的一个部分。

How would cpp deal with that problem in the shortest possible way. cpp 如何以最短的方式处理这个问题。

Given (suitably initialized):给定(适当初始化):

std::vector<int> iv1, ivcr;
std::vector<std::vector<int>> val;

Then your iv1(ivcr(val(i,j)))=1 is simply那么你的iv1(ivcr(val(i,j)))=1就是

iv1[ivcr[val[i][j]]] = 1;

As for iv1(ivcr(val(i:j,j)))=1 , or just val(i:j, j) , there is no inbuilt way to slice into arrays like this.至于iv1(ivcr(val(i:j,j)))=1 ,或者只是val(i:j, j) ,没有内置的方法可以像这样切片成数组。 To be able to assign 1 to these kinds of nested datastructure accesses, you would need datastructures that provide expression templates.为了能够为这些类型的嵌套数据结构访问分配1 ,您需要提供表达式模板的数据结构。 The Eigen library has just that and is one of the major linear algebra libraries for C++. Eigen库就是这样,它是 C++ 的主要线性代数库之一。 Check out their documentation for indexing and slicing here:在此处查看有关索引和切片的文档:

https://eigen.tuxfamily.org/dox-devel/group__TutorialSlicingIndexing.html https://eigen.tuxfamily.org/dox-devel/group__TutorialSlicingIndexing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM