質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
FORTRAN

FORTRAN(フォートラン)は科学時術計算に向いた手続き型プログラミング言語です。 並列計算の最適化が行いやすい特性上、数値予報および気候モデルなどの大規模な計算を行う分野のスーパーコンピュータで使われています。

Q&A

解決済

1回答

428閲覧

subroutineの引数で受け取った配列の形を知りたい

mochmoch

総合スコア39

FORTRAN

FORTRAN(フォートラン)は科学時術計算に向いた手続き型プログラミング言語です。 並列計算の最適化が行いやすい特性上、数値予報および気候モデルなどの大規模な計算を行う分野のスーパーコンピュータで使われています。

0グッド

0クリップ

投稿2021/01/06 19:26

subroutineで受け取った配列の形を知りたい

fortran90でラプラシアンフィルタを実装しようと思い、以下のサブルーチンを作成しました。

fortran

1subroutine laplacian(img, output, width, height) 2 !!! laplacian filtering (8 neighborhood) 3 !!! input: 4 !!! img(integer, 2D): array. have pix value. 5 !!! output(integer, 2D): array for output. 6 !!! width(integer): image width. 7 !!! height(integer): image height. 8 9 implicit none 10 integer, dimension(width, height), intent(in) :: img 11 integer, dimension(width, height), intent(out) :: output 12 integer :: width, height 13 integer :: filter(3, 3) = reshape((/1, 1, 1, 1, -8, 1, 1, 1, 1/), shape(filter)) 14 integer :: w, h 15 16 do w = 1, width 17 do h = 1, height 18 if (w == 1 .or. w == width .or. h == 1 .or. h == height) then 19 output(h, w) = img(h, w) 20 else 21 output(h, w) = max(0, min(255, sum(img(h - 1:h + 1, w - 1:w + 1)*filter)), 0) 22 end if 23 end do 24 end do 25end subroutine laplacian

引数としてimgのサイズを渡しているのですが、この部分をなくしたいです。
イメージとしては以下のようになります。

fortran

1subroutine laplacian(img, output) 2 !!! laplacian filtering (8 neighborhood) 3 !!! input: 4 !!! img(integer, 2D): array. have pix value. 5 !!! output(integer, 2D): array for output. 6 7 implicit none 8 integer, ?? :: img ! 9 integer, dimension(shape(img)) :: output ! 10 11 integer :: width, height = shape(img) 12 integer :: filter(3, 3) = reshape((/1, 1, 1, 1, -8, 1, 1, 1, 1/), shape(filter)) 13 integer :: w, h 14 15 do w = 1, width 16 do h = 1, height 17 if (w == 1 .or. w == width .or. h == 1 .or. h == height) then 18 output(h, w) = img(h, w) 19 else 20 output(h, w) = max(0, min(255, sum(img(h - 1:h + 1, w - 1:w + 1)*filter)), 0) 21 end if 22 end do 23 end do 24end subroutine laplacian

試してみたこと

  • imgの宣言をallocatableにした状態でshape(img)をする
    • 受け取ったimgとは違う形状が出力されました

環境

Manharo Linux
コンパイラ: gfortran

どのように書けば所望の動作をするでしょうか?
ご教授よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

moduleを使うことで解決できました

fortran

1module filtering 2 implicit none 3 4contains 5 subroutine laplacian(img, output, depth) 6 !!! laplacian filtering (8 neighborhood) 7 !!! input: 8 !!! img(integer, 2D): array. have pix value. 9 !!! output(integer, 2D): array for output. 10 !!! width(integer): image width. 11 !!! height(integer): image height. 12 13 implicit none 14 integer, intent(in), dimension(:, :) :: img 15 integer, intent(inout), dimension(:, :) :: output 16 integer :: depth 17 integer :: filter(3, 3) = reshape((/1, 1, 1, 1, -8, 1, 1, 1, 1/), shape(filter)) 18 integer, dimension(2) :: img_shape 19 integer :: w, h, width, height 20 21 img_shape = shape(img) 22 height = img_shape(1) 23 width = img_shape(2) 24 25 do h = 1, height 26 do w = 1, width 27 if (w == 1 .or. w == width .or. h == 1 .or. h == height) then 28 output(h, w) = img(h, w) 29 else 30 output(h, w) = max(0, min(depth, sum(img(h - 1:h + 1, w - 1:w + 1)*filter)), 0) 31 end if 32 end do 33 end do 34 end subroutine laplacian 35 36end module filtering

この状態で呼び出し元でuse filteringimplicit none行より前に書くことで所望の動作ができました。

投稿2021/01/07 05:49

mochmoch

総合スコア39

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問