跳轉到內容

Fortran/Fortran 變數

75% developed
來自華夏公益教科書,開放的書籍,開放的世界

在程式設計中,變數是程式可以更改的資料容器。 您通常會在使用變數之前宣告它們,以提供有關它們應該儲存何種資料的的資訊。 但是,Fortran 允許隱式建立變數。 在沒有implicit語句的情況下,未宣告的變數和以i/In/N(“in”組)開頭的引數將是integer,所有其他未宣告的變數和引數將是real

許多人認為在沒有宣告的情況下使用變數是一種不好的做法。 如果你想被迫宣告變數,請首先編寫implicit none

一般示例

[編輯 | 編輯原始碼]

以下列出了常用變數的示例

! Declare a constant, whose value cannot be changed.
integer, parameter :: num_days_week = 7
! Declare i as an integer, j as an array of 2 integers from j(1) to j(2), k as
! an array of 2 integers from '''k(0)''' to k(1), and m as a 2-dimensional
! array of 12 elements.
integer :: i, j(2), k(0:1), m(3,4)
! Declare c as an array of 4 floating point numbers from c(0) to c(3).
real :: c(0:3)
! Declare word as a string of length 5
character (len=5) :: word
! Declare a boolean variable with values .TRUE. or .FALSE.
logical :: tf

以下程式碼執行完全相同的功能,但採用更短、更古老的形式

INTEGER, PARAMETER :: num_days_week = 7
DIMENSION j(2), k(0:1), m(3,4), c(0:3)
CHARACTER*5 word
LOGICAL tf

如果您考慮記憶體佈局,請注意 m(1,1) 在記憶體中緊隨其後的是 m(2,1),而不是 m(1,2)。

可以透過將變數放在等號之前來設定變數,等號後面是設定的值。 鑑於上述宣告,以下賦值是可能的

i    = 3*4                  ! Set i to 3*4 = 12         
j    = [1, 4]               ! Set j(1) to 1, j(2) to 4
c    = [1.0, 4.0, 5.0, 9.0] ! Set c(0) to 1.0, c(1) to 4.0, c(2) to 5.0, c(3) to 9.0
word = 'dog'                ! Set word = "dog  " . The variable word is padded with spaces on the right
tf   = .true.               ! Set tf to True

變數可以出現在賦值的等號兩側。 右側首先被評估,然後變數被賦值給該值

i = 3     ! i has value 3
i = i**i  ! i has value 3**3 = 27

變數可以從一種型別轉換為另一種型別,但與 C++ 或 Java 不同,您不會在其中對變數進行型別轉換,在 Fortran 中,您使用內在過程

real          :: r = 1.5
real (kind=8) :: d = 1.5
integer       :: i = 1

print *, dble(r), dble(d), dble(i)   ! Convert number to a double precision
print *, real(r), real(d), real(i)   ! Convert number to a single precision (REAL)
print *, int(r), int(d), int(i)      ! Convert number to an integer

同樣,使用更簡單、更古老的形式,可以實現相同的功能

 DOUBLE PRECISION d = 1.5
 r = 1.5
 i = 1
 PRINT *, DBLE(r), DBLE(d), DBLE(i)
 PRINT *, REAL(r), REAL(d), REAL(i)
 PRINT *, INT(r), INT(d), INT(i)

可以使用兩種不同的表示法來宣告陣列。 以下示例說明了長度為 5 的integer型別陣列的表示法。

integer, dimension (5) :: arr1
integer                :: arr2(5)

對於多維陣列,需要指定每個維度的長度。 以下示例重點介紹了 5x6 整數矩陣(也稱為長度為 (5,6) 的二維陣列)的情況。(同樣,顯示兩種表示法。)

integer, dimension (5,6) :: arr1
integer                  :: arr2(5,6)

初始化

[編輯 | 編輯原始碼]

要使用實際值初始化陣列,有多種選擇:設定特定元素、特定範圍或整個陣列。

integer :: arr(3)

arr(1)   = 4            ! set specific element
arr(1:2) = [4, 5]       ! set a range aka slicing notation
arr      = [4, 5, 6]    ! set whole array

要設定多維陣列,需要使用reshapeshape命令。

integer :: arr(2,3)

arr = reshape([1,2,3,4,5,6], shape(arr))
! arr = reshape([1,2,3,4,5,6], shape=[2,1])  ! same effect as above command - hardcode the shape of arr

! arr represents matrix:
! 1 3 5
! 2 4 6

Fortran 使用列優先排序,因此上面的示例生成一個經常令人困惑的矩陣。 對於行優先排序,可以使用以下示例,其中重點介紹了使用 order 引數來指定首先排序的維度。

integer :: arr(2,3)

arr = reshape([1,2,3,4,5,6], shape(arr), order=[2,1])

! arr represents matrix:
! 1 2 3
! 4 5 6


華夏公益教科書