I can't find a small bug in this Fortran code

  • Context: Fortran 
  • Thread starter Thread starter O S
  • Start date Start date
  • Tags Tags
    Bug Code Fortran
Click For Summary
SUMMARY

The forum discussion revolves around a bug in a Fortran subroutine designed for QR factorization using the Householder method. The user is attempting to compute the factorization of a specific matrix but encounters unexpected sign changes in the resulting upper triangular matrix. The subroutine, named full_QR, is intended to manipulate the input matrix A and produce an orthogonal matrix Q, but the output deviates from the expected results. The community suggests that the user should consider adjusting sign changes and emphasizes the importance of test-driven development (TDD) for effective debugging.

PREREQUISITES
  • Understanding of QR factorization and the Householder method
  • Familiarity with Fortran programming language
  • Knowledge of linear algebra concepts, particularly matrix operations
  • Experience with debugging techniques in programming
NEXT STEPS
  • Review Fortran's matrix manipulation functions and their syntax
  • Learn about test-driven development (TDD) practices for debugging
  • Explore the mathematical foundations of QR factorization
  • Investigate common pitfalls in implementing the Householder method
USEFUL FOR

Fortran developers, mathematicians working with numerical methods, and anyone involved in implementing or debugging algorithms for matrix factorization.

O S
Messages
1
Reaction score
0
So I'm writing a short subroutine to compute a QR factorization of an (mxn) matrix using the Householder method. To test this, I'm computing the factorization of the matrix

12 -51 4
A = 6 167 -68 .
-4 24 -41

My householder method should return the following upper triangular matrix:

14 21 -14
R = 0 175 -70.
0 0 35

However, what I actually get is

-14 -21 14
R = 0 -175 70.
-0 0 35

So as you can see, it looks almost correct, but with some strange sign changes. I've been staring at the code for hours trying to see where these sign changes are being introduced, but I just can't see where the bug is. Could anyone please have a look through my code to see if there are any obvious issues:

Fortran:
subroutine full_QR(A, Q, m, n)
!==========================================================
! Subroutine for full QR factorization using the
! Householder Method.
!==========================================================

    integer, parameter :: dp = selected_real_kind(15)
  
    integer, intent(in) :: m, n
    real(dp), dimension(m,n), intent(inout) :: A
    real(dp), dimension(m,m), intent(out) :: Q
  
    integer :: k
    real(dp) :: two_norm
    real(dp), dimension(m) :: x, e1
    real(dp), dimension(m,n) :: v
    real(dp), dimension(m,m) :: outprod_vv

    v = 0.0_dp
  
    do k=1,m
        Q(k,k) = 1.0_dp
    end do
  
    !Householder triangularization
    do k=1,n
      
        e1(k) = 1.0_dp

        x(k:m) = A(k:m,k)
        v(k:m,k) = sign( sqrt(dot_product(x(k:m),x(k:m))), x(k) )* &
            e1(k:m) + x(k:m)
      
        v(k:m,k) = v(k:m,k)/(sqrt(dot_product(v(k:m,k),v(k:m,k))))
        call outer_product(v(k:m,k), m-k+1, outprod_vv(k:m,k:m))
      
        A(k:m,k:n) = A(k:m,k:n) - &
            2.0_dp*matmul(outprod_vv(k:m,k:m), A(k:m,k:n))
      
        !Form Q implicitly  
        Q(k:m,k:m) = Q(k:m,k:m) - 2.0_dp* &
            matmul(outprod_vv(k:m,k:m), Q(k:m,k:m))
                       
    end do
  
    Q = transpose(Q)
      
end subroutine full_QR
 
Last edited:
Technology news on Phys.org
O S said:
dfg
"dfg" is your whole set of code? Doesn't seem like it's going to do much

Mod note (Mark44): I have restored the next-to-last version of the post from O S.
 
Last edited by a moderator:
Staring at it for hours? Would be quicker to change a sign, recompile and repeat until it worked.

Sometimes programming isn't as clever as they make it out to be in the adverts. We have a whole discipline (TDD) devoted to building effective tests, just so that we could debug in this manner! :-)
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K