Changeset 2735


Ignore:
Timestamp:
May 29, 2012, 12:03:15 PM (9 years ago)
Author:
Peter
Message:

fix matrix test and add new function trace

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/matrix.cc

    r2370 r2735  
    55  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
    66  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
    7   Copyright (C) 2010 Peter Johansson
     7  Copyright (C) 2010, 2012 Peter Johansson
    88
    99  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    2727#include "yat/utility/Matrix.h"
    2828
     29#include <cmath>
    2930#include <cstdio>
    3031#include <fstream>
     
    5253  test::Suite suite(argc, argv);
    5354
    54   suite.err() << "Testing matrix class" << std::endl;
     55  suite.err() << "Testing Matrix class" << std::endl;
    5556  utility::Matrix unit3x3(3,3);
    5657  for (size_t i=0; i<unit3x3.rows(); ++i)
     
    102103    suite.err() << "\tcopy constructor" << std::endl;
    103104    utility::Matrix m2(m5);
    104     suite.add(m2.rows()==m5.rows());
    105     suite.add(m2.columns()==m5.columns());
    106     suite.add(m2==m5);
    107     suite.add(&m2 != &m5);
     105    if (!suite.add(m2.rows()==m5.rows()))
     106      suite.err() << "m2.rows() != m5.rows()\n";
     107    else if (!suite.add(m2.columns()==m5.columns()))
     108      suite.err() << "m2.columns != m5.columns()\n";
     109    else if (!suite.add(&m2 != &m5))
     110      suite.err() << "m2 and m5 are the same instance\n";
     111    else {
     112      for (size_t i=0; i<m2.rows(); ++i)
     113        for (size_t j=0; j<m2.columns(); ++j) {
     114          if (m2(i,j)!=m5(i,j)) {
     115            if (std::isnan(m2(i,j)) && std::isnan(m5(i,j)))
     116              continue;
     117            suite.add(false);
     118            suite.err() << "element " << i << " " << j << ": " << m2(i,j)
     119                        << " " << m5(i,j) << "\n";
     120          }
     121        }
     122    }
    108123  }
    109124
     
    238253  m7 = utility::Matrix(0,0);
    239254
     255  utility::Matrix m8(10,10,2);
     256  if (!suite.add(suite.equal(trace(m8), 20))) {
     257    suite.err() << "trace failed\n";
     258  }
    240259  const utility::Matrix const_m(2,2,0);
    241260  utility::Matrix mutable_m(2,2,0);
     
    244263  suite.test_concept_mutable_container2d(mutable_m);
    245264
    246   suite.return_value();
     265  return suite.return_value();
    247266}
  • trunk/yat/utility/Matrix.cc

    r2689 r2735  
    77  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
    88  Copyright (C) 2009, 2010 Peter Johansson
     9  Copyright (C) 2012 Peter Johansson
    910
    1011  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    574575  }
    575576
     577
     578  double trace(const Matrix& m)
     579  {
     580    size_t n = std::min(m.rows(), m.columns());
     581    double sum = 0.0;
     582    for (size_t i=0; i<n; ++i)
     583      sum += m(i,i);
     584    return sum;
     585  }
     586
    576587}}} // of namespace utility, yat and thep
  • trunk/yat/utility/Matrix.h

    r2689 r2735  
    99  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
    1010  Copyright (C) 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
     11  Copyright (C) 2012 Peter Johansson
    1112
    1213  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    618619  Vector operator*(const VectorBase&, const Matrix&);
    619620
     621  /**
     622     \brief Trace of matrix
     623
     624     \relates Matrix
     625
     626     \return sum of diagonal elements
     627
     628     \since New in yat 0.9
     629  */
     630  double trace(const Matrix&);
     631
    620632}}} // of namespace utility, yat, and theplu
    621633
Note: See TracChangeset for help on using the changeset viewer.