Skip to content

Commit c4433cf

Browse files
committed
add examples
1 parent 98b9b55 commit c4433cf

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

example/linalg/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ ADD_EXAMPLE(blas_gemv)
2828
ADD_EXAMPLE(lapack_getrf)
2929
ADD_EXAMPLE(lstsq1)
3030
ADD_EXAMPLE(lstsq2)
31+
ADD_EXAMPLE(norm)
32+
ADD_EXAMPLE(get_norm)
3133
ADD_EXAMPLE(solve1)
3234
ADD_EXAMPLE(solve2)
3335
ADD_EXAMPLE(solve3)

example/linalg/example_get_norm.f90

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

example/linalg/example_norm.f90

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
! Vector norm: demonstrate usage of the function interface
2+
program example_norm
3+
use stdlib_linalg, only: norm, linalg_state_type
4+
implicit none
5+
6+
real :: a(3,3),na
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+
print *, 'Euclidean norm = ',norm(a, 2) ! 8.30662346
19+
20+
! Norm with character input
21+
print *, 'Euclidean norm = ',norm(a, '2') ! 8.30662346
22+
23+
! Euclidean norm of row arrays, a(i,:)
24+
print *, 'Rows norms = ',norm(a, 2, dim=2) ! 4.24264050 4.58257580 5.47722578
25+
26+
! Euclidean norm of columns arrays, a(:,i)
27+
print *, 'Columns norms = ',norm(a, 2, dim=1) ! 3.74165750 2.23606801 7.07106781
28+
29+
! Infinity norms
30+
print *, 'maxval(||a||) = ',norm(a, 'inf') ! 5.00000000
31+
print *, 'maxval(||a(i,:)||) = ',norm(a, 'inf', dim=2) ! 3.00000000 4.00000000 5.00000000
32+
print *, 'minval(||a||) = ',norm(a, '-inf') ! 0.00000000
33+
print *, 'minval(||a(:,i)||) = ',norm(a, '-inf', dim=1) ! 1.00000000 0.00000000 3.00000000
34+
35+
! Catch Error:
36+
! [norm] returned Value Error: dimension 4 is out of rank for shape(a)= [3 3]
37+
print *, 'invalid: ',norm(a,'inf', dim=4, err=err)
38+
print *, 'error = ',err%print()
39+
40+
end program example_norm

0 commit comments

Comments
 (0)