- Fortran 教程
- Fortran - 主页
- Fortran - 概述
- Fortran - 环境设置
- Fortran - 基本语法
- Fortran - 数据类型
- Fortran - 变量
- Fortran - 常量
- Fortran - 运算符
- Fortran - 决策
- Fortran - 循环
- Fortran - 数字
- Fortran - 字符
- Fortran - 字符串
- Fortran - 数组
- Fortran - 动态数组
- Fortran - 派生数据类型
- Fortran - 指针
- Fortran - 基本输入输出
- Fortran - 文件输入输出
- Fortran - 过程
- Fortran - 模块
- Fortran - 内在函数
- Fortran - 数值精度
- Fortran - 程序库
- Fortran - 编程风格
- Fortran - 调试程序
- Fortran 资源
- Fortran - 快速指南
- Fortran - 有用的资源
- Fortran - 讨论
Fortran - 重塑函数
下表描述了重塑功能:
功能 | 描述 |
---|---|
重塑(源、形状、焊盘、顺序) | 它从给定数组源中的元素开始构造一个具有指定形状 shape 的数组。如果不包括焊盘,则源的尺寸必须至少为产品(形状)。如果包含 pad,则它必须与源具有相同的类型。如果包含 order ,则它必须是与 shape 具有相同形状的整数数组,并且值必须是 (1,2,3,...,n) 的排列,其中 n 是 shape 中的元素数量,它必须小于或等于 7。 |
例子
下面的例子演示了这个概念:
program arrayReshape implicit none interface subroutine write_matrix(a) real, dimension(:,:) :: a end subroutine write_matrix end interface real, dimension (1:9) :: b = (/ 21, 22, 23, 24, 25, 26, 27, 28, 29 /) real, dimension (1:3, 1:3) :: c, d, e real, dimension (1:4, 1:4) :: f, g, h integer, dimension (1:2) :: order1 = (/ 1, 2 /) integer, dimension (1:2) :: order2 = (/ 2, 1 /) real, dimension (1:16) :: pad1 = (/ -1, -2, -3, -4, -5, -6, -7, -8, & & -9, -10, -11, -12, -13, -14, -15, -16 /) c = reshape( b, (/ 3, 3 /) ) call write_matrix(c) d = reshape( b, (/ 3, 3 /), order = order1) call write_matrix(d) e = reshape( b, (/ 3, 3 /), order = order2) call write_matrix(e) f = reshape( b, (/ 4, 4 /), pad = pad1) call write_matrix(f) g = reshape( b, (/ 4, 4 /), pad = pad1, order = order1) call write_matrix(g) h = reshape( b, (/ 4, 4 /), pad = pad1, order = order2) call write_matrix(h) end program arrayReshape subroutine write_matrix(a) real, dimension(:,:) :: a write(*,*) do i = lbound(a,1), ubound(a,1) write(*,*) (a(i,j), j = lbound(a,2), ubound(a,2)) end do end subroutine write_matrix
当上面的代码被编译并执行时,会产生以下结果:
21.0000000 24.0000000 27.0000000 22.0000000 25.0000000 28.0000000 23.0000000 26.0000000 29.0000000 21.0000000 24.0000000 27.0000000 22.0000000 25.0000000 28.0000000 23.0000000 26.0000000 29.0000000 21.0000000 22.0000000 23.0000000 24.0000000 25.0000000 26.0000000 27.0000000 28.0000000 29.0000000 21.0000000 25.0000000 29.0000000 -4.00000000 22.0000000 26.0000000 -1.00000000 -5.00000000 23.0000000 27.0000000 -2.00000000 -6.00000000 24.0000000 28.0000000 -3.00000000 -7.00000000 21.0000000 25.0000000 29.0000000 -4.00000000 22.0000000 26.0000000 -1.00000000 -5.00000000 23.0000000 27.0000000 -2.00000000 -6.00000000 24.0000000 28.0000000 -3.00000000 -7.00000000 21.0000000 22.0000000 23.0000000 24.0000000 25.0000000 26.0000000 27.0000000 28.0000000 29.0000000 -1.00000000 -2.00000000 -3.00000000 -4.00000000 -5.00000000 -6.00000000 -7.00000000
fortran_arrays.htm