toon-members
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Toon-members] TooN determinant.h TooN.h


From: Edward Rosten
Subject: [Toon-members] TooN determinant.h TooN.h
Date: Sun, 06 Sep 2009 16:36:10 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Edward Rosten <edrosten>        09/09/06 16:36:10

Modified files:
        .              : determinant.h TooN.h 

Log message:
        Fixed some bugs in the sign and scaling of the determinant.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/determinant.h?cvsroot=toon&r1=1.3&r2=1.4
http://cvs.savannah.gnu.org/viewcvs/TooN/TooN.h?cvsroot=toon&r1=1.53&r2=1.54

Patches:
Index: determinant.h
===================================================================
RCS file: /cvsroot/toon/TooN/determinant.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- determinant.h       10 Jul 2009 14:05:55 -0000      1.3
+++ determinant.h       6 Sep 2009 16:36:10 -0000       1.4
@@ -1,5 +1,8 @@
 #ifndef TOON_INCLUDE_DETERMINANT_H
 #define TOON_INCLUDE_DETERMINANT_H
+#include <TooN/TooN.h>
+#include <cstdlib>
+#include <utility>
 #ifdef TOON_DETERMINANT_LAPACK
        #include <TooN/LU.h>
 #endif
@@ -65,15 +68,15 @@
                Matrix<Internal::Square<R,C>::Size, 
Internal::Square<R,C>::Size,Precision> A = A_;
                TooN::SizeMismatch<R, C>::test(A.num_rows(), A.num_cols());
                using std::swap;
+               using std::abs;
 
                int size=A.num_rows();
                
                //If row operations of the form row_a += alpha * row_b
                //then the determinant is unaffected. However, if a row
                //is scaled, then the determinant is scaled by the same 
-               //amount. The total scaling is held in divisor.
+               //amount. 
                Precision determinant=1;
-               Precision divisor=1;
 
                for (int i=0; i<size; ++i) {
 
@@ -93,7 +96,9 @@
                        //assert(abs(pivot) > 1e-16);
                        
                        //Swap the current row with the pivot row if necessary.
+                       //A row swap negates the determinant.
                        if (argmax != i) {
+                               determinant*=-1;
                                for (int j=i; j<size; ++j)
                                        swap(A[i][j], A[argmax][j]);
                        }
@@ -104,20 +109,16 @@
                                return 0;
                        
                        for (int u=i+1; u<size; ++u) {
-                               //Multiply out the usual 1/pivot term
-                               //to avoid division.
-                               double factor = A[u][i];
-                               //A[u][i] = 0;
-                               divisor*=pivot;
+                               //Do not multiply out the usual 1/pivot term
+                               //to avoid division. It causes poor scaling.
+                               double factor = A[u][i]/pivot;
+
                                for (int j=i+1; j<size; ++j)
-                                       A[u][j] = A[u][j]*pivot - factor * 
A[i][j];
+                                       A[u][j] = A[u][j] - factor * A[i][j];
                        }
                }
 
-               if(size %2 == 0) 
-                       return -determinant/divisor;
-               else
-                       return determinant/divisor;
+               return determinant;
        }
        
        /** Compute the determinant using TooN::LU.

Index: TooN.h
===================================================================
RCS file: /cvsroot/toon/TooN/TooN.h,v
retrieving revision 1.53
retrieving revision 1.54
diff -u -b -r1.53 -r1.54
--- TooN.h      26 Aug 2009 09:34:44 -0000      1.53
+++ TooN.h      6 Sep 2009 16:36:10 -0000       1.54
@@ -40,13 +40,20 @@
 #include <TooN/internal/typeof.hh>
 #include <TooN/internal/deprecated.hh>
 
+#if defined TOON_NDEBUG || defined NDEBUG
+       #define TOON_NDEBUG_MISMATCH
+       #define TOON_NDEBUG_SLICE
+       #define TOON_NDEBUG_SIZE
+       #define TOON_NDEBUG_FILL
+#endif
+
 #ifdef TOON_INITIALIZE_RANDOM
 #include <ctime>
 #endif
 
 #ifdef TOON_USE_LAPACK
        #ifndef TOON_DETERMINANT_LAPACK
-               #define TOON_DETERMINANT_LAPACK 30
+               #define TOON_DETERMINANT_LAPACK 35
        #endif
 #endif
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]