tips
Fortran
ベクトルの演算定義モジュール
使用例
(未)
モジュール
001 module vector_operation 002 type vector 003 real(8) :: x,y,z 004 end type vector 005 006 interface operator(+) 007 module procedure vector_plus_vector 008 end interface 009 interface operator(-) 010 module procedure vector_minus_vector 011 end interface 012 interface operator(*) 013 module procedure inner_product 014 end interface 015 interface operator(.x.) 016 module procedure outer_product 017 end interface 018 interface operator(*) 019 module procedure scaler_times_vector 020 end interface 021 interface operator(*) 022 module procedure vector_times_scaler 023 end interface 024 interface operator(-) 025 module procedure minus 026 end interface 027 interface operator(/) 028 module procedure vector_over_scaler 029 end interface 030 interface operator(/) 031 module procedure vector_over_vector 032 end interface 033 interface operator(.times.) 034 module procedure vector_times_vector 035 end interface 036 037 038 contains 039 ! Definition of summation operator for vector 040 function vector_plus_vector(a,b) result(c) 041 type(vector), intent(in) :: a,b 042 type(vector) :: c 043 c%x = a%x + b%x 044 c%y = a%y + b%y 045 c%z = a%z + b%z 046 end function vector_plus_vector 047 048 ! Definition of subtraction operator for vector 049 function vector_minus_vector(a,b) result(c) 050 type(vector), intent(in) :: a,b 051 type(vector) :: c 052 c%x = a%x - b%x 053 c%y = a%y - b%y 054 c%z = a%z - b%z 055 end function vector_minus_vector 056 057 ! Definition of inner product operator for vector 058 function inner_product(a,b) result(c) 059 type(vector), intent(in) :: a,b 060 real(8) :: c 061 c = (a%x * b%x) + (a%y * b%y) + (a%z * b%z) 062 end function inner_product 063 064 ! Definition of outer product operator for vector 065 function outer_product(a,b) result(c) 066 type(vector), intent(in) :: a,b 067 type(vector) :: c 068 c%x = (a%y * b%z) - (a%z * b%y) 069 c%y = (a%z * b%x) - (a%x * b%z) 070 c%z = (a%x * b%y) - (a%y * b%x) 071 end function outer_product 072 073 ! Definition of scaler times vector 074 function scaler_times_vector(a,b) result(c) 075 real(8), intent(in) :: a 076 type(vector), intent(in) :: b 077 type(vector) :: c 078 c%x = a * b%x 079 c%y = a * b%y 080 c%z = a * b%z 081 end function scaler_times_vector 082 083 ! Definition of vector times scaler 084 function vector_times_scaler(a,b) result(c) 085 type(vector), intent(in) :: a 086 real(8), intent(in) :: b 087 type(vector) :: c 088 c%x = a%x * b 089 c%y = a%y * b 090 c%z = a%z * b 091 end function vector_times_scaler 092 093 function minus(a) result(b) 094 type(vector), intent(in) :: a 095 type(vector) :: b 096 b%x = - a%x 097 b%y = - a%y 098 b%z = - a%z 099 end function minus 100 101 function vector_over_scaler(a,b) result(c) 102 type(vector), intent(in) :: a 103 real(8), intent(in) :: b 104 type(vector) :: c 105 c%x = a%x / b 106 c%y = a%y / b 107 c%z = a%z / b 108 end function vector_over_scaler 109 110 function vector_over_vector(a,b) result(c) 111 type(vector), intent(in) :: a, b 112 type(vector) :: c 113 c%x = a%x / b%x 114 c%y = a%y / b%y 115 c%z = a%z / b%z 116 end function vector_over_vector 117 118 function vector_times_vector(a,b) result(c) 119 type(vector), intent(in) :: a, b 120 type(vector) :: c 121 c%x = a%x * b%x 122 c%y = a%y * b%y 123 c%z = a%z * b%z 124 end function vector_times_vector 125 126 ! Function(s) for type(vector) 127 real(8) function abs_vec(a) 128 type(vector), intent(in) :: a 129 abs_vec = sqrt(a%x*a%x + a%y*a%y + a%z*a%z) 130 return 131 end function abs_vec 132 133 end module vector_operation
Keyword(s):
References: