|
| 1 | +! Vector norm: demonstrate usage of the function interface |
| 2 | +program example_get_norm |
| 3 | + use stdlib_linalg, only: get_norm, linalg_state_type |
| 4 | + implicit none |
| 5 | + |
| 6 | + real :: a(3,3),nrm,nrmd(3) |
| 7 | + integer :: j |
| 8 | + type(linalg_state_type) :: err |
| 9 | + |
| 10 | + ! a = [ -3.00000000 0.00000000 3.00000000 |
| 11 | + ! -2.00000000 1.00000000 4.00000000 |
| 12 | + ! -1.00000000 2.00000000 5.00000000 ] |
| 13 | + a = reshape([(j-4,j=1,9)], [3,3]) |
| 14 | + |
| 15 | + print "(' a = [ ',3(g0,3x),2(/9x,3(g0,3x)),']')", transpose(a) |
| 16 | + |
| 17 | + ! Norm with integer input |
| 18 | + call get_norm(a, nrm, 2) |
| 19 | + print *, 'Euclidean norm = ',nrm ! 8.30662346 |
| 20 | + |
| 21 | + ! Norm with character input |
| 22 | + call get_norm(a, nrm, '2') |
| 23 | + print *, 'Euclidean norm = ',nrm ! 8.30662346 |
| 24 | + |
| 25 | + ! Euclidean norm of row arrays, a(i,:) |
| 26 | + call get_norm(a, nrmd, 2, dim=2) |
| 27 | + print *, 'Rows norms = ',nrmd ! 4.24264050 4.58257580 5.47722578 |
| 28 | + |
| 29 | + ! Euclidean norm of columns arrays, a(:,i) |
| 30 | + call get_norm(a, nrmd, 2, dim=1) |
| 31 | + print *, 'Columns norms = ',nrmd ! 3.74165750 2.23606801 7.07106781 |
| 32 | + |
| 33 | + ! Infinity norms |
| 34 | + call get_norm(a, nrm, 'inf') |
| 35 | + print *, 'maxval(||a||) = ',nrm ! 5.00000000 |
| 36 | + |
| 37 | + call get_norm(a, nrmd, 'inf', dim=2) |
| 38 | + print *, 'maxval(||a(i,:)||) = ',nrmd ! 3.00000000 4.00000000 5.00000000 |
| 39 | + |
| 40 | + call get_norm(a, nrm, '-inf') |
| 41 | + print *, 'minval(||a||) = ',nrm ! 0.00000000 |
| 42 | + |
| 43 | + call get_norm(a, nrmd, '-inf', dim=1) |
| 44 | + print *, 'minval(||a(:,i)||) = ',nrmd ! 1.00000000 0.00000000 3.00000000 |
| 45 | + |
| 46 | + ! Catch Error: |
| 47 | + ! [norm] returned Value Error: dimension 4 is out of rank for shape(a)= [3 3] |
| 48 | + call get_norm(a, nrmd, 'inf', dim=4, err=err) |
| 49 | + print *, 'invalid: ',err%print() |
| 50 | + |
| 51 | +end program example_get_norm |
0 commit comments