From 35e7fda05ad5ae7536272526f7cff631ba905be0 Mon Sep 17 00:00:00 2001
From: Matthew Webster <mwebster@fmrib.ox.ac.uk>
Date: Thu, 15 Dec 2011 14:11:49 +0000
Subject: [PATCH] Limited dof trace calculation to 4000 EV's

---
 miscmaths.cc | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/miscmaths.cc b/miscmaths.cc
index 80a970d..9def672 100644
--- a/miscmaths.cc
+++ b/miscmaths.cc
@@ -2183,9 +2183,16 @@ void ols(const Matrix& data,const Matrix& des,const Matrix& tc, Matrix& cope,Mat
 }
 
 float ols_dof(const Matrix& des){
+  if ( des.Nrows() > 4000 ) //Use the simple version as huge designs require too much RAM in the full calculation
+    return des.Nrows() - des.Ncols();
+  try {
   Matrix pdes = pinv(des);
   Matrix R=IdentityMatrix(des.Nrows())-des*pdes;
-  return R.Trace();
+  return R.Trace();}
+  catch (...) {
+    cerr << "ols_dof: Error in determining the trace, resorting to basic calculation" << endl;
+  }
+  return des.Nrows() - des.Ncols();
 }
 
 int conjgrad(ColumnVector& x, const Matrix& A, const ColumnVector& b, int maxit,
-- 
GitLab