|
| 1 | +# Linear Algebra |
| 2 | + |
| 3 | +* [`diag` - Create a diagonal array or extract the diagonal elements of an array](#diag---create-a-diagonal-array-or-extract-the-diagonal-elements-of-an-array) |
| 4 | +* [`eye` - Construct the identity matrix](#eye---construct-the-identity-matrix) |
| 5 | +* [`trace` - Trace of a matrix](#trace---trace-of-a-matrix) |
| 6 | + |
| 7 | +## `diag` - Create a diagonal array or extract the diagonal elements of an array |
| 8 | + |
| 9 | +### Description |
| 10 | + |
| 11 | +Create a diagonal array or extract the diagonal elements of an array |
| 12 | + |
| 13 | +### Syntax |
| 14 | + |
| 15 | +`d = diag(a [, k])` |
| 16 | + |
| 17 | +### Arguments |
| 18 | + |
| 19 | +`a`: Shall be a rank-1 or or rank-2 array. If `a` is a rank-1 array (i.e. a vector) then `diag` returns a rank-2 array with the elements of `a` on the diagonal. If `a` is a rank-2 array (i.e. a matrix) then `diag` returns a rank-1 array of the diagonal elements. |
| 20 | + |
| 21 | +`k` (optional): Shall be a scalar of type `integer` and specifies the diagonal. The default `k = 0` represents the main diagonal, `k > 0` are diagonals above the main diagonal, `k < 0` are diagonals below the main diagonal. |
| 22 | + |
| 23 | +### Return value |
| 24 | + |
| 25 | +Returns a diagonal array or a vector with the extracted diagonal elements. |
| 26 | + |
| 27 | +### Example |
| 28 | + |
| 29 | +```fortran |
| 30 | +program demo_diag1 |
| 31 | + use stdlib_experimental_linalg, only: diag |
| 32 | + implicit none |
| 33 | + real, allocatable :: A(:,:) |
| 34 | + integer :: i |
| 35 | + A = diag([(1,i=1,10)]) ! creates a 10 by 10 identity matrix |
| 36 | +end program demo_diag1 |
| 37 | +``` |
| 38 | + |
| 39 | +```fortran |
| 40 | +program demo_diag2 |
| 41 | + use stdlib_experimental_linalg, only: diag |
| 42 | + implicit none |
| 43 | + real :: v(:) |
| 44 | + real, allocatable :: A(:,:) |
| 45 | + integer :: i |
| 46 | + v = [1,2,3,4,5] |
| 47 | + A = diag(v) ! creates a 5 by 5 matrix with elements of v on the diagonal |
| 48 | +end program demo_diag2 |
| 49 | +``` |
| 50 | + |
| 51 | +```fortran |
| 52 | +program demo_diag3 |
| 53 | + use stdlib_experimental_linalg, only: diag |
| 54 | + implicit none |
| 55 | + integer, parameter :: n = 10 |
| 56 | + real :: c(n), ul(n-1) |
| 57 | + real :: A(n,n) |
| 58 | + integer :: i |
| 59 | + c = 2 |
| 60 | + ul = -1 |
| 61 | + A = diag(ul,-1) + diag(c) + diag(ul,1) ! Gil Strang's favorite matrix |
| 62 | +end program demo_diag3 |
| 63 | +``` |
| 64 | + |
| 65 | +```fortran |
| 66 | +program demo_diag4 |
| 67 | + use stdlib_experimental_linalg, only: diag |
| 68 | + implicit none |
| 69 | + integer, parameter :: n = 12 |
| 70 | + real :: A(n,n) |
| 71 | + real :: v(n) |
| 72 | + integer :: i |
| 73 | + call random_number(A) |
| 74 | + v = diag(A) ! v contains diagonal elements of A |
| 75 | +end program demo_diag4 |
| 76 | +``` |
| 77 | + |
| 78 | +```fortran |
| 79 | +program demo_diag5 |
| 80 | + use stdlib_experimental_linalg, only: diag |
| 81 | + implicit none |
| 82 | + integer, parameter :: n = 3 |
| 83 | + real :: A(n,n) |
| 84 | + real, allocatable :: v(:) |
| 85 | + integer :: i |
| 86 | + A = reshape([1,2,3,4,5,6,7,8,9],[n,n]) |
| 87 | + v = diag(A,-1) ! v is [2,6] |
| 88 | + v = diag(A,1) ! v is [4,8] |
| 89 | +end program demo_diag5 |
| 90 | +``` |
| 91 | + |
| 92 | +## `eye` - Construct the identity matrix |
| 93 | + |
| 94 | +### Description |
| 95 | + |
| 96 | +Construct the identity matrix |
| 97 | + |
| 98 | +## Syntax |
| 99 | + |
| 100 | +`I = eye(n)` |
| 101 | + |
| 102 | +### Arguments |
| 103 | + |
| 104 | +`n`: Shall be a scalar of default type `integer`. |
| 105 | + |
| 106 | +### Return value |
| 107 | + |
| 108 | +Returns the identity matrix, i.e. a square matrix with ones on the main diagonal and zeros elsewhere. The return value is of type `integer(int8)`. |
| 109 | + |
| 110 | +### Example |
| 111 | + |
| 112 | +```fortran |
| 113 | +program demo_eye1 |
| 114 | + use stdlib_experimental_linalg, only: eye |
| 115 | + implicit none |
| 116 | + real :: a(3,3) |
| 117 | + A = eye(3) |
| 118 | +end program demo_eye1 |
| 119 | +``` |
| 120 | + |
| 121 | +```fortran |
| 122 | +program demo_eye2 |
| 123 | + use stdlib_experimental_linalg, only: eye, diag |
| 124 | + implicit none |
| 125 | + print *, all(eye(4) == diag([1,1,1,1])) ! prints .true. |
| 126 | +end program demo_eye2 |
| 127 | +``` |
| 128 | + |
| 129 | +## `trace` - Trace of a matrix |
| 130 | + |
| 131 | +### Description |
| 132 | + |
| 133 | +Trace of a matrix (rank-2 array) |
| 134 | + |
| 135 | +### Syntax |
| 136 | + |
| 137 | +`result = trace(A)` |
| 138 | + |
| 139 | +### Arguments |
| 140 | + |
| 141 | +`A`: Shall be a rank-2 array. If `A` is not square, then `trace(A)` will return the sum of diagonal values from the square sub-section of `A`. |
| 142 | + |
| 143 | +### Return value |
| 144 | + |
| 145 | +Returns the trace of the matrix, i.e. the sum of diagonal elements. |
| 146 | + |
| 147 | +### Example |
| 148 | +```fortran |
| 149 | +program demo_trace |
| 150 | + use stdlib_experimental_linalg, only: trace |
| 151 | + implicit none |
| 152 | + real :: A(3,3) |
| 153 | + A = reshape([1,2,3,4,5,6,7,8,9],[3,3]) |
| 154 | + print *, trace(A) ! 1 + 5 + 9 |
| 155 | +end program demo_trace |
| 156 | +``` |
0 commit comments