*Fortran [#pbb06ff1] -[[Fortran Wiki:http://fortranwiki.org/]] --[[Fortran 2008 status:http://fortranwiki.org/fortran/show/Fortran+2008+status]] --[[Fortran 2003 status:http://fortranwiki.org/fortran/show/Fortran+2003+status]] -[[Fortran入門:http://www.nag-j.co.jp/fortran/]] -[[Fortran 2003入門:http://www.nag-j.co.jp/fortran/fortran2003/]] -[[インテル® Fortran Composer XE 2013 Linux* 版入門:http://nf.nci.org.au/facilities/software/intel-ct/13.5.192/Documentation/ja_JP/get_started_lf.htm]] -[[インテル® Fortran コンパイラー XE 13.0 ユーザー・リファレンス・ガイド:http://nf.nci.org.au/facilities/software/intel-ct/13.5.192/Documentation/ja_JP/compiler_f/main_for/index.htm]] -http://pic.dhe.ibm.com/infocenter/lnxpcomp/v121v141/index.jsp -[[Fortran2003のページ:http://www.geocities.jp/wjtcx143/fortran.html]] -[[Tagged "Fortran2003" | ドウジンテイスウ.log:http://sage-t.tumblr.com/tagged/Fortran2003]] -[[fortran66のブログ:http://fortran66.hatenablog.com/]] -[[Fortran日記:http://fortran.cscblog.jp/]] -[[なんとなく始めたブログ: fortran:http://sak12.blogspot.jp/search/label/fortran]] -[[Fortrantips:http://pmt.sakura.ne.jp/wiki/index.php?title=Fortrantips]] -https://github.com/OpenFortranProject -[[Fortran Archives - Moonmile Solutions Blog:http://www.moonmile.net/blog/archives/category/dev/fortran]] -http://qiita.com/tags/fortran -[[FortranからPythonへ:http://www.slideshare.net/shibukawa/fortranpython-presentation]] -[[2.2 Fortran90/95入門 後半:http://exp.cs.kobe-u.ac.jp/wiki/comp_practice/index.php?2.2%20Fortran90%2F95%C6%FE%CC%E7%20%B8%E5%C8%BE]] -[[Fortran90/95での引数の値渡し (Pass by value):http://sgks.blogspot.jp/2008/02/fortran9095-pass-by-value.html]] -http://gcc.gnu.org/onlinedocs/gfortran/Interoperable-Subroutines-and-Functions.html -http://software.intel.com/en-us/forums/topic/270769 -http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlf101a.doc%2Fxlflr%2Finterop-iso-c-binding-module.htm -http://be.nucl.ap.titech.ac.jp/~koba/cgi-bin/moin.cgi/FORTRAN -[[高速化プログラミング:http://fast-programming.aglk.net/]] -[[Fortran 覚書:http://www.gcxx.jp/memo/?fortran]] -[[Fortranスマートプログラミング:http://www.ile.osaka-u.ac.jp/research/cmp/text.html]] (PDF) -[[Fortran 90/95 入門①:http://www.hucc.hokudai.ac.jp/~a10019/kosyu/pdf2/Fortran_1.pdf]] (PDF) -[[Fortran 90/95 入門②:http://www.hucc.hokudai.ac.jp/~a10019/kosyu/pdf2/Fortran_2.pdf]] (PDF) -[[データ解析のためのFortran90/95:http://web.agr.ehime-u.ac.jp/~kishou/Lecture/atmosphere/atmo06.htm]] -[[Fortran90プログラミング:http://www7b.biglobe.ne.jp/~fortran/]] -[[Fortran エラー処理で文番号を使わない方法 :http://seismon.blog85.fc2.com/blog-entry-90.html]] -[[Fortran90で構造解析をしよう!:http://www.rcs.arch.t.u-tokyo.ac.jp/kusuhara/tips/linux/fortran.html]] **処理系 [#xfad745a] -[[GNU Fortran (gfortran):http://gcc.gnu.org/wiki/GFortran]] --[[Fortran2008Status - GCC Wiki:http://gcc.gnu.org/wiki/Fortran2008Status]] --[[Fortran2003Status - GCC Wiki:http://gcc.gnu.org/wiki/Fortran2003Status]] --http://gcc.gnu.org/onlinedocs/gfortran/Fortran-2003-and-2008-status.html --Windows では [[MinGW]], [[Cygwin]], [[Strawberry Perl:http://strawberryperl.com/]] などに収録されています. --OS X では [[High Performance Computing for Mac OS X:http://hpc.sourceforge.net/]] からバイナリが取得できます. -[[Intel Fortran Compiler (ifort):http://software.intel.com/en-us/fortran-compilers]] --http://software.intel.com/en-us/non-commercial-software-development (Linux, 非商用での使用のみ可) --[[Fortran (Intel Parallel Studio) のインストール:http://pen.agbi.tsukuba.ac.jp/~RStiger/hiki2/?Fortran+%28Intel+Parallel+Studio+XE%29+%A4%CE%A5%A4%A5%F3%A5%B9%A5%C8]] **Fortran 2008 の使い方 [#r3bbac60] ***使用可能な拡張子 [#d9d770b5] GNU Fortran の場合は拡張子に .f90, .f95, .f03, .f08, .F90, .F95, .F03, .F08 などが使用できます.~ GNU Fortran と Intel Fortran Compiler を両方使用してプログラムを作成する場合は拡張子は .f90 または .F90 を使用することをおすめします.~ |自由形式 (free form)|.f90, .f95, .f03, .f08, .F90, .F95, .F03, .F08| |固定形式 (fixed form)|.f, .for, .fpp, .ftn, .F, .FOR, .FPP, .FTN| -http://gcc.gnu.org/onlinedocs/gfortran/GNU-Fortran-and-GCC.html -http://nf.nci.org.au/facilities/software/intel-ct/13.5.192/Documentation/ja_JP/compiler_f/main_for/GUID-75F416C4-ADD7-41D0-AEFE-E61F50C04A82.htm ***標準出力に "Hello World" を出力 [#x79017be] ---- -helloworld.f90 ---- program hello_world implicit none print '(g0)', "Hello World" end program hello_world ---- program hello_world use, intrinsic :: iso_fortran_env implicit none write(unit=output_unit, fmt='(g0)') "Hello World" end program hello_world ---- module c_stdio use, intrinsic :: iso_c_binding implicit none interface subroutine c_puts(s) bind(c, name='puts') import character(kind=c_char), intent(in) :: s end subroutine c_puts end interface end module c_stdio program hello_world use, intrinsic :: iso_c_binding use c_stdio implicit none call c_puts("Hello World" // c_null_char) end program hello_world ---- GNU Fortran 4.8.1 で動作確認しています.~ データの出力は print 文や write 文で行います.~ C の puts 関数などを呼び出して出力することもできます.~ Fortran の文字列は null 終端文字列ではないので C の関数に文字列を渡す場合は c_null_char を末尾に追加して渡す必要があります.~ $ gfortran -std=f2008 -static -o helloworld helloworld.f90 $ ./helloworld MinGW の gfortran でビルドしたバイナリで -Windows PowerShell から実行するとなにも表示されない -コマンド プロンプトから実行するとアプリケーションエラーが発生する といった現象が発生する場合は $ gfortran -std=f2008 -static -o helloworld helloworld.f90 のように -static オプションを追加してビルドすれば OK です.~ ***標準出力に 1 から 100 までの和を出力 [#m969de65] ---- -sum.f90 ---- program sum_from_1_to_100 implicit none integer :: i print '(g0, g0)', "Sum from 1 to 100 = ", sum((/(i, i = 1, 100)/)) end program sum_from_1_to_100 ---- 配列要素の足し算をする場合は sum 関数を使用すると便利です.~ g0 編集記述子は print 文や write 文で整数や文字列などの任意の組み込み型に対して使用できます.~ * を使用して出力した場合は処理系によって出力フォーマットが異なる場合があります.~ 処理系によって出力フォーマットが異なるのを回避したい場合は * を使用せずに書式指定してください.~ ***コマンドライン引数から値 N を取得して標準出力に 1 から N までの和を出力 [#cd8aab45] ---- -sum.f90 ---- program sum_from_1_to_n implicit none integer :: i, j integer :: n character(len=3) :: arg_length_format character(len=256) :: arg if (command_argument_count() == 0) then call usage() stop end if do i = 1, command_argument_count() call get_command_argument(i, arg) write(unit=arg_length_format, fmt='(g0)') len_trim(arg) read(unit=arg, fmt='(i' // trim(arg_length_format) // ')') n if (n /= 0) then print '(g0, g0, g0, g0)', "Sum from 1 to ", n, " = ", sum((/(j, j = 1, n)/)) end if end do contains subroutine usage() use, intrinsic :: iso_fortran_env character(len=256) :: cmd call get_command(cmd) write(unit=error_unit, fmt='(g0, g0, g0)') "usage: ", trim(cmd), " number ..." end subroutine usage end program sum_from_1_to_n ---- module c_stdlib use, intrinsic :: iso_c_binding implicit none interface function c_atoi(s) result(i) bind(c, name='atoi') import character(kind=c_char), intent(in) :: s integer(kind=c_int) :: i end function c_atoi end interface end module c_stdlib program sum_from_1_to_n use, intrinsic :: iso_c_binding use c_stdlib implicit none integer :: i, j integer :: n character(len=256) :: arg if (command_argument_count() == 0) then call usage() stop end if do i = 1, command_argument_count() call get_command_argument(i, arg) n = c_atoi(trim(arg) // c_null_char) if (n /= 0) then print '(g0, g0, g0, g0)', "Sum from 1 to ", n, " = ", sum((/(j, j = 1, n)/)) end if end do contains subroutine usage() use, intrinsic :: iso_fortran_env character(len=256) :: cmd call get_command(cmd) write(unit=error_unit, fmt='(g0, g0, g0)') "usage: ", trim(cmd), " number ..." end subroutine usage end program sum_from_1_to_n ---- 文字列から数値への変換は read 文で行います.~ C の atoi 関数で文字列から数値へ変換することもできます.~ 数値から文字列への変換は write 文で行います.~ **Python や C などのプログラミング言語から Fortran を呼び出す [#kaea5749] -[[Calling Fortran from Python:http://www.sfu.ca/~mawerder/notes/calling_fortran_from_python.html]] -[[Calling Fortran from C:http://www.sfu.ca/~mawerder/notes/calling_fortran_from_c.html]] -[[f2pyを使ってfortranでpythonのモジュールを書く:http://qiita.com/airtoxin/items/b632f2b3f219610f3990]] -[[PythonからFortranのサブルーチンを呼ぶ。:http://d.hatena.ne.jp/ignisan/20121017/p1]]