fortran66のブログ

fortran について書きます。

分割をフロベニウス記法へ

分割のヤング図をフロベニウス記法へ

分割 [4,3,3,3,1,1] rank=3
フロベニウス記法 a=(3,1,0)、 b=(5,2,1)

■□□□
□■□
□□■
□□□

その逆も作りましたが、まだ詰め足りないです。途中ということでw(追記:少し直しました)

関数の総称名の作り方もよく分かりません。

実行結果

([3,1,0],[5,2,1])

ソースプログラム

main = print (frobenius [4,3,3,3,1,1]) 
--print (ftop ([3,1,0], [5,2,1]) ) 

type Partition = [Int]
type Frobenius = ([Int], [Int])

frobenius :: Partition -> Frobenius
frobenius xs = (a xs, b xs)
  where
    a :: Partition -> [Int]
    a = a' 1 
    
    a' :: Int -> Partition -> [Int]
    a' _ [] = [] 
    a' k (x:xs) = if (x-k) < 0 then [] 
                               else (x-k): a' (k+1) xs
                        
    b :: Partition -> [Int]                   
    b = a . conjugate 
    
conjugate :: Partition -> Partition
conjugate [] = []
conjugate xs = (length xs): conjugate (filter (>0) (map (\x -> x-1) xs))

conjugate'  :: Frobenius -> Frobenius
conjugate' (x,y) = (y,x) 


--Frobenius to Partition 
ftop :: Frobenius -> Partition
ftop xs = (atop 1 xs) ++ (btop xs)

atop :: Int -> Frobenius -> Partition
atop _ ([],[]) = []
atop i (a:as,b:bs) = (a+i): (atop (i+1) (as,bs) )   

btop :: Frobenius -> Partition
btop = conjugate . btop' 
  where
    btop' :: Frobenius -> Partition
    btop' ([],[]) = []
    btop' (a:as, b:bs) = if (b-length as) <= 0 then []
                       else (b-length as):(btop' (as, bs))  

Fortran 分割からフロベニウス記法 (逆は無し)

Fortran で考えた方が、考えをそのまま書けるので便利です。

実行結果

 3 1 0 : 5 2 1
続行するには何かキーを押してください . . .

ソースプログラム

    module m_young
      implicit none
    contains
      integer function irank(list)
        integer, intent(in) :: list(:)
        do irank = 1, size(list)
          if (list(irank) < irank) exit  
        end do    
        irank = irank -1
      end function irank
      
      function a(list) result(ires)
        integer, intent(in)  :: list(:)
        integer, allocatable :: ires(:)
        integer :: i, n
        n = irank(list)
        allocate(ires(n))
        do i = 1, n
          ires(i) = list(i) - i  
        end do 
      end function a
      
      function b(list) result(ires)
        integer, intent(in) :: list(:)
        integer, allocatable :: ires(:)
        ires = a( conj(list) )
      end function b
    
      function conj(list) result(ires)
        integer, value  :: list(:)
        integer, allocatable :: ires(:), l(:)
        integer :: i
        l = list
        allocate(ires(list(1)))
        do i = 1, list(1)
          ires(i) = size(l)
          l = pack(l - 1, l > 1) 
        end do
      end function conj  
    end module m_young
    
    program Console10
      use m_young
      implicit none
      integer :: i, j, n, nrank
      integer, allocatable :: ll(:)
      ll = [4,3,3,3,1,1]
      print *, a(ll), ':', b(ll)
    end program Console10

中共 Fortran NPO fcode.cn

先日、中共Fortran サイトを紹介しましたが、少しだけ中を見てみました。

2006年設立の NPO 法人によるもので、広告やPOPアップやhao123が出てくることも無く、比較的真っ当なサイトです。ただ資源コーナーには、相変わらずの電子書籍の違法コピーがうpられています。乞食の皆さんはレッツ・ゴー!

中国人(清華大学)による F95/2003 と銘打った資料(一応、違法ものではない模様?)をのぞいてみたところ、1行目からして

implicit real*8(a-z)

とあって、これで勉強する中共の学生さんも大変だなwと思いました。implicit real*8(a-h,o-z) は 77 時代にはよく使いましたが、その頃でも (a-z) は見たことがありません。団塊のお爺ちゃんたちが大喜びする大陸的おおらかさ(略

fortran66.hatenablog.com

教程の所は結構面白いプログラム例もあります。Big Endian は、『大端』というようです。一つ勉強になりました。

アラビヤ圏サイトへ行くと、いまだに MS-Fortran Power Station の違法コピーが出回っていますが、fcode.cn によると、MS-Fortran Power Station や DEC/Compaq Visual Fortran は、Windows7/8/10 に対応していないらしく Intel Fortran や gfortran に乗り換えるべしと、特別ページまで作られています。

MS-FPS や DVF/CVF はライセンス認証が単純だったので、まだ広く不法に流通しているようです。また中国では、MS-Windows が代替わりしているのに、アラビヤ圏ではまだ XP/Vista とか使っているのでしょう。MS-FPS/DVF5 は Fortran90、DVF6/CVF6.x は Fortran95 まで対応です。

fortran66.hatenablog.com
ここで見たように、昔のコンパイラでは10倍くらい計算が遅いのですが、それも大陸的おおらかさ/アラーの神の思し召し(以下略


中共も元々は今のイスラム国みたいなものだから、ISIS もこの先どうなるか分からないです。ISIS Fortran とかw「回教王に俺はなる!」

オンラインで、固定形式のコードを自由形式に変換してくれるページもあります。
quill.fcode.cn

分割数計算

暑いので、またぞろ分割数計算を。

fortran66.hatenablog.com

Haskell で書いて見ましたが、文法が頭に入っていないので面倒です。途中結果などをプリントできないのも困ります。

オイラーの制限付き分割の漸化式による方法を参考に書きます。

制限付き分割 p n k は、整数 n が分割される数ですが、分割したかけらの大きさに k による何らかの制限を置くものです。分割数は p n k の関数として得られます。

このような制限付き分割を経由することで、ヤング図のマスを書きながら色々な漸化式を作ることが出来ます。

分割の生成

ここでは制限付き分割 p n k で、k は分割した破片の最大数とする。横第一行と二行目以下の再帰的合体で漸化式を作る。

制限のない分割は p n n で得られる。

main = print (partition 6) 

partition n = p n n
  where    
    p 0 _ = [[]]
    p _ 0 = []
    p n k | n  < k = []
          | otherwise = concat [p' n i| i <-[1..k]]
          
    p' n i = map (i:) (p (n-i) (min i (n-i)))      

実行結果

[[1,1,1,1,1,1],[2,1,1,1,1],[2,2,1,1],[2,2,2],[3,1,1,1],[3,2,1],[3,3],[4,1,1],[4,2],[5,1],[6]]

分割数

main = print (map (length . partition) [1..20]) 

partition n = p n n
  where    
    p 0 _ = [[]]
    p _ 0 = []
    p n k | n  < k = []
          | otherwise = concat [p' n i| i <-[1..k]]
          
    p' n i = map (i:) (p (n-i) (min i (n-i)))    

実行結果

[1,2,3,5,7,11,15,22,30,42,56,77,101,135,176,231,297,385,490,627]

制限付き漸化式による分割数の計算

fortran66.hatenablog.com

その1

横第一行と二行目以下の再帰的合体。

main = print (map q [1..20])

q n = p n n
  where
    p 0 m = 1
    p n 1 = 1
    p n m | n < 0 = 0
          | m < 0 = 0
          | otherwise = p (n - m) m  +  p n (m-1)

その2

横最下行とそれより上の行の再帰的合体。

main = print (map q [1..20])

q n = p n 1
  where
    p 0 m = 0
    p n 0 = 0
    p n m | n  < m = 0
          | n == m = 1
          | otherwise = p (n-m) m + p n (m+1)

その3

縦第一列と二列目以降の再帰的合体。

main = print (map q [1..20])

q n = sum (map (p n) [1..n]) 
  where
    p 0 m = 0
    p n 0 = 0
    p n m | n  < m = 0
          | n == m = 1
          | otherwise = p (n-1) (m-1) + p (n-m) m

実行結果

[1,2,3,5,7,11,15,22,30,42,56,77,101,135,176,231,297,385,490,627]

オンライン・コンパイラFortran 無し)にて
https://repl.it


Fortran 2020(仮)に向けた調査投票開始さる!

Fortran 2020(仮)

昨日の書いた Fortran 2020(仮)に取り入れる機能についての web サーベイがすでに始まったようです。

先年 intel を定年退職になった Steve Lionel(a.k.a. Dr.Fortran) 氏が intel 掲示板に書き込みをしています。

software.intel.com

Fortran 規格グループのウェブページも、長年の NAG の間借りを脱して、独自ドメインのサイトを持ったとも書いてあります。

wg5-fortran.org

参考:
英NAG
ISO/IEC JTC1/SC22/WG5 (Fortran)
http://www.nag.co.uk/sc22wg5/docs.html


当該サイトへ行くと、Fortran 2020(仮)に向けた目安箱がでかでかと設置されています。

Help Shape the Future of Fortran!
The Fortran standards committee invites you to submit suggestions for features you would like to see in the next Fortran standard, tentatively titled Fortran 2020.

なお 目安箱ページに参りますと、以下のような質問が挙げられています。C の三項演算子?類似条件文・符号なし整数、ジェネリック、例外、データファイル読み込み時の配列/文字列サイズ自動割り付け、文字列の NULL 終端、ビット列、その他

たしかに符号なし整数やデータ読み込み時の自動割り付け等は、欲しかったことがあります。NULL 終端は地獄の始まりのような気がします。三項演算子ジェネリック・例外は、あると世間体はいいだろうけど、新たに引き起こす問題との損得からすると微妙な気もします。

f:id:fortran66:20170721015247p:plain

NASA and The Future of Fortran より

2015年の時点でのスライドですが、以下のようなものがありました。すでに Fortran 2020 の名が出ています。Steve Lionel 氏の資料を借りてきているようなので、情報の出どころは同じになりますが。

f:id:fortran66:20170721023429p:plain

PDF直リン
https://www.nas.nasa.gov/assets/pdf/ams/2015/AMS_20150428_Clune.pdf

考えられる機能として以下のようなものが挙げられています。
f:id:fortran66:20170721022952p:plain

次々期 Fortran 規格は Fortran 2020 ?

NAG のサイトの Fortran 規格の Working group のページに Fortran 次々期規格についての話が出ていました。西暦 2020 年に話が始まるようで、ケムール人もびっくりの Fortran 2020 となるのでしょうか。またしてもマイナーチェンジを目指すようです。

入れてほしい機能を目安箱よろしく WEB で広く募るようです。優先されるのは現今のコンパイラ構造や実行速度に不利にならないということのようで、いままで通りでブレがありません。

ISO/IEC JTC1/SC22/WG5 (Fortran)

12 July 2017
The next revision of the standard

The meeting also began thinking about the next revision. The intention is that this will be small with the technical content chosen in 2020. New features should be adopted only if they had long-term viability; those with only a minor benefit for users but a negative effect on compiler construction or performance should be avoided. There is a need to investigate what Fortran users want to do in the language and is not currently possible. It was agreed that a web survey of users would be instituted.

https://i.ytimg.com/vi/EOtl4uW5n-4/maxresdefault.jpg

私が思いついたのは、Python の如く左辺に [x, y] = f(a, b) みたいに配列構成子を置いてスカラー変数の組に受けたり、y = f(x) !! n みたいに配列を返す戻り値の n 番目の要素を取り出すような命令ですが、左辺をいじるのはコンパイラ構造に大きく影響しそうな気がします。また n 番目の値を返すのは自分で関数を書けばいい気がします。

あとは、以前書いた気がしますが assignment operator への overload で、 elementai な subroutine は代入による自動 allocation が出来なくなることが気になります。

ネット界隈では、関数を返り値とすることや、ジェネリックが欲しいというのを見たことがあった気がします。

最近のよさげな online compiler 2017 年版

coding ground

google drivedropbox 等のオンラインストレージに保存できるなど、いちばん洗練されていると思うのですが、時々休みになっていたりします。オプションを変えれば f2003 も行けます。

処理系は gfortran, gcc version 5.4.0 20160609

Free Online IDE and Terminal

ideone.com

古参で安定しています。
処理系は不明。コンパイル時のオプションもどうやって変えるのか不明です。

ideone.com

f:id:fortran66:20170713015851j:plain

rextester.com

あまり使ったことは無いのですが、柔軟な感じです。Fortran 利用者ランキングがビリから二番目で、ビリの brainfuck の2倍とかw

処理系は gfortran, gcc version 5.4.0 20160609

compile fortran online

JDOODLE

モッサリ感が漂うインターフェース。

処理系は gfortran version 5.3.0

www.jdoodle.com

Jupyter 上の coarray 環境

すぐコネクションが切れます。

Binder (beta)