1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| subroutine r_calc(x, nx, y, ny, r) implicit none integer :: nx, ny real(8) :: x(nx) real(8) :: y(ny) real(8) :: r !f2py intent(in),depend(nx) x !f2py intent(in),depend(ny) y !f2py intent(out) r
real(8) :: x_mean = 0, y_mean = 0 ! 用于保存计算中间值 real(8) :: cov_xy = 0 ! 用于保存计算中间值 real(8) :: sigma_x = 0, sigma_y = 0 ! 用于保存计算中间值 integer :: i do i = 1, 10000 x_mean = x_mean + x(i) y_mean = y_mean + y(i) enddo x_mean = x_mean / 10000 ! 计算x的均值 y_mean = y_mean / 10000 ! 计算y的均值 do i = 1, 10000 cov_xy = cov_xy + (x(i) - x_mean) * (y(i) - y_mean) sigma_x = sigma_x + (x(i) - x_mean) ** 2 sigma_y = sigma_y + (y(i) - y_mean) ** 2 enddo sigma_x = sigma_x ** 0.5 sigma_y = sigma_y ** 0.5 r = cov_xy / (sigma_x * sigma_y) end subroutine r_calc
|