Compute the eigenvalues of v ⊗ v - vector

I need to compute the eigenvalues of a symmetric matrix
M = v ⊗ v,
where v is a known, one dimensional, array (a vector) and ⊗ denotes the outer product.
I currently compute v ⊗ v explicitly via a self defined function and then call the lapack routine dsyev to get the desired eigenvalues.
Is there a more efficient method to calculate those eigenvalues (maybe directly from v?) using lapack, blas or a self written algorithm in fortran 2003?

It is shown on the sister maths site that
The eigenvalues for this problem are all zero, except one which has value the length squared of v
The eigenvector corresponding to the unique value is the (normalized) vector itself
As a result of 1, 2 and the symmetry of the matrix the N-1 eigenvectors with eigenvalue zero span a space orthogonal to the original vector
Below is a naive implementation of the method. I use a simple Modified Gram-Schmidt to generate the evecs, there is almost certainly a better way. It checks the results and compares the time with LAPACK. The accuracy is comparable to LAPACK, and on single thread on my laptop using openblas and gfortran 9.4 it is about twice as quick as LAPACK, as shown by the runs at the end. Note the first run is compiled with all run time checks turned on to demonstrate code correctness. Again you can almost certainly do better than this.
ijb#ijb-Latitude-5410:~/work/stack$ cat outer_eigen.f90
Module numbers_module
Use, Intrinsic :: iso_fortran_env, Only : wp => real64
Implicit None
Public :: wp
Private
End Module numbers_module
Module lapack_interfaces_module
Interface
Subroutine dsyev( jobz, uplo, n, a, lda, w, work, lwork, info )
Use numbers_module, Only : wp
Implicit None
Character :: jobz, uplo
Integer :: info, lda, lwork, n
Real( wp ) :: a( lda, * ), w( * ), work( * )
End Subroutine dsyev
End Interface
Public :: dsyev
Private
End Module lapack_interfaces_module
Module outer_product_eigen_module
Use numbers_module, Only : wp
Implicit None
Public :: outer_product_eigen
Private
Contains
Subroutine outer_product_eigen( v, evals, evecs )
! Returns the evals and evecs of the symmetirx matrix formed by taking the outer product of v with itself
Use numbers_module, Only : wp
Implicit None
Real( wp ), Dimension( : ), Intent( In ) :: v
Real( wp ), Dimension( : ), Allocatable, Intent( Out ) :: evals
Real( wp ), Dimension( :, : ), Allocatable, Intent( Out ) :: evecs
Real( wp ) :: swap
Real( wp ) :: dot
Integer :: n
Integer :: i, j, k
n = Size( v )
! The eigenvalues
Allocate( evals( 1:n ) )
evals( 1:n - 1 ) = 0.0_wp
evals( n ) = Dot_product( v, v )
! The eigenvectors - generate by modified Gram-Schmidt making orthogonal to
! the original vector
Allocate( evecs( 1:n, 1:n ) )
! To tax my little brain less initially stick the normalized original vector in the first column. Will swap with
! the last column once we have generated all the vectors.
evecs( :, 1 ) = v / Norm2( v )
! Now generate the other vectors by making them orthonormal to the previous vectors
! Use a simple modified Gram-Shmidt for this. There must be a better mehtod.
Do i = 2, n
! Inital "guess" at vector - could this fail in a pathological case????
evecs( :, i ) = 0.0_wp
evecs( i, i ) = 1.0_wp
! Project out the previous vectors
Do j = 1, i - 1
!!$ evecs( :, i ) = evecs( :, i ) - Dot_product( evecs( :, j ), evecs( :, i ) ) * evecs( :, j )
dot = 0.0_wp
Do k = 1, n
dot = dot + evecs( k, j ) * evecs( k, i )
End Do
Do k = 1, n
evecs( k, i ) = evecs( k, i ) - dot * evecs( k, j )
End Do
End Do
! And normalise
evecs( :, i ) = evecs( :, i ) / Norm2( evecs( :, i ) )
End Do
! And swap the columns so the evecs are in the same order as the evals
Do i = 1, n
swap = evecs( i, 1 )
evecs( i, 1 ) = evecs( i, n )
evecs( i, n ) = swap
End Do
End Subroutine outer_product_eigen
End Module outer_product_eigen_module
Program testit
Use, Intrinsic :: iso_fortran_env, Only : stdio => output_unit
Use numbers_module , Only : wp
Use outer_product_eigen_module, Only : outer_product_eigen
Use lapack_interfaces_module , Only : dsyev
Implicit None
Real( wp ), Dimension( :, : ), Allocatable :: m
Real( wp ), Dimension( :, : ), Allocatable :: evecs
Real( wp ), Dimension( : ), Allocatable :: v
Real( wp ), Dimension( : ), Allocatable :: evals
Real( wp ), Dimension( : ), Allocatable :: work
Real( wp ), Dimension( 1:1 ) :: size_work
Integer :: n
Integer :: info
Integer :: start, finish, rate
! Size of problem
Write( stdio, '( "Order of problem?" )' )
Read ( *, * ) n
Write( stdio, * )
! Insitalise v
Allocate( v( 1:n ) )
Call Random_Number( v )
v = v - 0.5_wp
! Solve via method in https://math.stackexchange.com/questions/403614/eigenvalues-of-outer-product-matrix-of-two-n-dimensional-vectors
Call system_clock( start, rate )
Call outer_product_eigen( v, evals, evecs )
Call system_clock( finish, rate )
Call check_results( 'Outer eigen', v, evals, evecs )
Write( stdio, '( "Time for outer eigen: ", f0.3, " seconds" )' ) Real( finish - start ) / rate
Write( stdio, * )
! Find the results via LAPACK
Deallocate( evals )
Call system_clock( start, rate )
Call generate_matrix( v, m )
! LAPACK overwtites the matrix with the evecs
evecs = m
Allocate( evals( 1:n ) )
Call dsyev( 'V', 'U', n, evecs, Size( evecs, Dim = 1 ), evals, size_work, -1, info )
Allocate( work( 1:Nint( size_work( 1 ) ) ) )
Call dsyev( 'V', 'U', n, evecs, Size( evecs, Dim = 1 ), evals, work, Size( work ), info )
Call system_clock( finish, rate )
Write( stdio, '( "LAPACK info = ", i0 )' ) info
If( info == 0 ) Then
Call check_results( 'LAPACK', v, evals, evecs )
Write( stdio, '( "Time for LAPACK: ", f0.3, " seconds" )' ) Real( finish - start ) / rate
End If
Contains
Pure Subroutine generate_matrix( v, m )
! Generate the matrix M = V .outer_product. V
Use numbers_module, Only : wp
Implicit None
Real( wp ), Dimension( : ) , Intent( In ) :: v
Real( wp ), Dimension( :, : ), Allocatable, Intent( Out ) :: m
Integer :: n
Integer :: i, j
n = Size( v )
Allocate( m( 1:n, 1:n ) )
Do j = 1, n
Do i = 1, n
m( i, j ) = v( i ) * v( j )
End Do
End Do
End Subroutine generate_matrix
Subroutine check_results( method, v, evals, evecs )
! Check the results of the given method
Use, Intrinsic :: iso_fortran_env, Only : stdio => output_unit
Use numbers_module, Only : wp
Implicit None
Character( Len = * ) , Intent( In ) :: method
Real( wp ), Dimension( : ), Intent( In ) :: v
Real( wp ), Dimension( : ), Intent( In ) :: evals
Real( wp ), Dimension( :, : ), Intent( In ) :: evecs
Real( wp ), Dimension( :, : ), Allocatable :: m
Real( wp ), Dimension( :, : ), Allocatable :: tmp
Integer :: i
Write( stdio, '( "Checking results from ", a )' ) method
! Check orthonormality
tmp = Matmul( Transpose( evecs ), evecs )
Do i = 1, Size( tmp, Dim = 1 )
tmp( i, i ) = tmp( i, i ) - 1.0_wp
End Do
Write( stdio, '( "Maximum error in orthonormality: ", g20.12 )' ) Maxval( Abs( tmp ) )
! Check is solution to eval problem
Call generate_matrix( v, m )
tmp = Matmul( Transpose( evecs ), Matmul( m, evecs ) )
Do i = 1, Size( tmp, Dim = 1 )
tmp( i, i ) = tmp( i, i ) - evals( i )
End Do
Write( stdio, '( "Maximum error in solution : ", g20.12 )' ) Maxval( Abs( tmp ) )
End Subroutine check_results
End Program testit
ijb#ijb-Latitude-5410:~/work/stack$ gfortran -std=f2018 -Wall -Wextra -fcheck=all -O -g -Wimplicit-procedure -Wuse-without-only outer_eigen.f90 -lopenblas
ijb#ijb-Latitude-5410:~/work/stack$ ./a.out
Order of problem?
250
Checking results from Outer eigen
Maximum error in orthonormality: 0.598794597221E-14
Maximum error in solution : 0.881524230182E-13
Time for outer eigen: .051 seconds
LAPACK info = 0
Checking results from LAPACK
Maximum error in orthonormality: 0.777156117238E-14
Maximum error in solution : 0.112331207400E-13
Time for LAPACK: .020 seconds
ijb#ijb-Latitude-5410:~/work/stack$ gfortran -std=f2018 -Wall -Wextra -Ofast -g -Wimplicit-procedure -Wuse-without-only outer_eigen.f90 -lopenblas
ijb#ijb-Latitude-5410:~/work/stack$ ./a.out
Order of problem?
250
Checking results from Outer eigen
Maximum error in orthonormality: 0.119649769260E-13
Maximum error in solution : 0.215376845007E-12
Time for outer eigen: .011 seconds
LAPACK info = 0
Checking results from LAPACK
Maximum error in orthonormality: 0.976996261670E-14
Maximum error in solution : 0.982076900499E-14
Time for LAPACK: .021 seconds
ijb#ijb-Latitude-5410:~/work/stack$ ./a.out
Order of problem?
500
Checking results from Outer eigen
Maximum error in orthonormality: 0.115890803898E-12
Maximum error in solution : 0.469814678927E-11
Time for outer eigen: .088 seconds
LAPACK info = 0
Checking results from LAPACK
Maximum error in orthonormality: 0.888178419700E-14
Maximum error in solution : 0.410210860282E-13
Time for LAPACK: .108 seconds
ijb#ijb-Latitude-5410:~/work/stack$ ./a.out
Order of problem?
1000
Checking results from Outer eigen
Maximum error in orthonormality: 0.357838758624E-13
Maximum error in solution : 0.291765283970E-11
Time for outer eigen: .406 seconds
LAPACK info = 0
Checking results from LAPACK
Maximum error in orthonormality: 0.150990331349E-13
Maximum error in solution : 0.124051162678E-12
Time for LAPACK: .925 seconds
ijb#ijb-Latitude-5410:~/work/stack$ gfortran -std=f2018 -Wall -Wextra -Ofast -g -Wimplicit-procedure -Wuse-without-only outer_eigen.f90 -lopenblas
ijb#ijb-Latitude-5410:~/work/stack$ ./a.out
Order of problem?
2000
Checking results from Outer eigen
Maximum error in orthonormality: 0.866971425206E-14
Maximum error in solution : 0.147852563526E-11
Time for outer eigen: 4.076 seconds
LAPACK info = 0
Checking results from LAPACK
Maximum error in orthonormality: 0.264233079861E-13
Maximum error in solution : 0.373884284743E-12
Time for LAPACK: 8.918 seconds
ijb#ijb-Latitude-5410:~/work/stack$ gfortran --version
GNU Fortran (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
ijb#ijb-Latitude-5410:~/work/stack$

Related

What is causing an infinite recursion? (Prolog)

I'm trying to approach a problem in which given M and N integers, returns in res a list with the powers of M that are less than or equal to N, in descending order.
example: powers(3,9,res).
res = [9,3,1]
My approach is as follows:
power(X,0,1).
power(X,Y,Z) :- X>0,
Yminus1 is Y - 1,
power(X,Yminus1,Z1),
Z is X*Z1.
increment(X,newX) :- newX is X + 1.
powers(M,N,res) :- integer(M), integer(N),
powersAux(M,N,0,res).
powersAux(M,N,E,res) :- power(M,E,Z),
Z=<N,
increment(E,E1),
res1 = [Z|res],
powersAux(M,N,E1,res1).
I'm getting my memory stack filled so I understand that the recursion never ends.
You need to handle special cases:
0n is always 0
1n is always 1
And Prolog has an in-built exponiation function: **/2.
A common Prolog idiom is to have a public predicate that does little outside of constraint validation, that invokes an "internal" helper predicate that does the work. The helper predicate often takes additional parameters that maintain state needed for computation.
That leads to this:
powers( X , L, Ps ) :-
non_negative_integer(X),
non_negative_integer(L),
powers(X,0,L,[],Ps)
.
non_negative_integer(X) :- integer(X), X >= 0 .
% ---------------------------------------------------------------
%
% powers( +Base, +Exponent, +Limit, +Accumulator, ?Results )
%
% where Base and Radix are guaranteed to be non-negative integers
% ---------------------------------------------------------------
powers( 0 , _ , _ , _ , [0] ) :- ! . % 0^n is always 0
powers( 1 , _ , 0 , _ , [] ) :- ! . % 1^n is always 1
powers( 1 , _ , L , _ , [1] ) :- L >= 1 , !. % 1^n is always 1
powers( X , Y , L , Ps , Ps ) :- X**Y > L , !. % when x^y exceeds the limit, we're done, and
powers( X , Y , L , Ts , Ps ) :- % otherrwise...
T is X**Y , % - compute T as x^y
Y1 is Y+1, % - increment Y
powers(X,Y1,L,[T|Ts],Ps) % - recurse down, prepending T to the accumulator list.
. % Easy!
Which gives us
?- powers(2,1024,Ps).
Ps = [1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1]

Arguments of different types in a same Fortran subroutine

I want to define a subroutine in modern fortran (90 or newer) to print a matrix (with integer or real numbers) or vectors (with integer o numbers).
SUBROUTINE print_matrix_vector( A )
! WHAT TYPE CAN I WRITE TO A ???
END SUBROUTINE
Of course, I wanted to call a subrutine
CALL print_matrix_vector( A )
independent if A is a matrix of real or integer numbers, and if A is a vector of real or integer numbers.
Thanks in advance for your comments. I think this is something very usual, but I did not find nothing clear.
So Fortran uses interface statements to declare overloads to functions. I have done the exact same thing recently. The key is to create an interface, I call show which is used as an alias for all the separate functions needed.
interface show
procedure show_vector_i, show_vector_r, show_vector_d
procedure show_matrix_i, show_matrix_r, show_matrix_d
end interface
Here is the output first.
Display Matrices/Vectors
vector=
1
2
3
4
matrix=
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
A=
4.47723 3.36660 1.48809 -.752551
6.36660 7.19091 6.67333 5.54482
7.48809 9.67333 10.1187 9.77902
8.24745 11.5448 12.7790 13.0861
u=
6.36660
7.19091
6.67333
5.54482
v=
3.36660
7.19091
9.67333
11.5448
and the code
program FortranConsole1
use, intrinsic :: iso_fortran_env
implicit none
interface show
procedure show_matrix_i, show_matrix_r, show_matrix_d
procedure show_vector_r, show_vector_d
end interface
integer :: row(16), matrix(4,4), i
real(real64), allocatable, target :: A(:,:)
real(real64), pointer :: B(:,:)
real(real64), allocatable :: v(:), u(:)
row = [(i,i=1,16)]
matrix = reshape( row, [4, 4])
print *, "Display Matrices/Vectors"
print *, ""
print *, "vector="
call show(row(1:4))
print *, "matrix="
call show(matrix)
A = dble(matrix)
A = sqrt( matmul( transpose(A), A) ) - A
print *, "A="
call show(A)
v = A(:,2)
u = A(2,:)
print *, "u="
call show(u)
print *, "v="
call show(v)
contains
subroutine show_vector_i(v, w)
! Display the vector 'v' in a single column
! v : the array of real numbers
! w : the column width. default = 5
! s : sig. figures w-5 (calculated)
integer, intent(in) :: v(:)
integer, intent(in), optional :: w
integer :: i,n,wt
character(len=16) :: fmt
if(present(w)) then
wt = w
else
wt = 5
end if
n = size(v)
write( fmt, "(a,g0,a)") "(*(g",wt,".0))"
write( * , fmt ) ( v(i), new_line("A"), i=1,n )
end subroutine
subroutine show_vector_r(v, w)
! Display the vector 'v' in a single column
! v : the array of real numbers
! w : the column width. deafult = 12
! s : sig. figures w-5 (calculated)
real(real32), intent(in) :: v(:)
integer, intent(in), optional :: w
integer :: i,n,dg,wt
character(len=16) :: fmt
if(present(w)) then
wt = w
else
wt = 12
end if
dg = wt - 6
n = size(v)
write( fmt, "(a,g0,a,g0,a)") "(*(g",wt,".",dg,"))"
write( * , fmt ) ( v(i), new_line("A"), i=1,n )
end subroutine
subroutine show_vector_d(v, w)
! Display the vector 'v' in a single column
! v : the array of real numbers
! w : the column width. deafult = 12
! s : sig. figures w-5 (calculated)
real(real64), intent(in) :: v(:)
integer, intent(in), optional :: w
call show_vector_r(real(v),w)
end subroutine
subroutine show_matrix_i(A, w)
! Display the matrix 'A' in columns
! A : the array of integers
! w : the column width. Default = 5
integer, intent(in) :: A(:,:)
integer, intent(in), optional :: w
integer :: i,j,n,m, wt
character(len=16) :: fmt
if(present(w)) then
wt = w
else
wt = 5
end if
n = size(A,1)
m = size(A,2)
write( fmt, "(a,g0,a)") "(*(g",wt,".0))"
write( * , fmt ) ( (A(i,j),j=1,m), new_line("A"), i=1,n )
end subroutine
subroutine show_matrix_r(A, w)
! Display the matrix 'A' in columns
! A : the array of real numbers
! w : the column width. deafult = 12
! s : sig. figures w-5 (calculated)
real(real32), intent(in) :: A(:,:)
integer, intent(in), optional :: w
integer :: i,j,n,m,dg,wt
character(len=16) :: fmt
if(present(w)) then
wt = w
else
wt = 12
end if
dg = wt - 6
n = size(A,1)
m = size(A,2)
write( fmt, "(a,g0,a,g0,a)") "(*(g",wt,".",dg,"))"
write( * , fmt ) ( (A(i,j),j=1,m), new_line("A"), i=1,n )
end subroutine
subroutine show_matrix_d(A,w)
! Display the matrix 'A' in columns
! A : the array of dble numbers
! w : the column width. default = 12
! Converts 'A' into single precision and calls `show_matrix_r`
real(real64), intent(in) :: A(:,:)
integer, intent(in), optional :: w
call show_matrix_r(real(A),w)
end subroutine
end program FortranConsole1
In reality, the functions should be in a module file in order to be reused again and again in different programs.

Gamma function implementation not producing correct values

Function programmed in Fortran 95 to compute values of the Gamma function from mathematics is not producing the correct values.
I am trying to implement a recursive function in Fortran 95 that computes values of the Gamma function using the Lanczos approximation (yes I know that there is an intrinsic function for this in the 2003 standard and later). I've followed the standard formula very closely so I'm not certain what is wrong. Correct values for the Gamma function are crucial for some other numerical computations I am doing involving the numerical computation of the Jacobi polynomials by means of a recursion relation.
program testGam
implicit none
integer, parameter :: dp = selected_real_kind(15,307)
real(dp), parameter :: pi = 3.14159265358979324
real(dp), dimension(10) :: xGam, Gam
integer :: n
xGam = (/ -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5 /)
do n = 1,10
Gam(n) = GammaFun(xGam(n))
end do
do n = 1,10
write(*,*) xGam(n), Gam(n)
end do
contains
recursive function GammaFun(x) result(G)
real(dp), intent(in) :: x
real(dp) :: G
real(dp), dimension(0:8), parameter :: q = &
(/ 0.99999999999980993, 676.5203681218851, -1259.1392167224028, &
771.32342877765313, -176.61502916214059, 12.507343278686905, &
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7 /)
real(dp) :: t, w, xx
integer :: n
xx = x
if ( xx < 0.5_dp ) then
G = pi / ( sin(pi*xx)*GammaFun(1.0_dp - xx) )
else
xx = xx - 1.0_dp
t = q(0)
do n = 1,9
t = t + q(n) / (xx + real(n, dp))
end do
w = xx + 7.5_dp
G = sqrt(2.0_dp*pi)*(w**(xx + 0.5_dp))*exp(-w)*t
end if
end function GammaFun
end program testGam
Whereas this code should be producing correct values for the Gamma function over the whole real line, it seems only to produce a constant value close to 122 regardless of the input. I suspect that there is some weird floating point arithmetic issue that I am not seeing.
There are two obvious issues with your code
Most seriously the code accesses an array out of bounds at line 42, i.e. in the loop
do n = 1,9
t = t + q(n) / (xx + real(n, dp))
end do
You have mixed up your precision somewhat, with some of the constants being of kind dp, other being of default kind
Making what I believe are the appropriate fixes to these your program compiles, links and runs correctly, at least as far as I can see. See below:
ian#eris:~/work/stackoverflow$ cat g.f90
program testGam
implicit none
integer, parameter :: dp = selected_real_kind(15,307)
real(dp), parameter :: pi = 3.14159265358979324_dp
real(dp), dimension(10) :: xGam, Gam
integer :: n
xGam = (/ -3.5_dp, -2.5_dp, -1.5_dp, -0.5_dp, 0.5_dp, 1.5_dp, 2.5_dp, 3.5_dp, 4.5_dp, 5.5_dp /)
do n = 1,10
Gam(n) = GammaFun(xGam(n))
end do
do n = 1,10
write(*,*) xGam(n), Gam(n), gamma( xGam( n ) ), Abs( Gam( n ) - gamma( xGam( n ) ) )
end do
contains
recursive function GammaFun(x) result(G)
real(dp), intent(in) :: x
real(dp) :: G
real(dp), dimension(0:8), parameter :: q = &
(/ 0.99999999999980993_dp, 676.5203681218851_dp, -1259.1392167224028_dp, &
771.32342877765313_dp, -176.61502916214059_dp, 12.507343278686905_dp, &
-0.13857109526572012_dp, 9.9843695780195716e-6_dp, 1.5056327351493116e-7_dp /)
real(dp) :: t, w, xx
integer :: n
xx = x
if ( xx < 0.5_dp ) then
G = pi / ( sin(pi*xx)*GammaFun(1.0_dp - xx) )
else
xx = xx - 1.0_dp
t = q(0)
do n = 1,8
t = t + q(n) / (xx + real(n, dp))
end do
w = xx + 7.5_dp
G = sqrt(2.0_dp*pi)*(w**(xx + 0.5_dp))*exp(-w)*t
end if
end function GammaFun
end program testGam
ian#eris:~/work/stackoverflow$ gfortran -O -std=f2008 -Wall -Wextra -fcheck=all g.f90
ian#eris:~/work/stackoverflow$ ./a.out
-3.5000000000000000 0.27008820585226917 0.27008820585226906 1.1102230246251565E-016
-2.5000000000000000 -0.94530872048294168 -0.94530872048294179 1.1102230246251565E-016
-1.5000000000000000 2.3632718012073521 2.3632718012073548 2.6645352591003757E-015
-0.50000000000000000 -3.5449077018110295 -3.5449077018110318 2.2204460492503131E-015
0.50000000000000000 1.7724538509055159 1.7724538509055161 2.2204460492503131E-016
1.5000000000000000 0.88622692545275861 0.88622692545275805 5.5511151231257827E-016
2.5000000000000000 1.3293403881791384 1.3293403881791370 1.3322676295501878E-015
3.5000000000000000 3.3233509704478430 3.3233509704478426 4.4408920985006262E-016
4.5000000000000000 11.631728396567446 11.631728396567450 3.5527136788005009E-015
5.5000000000000000 52.342777784553583 52.342777784553519 6.3948846218409017E-014
ian#eris:~/work/stackoverflow$

Compiling A Mexfile using R CMD SHLIB

I am trying to import a number of Fortran 90 codes into R for a project. They were initially written with a mex (matlab integration of Fortran routines) type compilation in mind. This is what one of the codes look like:
# include <fintrf.h>
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
!--------------------------------------------------------------
! MEX file for VFI3FCN routine
!
! log M_{t,t+1} = log \beta + gamma (x_t - x_{t+1})
! gamma = gamA + gamB (x_t - xbar)
!
!--------------------------------------------------------------
implicit none
mwPointer plhs(*), prhs(*)
integer nlhs, nrhs
mwPointer mxGetM, mxGetPr, mxCreateDoubleMatrix
mwPointer nk, nkp, nz, nx, nh
mwSize col_hxz, col_hz, col_xz
! check for proper number of arguments.
if(nrhs .ne. 31) then
call mexErrMsgTxt('31 input variables required.')
elseif(nlhs .ne. 4) then
call mexErrMsgTxt('4 output variables required.')
endif
! get the size of the input array.
nk = mxGetM(prhs(5))
nx = mxGetM(prhs(7))
nz = mxGetM(prhs(11))
nh = mxGetM(prhs(14))
nkp = mxGetM(prhs(16))
col_hxz = nx*nz*nh
col_xz = nx*nz
col_hz = nz*nh
! create matrix for the return arguments.
plhs(1) = mxCreateDoubleMatrix(nk, col_hxz, 0)
plhs(2) = mxCreateDoubleMatrix(nk, col_hxz, 0)
plhs(3) = mxCreateDoubleMatrix(nk, col_hxz, 0)
plhs(4) = mxCreateDoubleMatrix(nk, col_hxz, 0)
call vfi3fcnIEccB(%val(mxGetPr(plhs(1))), nkp)
return
end
subroutine vfi3fcnIEccB(optK, V, I, div, & ! output variables
alp1, alp2, alp3, V0, k, nk, x, xbar, nx, Qx, z, nz, Qz, h, nh, kp, &
alpha, beta, delta, f, gamA, gamB, gP, gN, istar, kmin, kmtrx, ksubm, hmtrx, xmtrx, zmtrx, &
nkp, col_hxz, col_xz, col_hz)
use ifwin
implicit none
! specify input and output variables
integer, intent(in) :: nk, nkp, nx, nz, nh, col_hxz, col_xz, col_hz
real*8, intent(out) :: V(nk, col_hxz), optK(nk, col_hxz), I(nk, col_hxz), div(nk, col_hxz)
real*8, intent(in) :: V0(nk, col_hxz), k(nk), kp(nkp), x(nx), z(nz), Qx(nx, nx), Qz(nz, nz), h(nh)
real*8, intent(in) :: alp1, alp2, alp3, xbar, kmin, alpha, gP, gN, beta, delta, gamA, gamB, f, istar
real*8, intent(in) :: kmtrx(nk, col_hxz), ksubm(nk, col_hz), zmtrx(nk, col_hxz), xmtrx(nk, col_hxz), hmtrx(nk, col_hxz)
! specify intermediate variables
real*8 :: Res(nk, col_hxz), Obj(nk, col_hxz), optKold(nk, col_hxz), Vold(nk, col_hxz), tmpEMV(nkp, col_hz), tmpI(nkp), &
tmpObj(nkp, col_hz), tmpA(nk, col_hxz), tmpQ(nx*nh, nh), detM(nx), stoM(nx), g(nkp), tmpInd(nh, nz)
real*8 :: Qh(nh, nh, nx), Qxh(nx*nh, nx*nh), Qzxh(col_hxz, col_hxz)
real*8 :: hp, d(nh), errK, errV, T1, lapse
integer :: ix, ih, iter, optJ(col_hz), ik, iz, ind(nh, col_xz), subindex(nx, col_hz)
logical*4 :: statConsole
! construct the transition matrix for kh --- there are nx number of these transition matrix: 3-d
Qh = 0.0
do ix = 1, nx
do ih = 1, nh
! compute the predicted next period kh
hp = alp1 + alp2*h(ih) + alp3*(x(ix) - xbar)
! construct transition probability vector
d = abs(h - hp) + 1D-32
Qh(:, ih, ix) = (1/d)/sum(1/d)
end do
end do
! construct the compound transition matrix over (z x h) space
! compound the (x h) space
Qxh = 0.0
do ix = 1, nx
call kron(tmpQ, Qx(:, ix), Qh(:, :, ix), nx, 1, nh, nh)
Qxh(:, (ix - 1)*nh + 1 : ix*nh) = tmpQ
end do
! compound the (z x h) space: h changes the faster, followed by x, and z changes the slowest
call kron(Qzxh, Qz, Qxh, nz, nz, nx*nh, nx*nh)
! available funds for the firm
Res = dexp(xmtrx + zmtrx + hmtrx)*(kmtrx**alpha) + (1 - delta)*kmtrx - f
! initializing
Obj = 0.0
optK = 0.0
optKold = optK + 1.0
Vold = V0
! Some Intermediate Variables Used in Stochastic Discount Factor
detM = beta*dexp((gamA - gamB*xbar)*x + gamB*x**2)
stoM = -(gamA - gamB*xbar + gamB*x)
! Intermediate index vector to facilitate submatrix extracting
ind = reshape((/1 : col_hxz : 1/), (/nh, col_xz/))
do ix = 1, nx
tmpInd = ind(:, ix : col_xz : nx)
do iz = 1, nz
subindex(ix, (iz - 1)*nh + 1 : iz*nh) = tmpInd(:, iz)
end do
end do
! start iterations
errK = 1.0
errV = 1.0
iter = 0
T1 = secnds(0.0)
do
if (errV <= 1D-3 .AND. errK <= 1D-8) then
exit
else
iter = iter + 1
do ix = 1, nx
! next period value function by linear interpolation: nkp by nz*nh matrix
call interp1(tmpEMV, k, detM(ix)*(matmul(dexp(stoM(ix)*xmtrx)*Vold, Qzxh(:, subindex(ix, :)))) - ksubm, kp, &
nk, nkp, col_hz)
! maximize the right-hand size of Bellman equation on EACH grid point of capital stock
do ik = 1, nk
! with istar tmpI is no longer investment but a linear transformation of that
tmpI = (kp - (1.0 - delta)*k(ik))/k(ik) - istar
where (tmpI >= 0.0)
g = gP
elsewhere
g = gN
end where
tmpObj = tmpEMV - spread((g/2.0)*(tmpI**2)*k(ik), 2, col_hz)
! direct discrete maximization
Obj(ik, subindex(ix, :)) = maxval(tmpObj, 1)
optJ = maxloc(tmpObj, 1)
optK(ik, subindex(ix, :)) = kp(optJ)
end do
end do
! update value function and impose limited liability condition
V = max(Res + Obj, 1D-16)
! convergence criterion
errK = maxval(abs(optK - optKold))
errV = maxval(abs(V - Vold))
! revise Initial Guess
Vold = V
optKold = optK
! visual
if (modulo(iter, 50) == 0) then
lapse = secnds(T1)
statConsole = AllocConsole()
print "(a, f10.7, a, f10.7, a, f8.1, a)", " errV:", errV, " errK:", errK, " Time:", lapse, "s"
end if
end if
end do
! visual check on errors
lapse = secnds(T1)
statConsole = AllocConsole()
print "(a, f10.7, a, f10.7, a, f8.1, a)", " errV:", errV, " errK:", errK, " Time:", lapse, "s"
! optimal investment and dividend
I = optK - (1.0 - delta)*kmtrx
tmpA = I/kmtrx - istar
where (tmpA >= 0)
div = Res - optK - (gP/2.0)*(tmpA**2)*kmtrx
elsewhere
div = Res - optK - (gN/2.0)*(tmpA**2)*kmtrx
end where
return
end
subroutine interp1(v, x, y, u, m, n, col)
!-------------------------------------------------------------------------------------------------------
! Linear interpolation routine similar to interp1 with 'linear' as method parameter in Matlab
!
! OUTPUT:
! v - function values on non-grid points (n by col matrix)
!
! INPUT:
! x - grid (m by one vector)
! y - function defined on the grid x (m by col matrix)
! u - non-grid points on which y(x) is to be interpolated (n by one vector)
! m - length of x and y vectors
! n - length of u and v vectors
! col - number of columns of v and y matrices
!
! Four ways to pass array arguments:
! 1. Use explicit-shape arrays and pass the dimension as an argument(most efficient)
! 2. Use assumed-shape arrays and use interface to call external subroutine
! 3. Use assumed-shape arrays and make subroutine internal by using "contains"
! 4. Use assumed-shape arrays and put interface in a module then use module
!
! This subroutine is equavilent to the following matlab call
! v = interp1(x, y, u, 'linear', 'extrap') with x (m by 1), y (m by col), u (n by 1), and v (n by col)
!------------------------------------------------------------------------------------------------------
implicit none
integer :: m, n, col, i, j
real*8, intent(out) :: v(n, col)
real*8, intent(in) :: x(m), y(m, col), u(n)
real*8 :: prob
do i = 1, n
if (u(i) < x(1)) then
! extrapolation to the left
v(i, :) = y(1, :) - (y(2, :) - y(1, :)) * ((x(1) - u(i))/(x(2) - x(1)))
else if (u(i) > x(m)) then
! extrapolation to the right
v(i, :) = y(m, :) + (y(m, :) - y(m-1, :)) * ((u(i) - x(m))/(x(m) - x(m-1)))
else
! interpolation
! find the j such that x(j) <= u(i) < x(j+1)
call bisection(x, u(i), m, j)
prob = (u(i) - x(j))/(x(j+1) - x(j))
v(i, :) = y(j, :)*(1 - prob) + y(j+1, :)*prob
end if
end do
end subroutine interp1
subroutine bisection(list, element, m, k)
!--------------------------------------------------------------------------------
! find index k in list such that (list(k) <= element < list(k+1)
!--------------------------------------------------------------------------------
implicit none
integer*4 :: m, k, first, last, half
real*8 :: list(m), element
first = 1
last = m
do
if (first == (last-1)) exit
half = (first + last)/2
if ( element < list(half) ) then
! discard second half
last = half
else
! discard first half
first = half
end if
end do
k = first
end subroutine bisection
subroutine kron(K, A, B, rowA, colA, rowB, colB)
!--------------------------------------------------------------------------------
! Perform K = kron(A, B); translated directly from kron.m
!
! OUTPUT:
! K -- rowA*rowB by colA*colB matrix
!
! INPUT:
! A -- rowA by colA matrix
! B -- rowB by colB matrix
! rowA, colA, rowB, colB -- integers containing shape information
!--------------------------------------------------------------------------------
implicit none
integer, intent(in) :: rowA, colA, rowB, colB
real*8, intent(in) :: A(rowA, colA), B(rowB, colB)
real*8, intent(out) :: K(rowA*rowB, colA*colB)
integer :: t1(rowA*rowB), t2(colA*colB), i, ia(rowA*rowB), ja(colA*colB), ib(rowA*rowB), jb(colA*colB)
t1 = (/ (i, i = 0, (rowA*rowB - 1)) /)
ia = int(t1/rowB) + 1
ib = mod(t1, rowB) + 1
t2 = (/ (i, i = 0, (colA*colB - 1)) /)
ja = int(t2/colB) + 1
jb = mod(t2, colB) + 1
K = A(ia, ja)*B(ib, jb)
end subroutine kron
My initial plan was to remove the mexFunction subroutine and compile the main Fortran subroutines using the R CMD SHLIB command but then I run into the Rtools compiler not knowing where to find the ifwin library even though I have the library in my intel fortran compiler folder.
So my first question is:
1) Is there a way for me to tell rtools where to find the ifwin library and any other library I need to include? Or is there a way to include the dependency libraries in the R CMD SHLIB command so the compiler can find the necessary libraries and compile?
2) If the answer to two is no, can I some how use the compiled version from Matlab in R. I can compile the file as is in matlab using the mex Zhang_4.f90 command with no errors.
3) Is there a way of setting up an environment in Visual Studio 2015 so I can compile Fortran subroutines for use in R using the Intel compiler?
When I take out the mexFunction subroutine and try compiling the rest of the code, I get the following error:
D:\SS_Programming\Fortran>R CMD SHLIB Zhang_4.f90
c:/Rtools/mingw_64/bin/gfortran -O2 -mtune=core2 -c Zhang_4.f90 -o
Zhang_4.o
Zhang_4.f90:6.4:
use ifwin
1
Fatal Error: Can't open module file 'ifwin.mod' for reading at (1): No
such file or directory
make: *** [Zhang_4.o] Error 1
Warning message:
running command 'make -f "C:/PROGRA~1/R/R-34~1.2/etc/x64/Makeconf" -f
"C:/PROGRA~1/R/R-34~1.2/share/make/winshlib.mk"
SHLIB_LDFLAGS='$(SHLIB_FCLDFLAGS)' SHLIB_LD='$(SHLIB_FCLD)'
SHLIB="Zhang_4.dll" SHLIB_LIBADD='$(FCLIBS)' WIN=64 TCLBIN=64
OBJECTS="Zhang_4.o"' had status 2
I don't think there is any other way then rewrite the code to not use IFWIN. Unless you manage to use Intel Fortran for R (that might require recompiling the whole R distribution...). Matlab is tied to Intel Fortran, that's why the code worked there.
You have to adjust the code anyway, you cannot use it as it stands.
Just get rid of the AllocConsole() calls and the print statements. You will need to use the R routines to print to console. See https://cran.r-project.org/doc/manuals/R-exts.html#Printing-from-FORTRAN

Numerical integration in Fortran 90

In Fortran 90, I want to numerically integrate a mathematical function with one variable within a given limit. For example, integrating f(x) = x**2 from 0 to 10. The function I have is more complicated than this one and I have to run it several times changing the integration limits. I found out on internet that the 'QUADPACK' library might help me with this. But how can I install this library so that I can call this in my code? Provide some details as I won't be able to follow advanced instructions quickly.
I've provided a simple program where midpoint method is used to integrate x^2. A more complicated formula may be entered, so long the mesh is fine enough (and the function is smooth), this should work..
program integrate
implicit none
integer,parameter :: cp = selected_real_kind(14)
integer,parameter :: N = 1000
real(cp),dimension(N) :: f,xc
real(cp),dimension(N+1) :: x
real(cp) :: s,xmax,xmin,dx
integer :: i
xmin = 0.0_cp
xmax = 10.0_cp
dx = (xmax - xmin)/real(N,cp)
x = (/(xmin + dx*(i-1),i=1,N+1)/)
! Define x at center
do i=1,N
xc(i) = x(i) + 0.5_cp*dx
enddo
! Define f
do i=1,N
f(i) = xc(i)**2
enddo
! Integrate (Midpoint method)
s = 0.0_cp
do i=1,N
s = s + f(i)*dx
enddo
write(*,*) 'sum = ',s
end program
Here is one possible solution to integrate your function f(x) = x**2 from 0 to 10. This uses the Gaussian quadrature formula for 8 and 16 points.
program quadrature
implicit none
! Declare variables
integer, parameter :: n8 = 8, n16 = 16
real(8) :: r, m, c
real(8) :: a, b, result8, result16
real(8), dimension (n8) :: x8, w8
real(8), dimension(n16) :: x16, w16
integer :: i
! Define the limits of integration
a = 0.D0
b = 10.D0
! Define the abscissas and weights for 8-point Gauss quadrature
data x8 /-0.1834346424956498D0, 0.1834346424956498D0, -0.5255324099163290D0, 0.5255324099163290D0, &
-0.7966664774136267D0, 0.7966664774136267D0, -0.9602898564975363D0, 0.9602898564975363D0/
data w8 / 0.3626837833783620D0, 0.3626837833783620D0, 0.3137066458778873D0, 0.3137066458778873D0, &
0.2223810344533745D0, 0.2223810344533745D0, 0.1012285362903763D0, 0.1012285362903763D0/
! Define the abscissas and weights for 16-point Gauss quadrature
data x16 /-0.0950125098376374D0, 0.0950125098376374D0, -0.2816035507792589D0, 0.2816035507792589D0, &
-0.4580167776572274D0, 0.4580167776572274D0, -0.6178762444026438D0, 0.6178762444026438D0, &
-0.7554044083550030D0, 0.7554044083550030D0, -0.8656312023878318D0, 0.8656312023878318D0, &
-0.9445750230732326D0, 0.9445750230732326D0, -0.9894009349916499D0, 0.9894009349916499D0 /
data w16 /0.1894506104550685D0, 0.1894506104550685D0, 0.1826034150449236D0, 0.1826034150449236D0, &
0.1691565193950025D0, 0.1691565193950025D0, 0.1495959888165767D0, 0.1495959888165767D0, &
0.1246289712555339D0, 0.1246289712555339D0, 0.0951585116824928D0, 0.0951585116824928D0, &
0.0622535239386479D0, 0.0622535239386479D0, 0.0271524594117541D0, 0.0271524594117541D0 /
! Compute the results using 8-point and 16-point Gauss quadrature
r = 0.D0
m = (b-a)/2.D0
c = (b+a)/2.D0
result8 = 0.D0
result16 = 0.D0
do i = 1, n8
result8 = result8 + w8(i) * f(m*x8(i) + c)
end do
result8 = result8*m
do i = 1, n16
result16 = result16 + w16(i) * f(m*x16(i) + c)
end do
result16 = result16*m
! Print the results
print *, "Result using 8-point Gauss quadrature: ", result8
print *, "Result using 16-point Gauss quadrature: ", result16
contains
! Function to be integrated
real(8) function f(x)
real(8), intent(in) :: x
f = x**2.D0
end function
end program

Resources