返回介绍

Fortran

发布于 2025-02-25 23:44:00 字数 1218 浏览 0 评论 0 收藏 0

This is almost trivial with the Fortran Magic extnesion.

! pip install fortran-magic &> /dev/null
%load_ext fortranmagic
%%fortran

subroutine fib3(n, a)
    integer, intent(in) :: n
    real, intent(out) :: a

    integer :: i
    real :: b, tmp

    a = 0
    b = 1
    do i = 1, n
        tmp = b
        b = a
        a = a + tmp
    end do
end subroutine
fib3(100)
354224717716315439104.0000

Antoher example from the documentation

%%fortran --link lapack

subroutine solve(A, b, x, n)
    ! solve the matrix equation A*x=b using LAPACK
    implicit none

    real*8, dimension(n,n), intent(in) :: A
    real*8, dimension(n), intent(in) :: b
    real*8, dimension(n), intent(out) :: x

    integer :: pivot(n), ok

    integer, intent(in) :: n
    x = b

    ! find the solution using the LAPACK routine SGESV
    call DGESV(n, 1, A, n, pivot, x, n, ok)

end subroutine
A = np.array([[1, 2.5], [-3, 4]])
b = np.array([1, 2.5])

solve(A, b)
array([-0.1957,  0.4783])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文