nano_exit

基礎的なことこそ、簡単な例が必要だと思うのです。

波動関数の節を数える

文字通り、波動関数の節を数える。

ndarrayの中で条件を満たす要素数を数える方法。
NumPy配列ndarrayの条件を満たす要素数をカウント | note.nkmk.me

import numpy as np

x_min, x_max, N = 0, 3*np.pi, 100
x = np.linspace( x_min, x_max, N )
wave = np.cos( x )
wave_previous = np.zeros( len( wave ) )
wave_previous[ 1 : ] = [ w for w in wave[ 0 : -1 ] ]
product = wave_previous * wave
node = np.sum( np.sign( product ) < 0 )  # 3

これをFortranで書けば、

implicit none
integer :: i, node
integer, parameter :: N = 100
real( kind( 0d0 ) ) :: x_min, x_max, previous, product_w
real( kind( 0d0 ) ) :: x( N ), wave( N )
real( kind( 0d0 ) ), parameter :: PI = 3.14159

x_min = 0.0d0
x_max = 3.0d0 * PI
do i = 1, N
  x( i ) = x_min + ( x_max - x_min ) / ( N - 1 ) * ( i - 1 )
enddo
wave( : ) = cos( x( : ) )

node = 0
previous = wave( 1 )
do i = 1, N
  product_w = previous * wave( i )
  if( sign( 1.0d0, product_w ) < 0.0d0 ) node = node + 1
  previous = wave( i )
enddo