From dcf1f324ce02812966d522ed3b5a06c6984934d6 Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Thu, 24 Jul 2025 17:31:48 +0800 Subject: [PATCH] update --- .../example_1/a.py | 2 + .../example_1/example.f90 | 3 ++ .../example_2/a.py | 46 +++++++++++++++++++ .../example_2/example.f90 | 17 +++++++ .../example_3/a.py | 31 +++++++++++++ .../example_3/example.f90 | 19 ++++++++ 6 files changed, 118 insertions(+) create mode 100644 2025.07.24_programming_with_python_and_fortran/example_1/a.py create mode 100644 2025.07.24_programming_with_python_and_fortran/example_1/example.f90 create mode 100644 2025.07.24_programming_with_python_and_fortran/example_2/a.py create mode 100644 2025.07.24_programming_with_python_and_fortran/example_2/example.f90 create mode 100644 2025.07.24_programming_with_python_and_fortran/example_3/a.py create mode 100644 2025.07.24_programming_with_python_and_fortran/example_3/example.f90 diff --git a/2025.07.24_programming_with_python_and_fortran/example_1/a.py b/2025.07.24_programming_with_python_and_fortran/example_1/a.py new file mode 100644 index 0000000..41f82fc --- /dev/null +++ b/2025.07.24_programming_with_python_and_fortran/example_1/a.py @@ -0,0 +1,2 @@ +import example +example.hello() \ No newline at end of file diff --git a/2025.07.24_programming_with_python_and_fortran/example_1/example.f90 b/2025.07.24_programming_with_python_and_fortran/example_1/example.f90 new file mode 100644 index 0000000..8d9dee9 --- /dev/null +++ b/2025.07.24_programming_with_python_and_fortran/example_1/example.f90 @@ -0,0 +1,3 @@ +subroutine hello() + print *, "Hello from Fortran!" +end subroutine hello \ No newline at end of file diff --git a/2025.07.24_programming_with_python_and_fortran/example_2/a.py b/2025.07.24_programming_with_python_and_fortran/example_2/a.py new file mode 100644 index 0000000..6557e2a --- /dev/null +++ b/2025.07.24_programming_with_python_and_fortran/example_2/a.py @@ -0,0 +1,46 @@ +import example + +matrix = [[1, 2], [3, 4]] +print(example.sum_matrix(matrix)) + +print('---') + +# 时间对比 + +import numpy as np +import time + +matrix = np.random.rand(5000, 5000) + +start = time.time() +print(example.sum_matrix(matrix)) +end = time.time() +print('Python + Fortran 循环求和时间:', end - start) + +start = time.time() +sum_result = 0 +for i0 in range(matrix.shape[0]): + for j0 in range(matrix.shape[1]): + sum_result += matrix[i0, j0] +print(sum_result) +end = time.time() +print('Python 循环求和时间:', end - start) + +from numba import jit +@jit +def numba_for_sum_matrix(matrix): + sum_result = 0 + for i0 in range(matrix.shape[0]): + for j0 in range(matrix.shape[1]): + sum_result += matrix[i0, j0] + return sum_result + +start = time.time() +print(numba_for_sum_matrix(matrix)) +end = time.time() +print('Python numba 循环求和时间:', end - start) + +start = time.time() +print(np.sum(matrix)) +end = time.time() +print('Python numpy.sum() 求和时间:', end - start) \ No newline at end of file diff --git a/2025.07.24_programming_with_python_and_fortran/example_2/example.f90 b/2025.07.24_programming_with_python_and_fortran/example_2/example.f90 new file mode 100644 index 0000000..9883054 --- /dev/null +++ b/2025.07.24_programming_with_python_and_fortran/example_2/example.f90 @@ -0,0 +1,17 @@ +subroutine sum_matrix(matrix, total) + implicit none + ! 输入参数 + real, intent(in) :: matrix(:,:) ! 假定形状数组 + real, intent(out) :: total ! 输出总和 + + ! 局部变量 + integer :: i, j + + ! 双重循环计算总和 + total = 0.0 + do j = 1, size(matrix, 2) ! 获取列数 + do i = 1, size(matrix, 1) ! 获取行数 + total = total + matrix(i, j) + end do + end do +end subroutine sum_matrix \ No newline at end of file diff --git a/2025.07.24_programming_with_python_and_fortran/example_3/a.py b/2025.07.24_programming_with_python_and_fortran/example_3/a.py new file mode 100644 index 0000000..f54db67 --- /dev/null +++ b/2025.07.24_programming_with_python_and_fortran/example_3/a.py @@ -0,0 +1,31 @@ +import numpy as np +import example + +A = [[1.0, 2.0, 3.0], + [0.0, 1.0, 4.0], + [5.0, 6.0, 0.0]] + +Ainv_1 = np.asfortranarray(np.empty_like(A)) +example.inverse_matrix(A, Ainv_1) # 调用 Fortran 子程序 +print(Ainv_1) + +Ainv_2 = np.linalg.inv(A) +print(Ainv_2) + +print('---') + +# 时间对比 + +A = np.random.rand(3000, 3000) + +import time +start = time.time() +Ainv_1 = np.asfortranarray(np.empty_like(A)) +example.inverse_matrix(A, Ainv_1) # 调用 Fortran 子程序 +end = time.time() +print('Python + Fortran 求逆时间:', end - start) + +start = time.time() +Ainv_2 = np.linalg.inv(A) +end = time.time() +print('Python np.linalg.inv() 求逆时间:', end - start) \ No newline at end of file diff --git a/2025.07.24_programming_with_python_and_fortran/example_3/example.f90 b/2025.07.24_programming_with_python_and_fortran/example_3/example.f90 new file mode 100644 index 0000000..5228891 --- /dev/null +++ b/2025.07.24_programming_with_python_and_fortran/example_3/example.f90 @@ -0,0 +1,19 @@ +! 矩阵求逆子程序 +subroutine inverse_matrix(A, Ainv) + implicit none + real(8), intent(in) :: A(:,:) ! 输入矩阵 + real(8), intent(inout) :: Ainv(:,:) ! 输出逆矩阵 + real(8), allocatable :: work(:) + integer, allocatable :: ipiv(:) + integer :: n, info + + n = size(A,1) + allocate(ipiv(n), work(n)) + + Ainv = A ! 复制输入矩阵 + + call dgetrf(n, n, Ainv, n, ipiv, info) ! LU 分解 + call dgetri(n, Ainv, n, ipiv, work, n, info) ! 计算逆矩阵 + + deallocate(ipiv, work) +end subroutine inverse_matrix \ No newline at end of file