fortrans等效语法与C
Fortrans equivalent syntax to C++
我正在处理文件。内容的格式如下:
name - problem name (A string value)
m - number or rows (int value)
n - number of columns (int value)
Ap - pointers to the begining of storage of column (size n+1)(an array of size (n+1) )
Ai - row indices for each non zero entry (input, nnz A)
Ax - non zero entries (input, nnz A)
b - right hand side (input, size m)(an double array of size m )
c - objective vector (minimize, size n) (an double array of size n) )
z0 - initial fixed value for objective (double value)
lobnd - lower bounds on variables (size n) (an double array of size n )
upbnd - upper bounds on variables (size n) (an double array of size n )
在fortran中读取此文件的语法如下:
Ap (j) = location of start of column j
Ai (Ap (j)) through Ai (Ap (j+1)-1) are the row indices in column j
Ax (Ap (j)) through Ax (Ap (j+1)-1) are the numerical values in column j
read(file,'(a8)') name
read(file,*) m,n
read(file,*) (ia(i),i=1,n+1)
read(file,*) (ja(i),i=1,ia(n+1)-1)
read(file,*) (a(i),i=1,ia(n+1)-1)
read(file,*) (b(i),i=1,m)
read(file,*) (c(i),i=1,n)
read(file,*) z0
read(file,*) (lobnd(i),i=1,n)
read(file,*) (upbnd(i),i=1,n)
我想知道C 中的相应语法。有人知道如何将此程序从Fortran转换为C ?这是文件的示例。
根据上述文件格式的描述
name = 'BLEND'
m = 74
n = 114
upbnd = I can see the n or 114 double values at the end of the file
lobnd = I can see the n or 114 double values before the values of upbnd
z0 = here I can see 0. is the value of z0
c = I can see n or 114 values before z0 in the file and understand this
b = I understand the right hand side and I can see the m or 74 values
Ai - I understand row indices for each non zero entry (input, nnz A)
Ax - non zero entries (input, nnz A)
Now I can not understand the following values in the file:
Ap = I can not understand what do these (n+1) or 115 integers mean
我想了解文件中的此AP值。预先感谢。
read(file,'(a8)') name
大致像scanf("%8s", name);
。
read(file,*) m,n
大致等于file >> m >> n;
read(file,*) (ia(i),i=1,n+1)
之类的线无疑是最棘手的。第一个逗号之后的部分是"隐含do循环"。这基本上意味着这大致相当于以下顺序:
for (int i=1; i<n+1; i++)
file >> ia[i];
我相信其余的只是上面显示的一个或另一个的重复。
但是,还有一个要记住的一点:fortran存储阵列按列主要顺序。C和C 存储阵列以行订单为单位。这意味着,当您穿越C或C 的数组时,通常需要行遍历它。除其他外,这将优化缓存使用情况,因为每行都连续存储在内存中。
fortran是列。这意味着每列在内存中是连续的,并且遍历数组的自然方法是一次。由于每列在内存中都连续,因此(当然((当然(优化了缓存的用法。
AP中的值包含每列开头的位置。那就是AP(1(是第一列中第一项的索引。AP(2(是第二列中第一项的索引,依此类推。如果您需要读取N th 列,则AP(N(将告诉您主数组中的位置,您开始阅读以获取该列的数据。由于AP(n 1(是N 1列的开头,因此N列中的最后一项是AP(n 1(-1。
因此,假设您将主要数据数组读取到平面(1D(数组中,我们将仅致电data
。要读取data
中的N th 列,您可以使用Ap
。例如,要打印出N th 列,我们可以写这样的代码:
void print_column(int n) {
for (int i=Ap[n]; i<Ap[n+1]; i++)
std::cout << data[i] << 't';
}
这使您可以避免进行动态分配2D数组的处理,而仅使用单个new
/malloc
/vector
保存数据,以及第二个数据以保存每列开始的索引。在C 中,很容易创建一个2D矩阵类,该类使operator()
超载以将2D地址用于vector
中存储的数据。您可以使用Ap
提供的额外间接级别,也可以使用乘法到达正确的位置。在当前处理器上,乘法可能比内存参考快,但是在较旧的处理器上,相对于内存访问,乘法通常要慢得多。
- 1d 智能指针不适用于语法 (*)++
- 助记符和指向成员语法的指针
- 有人能分解一下这个c++模板的语法吗
- C++避免重复声明的语法是什么
- QMetaObject invokeMethod的基于函数指针的语法
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 单独定义模板化嵌套类方法的正确语法
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 使用基类指针调用基类的值构造函数的语法是什么?
- 很好的语法来获取对向量/数组数据的大小引用?
- C++语法运算符功能?
- C++使用 rand 定义函数语法
- 什么文件可以修改 atom 的C++语法?
- 创建模板嵌套类实例的语法?
- C++语法差异:二维和一维数组(指针算术)
- fortrans等效语法与C