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

In summary, the subroutine computes a QR factorization of an (mxn) matrix using the Householder method, but apparently something went wrong and the results are not correct.
  • #1
O S
1
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
  • #2
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:
  • #3
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! :-)
 

1. Why is it important to find and fix bugs in Fortran code?

It is important to find and fix bugs in Fortran code because they can cause errors or unexpected behavior in a program, leading to incorrect results or program crashes. This can be especially problematic in scientific research, where accurate and reliable results are crucial.

2. How can I effectively search for bugs in Fortran code?

Effective bug searching in Fortran code involves carefully reviewing the code, paying attention to details, and using debugging tools such as print statements or a debugger. It is also helpful to have a good understanding of the Fortran language and common coding mistakes.

3. What are some common reasons for bugs in Fortran code?

Some common reasons for bugs in Fortran code include syntax errors, logical errors, and incorrect use of variables or functions. Other factors such as outdated compilers or external libraries can also contribute to bugs.

4. How can I prevent bugs from occurring in my Fortran code?

To prevent bugs from occurring in Fortran code, it is important to write clear and organized code, use consistent naming conventions, and thoroughly test the code before using it for important tasks. It is also helpful to regularly update compilers and libraries.

5. What should I do if I can't find a bug in my Fortran code?

If you are having trouble finding a bug in your Fortran code, it can be helpful to ask for assistance from a colleague or seek help from online communities. Sometimes, a fresh perspective can help identify the issue. It is also important to be patient and persistent in your bug searching process.

Similar threads

  • Programming and Computer Science
Replies
4
Views
617
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
944
  • Programming and Computer Science
Replies
21
Views
2K
  • Programming and Computer Science
Replies
2
Views
6K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
26
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
11K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top