File: example_pseudoinverse.f90

package info (click to toggle)
fortran-stdlib 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 34,008 kB
  • sloc: f90: 24,178; ansic: 1,244; cpp: 623; python: 119; makefile: 13
file content (30 lines) | stat: -rw-r--r-- 945 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
! Matrix pseudo-inversion example: function, subroutine, and operator interfaces
program example_pseudoinverse
  use stdlib_linalg, only: pinv, pseudoinvert, operator(.pinv.), linalg_state_type
  implicit none

  real :: A(15,5), Am1(5,15)
  type(linalg_state_type) :: state
  
  ! Generate random matrix A (15x15)
  call random_number(A)

  ! Pseudo-inverse: Function interfcae
  Am1 = pinv(A, err=state)
  print *, 'Max error (function)  : ', maxval(abs(A-matmul(A, matmul(Am1,A))))
  
  ! User threshold
  Am1 = pinv(A, rtol=0.001, err=state)
  print *, 'Max error (rtol=0.001): ', maxval(abs(A-matmul(A, matmul(Am1,A))))  

  ! Pseudo-inverse: Subroutine interface
  call pseudoinvert(A, Am1, err=state)

  print *, 'Max error (subroutine): ', maxval(abs(A-matmul(A, matmul(Am1,A))))

  ! Operator interface
  Am1 = .pinv.A
  
  print *, 'Max error (operator)  : ', maxval(abs(A-matmul(A, matmul(Am1,A))))

end program example_pseudoinverse