Fortran 入門——基本矩陣運算

來源:互聯網
上載者:User

學Fortran就是為瞭解決一些矩陣運算的問題,傳說它的運算非常快,對矩陣的支援也非常好.

敲了幾行代碼,實現了一些基本運算,果然很方便.

 1     program Console4
2 implicit none
3 parameter N=5
4 integer i,j
5 real array1(2,N),array2(N,2),array3(2,2)
6 real array4(2,3),array5(2,3),array6(2,3)
7 array1(1,1)=3.2;array1(1,2)=4.5;array1(1,3)=7.56;array1(1,4)=6.34;array1(1,5)=34.2;
8 array1(2,1)=44.3;array1(2,2)=12.3;array1(2,3)=1.236;array1(2,4)=12.2;array1(2,5)=9.5;
9 !對array1 求轉置矩陣
10 array2=transpose(array1)
11 call PrintArray(array1)
12 call PrintArray(array2)
13 !對array1和array2求乘積
14 array3=matmul(array1,array2)
15 call PrintArray(array3)
16 array4=3
17 array5=2
18 array6=array4+array5
19 call PrintArray(array6)
20 array6=array4-array5
21 call PrintArray(array6)
22 !array6= lincg(array5)
23 read *,i
24
25 end program Console4
26
27 subroutine PrintArray(array)
28 real array(:,:)
29 integer length,i
30 length=size(array,1)
31 do i=1,length
32 print *,array(i,:)
33 end do
34 print *,''
35 end subroutine

我定義了幾個矩陣,分別用它們做了矩陣的加,減,乘,求轉置運算,可以看到加法和減法直接調用運算子就行了,不需要重載.乘法和求轉置矩陣則可以直接調用內建函式.

另外我定義了負責在螢幕上輸入矩陣的函數PrintArray(array),將矩陣作為函數參數也是很方便的.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.