GTK+ IOStream  Beta
<< GTK+ >> add C++ IOStream operators to GTK+. Now with extra abilities ... like network serialisation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Octave.C
Go to the documentation of this file.
1 /* Copyright 2000-2013 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 
18 #include "Octave.H"
19 #include <iostream>
20 
21 #include <octave/config.h>
22 #include <octave/octave.h>
23 #include <octave/oct-obj.h>
24 #include <octave/oct-map.h>
25 
26 // taken from octave/toplev.h
27 typedef void (*octave_exit_func) (int);
28 extern OCTINTERP_API octave_exit_func octave_exit;
29 
30 extern OCTINTERP_API octave_value_list
31 feval (const std::string& name, const octave_value_list& args = octave_value_list (), int nargout = 0);
32 
33 Octave::Octave(const vector<string> &args) {
34  int argc=args.size();
35  vector<const char *> argsIn(argc);
36  for (int i=0; i<argc; i++){
37  argsIn[i]=&args[i][0];
38  cout<<argsIn[i]<<endl;
39  }
40  int embedded=1;
41  octave_main((int)argsIn.size(), (char**)&argsIn[0], embedded);
42 }
43 
46  octave_exit(0); // close the octave instance
47 }
48 
49 template<class TYPE>
50 vector<Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic> > Octave::runM(const char* commandName, vector<Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic> > &in) {
51  octave_value_list input;
52 
53  // this code can most likely be sped up using Eigen::Matrix.block operations for memory copies.
54  input.resize(in.size());
55  for (int i=0; i<in.size(); i++) {
56  Matrix m(in[i].rows(),in[i].cols());
57  for (int j=0; j<in[i].rows(); j++)
58  for (int k=0; k<in[i].cols(); k++)
59  m.elem(j,k)=(double)in[i](j,k);
60  input(i)=m;
61  }
62 
63  octave_value_list output;
64  //output = feval(string(commandName), input, retCount);
65  output = feval(string(commandName), input);
66 
67  vector<Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic> > out(output.length());
68  for (int i=0; i<out.size(); i++) {
69  Matrix m=output(i).matrix_value();
70  Eigen::Matrix<TYPE, Eigen::Dynamic, Eigen::Dynamic> eigenM(m.rows(),m.columns());
71  for (int j=0; j<m.rows(); j++)
72  for (int k=0; k<m.columns(); k++)
73  eigenM(j,k)=(TYPE)m.elem(j,k);
74  out[i]=eigenM;
75  }
76 
77  return out;
78 }
79 template vector<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> > Octave::runM(const char* commandName, vector<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> > &in);
80 template vector<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> > Octave::runM(const char* commandName, vector<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> > &in);