Public
Authored by Elias Pipping

Backward-compatible cleanup of fmatrixev.hh (makes fmatrixev.cc obsolete); version 2

With this, the test

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <iomanip>

#include <dune/common/fmatrix.hh>
#include <dune/common/fmatrixev.hh>
#include <dune/common/fvector.hh>

template <int n, typename T>
void printNative(Dune::FieldMatrix<T, n, n> const &mat)
{
  Dune::FieldVector<T, n> vec;
  Dune::FMatrixHelp::eigenValues(mat, vec);
  std::cout << "native: " << vec << std::endl;
}

template <int n, typename T>
void printLapack(Dune::FieldMatrix<T, n, n> const &mat)
{
  Dune::FieldVector<T, n> vec;
  Dune::FMatrixHelp::eigenValues<n, T>(mat, vec);
  std::cout << "lapack: " << vec << std::endl;
}

template <typename T>
void test0() {
    std::cout << "2x2: mat #0:" << std::endl;
    Dune::FieldMatrix<T, 2, 2> mat = {{1,2},{2,4-1e-4}};
    printNative(mat);
    printLapack(mat);

    std::cout << "Rescaling matrix: mat -> 1e-5*mat" << std::endl;
    mat *= 1e-5;
    printNative(mat);
    printLapack(mat);
}

template <typename T>
void test1() {
    std::cout << "3x3: mat #1:" << std::endl;
    Dune::FieldMatrix<T, 3, 3> mat = {{1,2,3},{2,4,5},{3,5,6}};
    printNative(mat);
    printLapack(mat);

    std::cout << "Rescaling matrix: mat -> 1e-5*mat" << std::endl;
    mat *= 1e-5;
    printNative(mat);
    printLapack(mat);
}

template <typename T>
void test2() {
    std::cout << "3x3: mat #2:" << std::endl;
    Dune::FieldMatrix<T, 3, 3> mat = {{6,0,0},{0,6,0},{0,0,6}};
    printNative(mat);
    printLapack(mat);

    std::cout << "Rescaling matrix: mat -> 1e-5*mat" << std::endl;
    mat *= 1e-5;
    printNative(mat);
    printLapack(mat);
}

int main() {
  std::cout << std::showpos << std::scientific << std::setprecision(16) << std::endl;

  std::cout << "float:\n";
  test0<float>();
  std::cout << "\n";
  test1<float>();
  std::cout << "\n";
  test2<float>();

  std::cout << "\n\ndouble:\n";
  test0<double>();
  std::cout << "\n";
  test1<double>();
  std::cout << "\n";
  test2<double>();
}

prints

float:
2x2: mat #0:
native: -2.0027160644531250e-05 +4.9999198913574219e+00
lapack: -1.9967555999755859e-05 +4.9999198913574219e+00
Rescaling matrix: mat -> 1e-5*mat
native: -2.0008883439004421e-10 +4.9999198381556198e-05
lapack: -2.0008883439004421e-10 +4.9999198381556198e-05

3x3: mat #1:
native: -5.1572775840759277e-01 +1.7091369628906250e-01 +1.1344814300537109e+01
lapack: -5.1572990417480469e-01 +1.7091475427150726e-01 +1.1344813346862793e+01
Rescaling matrix: mat -> 1e-5*mat
native: -5.1572460506577045e-06 +1.7091006156988442e-06 +1.1344813538016751e-04
lapack: -5.1572956181189511e-06 +1.7091514337153058e-06 +1.1344813538016751e-04

3x3: mat #2:
native: +6.0000000000000000e+00 +6.0000000000000000e+00 +6.0000000000000000e+00
lapack: +6.0000000000000000e+00 +6.0000000000000000e+00 +6.0000000000000000e+00
Rescaling matrix: mat -> 1e-5*mat
native: +5.9999998484272510e-05 +5.9999998484272510e-05 +6.0000005760230124e-05
lapack: +5.9999998484272510e-05 +5.9999998484272510e-05 +5.9999998484272510e-05


double:
2x2: mat #0:
native: -2.0000320003710215e-05 +4.9999200003200039e+00
lapack: -2.0000320003821237e-05 +4.9999200003200039e+00
Rescaling matrix: mat -> 1e-5*mat
native: -2.0000320004009129e-10 +4.9999200003200044e-05
lapack: -2.0000320003839723e-10 +4.9999200003200044e-05

3x3: mat #1:
native: -5.1572947158925464e-01 +1.7091518882717693e-01 +1.1344814282762078e+01
lapack: -5.1572947158925764e-01 +1.7091518882717918e-01 +1.1344814282762076e+01
Rescaling matrix: mat -> 1e-5*mat
native: -5.1572947158925171e-06 +1.7091518882717557e-06 +1.1344814282762079e-04
lapack: -5.1572947158925781e-06 +1.7091518882717905e-06 +1.1344814282762076e-04

3x3: mat #2:
native: +6.0000000000000000e+00 +6.0000000000000000e+00 +6.0000000000000000e+00
lapack: +6.0000000000000000e+00 +6.0000000000000000e+00 +6.0000000000000000e+00
Rescaling matrix: mat -> 1e-5*mat
native: +6.0000000000000008e-05 +6.0000000000000008e-05 +6.0000000000000022e-05
lapack: +6.0000000000000008e-05 +6.0000000000000008e-05 +6.0000000000000008e-05

which looks good.

Edited
fmatrixev.hh 20.5 KB
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment