KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
KernelExpansion.cpp
Go to the documentation of this file.
1 /*
2  * KernelExpansion.cpp
3  *
4  * Created on: 22.07.2013
5  * Author: CreaByte
6  */
7 
8 #include "kermorpp.h"
9 #include <iostream>
10 #include <typeinfo>
11 
12 using namespace std;
13 using namespace Eigen;
14 
15 namespace kermorpp {
16 
17 ostream & operator<<(ostream & s, const KernelExpansion &k) {
18 
19  s << "KernelExpansion centers: " << k.centers.rows() << " x " << k.centers.cols();
20  s << ", " << "coeffs: " << k.coeffs.rows() << " x " << k.coeffs.cols();
21  s << ", kernel: " << typeid(k.kernel).name() << ": " << *k.kernel;
22  return s;
23 
24 }
25 
26 KernelExpansion.KernelExpansion() :
27  coeffs(MatrixXd(0, 0)), centers(MatrixXd(0, 0)), kernel(0) {
28 }
29 
31  kernel->~RBFKernel();
32  free(kernel);
33 }
34 
35 MatrixXd KernelExpansion.evaluate(MatrixXd points) {
36  if (centers.rows() != points.rows()) {
37  cerr << "Argument dimension mismatch. Center dimension:"
38  << centers.rows() << " vs argument dimension: " << points.rows()
39  << endl;
40  exit(-1);
41  }
42 
43 #if DEBUG
44  cout << "evaluating kexp.. " << endl << "centers: " << centers << endl
45  << "points: " << points << endl;
46 #endif
47 
48  MatrixXd kvec = kernel->evaluate(centers, points);
49 
50 #if DEBUG
51  cout << "done eval kexp. kernel vector: " << kvec << ", coeffs: " << coeffs << endl;
52 #endif
53 
54  return coeffs * kvec;
55 }
56 
58 
59  KernelExpansion* res = new KernelExpansion();
60  string file = dir + DIR_SEPARATOR + "kernel.bin";
61  VectorXd kdata = Util.loadVector(file);
62  switch ((int) kdata(0)) {
63  case 1:
64  res->kernel = new Gaussian(kdata(1));
65  break;
66  case 2:
67  res->kernel = new Wendland(kdata(1), kdata(2), kdata(3));
68  break;
69  default:
70  cerr << "Unknown kernel data: " << kdata << endl;
71  break;
72  }
73 
74  res->centers = Util.loadMatrix(dir + DIR_SEPARATOR + "centers.bin");
75 
76 #if DEBUG
77  cout << "done loading centers: " << res.centers << endl;
78 #endif
79 
80  res->coeffs = Util.loadMatrix(file = dir + DIR_SEPARATOR + "coeffs.bin");
81 
82 #if DEBUG
83  cout << "done loading coeffs: " << res.coeffs << endl;
84 #endif
85 
86  return res;
87 }
88 
89 }
90 
MatrixXd evaluate(MatrixXd x, MatrixXd y)
Definition: RBFKernel.cpp:36
#define DIR_SEPARATOR
Definition: kermorpp.h:16
MatrixXd evaluate(MatrixXd points)
static MatrixXd loadMatrix(string file)
Definition: Util.cpp:56
static VectorXd loadVector(string file)
Definition: Util.cpp:17
static KernelExpansion * loadFrom(string dir)
RBFKernel * kernel
Definition: kermorpp.h:64
virtual ~RBFKernel()
Definition: RBFKernel.cpp:22
ostream & operator<<(ostream &s, const KernelExpansion &k)