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++?
Here is an example of an file .
根据上面文件这里的文件格式说明
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)
这样的行无疑是最棘手的。第一个逗号之后的部分是 "implied DO loop"。这基本上意味着这大致相当于以下顺序的东西:
for (int i=1; i<n+1; i++)
file >> ia[i];
我相信其余的只是上面显示的其中一个或另一个的重复。
不过还有一点需要牢记:Fortran 以列主顺序存储数组。 C 和 C++ 以行优先顺序存储数组。这意味着当您在 C 或 C++ 中遍历数组时,您通常希望逐行遍历它。这将优化缓存使用,因为每一行都连续存储在内存中。
Fortran 是列专业的。这意味着每一列在内存中都是连续的,并且遍历数组的自然方式是一次一列。由于每一列在内存中都是连续的,这(当然)优化了缓存的使用。
Ap 中的值包含每列开始的位置。即 Ap(1) 是第一列中第一项的索引。 Ap(2) 是第二列第一项的索引,依此类推。如果您需要读取第 Nth 列,Ap(N) 会告诉您主数组中您开始读取的位置以获取该列的数据。由于 Ap(N+1) 是 N+1 列的开头,因此 N 列的最后一项位于 Ap(N+1)-1.
因此,假设您将主数据数组读入平面 (1D) 数组,我们称之为 data
。要阅读 data
中的第 N 列,您可以使用 Ap
。例如,要打印出第 Nth 列,我们可以这样编写代码:
void print_column(int n) {
for (int i=Ap[n]; i<Ap[n+1]; i++)
std::cout << data[i] << '\t';
}
这可以让您避免处理动态分配二维数组,而是仅使用单个 new
/malloc
/vector
来保存数据,以及第二个一个用于保存每列开头的索引。在 C++ 中,创建一个二维矩阵 class 非常容易,它重载 operator()
以对存储在 vector
中的数据进行二维寻址。您可以使用 Ap
提供的额外级别的间接寻址,也可以只使用乘法来到达正确的位置。在当前的处理器上,乘法可能比内存引用更快,但在较旧的处理器上,乘法通常比内存访问慢得多。
我正在处理一个文件。内容格式如下:
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++? Here is an example of an file .
根据上面文件这里的文件格式说明
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)
这样的行无疑是最棘手的。第一个逗号之后的部分是 "implied DO loop"。这基本上意味着这大致相当于以下顺序的东西:
for (int i=1; i<n+1; i++)
file >> ia[i];
我相信其余的只是上面显示的其中一个或另一个的重复。
不过还有一点需要牢记:Fortran 以列主顺序存储数组。 C 和 C++ 以行优先顺序存储数组。这意味着当您在 C 或 C++ 中遍历数组时,您通常希望逐行遍历它。这将优化缓存使用,因为每一行都连续存储在内存中。
Fortran 是列专业的。这意味着每一列在内存中都是连续的,并且遍历数组的自然方式是一次一列。由于每一列在内存中都是连续的,这(当然)优化了缓存的使用。
Ap 中的值包含每列开始的位置。即 Ap(1) 是第一列中第一项的索引。 Ap(2) 是第二列第一项的索引,依此类推。如果您需要读取第 Nth 列,Ap(N) 会告诉您主数组中您开始读取的位置以获取该列的数据。由于 Ap(N+1) 是 N+1 列的开头,因此 N 列的最后一项位于 Ap(N+1)-1.
因此,假设您将主数据数组读入平面 (1D) 数组,我们称之为 data
。要阅读 data
中的第 N 列,您可以使用 Ap
。例如,要打印出第 Nth 列,我们可以这样编写代码:
void print_column(int n) {
for (int i=Ap[n]; i<Ap[n+1]; i++)
std::cout << data[i] << '\t';
}
这可以让您避免处理动态分配二维数组,而是仅使用单个 new
/malloc
/vector
来保存数据,以及第二个一个用于保存每列开头的索引。在 C++ 中,创建一个二维矩阵 class 非常容易,它重载 operator()
以对存储在 vector
中的数据进行二维寻址。您可以使用 Ap
提供的额外级别的间接寻址,也可以只使用乘法来到达正确的位置。在当前的处理器上,乘法可能比内存引用更快,但在较旧的处理器上,乘法通常比内存访问慢得多。