Helper.h 6.18 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef HELPER_H
#define HELPER_H
#define _GLIBCXX_USE_CXX11_ABI 0
#include <iostream>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/interpreter.h>
#include <stdarg.h>
#include <initializer_list>
class Helper
{
public:
    static void init()
    {
        string_vector argv(2);
        argv(0) = "embedded";
        argv(1) = "-q";
        octave_main(2, argv.c_str_vec(), 1);
        //octave_debug=1;
        //feval ("pkg", ovl ("load", "all"), 0);
    }

    static octave_value_list convertToOctaveValueList(double a)
    {
        octave_value_list in;
        in(0) = a;

        return in;
    }

    static octave_value_list convertToOctaveValueList(Matrix a)
    {
        octave_value_list in;
        in(0) = a;

        return in;
    }

    static octave_value_list convertToOctaveValueList(RowVector a)
    {
        octave_value_list in;
        in(0) = a;

        return in;
    }

    static octave_value_list convertToOctaveValueList(ColumnVector a)
    {
        octave_value_list in;
        in(0) = a;

        return in;
    }

    static octave_value_list convertToOctaveValueList(double a, double b)
    {
        octave_value_list in;
        in(0) = a;
        in(1) = b;

        return in;
    }

    static octave_value_list convertToOctaveValueList(std::initializer_list<double> args)
    {
        octave_value_list in;
        int counter = 0;
        for(double element : args) {
            in(counter) = octave_value(element);
            ++counter;
        }

        return in;
    }

    static octave_value_list convertToOctaveValueList(Matrix a, double b)
    {
        octave_value_list in;
        in(0) = a;
        in(1) = b;
        return in;
    }

    static octave_value_list convertToOctaveValueList(RowVector a, double b)
    {
        octave_value_list in;
        in(0) = a;
        in(1) = b;

        return in;
    }

    static octave_value_list convertToOctaveValueList(ColumnVector a, double b)
    {
        octave_value_list in;
        in(0) = a;
        in(1) = b;

        return in;
    }

    static octave_value_list callOctaveFunction(octave_value_list in, std::string functionName,int argsOut)
    {

        /*octave_idx_type n = 2;
        octave_value_list in;

        for(octave_idx_type i = 0; i < n; i++)
            in(i) = octave_value(5 * (i + 2));

        octave_value_list out = feval("gcd", in, 1);

        if(!error_state && out.length() > 0)
            std::cout << "GCD of [" << in(0).int_value() << ", " << in(1).int_value() << "] is " << out(0).int_value()
                      << std::endl;
        else
            std::cout << "invalid\n";

        clean_up_and_exit(0);*/
  /*      if(functionName=="eigs")
            return feval(functionName, in, 2);
        else if(functionName=="kmeans")
            return feval(functionName, in, 2);
*/
        return feval(functionName, in, argsOut);
    }

    static int callOctaveFunctionIntFirstResult(octave_value_list in, std::string functionName, int argsOut)
    {
        // printf("callOctaveFunctionIntFirstResult pre return functionName: %s\n",functionName.c_str());
        return callOctaveFunction(in, functionName,argsOut)(0).int_value();
    }

    static double callOctaveFunctionDoubleFirstResult(octave_value_list in, std::string functionName, int argsOut)
    {
        // printf("callOctaveFunctionDoubleFirstResult pre return functionName: %s\n",functionName.c_str());
        return callOctaveFunction(in, functionName,argsOut)(0).double_value();
    }

    static Matrix callOctaveFunctionMatrixFirstResult(octave_value_list in, std::string functionName, int argsOut)
    {
        return callOctaveFunction(in, functionName,argsOut)(0).matrix_value();
    }
    static ColumnVector callOctaveFunctionColumnVectorFirstResult(octave_value_list in, std::string functionName, int argsOut)
    {
        printf("pre Call %s\n", functionName.c_str());
        try {
            in=octave_value_list();
            octave_value_list list = callOctaveFunction(in, functionName,argsOut);
            printf("post Call %s\n", functionName.c_str());
            return list(0).array_value().as_column();
        } catch(const std::exception& e) {
            printf("%s\n", e.what());
        }
        return ColumnVector();
    }

    static RowVector callOctaveFunctionRowVectorFirstResult(octave_value_list in, std::string functionName, int argsOut)
    {
        return callOctaveFunction(in, functionName,argsOut)(0).array_value().as_row();
    }

    static int callOctaveFunctionIntSecondResult(octave_value_list in, std::string functionName, int argsOut)
    {
        return callOctaveFunction(in, functionName,argsOut)(1).int_value();
    }

    static double callOctaveFunctionDoubleSecondResult(octave_value_list in, std::string functionName, int argsOut)
    {
        return callOctaveFunction(in, functionName,argsOut)(1).double_value();
    }

    static Matrix callOctaveFunctionMatrixSecondResult(octave_value_list in, std::string functionName, int argsOut)
    {
        return callOctaveFunction(in, functionName,argsOut)(1).matrix_value();
    }
    static ColumnVector callOctaveFunctionColumnVectorSecondResult(octave_value_list in, std::string functionName, int argsOut)
    {
        return callOctaveFunction(in, functionName,argsOut)(1).array_value().as_column();
    }

    static RowVector callOctaveFunctionRowVectorSecondResult(octave_value_list in, std::string functionName, int argsOut)
    {
        return callOctaveFunction(in, functionName,argsOut)(1).array_value().as_row();
    }
    
    static Matrix getMatrixFromOctaveListFirstResult(octave_value_list list){
        return list(0).matrix_value();
    }
    
    
    static RowVector getRowVectorFromOctaveListFirstResult(octave_value_list list){
        return list(0).array_value().as_row();
    }
    
    
    static ColumnVector getColumnVectorFromOctaveListFirstResult(octave_value_list list){
        return list(0).array_value().as_column();
    }
    
    
    static double getDoubleFromOctaveListFirstResult(octave_value_list list){
        return list(0).double_value();
    }
    
    
    static int getIntFromOctaveListFirstResult(octave_value_list list){
        return list(0).int_value();
    }
};

#endif // HELPER_H