| 1 | module math_utils |
| 2 | ! Simple math utilities for testing LSP features |
| 3 | ! |
| 4 | ! Test: |
| 5 | ! - Syntax highlighting (keywords, comments, strings) |
| 6 | ! - Go to Definition (F12 on subroutine calls) |
| 7 | ! - Find References (Shift+F12 on subroutine names) |
| 8 | ! - Document Symbols (Ctrl+Shift+O to see module structure) |
| 9 | ! - Formatting (Shift+Alt+F) |
| 10 | |
| 11 | implicit none |
| 12 | private |
| 13 | |
| 14 | public :: vector_add, vector_dot, vector_magnitude |
| 15 | public :: matrix_multiply |
| 16 | |
| 17 | contains |
| 18 | |
| 19 | subroutine vector_add(a, b, result, n) |
| 20 | ! Add two vectors element-wise |
| 21 | integer, intent(in) :: n |
| 22 | real, intent(in) :: a(n), b(n) |
| 23 | real, intent(out) :: result(n) |
| 24 | integer :: i |
| 25 | |
| 26 | do i = 1, n |
| 27 | result(i) = a(i) + b(i) |
| 28 | end do |
| 29 | end subroutine vector_add |
| 30 | |
| 31 | function vector_dot(a, b, n) result(dot_product) |
| 32 | ! Calculate dot product of two vectors |
| 33 | integer, intent(in) :: n |
| 34 | real, intent(in) :: a(n), b(n) |
| 35 | real :: dot_product |
| 36 | integer :: i |
| 37 | |
| 38 | dot_product = 0.0 |
| 39 | do i = 1, n |
| 40 | dot_product = dot_product + a(i) * b(i) |
| 41 | end do |
| 42 | end function vector_dot |
| 43 | |
| 44 | function vector_magnitude(vec, n) result(magnitude) |
| 45 | ! Calculate magnitude of a vector |
| 46 | integer, intent(in) :: n |
| 47 | real, intent(in) :: vec(n) |
| 48 | real :: magnitude |
| 49 | integer :: i |
| 50 | real :: sum_squares |
| 51 | |
| 52 | sum_squares = 0.0 |
| 53 | do i = 1, n |
| 54 | sum_squares = sum_squares + vec(i)**2 |
| 55 | end do |
| 56 | magnitude = sqrt(sum_squares) |
| 57 | end function vector_magnitude |
| 58 | |
| 59 | subroutine matrix_multiply(A, B, C, m, n, p) |
| 60 | ! Multiply two matrices: C = A * B |
| 61 | ! A is m x n, B is n x p, C is m x p |
| 62 | integer, intent(in) :: m, n, p |
| 63 | real, intent(in) :: A(m, n), B(n, p) |
| 64 | real, intent(out) :: C(m, p) |
| 65 | integer :: i, j, k |
| 66 | |
| 67 | ! Intentional error: missing initialization |
| 68 | do i = 1, m |
| 69 | do j = 1, p |
| 70 | ! ERROR: C(i,j) not initialized before use |
| 71 | do k = 1, n |
| 72 | C(i, j) = C(i, j) + A(i, k) * B(k, j) |
| 73 | end do |
| 74 | end do |
| 75 | end do |
| 76 | end subroutine matrix_multiply |
| 77 | |
| 78 | ! Intentional error function for diagnostics |
| 79 | subroutine broken_routine(x, y) |
| 80 | real, intent(in) :: x |
| 81 | real, intent(out) :: y |
| 82 | real :: temp |
| 83 | |
| 84 | temp = x * 2.0 |
| 85 | ! ERROR: using undefined variable |
| 86 | y = temp + undefined_var |
| 87 | end subroutine broken_routine |
| 88 | |
| 89 | end module math_utils |
| 90 |