首页 > 其他 > 详细

矩阵求逆

时间:2019-01-15 11:06:04      阅读:148      评论:0      收藏:0      [点我收藏+]
// 转载自: http://fortranwiki.org/fortran/show/Matrix+inversion
// 这里仅适用于 小型矩阵 2*2,3*3,4*4 的矩阵求逆。事实上,对于这种规模的矩阵,直接写出求逆的结果速度更快。
// 这里3*3 的矩阵我已经验证过了,其余的未验证。一般矩阵的求逆方法原链接中也有。



pure function matinv2(A) result(B)
!! Performs a direct calculation of the inverse of a 2×2 matrix. complex(wp), intent(in) :: A(2,2) !! Matrix complex(wp) :: B(2,2) !! Inverse matrix complex(wp) :: detinv ! Calculate the inverse determinant of the matrix detinv = 1/(A(1,1)*A(2,2) - A(1,2)*A(2,1)) ! Calculate the inverse of the matrix B(1,1) = +detinv * A(2,2) B(2,1) = -detinv * A(2,1) B(1,2) = -detinv * A(1,2) B(2,2) = +detinv * A(1,1) end function pure function matinv3(A) result(B) !! Performs a direct calculation of the inverse of a 3×3 matrix. complex(wp), intent(in) :: A(3,3) !! Matrix complex(wp) :: B(3,3) !! Inverse matrix complex(wp) :: detinv ! Calculate the inverse determinant of the matrix detinv = 1/(A(1,1)*A(2,2)*A(3,3) - A(1,1)*A(2,3)*A(3,2)& - A(1,2)*A(2,1)*A(3,3) + A(1,2)*A(2,3)*A(3,1)& + A(1,3)*A(2,1)*A(3,2) - A(1,3)*A(2,2)*A(3,1)) ! Calculate the inverse of the matrix B(1,1) = +detinv * (A(2,2)*A(3,3) - A(2,3)*A(3,2)) B(2,1) = -detinv * (A(2,1)*A(3,3) - A(2,3)*A(3,1)) B(3,1) = +detinv * (A(2,1)*A(3,2) - A(2,2)*A(3,1)) B(1,2) = -detinv * (A(1,2)*A(3,3) - A(1,3)*A(3,2)) B(2,2) = +detinv * (A(1,1)*A(3,3) - A(1,3)*A(3,1)) B(3,2) = -detinv * (A(1,1)*A(3,2) - A(1,2)*A(3,1)) B(1,3) = +detinv * (A(1,2)*A(2,3) - A(1,3)*A(2,2)) B(2,3) = -detinv * (A(1,1)*A(2,3) - A(1,3)*A(2,1)) B(3,3) = +detinv * (A(1,1)*A(2,2) - A(1,2)*A(2,1)) end function pure function matinv4(A) result(B) !! Performs a direct calculation of the inverse of a 4×4 matrix. complex(wp), intent(in) :: A(4,4) !! Matrix complex(wp) :: B(4,4) !! Inverse matrix complex(wp) :: detinv ! Calculate the inverse determinant of the matrix detinv = & 1/(A(1,1)*(A(2,2)*(A(3,3)*A(4,4)-A(3,4)*A(4,3))+A(2,3)*(A(3,4)*A(4,2)-A(3,2)*A(4,4))+A(2,4)*(A(3,2)*A(4,3)-A(3,3)*A(4,2)))& - A(1,2)*(A(2,1)*(A(3,3)*A(4,4)-A(3,4)*A(4,3))+A(2,3)*(A(3,4)*A(4,1)-A(3,1)*A(4,4))+A(2,4)*(A(3,1)*A(4,3)-A(3,3)*A(4,1)))& + A(1,3)*(A(2,1)*(A(3,2)*A(4,4)-A(3,4)*A(4,2))+A(2,2)*(A(3,4)*A(4,1)-A(3,1)*A(4,4))+A(2,4)*(A(3,1)*A(4,2)-A(3,2)*A(4,1)))& - A(1,4)*(A(2,1)*(A(3,2)*A(4,3)-A(3,3)*A(4,2))+A(2,2)*(A(3,3)*A(4,1)-A(3,1)*A(4,3))+A(2,3)*(A(3,1)*A(4,2)-A(3,2)*A(4,1)))) ! Calculate the inverse of the matrix B(1,1) = detinv*(A(2,2)*(A(3,3)*A(4,4)-A(3,4)*A(4,3))+A(2,3)*(A(3,4)*A(4,2)-A(3,2)*A(4,4))+A(2,4)*(A(3,2)*A(4,3)-A(3,3)*A(4,2))) B(2,1) = detinv*(A(2,1)*(A(3,4)*A(4,3)-A(3,3)*A(4,4))+A(2,3)*(A(3,1)*A(4,4)-A(3,4)*A(4,1))+A(2,4)*(A(3,3)*A(4,1)-A(3,1)*A(4,3))) B(3,1) = detinv*(A(2,1)*(A(3,2)*A(4,4)-A(3,4)*A(4,2))+A(2,2)*(A(3,4)*A(4,1)-A(3,1)*A(4,4))+A(2,4)*(A(3,1)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(4,3))+A(1,2)*(A(2,1)*A(4,3)-A(2,3)*A(4,1))+A(1,3)*(A(2,2)*A(4,1)-A(2,1)*A(4,2))) B(1,4) = detinv*(A(1,2)*(A(2,4)*A(3,3)-A(2,3)*A(3,4))+A(1,3)*(A(2,2)*A(3,4)-A(2,4)*A(3,2))+A(1,4)*(A(2,3)*A(3,2)-A(2,2)*A(3,3))) B(2,4) = detinv*(A(1,1)*(A(2,3)*A(3,4)-A(2,4)*A(3,3))+A(1,3)*(A(2,4)*A(3,1)-A(2,1)*A(3,4))+A(1,4)*(A(2,1)*A(3,3)-A(2,3)*A(3,1))) B(3,4) = detinv*(A(1,1)*(A(2,4)*A(3,2)-A(2,2)*A(3,4))+A(1,2)*(A(2,1)*A(3,4)-A(2,4)*A(3,1))+A(1,4)*(A(2,2)*A(3,1)-A(2,1)*A(3,2))) B(4,4) = detinv*(A(1,1)*(A(2,2)*A(3,3)-A(2,3)*A(3,2))+A(1,2)*(A(2,3)*A(3,1)-A(2,1)*A(3,3))+A(1,3)*(A(2,1)*A(3,2)-A(2,2)*A(3,1))) end function

 

矩阵求逆

原文:https://www.cnblogs.com/cofludy/p/10270518.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!