Reputation: 413
Hi I am trying to compute an inner product in Fortran. I provide a sample code below and explain the output I am getting, and the expected output. The code itself compiles with no errors, however the output I obtain is not what I am expecting. I think am not properly coding the inner product. The code is below.
EDIT: I edited the code based on the help obtained in the comments below.
program
integer :: i,j
integer, parameter :: nx = 10, ny = 10
complex, dimension(-nx:nx,-ny:ny) :: A,v
real :: B
B = 0.0
do j = -ny+1,ny-1
do i = -nx+1,nx-1
A(i,j) = v(i+1,j)+v(i-1,j)+v(i,j+1)+v(i,j-1)-4*v(i,j)
B = B + conjg(A(i,j))*A(i,j) !computing the inner product
end do
end do
print *, 'Result of the inner product of A with itself', B
end program
Am I computing the inner product correct now? Thanks.
Note: The trace of a matrix product is an inner product, e.g Frobenius inner product.just a generalization of the inner product to tensors of rank 2, Acts identical to the product between rank 1 tensors
Upvotes: 0
Views: 725
Reputation: 926
Are you trying to compute the inner product of two matrices? Could you define that?
In any case if you want to calculate the inner product of two vectors if Fortran, you could write
prod = sum( A * B )
Where, A
and B
are conformable arrays of a type for which multiplication is defined (real, complex, etc.), and prod
is a variable of the same type.
If A
and B
are one-dimensional, this calculates their inner product. I don't know what it is called otherwise.
EDIT
Based of the definition you provided ("Tr(A^\dagger A) = A_{ij}A^*_{ij} =Tr(AA^\dagger)"), you have got the bounds wrong. Put the inner product in a separate loop with
do i = -nx,nx
do j = -ny,ny
B = B + conjg(A(i,j))*A(i,j) !computing the inner product
end do
end do
Or use
B = sum( conjg(A)*A )
without a loop.
Upvotes: 1