-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfun.cpp
More file actions
197 lines (165 loc) · 4.75 KB
/
fun.cpp
File metadata and controls
197 lines (165 loc) · 4.75 KB
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
#include <iostream>
#include <stdlib.h>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/interpreter.h>
#include <math.h>
#include <string>
#include "fun.h"
extern "C"
{
int fun (octf *inp)
{
static octave::interpreter interpreter;
bool status = interpreter.initialized();
if(status==false)
{
interpreter.initialize ();
int status_exec = interpreter.execute ();
if (status_exec != 0)
{
std::cerr << "creating embedded Octave interpreter failed!"
<< std::endl;
}
}
try
{
unsigned int k=0;
////////////////First Input////////////////
octave_value_list in;
if(inp->size_input1[1]!=0 || inp->size_input1[2]!=0)
{
Matrix inMatrix_x(inp->size_input1[1],inp->size_input1[2]);
for( unsigned int i = 0; i < inp->size_input1[1]; i++ )
{
for( unsigned int j = 0; j < inp->size_input1[2]; j++ )
{
inMatrix_x(i, j) = inp->input1[k];
k++;
}
}
in(0) = inMatrix_x;
Matrix mIn(in(0).matrix_value());
//std::cout << "-------Input Matrix-------" << "\n";
//std::cout << mIn << "\n";
//std::cout << "--------------------" << "\n";
}
////////////////Second Input////////////////
if(inp->size_input2[1]!=0 || inp->size_input2[2]!=0)
{
octave_value_list in2;
Matrix inMatrix_x2(inp->size_input2[1],inp->size_input2[2]);
k=0;
for( unsigned int i = 0; i < inp->size_input2[1]; i++ )
{
for( unsigned int j = 0; j < inp->size_input2[2]; j++ )
{
inMatrix_x2(i, j) = inp->input2[k];
k++;
}
}
in(1) = inMatrix_x2;
Matrix mIn2(in(1).matrix_value());
//std::cout << "-------Input Matrix-------" << "\n";
//std::cout << mIn2 << "\n";
//std::cout << "--------------------" << "\n";
}
////////////////Third Input////////////////
if(inp->size_input3[1]!=0 || inp->size_input3[2]!=0)
{
octave_value_list in3;
Matrix inMatrix_x3(inp->size_input3[1],inp->size_input3[2]);
k=0;
for( unsigned int i = 0; i < inp->size_input3[1]; i++ )
{
for( unsigned int j = 0; j < inp->size_input3[2]; j++ )
{
inMatrix_x3(i, j) = inp->input3[k];
k++;
}
}
in(2) = inMatrix_x3;
Matrix mIn3(in(2).matrix_value());
//std::cout << "-------Input Matrix-------" << "\n";
//std::cout << mIn3 << "\n";
//std::cout << "--------------------" << "\n";
}
//-------------------------------------------------------
/* if(inp->name2)
{
in(4) = inp->name2;
}
*/
//octave::feval ("pkg", ovl("load", "signal"), 0);
if(inp->package)
{
std::cout << "Loading package: " << inp->package << "\n";
octave::feval ("pkg", ovl("load", inp->package), 0);
}
octave_value_list out = octave::feval (inp->name1, in, 1);
////////////////First Output//////////////////////////
int nouts = out.length();
//std::cout << "number of outputs are:" << nouts <<'\n';
inp->out_count=nouts;
Matrix mOut(out(0).matrix_value());
//std::cout << mOut << "\n";
int row1 = mOut.rows();
int col1 = mOut.columns();
//std::cout << "--output matrix: " << row1 << "X" << col1 << "\n";
inp->size_output1[1] = row1;
inp->size_output1[2] = col1;
int len = row1*col1;
inp->output1 = new double[len];
k=0;
for(unsigned int i=0;i<row1;i++)
{
for(unsigned int j=0;j<col1;j++)
{
inp->output1[k]=mOut(k);
k++;
}
}
//Matrix mOut2(out(1).matrix_value());
//(out(1)).exists();
//std::cout << out << '\n';
///-----------------------------------------------------
////////////////Second Output//////////////////////////
if(nouts>1)
{
Matrix mOut2(out(1).matrix_value());
//std::cout << mOut << "\n";
int row2 = mOut2.rows();
int col2 = mOut2.columns();
//std::cout << "--output matrix2: " << row2 << "X" << col2 << "\n";
inp->size_output2[1] = row2;
inp->size_output2[2] = col2;
//std::cout << "--output matrix2 inp->: " << inp->size_output2[1] << "X" << inp->size_output2[1] << "\n";
int len2 = row2*col2;
inp->output2 = new double[len2];
k=0;
for(unsigned int i=0;i<row2;i++)
{
for(unsigned int j=0;j<col2;j++)
{
inp->output2[k]=mOut2(k);
k++;
}
}
}
///-----------------------------------------------------
}
catch (const octave::exit_exception& ex)
{
std::cerr << "Octave interpreter exited with status = "
<< ex.exit_status () << std::endl;
return 1;
}
catch (const octave::execution_exception&)
{
std::cerr << "error encountered in Octave evaluator!" << std::endl;
return 1;
}
return 0;
}
}