当前位置:   article > 正文

fortran77 初始化矩阵 打印矩阵 模版 备拷

fortran77 初始化矩阵 打印矩阵 模版 备拷

1,源码

  1. SUBROUTINE INIT_MATRIX(A, m, n, lda)
  2. DOUBLE PRECISION A(*)
  3. CALL SRAND(2024)
  4. DO i=1, m
  5. DO j=1, n
  6. A(i + lda*(j-1)) = RAND() + RAND()
  7. C WRITE(*, '(F8.4)') A(i)
  8. END DO
  9. END DO
  10. END
  11. SUBROUTINE PRINT_MATRIX(A, m, n, lda)
  12. DOUBLE PRECISION A(*)
  13. DO i=1, m
  14. DO j=1, n
  15. C WRITE(*, '(F8.4 , $)') A(i + lda*(j-1))
  16. WRITE(*, 20) A(i + lda*(j-1))
  17. 20 FORMAT (1X,F8.4, $)
  18. END DO
  19. PRINT *,''
  20. END DO
  21. END
  22. PROGRAM MATRIX_EX
  23. INTEGER m, n, lda
  24. DOUBLE PRECISION A(20)
  25. lda = 4
  26. m = 4
  27. n = 5
  28. CALL INIT_MATRIX(A, m, n, lda)
  29. PRINT *, "A ="
  30. CALL PRINT_MATRIX(A, m, n, lda)
  31. PRINT *, "A(11) ="
  32. PRINT *, A(11)
  33. END

 Makefile

  1. matrix_ex: matrix_ex.f
  2. gfortran -g $< -o $@
  3. .PHONY: clean
  4. clean:
  5. -rm matrix_ex

2,执行
 

  1. make
  2. ./matrix_ex

效果:

3, dgesvd example

  1. * Copyright (C) 2009-2015 Intel Corporation. All Rights Reserved.
  2. * The information and material ("Material") provided below is owned by Intel
  3. * Corporation or its suppliers or licensors, and title to such Material remains
  4. * with Intel Corporation or its suppliers or licensors. The Material contains
  5. * proprietary information of Intel or its suppliers and licensors. The Material
  6. * is protected by worldwide copyright laws and treaty provisions. No part of
  7. * the Material may be copied, reproduced, published, uploaded, posted,
  8. * transmitted, or distributed in any way without Intel's prior express written
  9. * permission. No license under any patent, copyright or other intellectual
  10. * property rights in the Material is granted to or conferred upon you, either
  11. * expressly, by implication, inducement, estoppel or otherwise. Any license
  12. * under such intellectual property rights must be express and approved by Intel
  13. * in writing.
  14. * =============================================================================
  15. *
  16. * DGESVD Example.
  17. * ==============
  18. *
  19. * Program computes the singular value decomposition of a general
  20. * rectangular matrix A:
  21. *
  22. * 8.79 9.93 9.83 5.45 3.16
  23. * 6.11 6.91 5.04 -0.27 7.98
  24. * -9.15 -7.93 4.86 4.85 3.01
  25. * 9.57 1.64 8.83 0.74 5.80
  26. * -3.49 4.02 9.80 10.00 4.27
  27. * 9.84 0.15 -8.99 -6.02 -5.31
  28. *
  29. * Description.
  30. * ============
  31. *
  32. * The routine computes the singular value decomposition (SVD) of a real
  33. * m-by-n matrix A, optionally computing the left and/or right singular
  34. * vectors. The SVD is written as
  35. *
  36. * A = U*SIGMA*VT
  37. *
  38. * where SIGMA is an m-by-n matrix which is zero except for its min(m,n)
  39. * diagonal elements, U is an m-by-m orthogonal matrix and VT (V transposed)
  40. * is an n-by-n orthogonal matrix. The diagonal elements of SIGMA
  41. * are the singular values of A; they are real and non-negative, and are
  42. * returned in descending order. The first min(m, n) columns of U and V are
  43. * the left and right singular vectors of A.
  44. *
  45. * Note that the routine returns VT, not V.
  46. *
  47. * Example Program Results.
  48. * ========================
  49. *
  50. * DGESVD Example Program Results
  51. *
  52. * Singular values
  53. * 27.47 22.64 8.56 5.99 2.01
  54. *
  55. * Left singular vectors (stored columnwise)
  56. * -0.59 0.26 0.36 0.31 0.23
  57. * -0.40 0.24 -0.22 -0.75 -0.36
  58. * -0.03 -0.60 -0.45 0.23 -0.31
  59. * -0.43 0.24 -0.69 0.33 0.16
  60. * -0.47 -0.35 0.39 0.16 -0.52
  61. * 0.29 0.58 -0.02 0.38 -0.65
  62. *
  63. * Right singular vectors (stored rowwise)
  64. * -0.25 -0.40 -0.69 -0.37 -0.41
  65. * 0.81 0.36 -0.25 -0.37 -0.10
  66. * -0.26 0.70 -0.22 0.39 -0.49
  67. * 0.40 -0.45 0.25 0.43 -0.62
  68. * -0.22 0.14 0.59 -0.63 -0.44
  69. * =============================================================================
  70. *
  71. * .. Parameters ..
  72. INTEGER M, N
  73. PARAMETER ( M = 6, N = 5 )
  74. INTEGER LDA, LDU, LDVT
  75. PARAMETER ( LDA = M, LDU = M, LDVT = N )
  76. INTEGER LWMAX
  77. PARAMETER ( LWMAX = 1000 )
  78. *
  79. * .. Local Scalars ..
  80. INTEGER INFO, LWORK
  81. *
  82. * .. Local Arrays ..
  83. DOUBLE PRECISION A( LDA, N ), U( LDU, M ), VT( LDVT, N ), S( N ),
  84. $ WORK( LWMAX )
  85. DATA A/
  86. $ 8.79, 6.11,-9.15, 9.57,-3.49, 9.84,
  87. $ 9.93, 6.91,-7.93, 1.64, 4.02, 0.15,
  88. $ 9.83, 5.04, 4.86, 8.83, 9.80,-8.99,
  89. $ 5.45,-0.27, 4.85, 0.74,10.00,-6.02,
  90. $ 3.16, 7.98, 3.01, 5.80, 4.27,-5.31
  91. $ /
  92. *
  93. * .. External Subroutines ..
  94. EXTERNAL DGESVD
  95. EXTERNAL PRINT_MATRIX
  96. *
  97. * .. Intrinsic Functions ..
  98. INTRINSIC INT, MIN
  99. *
  100. * .. Executable Statements ..
  101. WRITE(*,*)'DGESVD Example Program Results'
  102. *
  103. * Query the optimal workspace.
  104. *
  105. LWORK = -1
  106. CALL DGESVD( 'All', 'All', M, N, A, LDA, S, U, LDU, VT, LDVT,
  107. $ WORK, LWORK, INFO )
  108. LWORK = MIN( LWMAX, INT( WORK( 1 ) ) )
  109. *
  110. * Compute SVD.
  111. *
  112. CALL DGESVD( 'All', 'All', M, N, A, LDA, S, U, LDU, VT, LDVT,
  113. $ WORK, LWORK, INFO )
  114. *
  115. * Check for convergence.
  116. *
  117. IF( INFO.GT.0 ) THEN
  118. WRITE(*,*)'The algorithm computing SVD failed to converge.'
  119. STOP
  120. END IF
  121. *
  122. * Print singular values.
  123. *
  124. CALL PRINT_MATRIX( 'Singular values', 1, N, S, 1 )
  125. *
  126. * Print left singular vectors.
  127. *
  128. CALL PRINT_MATRIX( 'Left singular vectors (stored columnwise)',
  129. $ M, N, U, LDU )
  130. *
  131. * Print right singular vectors.
  132. *
  133. CALL PRINT_MATRIX( 'Right singular vectors (stored rowwise)',
  134. $ N, N, VT, LDVT )
  135. STOP
  136. END
  137. *
  138. * End of DGESVD Example.
  139. *
  140. * =============================================================================
  141. *
  142. * Auxiliary routine: printing a matrix.
  143. *
  144. SUBROUTINE PRINT_MATRIX( DESC, M, N, A, LDA )
  145. CHARACTER*(*) DESC
  146. INTEGER M, N, LDA
  147. DOUBLE PRECISION A( LDA, * )
  148. *
  149. INTEGER I, J
  150. *
  151. WRITE(*,*)
  152. WRITE(*,*) DESC
  153. DO I = 1, M
  154. WRITE(*,9998) ( A( I, J ), J = 1, N )
  155. END DO
  156. *
  157. 9998 FORMAT( 11(:,1X,F6.2) )
  158. RETURN
  159. END

Makefile

  1. svd_dgesve_ex: svd_dgesve_ex.f
  2. gfortran -g $< ../lapack-3.11/liblapack.a ../lapack-3.11/librefblas.a -o $@

运行

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/630440
推荐阅读
相关标签
  

闽ICP备14008679号