LoginSignup
1
0

More than 3 years have passed since last update.

Fortranのallocatableがわかりにくいと言われたので対応してみました。

Last updated at Posted at 2021-05-01

学生さんに、「FortranのAllocatableってよくわからない」と言われた

ので、Allocatableを書かずに行列計算できるようにしてみました。
また始めたばかりですのでおいおい追記します。

ソースコードは
https://github.com/kazulagi/plantFEM.git

ここの、src/ArrayClass/ArrayClass.f90 にあります。

program main
    use ArrayClass
    implicit none

    type(Array_) :: A, B, C, I

    print *, " "

    ! create new matrices
    call A%random(3,3) ! random matrix
    call B%zeros(3,3)  ! zero matrix
    call I%unit(3,3)   ! unit matrix

    C = A * B ! A_ij B_jk

    C = A + B ! A_ij + B_ij

    call A%print()
    call B%print()
    call C%print()
    call I%print()

    print *, dot(C, C)

end program

簡単な行列を作り、行列積、行列和を計算します。
手抜き実装なのですべてreal(real64)型です。

結果はこちらです。


  0.10888631199620769       0.37555478464439451       0.29852568070134511     
  0.47234444240835993       0.90576310861565290       0.37337424467292313     
  0.37658869302848197       0.43165157640659590       0.99078645087898243     

 size :: 3 x 3

   0.0000000000000000        0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000        0.0000000000000000     

 size :: 3 x 3

  0.10888631199620769       0.37555478464439451       0.29852568070134511     
  0.47234444240835993       0.90576310861565290       0.37337424467292313     
  0.37658869302848197       0.43165157640659590       0.99078645087898243     

 size :: 3 x 3

   1.0000000000000000        0.0000000000000000        0.0000000000000000     
   0.0000000000000000        1.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000        1.0000000000000000     

   2.7347395334124354     

暇なときに拡充していきます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0