Weka M5Rules encoding - weka

I am beginner with Weka data mining tool.
I am wondering how can I encode an output model of M5Rules into a C program that could be embedded in an energy consumption simulator. The complete model is described below.
A priori I would expect a kind of if-then-else encoding in C, but there are strange expressions in IF conditions of these rules, which I do not understand.
E.g., in Rule: 1, is this IF condition equivalent to ExecTime <= 959678 && ExecTime > 767156?
How do one interpret the conditions in Rule: 15 and Rule: 16 with respect to 849021 (are these two conditions equivalent)?
I will appreciate any help towards a possible C encoding of these rules :-).
Thanks a lot!
JH
************************ M5Rules output
Rule: 1
IF
ExecTime <= 959678
ExecTime <= 841942.5
ExecTime > 767156
ExecTime <= 797926.5
THEN
EnergyTotal =
0.0474 * _H08Y299NEeOETou3oNmhPQ.x
+ 0.0444 * _H08aFt9NEeOETou3oNmhPQ.x
+ 0.0353 * _H08Yxt9NEeOETou3oNmhPQ.x
+ 0.1149 * _H08ZiN9NEeOETou3oNmhPQ.x
- 3.6692 * _H08aH99NEeOETou3oNmhPQ.y
- 0.0622 * _H08ZRt9NEeOETou3oNmhPQ.y
+ 0.0002 * ExecTime
+ 9283.9945 [905/5.48%]
Rule: 2
IF
ExecTime <= 990800
ExecTime > 841942.5
ExecTime > 891748.5
ExecTime <= 953522.5
THEN
EnergyTotal =
5.7526 * _H08Y999NEeOETou3oNmhPQ.x
+ 0.0241 * _H08aFt9NEeOETou3oNmhPQ.x
- 5.473 * _H08Yxt9NEeOETou3oNmhPQ.x
- 4.7454 * _H08aH99NEeOETou3oNmhPQ.y
- 0.0244 * _H08ZRt9NEeOETou3oNmhPQ.y
+ 0.0002 * ExecTime
+ 9962.3473 [1076/7.136%]
Rule: 3
IF
ExecTime <= 922644.5
ExecTime > 841942.5
ExecTime <= 875547
ExecTime <= 869750.5
THEN
EnergyTotal =
0.0538 * _H08aFt9NEeOETou3oNmhPQ.x
+ 0.0522 * _H08Yxt9NEeOETou3oNmhPQ.x
- 0.2158 * _H08ZiN9NEeOETou3oNmhPQ.x
- 3.025 * _H08ZRt9NEeOETou3oNmhPQ.y
+ 0.0858 * _H08aMd9NEeOETou3oNmhPQ.x
- 4.3856 * _H08Yud9NEeOETou3oNmhPQ.y
+ 0.0001 * ExecTime
+ 9675.1495 [890/5.322%]
Rule: 4
IF
ExecTime > 922644.5
ExecTime <= 1144991.5
ExecTime <= 1100913.5
ExecTime > 990800
ExecTime > 1023515
THEN
EnergyTotal =
0.0177 * _H08aFt9NEeOETou3oNmhPQ.x
+ 0.0212 * _H08Yxt9NEeOETou3oNmhPQ.x
- 0.0141 * _H08ZRt9NEeOETou3oNmhPQ.y
+ 0.2791 * _H08aMd9NEeOETou3oNmhPQ.x
+ 0.1004 * _H08ZAt9NEeOETou3oNmhPQ.x
+ 0.2871 * _H08ZvN9NEeOETou3oNmhPQ.x
+ 0.0029 * ExecTime
+ 7616.5061 [759/5.317%]
Rule: 5
IF
ExecTime > 922644.5
ExecTime <= 1175937.5
ExecTime > 1061802
ExecTime > 1112381.5
THEN
EnergyTotal =
0.1694 * _H08aFt9NEeOETou3oNmhPQ.x
+ 0.03 * _H08Yxt9NEeOETou3oNmhPQ.x
- 0.0678 * _H08ZRt9NEeOETou3oNmhPQ.y
+ 0.0004 * ExecTime
+ 10581.9568 [495/4.516%]
Rule: 6
IF
ExecTime <= 922644.5
ExecTime <= 768103.5
ExecTime > 721726.5
ExecTime <= 729278
ExecTime > 722094
ExecTime <= 724494
THEN
EnergyTotal =
0.4341 * _H08Y999NEeOETou3oNmhPQ.x
+ 0.0958 * _H08aFt9NEeOETou3oNmhPQ.x
+ 11.1267 * _H08Yxt9NEeOETou3oNmhPQ.x
+ 18.5669 * _H08ZiN9NEeOETou3oNmhPQ.x
- 0.2536 * _H08aH99NEeOETou3oNmhPQ.y
- 8.0408 * _H08ZRt9NEeOETou3oNmhPQ.y
+ 0.0942 * _H08aMd9NEeOETou3oNmhPQ.x
- 0.0121 * ExecTime
+ 17795.1451 [294/5.734%]
Rule: 7
IF
ExecTime <= 990800
ExecTime <= 869756.5
ExecTime <= 768103.5
ExecTime > 721636.5
ExecTime <= 730227.5
ExecTime <= 728964
THEN
EnergyTotal =
0.1634 * _H08Y299NEeOETou3oNmhPQ.x
+ 0.3896 * _H08Y999NEeOETou3oNmhPQ.x
+ 0.1997 * _H08aFt9NEeOETou3oNmhPQ.x
+ 0.1235 * _H08Yxt9NEeOETou3oNmhPQ.x
- 0.3961 * _H08ZRt9NEeOETou3oNmhPQ.y
+ 0.1817 * _H08aMd9NEeOETou3oNmhPQ.x
- 0.2828 * _H08Yud9NEeOETou3oNmhPQ.y
+ 0.4399 * _H08ZAt9NEeOETou3oNmhPQ.x
+ 0.0003 * ExecTime
+ 8867.5503 [402/4.404%]
Rule: 8
IF
ExecTime > 1061802
ExecTime <= 1266307
ExecTime > 1144257
ExecTime > 1193767.5
THEN
EnergyTotal =
-0.3281 * _H08Y299NEeOETou3oNmhPQ.x
+ 0.062 * _H08aFt9NEeOETou3oNmhPQ.x
+ 13.1349 * _H08Yxt9NEeOETou3oNmhPQ.x
- 0.0428 * _H08ZRt9NEeOETou3oNmhPQ.y
- 0.254 * _H08Yud9NEeOETou3oNmhPQ.y
+ 0.3602 * _H08ZAt9NEeOETou3oNmhPQ.x
+ 0.0028 * ExecTime
+ 8065.2375 [293/4.695%]
Rule: 9
IF
ExecTime > 1061802
ExecTime <= 1230073
THEN
EnergyTotal =
0.117 * _H08aFt9NEeOETou3oNmhPQ.x
+ 0.0723 * _H08Yxt9NEeOETou3oNmhPQ.x
- 0.0625 * _H08ZRt9NEeOETou3oNmhPQ.y
- 17.6704 * _H08ZvN9NEeOETou3oNmhPQ.x
+ 0.0051 * ExecTime
+ 5341.2406 [283/7.376%]
Rule: 10
IF
ExecTime > 1145764
ExecTime > 1349259.5
THEN
EnergyTotal =
0.1985 * _H08aFt9NEeOETou3oNmhPQ.x
+ 0.12 * _H08Yxt9NEeOETou3oNmhPQ.x
- 0.1002 * _H08ZRt9NEeOETou3oNmhPQ.y
+ 0.005 * ExecTime
+ 5325.0455 [281/6.55%]
Rule: 11
IF
ExecTime <= 922644.5
ExecTime > 869756.5
ExecTime > 881525
THEN
EnergyTotal =
0.151 * _H08Y299NEeOETou3oNmhPQ.x
+ 0.2828 * _H08aFt9NEeOETou3oNmhPQ.x
- 0.6376 * _H08ZiN9NEeOETou3oNmhPQ.x
- 0.0385 * _H08ZRt9NEeOETou3oNmhPQ.y
+ 0.0006 * ExecTime
+ 9300.7835 [253/4.516%]
Rule: 12
IF
ExecTime <= 917420.5
ExecTime > 768103.5
ExecTime <= 869756.5
ExecTime > 806810.5
THEN
EnergyTotal =
0.1013 * _H08Y299NEeOETou3oNmhPQ.x
+ 0.8919 * _H08aFt9NEeOETou3oNmhPQ.x
- 0.0522 * _H08ZRt9NEeOETou3oNmhPQ.y
- 0.0555 * _H08ZAt9NEeOETou3oNmhPQ.x
+ 0.0005 * ExecTime
+ 9049.7419 [228/4.466%]
Rule: 13
IF
ExecTime <= 917420.5
ExecTime > 768103.5
THEN
EnergyTotal =
0.1663 * _H08Y299NEeOETou3oNmhPQ.x
+ 0.3315 * _H08aFt9NEeOETou3oNmhPQ.x
- 0.0572 * _H08ZRt9NEeOETou3oNmhPQ.y
+ 0.0723 * _H08aMd9NEeOETou3oNmhPQ.x
- 0.0782 * _H08ZAt9NEeOETou3oNmhPQ.x
+ 0.0047 * ExecTime
+ 5620.2818 [696/9.48%]
Rule: 14
IF
ExecTime <= 1145764
ExecTime > 845905
ExecTime <= 959678
THEN
EnergyTotal =
-0.5334 * _H08Y999NEeOETou3oNmhPQ.x
+ 0.4001 * _H08aFt9NEeOETou3oNmhPQ.x
- 0.7092 * _H08ZRt9NEeOETou3oNmhPQ.y
+ 0.3418 * _H08aMd9NEeOETou3oNmhPQ.x
+ 0.0004 * ExecTime
+ 9771.3597 [269/4.341%]
Rule: 15
IF
ExecTime > 849021
ExecTime > 1145764
ExecTime > 1276509
THEN
EnergyTotal =
0.9858 * _H08aFt9NEeOETou3oNmhPQ.x
- 0.7268 * _H08ZRt9NEeOETou3oNmhPQ.y
+ 0.3091 * _H08aMd9NEeOETou3oNmhPQ.x
+ 0.0006 * ExecTime
+ 10999.3844 [193/4.635%]
Rule: 16
IF
ExecTime > 849021
THEN
EnergyTotal =
0.682 * _H08aFt9NEeOETou3oNmhPQ.x
- 15.4759 * _H08ZiN9NEeOETou3oNmhPQ.x
- 0.4643 * _H08ZRt9NEeOETou3oNmhPQ.y
+ 0.6401 * _H08aMd9NEeOETou3oNmhPQ.x
- 17.6902 * _H08Yud9NEeOETou3oNmhPQ.y
+ 15.1697 * _H08ZvN9NEeOETou3oNmhPQ.x
+ 0.005 * ExecTime
+ 5395.084 [268/9.01%]
Rule: 17
IF
ExecTime > 657837
ExecTime <= 730227.5
THEN
EnergyTotal =
1.749 * _H08Y299NEeOETou3oNmhPQ.x
+ 1.5395 * _H08aFt9NEeOETou3oNmhPQ.x
- 0.686 * _H08aH99NEeOETou3oNmhPQ.y
- 1.0553 * _H08ZRt9NEeOETou3oNmhPQ.y
+ 22.5421 * _H08aMd9NEeOETou3oNmhPQ.x
- 0.6172 * _H08Yud9NEeOETou3oNmhPQ.y
+ 0.0005 * ExecTime
+ 8690.2253 [161/37.564%]
Rule: 18
EnergyTotal =
12.3579 * _H08Y299NEeOETou3oNmhPQ.x
+ 16.0776 * _H08aMd9NEeOETou3oNmhPQ.x
- 10.38 * _H08ZvN9NEeOETou3oNmhPQ.x
+ 0.005 * ExecTime
+ 5484.0874 [252/25.693%]

Rule 1 is equivalent to ExecTime <= 797926.5 && ExecTime > 767156 (other conditions are spurious)
Rule 15 equals to ExecTime > 1276509
Rule 16 equals to ExecTime > 849021 (&& ExecTime <= 1276509)
(first rule that match wins)

Related

C++ code runs fine in Visual Studio (windows) but gives a segmentation fault in CodeLite (Linux)

My code compiles and runs without error in Visual Studio, however I need to to run in CodeLite on Linux and it gives me a segmentation fault for the same code.
For reference this is my code:
#include <string>
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <tuple>
using namespace std;
tuple<vector<double>, vector<double>, vector<double>> RK4() {
//open parameters.txt, put data into a vector
ifstream fin("parameters.txt");
vector<double> data;
data.reserve(8);
double element;
while (fin >> element) {
data.push_back(element);
}
//define tspan
vector<double> tspan(2);
tspan[0] = 0.0;
tspan[1] = data[7];
//define y0
vector<double> y0(4);
//CHANGE TO DATA[4], DATA[5]
const double a = 3.141592653589793238462643383279;
y0[0] = data[4];
y0[1] = data[5];
y0[2] = 0.0;
y0[3] = 0.0;
double theta1 = y0[0];
double theta2 = y0[1];
double omega1 = y0[2];
double omega2 = y0[3];
//define stepSize
double stepSize;
stepSize = data[6];
//define range
int range = int(tspan[1] / stepSize);
//define other constants
double m1, m2, l1, l2;
m1 = data[0];
m2 = data[1];
l1 = data[2];
l2 = data[3];
double g = 9.81;
//define y, t vectors
vector<double> y1(range);
vector<double> y2(range);
vector<double> y3(range);
vector<double> y4(range);
vector<double> t(range);
for (double i = 0.0; i < 1.0 * range; i++) {
t[i] = i * stepSize;
}
//enter y0 into first value
y1[0] = theta1;
y2[0] = theta2;
y3[0] = omega1;
y4[0] = omega2;
//loop to find y, t vectors
for (int i = 0; i < range - 1; i++) {
//finding all k values:
//k1
double dTheta1_1 = y3[i];
double dOmega1_1 = (-g * (2 * m1 + m2) * sin(y1[i]) - m2 * g * sin(y1[i] - 2 * y2[i]) - 2 * sin(y1[i] - y2[i]) * m2 * (pow(y4[i], 2) * l2 + pow(y3[i], 2) * l1 * cos(y1[i] - y2[i]))) / (l1 * (2 * m1 + m2 - m2 * cos(2 * y1[i] - 2 * y2[i])));
double dTheta2_1 = y4[i];
double dOmega2_1 = (2 * sin(y1[i] - y2[i]) * (pow(y3[i], 2) * l1 * (m1 + m2) + g * (m1 + m2) * cos(y1[i]) + pow(y4[i], 2) * l2 * m2 * cos(y1[i] - y2[i]))) / (l2 * (2 * m1 + m2 - m2 * cos(2 * y1[i] - 2 * y2[i])));
//k2
double dTheta1_2 = y3[i] + 0.5 * stepSize * dTheta1_1;
double dOmega1_2 = (-g * (2 * m1 + m2) * sin(y1[i] + 0.5 * stepSize * dTheta1_1) - m2 * g * sin((y1[i] + 0.5 * stepSize * dTheta1_1) - 2 * (y2[i] + 0.5 * stepSize * dTheta2_1)) - 2 * sin((y1[i] + 0.5 * stepSize * dTheta1_1) - (y2[i] + 0.5 * stepSize * dTheta2_1)) * m2 * (pow(y4[i] + 0.5 * stepSize * dOmega2_1, 2) * l2 + pow(y3[i] + 0.5 * stepSize * dOmega1_1, 2) * l1 * cos((y1[i] + 0.5 * stepSize * dTheta1_1) - (y2[i] + 0.5 * stepSize * dTheta2_1)))) / (l1 * (2 * m1 + m2 - m2 * cos(2 * (y1[i] + 0.5 * stepSize * dTheta1_1) - 2 * (y2[i] + 0.5 * stepSize * dTheta2_1))));
double dTheta2_2 = y4[i] + 0.5 * stepSize * dTheta2_1;
double dOmega2_2 = (2 * sin((y1[i] + 0.5 * stepSize * dTheta1_1) - (y2[i] + 0.5 * stepSize * dTheta2_1)) * (pow(y3[i] + 0.5 * stepSize * dOmega1_1, 2) * l1 * (m1 + m2) + g * (m1 + m2) * cos(y1[i] + 0.5 * stepSize * dTheta1_1) + pow(y4[i] + 0.5 * stepSize * dOmega2_1, 2) * l2 * m2 * cos((y1[i] + 0.5 * stepSize * dTheta1_1) - (y2[i] + 0.5 * stepSize * dTheta2_1)))) / (l2 * (2 * m1 + m2 - m2 * cos(2 * (y1[i] + 0.5 * stepSize * dTheta1_1) - 2 * (y2[i] + 0.5 * stepSize * dTheta2_1))));
//k3
double dTheta1_3 = y3[i] + 0.5 * stepSize * dTheta1_2;
double dOmega1_3 = (-g * (2 * m1 + m2) * sin(y1[i] + 0.5 * stepSize * dTheta1_2) - m2 * g * sin((y1[i] + 0.5 * stepSize * dTheta1_2) - 2 * (y2[i] + 0.5 * stepSize * dTheta2_2)) - 2 * sin((y1[i] + 0.5 * stepSize * dTheta1_2) - (y2[i] + 0.5 * stepSize * dTheta2_2)) * m2 * (pow(y4[i] + 0.5 * stepSize * dOmega2_2, 2) * l2 + pow(y3[i] + 0.5 * stepSize * dOmega1_2, 2) * l1 * cos((y1[i] + 0.5 * stepSize * dTheta1_2) - (y2[i] + 0.5 * stepSize * dTheta2_2)))) / (l1 * (2 * m1 + m2 - m2 * cos(2 * (y1[i] + 0.5 * stepSize * dTheta1_2) - 2 * (y2[i] + 0.5 * stepSize * dTheta2_2))));
double dTheta2_3 = y4[i] + 0.5 * stepSize * dTheta2_2;
double dOmega2_3 = (2 * sin((y1[i] + 0.5 * stepSize * dTheta1_2) - (y2[i] + 0.5 * stepSize * dTheta2_2)) * (pow(y3[i] + 0.5 * stepSize * dOmega1_2, 2) * l1 * (m1 + m2) + g * (m1 + m2) * cos(y1[i] + 0.5 * stepSize * dTheta1_2) + pow(y4[i] + 0.5 * stepSize * dOmega2_2, 2) * l2 * m2 * cos((y1[i] + 0.5 * stepSize * dTheta1_2) - (y2[i] + 0.5 * stepSize * dTheta2_2)))) / (l2 * (2 * m1 + m2 - m2 * cos(2 * (y1[i] + 0.5 * stepSize * dTheta1_2) - 2 * (y2[i] + 0.5 * stepSize * dTheta2_2))));
//k4
double dTheta1_4 = y3[i] + stepSize * dTheta1_3;
double dOmega1_4 = (-g * (2 * m1 + m2) * sin(y1[i] + stepSize * dTheta1_3) - m2 * g * sin((y1[i] + stepSize * dTheta1_3) - 2 * (y2[i] + stepSize * dTheta2_3)) - 2 * sin((y1[i] + stepSize * dTheta1_3) - (y2[i] + stepSize * dTheta2_3)) * m2 * (pow(y4[i] + stepSize * dOmega2_3, 2) * l2 + pow(y3[i] + stepSize * dOmega1_3, 2) * l1 * cos((y1[i] + stepSize * dTheta1_3) - (y2[i] + stepSize * dTheta2_3)))) / (l1 * (2 * m1 + m2 - m2 * cos(2 * (y1[i] + stepSize * dTheta1_3) - 2 * (y2[i] + stepSize * dTheta2_3))));
double dTheta2_4 = y4[i] + stepSize * dTheta2_3;
double dOmega2_4 = (2 * sin((y1[i] + stepSize * dTheta1_3) - (y2[i] + stepSize * dTheta2_3)) * (pow(y3[i] + stepSize * dOmega1_3, 2) * l1 * (m1 + m2) + g * (m1 + m2) * cos(y1[i] + stepSize * dTheta1_3) + pow(y4[i] + stepSize * dOmega2_3, 2) * l2 * m2 * cos((y1[i] + stepSize * dTheta1_3) - (y2[i] + stepSize * dTheta2_3)))) / (l2 * (2 * m1 + m2 - m2 * cos(2 * (y1[i] + stepSize * dTheta1_3) - 2 * (y2[i] + stepSize * dTheta2_3))));
double theta1New = y1[i] + (stepSize / 6.0) * (dTheta1_1 + 2 * dTheta1_2 + 2 * dTheta1_3 + dTheta1_4);
double omega1New = y3[i] + (stepSize / 6.0) * (dOmega1_1 + 2 * dOmega1_2 + 2 * dOmega1_3 + dOmega1_4);
double theta2New = y2[i] + (stepSize / 6.0) * (dTheta2_1 + 2 * dTheta2_2 + 2 * dTheta2_3 + dTheta2_4);
double omega2New = y4[i] + (stepSize / 6.0) * (dOmega2_1 + 2 * dOmega2_2 + 2 * dOmega2_3 + dOmega2_4);
// updating y arrays
y1[i + 1] = theta1New;
y2[i + 1] = theta2New;
y3[i + 1] = omega1New;
y4[i + 1] = omega2New;
}
return make_tuple(y1, y2, t);
}
int main() {
//open parameters.txt, put data into a vector
ifstream fin("parameters.txt");
vector<double> data;
data.reserve(8);
double element;
while (fin >> element) {
data.push_back(element);
}
//define tspan
vector<double> tspan(2);
tspan[0] = 0.0;
tspan[1] = data[7];
//define stepSize
double stepSize = data[6];
//define other constants
double l1 = data[2];
double l2 = data[3];
//get y1, y2, t from RK4 function
auto temp = RK4();
vector<double> y1 = get<0>(temp);
vector<double> y2 = get<1>(temp);
vector<double> t = get<2>(temp);
//define range
int const range = static_cast<int>(y1.size());
vector<double> x_1(range), y_1(range), x_2(range), y_2(range);
//define x_1, x_2, y_1, y_2
for (int i = 0; i < range; i++) {
x_1[i] = { sin(y1[i]) * l1 };
y_1[i] = { -cos(y1[i]) * l1 };
x_2[i] = { sin(y1[i]) * l1 + sin(y2[i]) * l2 };
y_2[i] = { -cos(y1[i]) * l1 - cos(y2[i]) * l2 };
}
//writing x,y positions at time t to output.txt
ofstream myfile;
myfile.open("output.txt");
if (myfile.is_open()) {
myfile << "t: " << endl;
for (int i = 0; i < range; i++) {
myfile << t[i] << " ";
}
cout << endl;
myfile << "x_1: " << endl;
for (int i = 0; i < range; i++) {
myfile << x_1[i] << " ";
}
cout << endl;
myfile << "y_1: " << endl;
for (int i = 0; i < range; i++) {
myfile << y_1[i] << " ";
}
cout << endl;
myfile << "x_2: " << endl;
for (int i = 0; i < range; i++) {
myfile << x_2[i] << " ";
}
cout << endl;
myfile << "y_2: " << endl;
for (int i = 0; i < range; i++) {
myfile << y_2[i] << " ";
}
cout << endl;
myfile.close();
}
else { cout << "Unable to open file"; }
return 0;
}
In both cases "parameters.txt" is in the working directory. Why does the operating system/compiler I use affect the outcome? What is the problem?

Converting RGB to Luv

i'm trying to convert an rgb image to Luv, i have some problem. The L component is good, but when i show the u and v component both are black(all pixels have value 0).
for (int i = 0; i<height; i++)
for (int j = 0; j<width; j++)
{
Vec3b v3 = src.at<Vec3b>(i, j);
float b = ((float)v3[0]) / 255;
float g = ((float)v3[1]) / 255;
float r = ((float)v3[2]) / 255;
float x = r * 0.412453 + g * 0.357580 + b * 0.180423;
float y = r * 0.212671 + g * 0.715160 + b * 0.072169;
float z = r * 0.019334 + g * 0.119193 + b * 0.950227;
//L
if (y > 0.008856) {
l_mat.at<uchar>(i, j) = 255 / 100 * (116 * pow(y, 1.0 / 3.0));
dst.at<Vec3b>(i, j)[0] = 255 / 100 * (116 * pow(y, 1.0 / 3.0));
// printf("%d / " , l_mat.at<uchar>(i, j));
}
else {
l_mat.at<uchar>(i, j) = 255 / 100 * (903.3 * y);
dst.at<Vec3b>(i, j)[0] = 255 / 100 * (903.3 * y);
}
float u = 4 * x / (x + 15 * y + 3 * z);
float v = 9 * y / (x + 15 * y + 3 * z);
//printf("u: %.2f , v:%.2f || ", u, v);
//U
u_mat.at<uchar>(i, j) = 255 / 354 * (13 * l_mat.at<uchar>(i, j)*(u - 0.19793943) + 134);
//printf("%d / ", u_mat.at<uchar>(i, j));
dst.at<Vec3b>(i, j) = 255 / 354 * (13 * l_mat.at<uchar>(i, j)*(u - 0.19793943) + 134);
//v
v_mat.at<uchar>(i, j) = 255 / 262 * (13 * l_mat.at<uchar>(i, j)*(v - 0.46831096)+140);
dst.at<Vec3b>(i, j) = 255 / 262 * (13 * l_mat.at<uchar>(i, j)*(v - 0.46831096) + 140);
}
I have to do the conversions pixel by pixel, i can't use cvtcolor.

C++ for loop stops before reaching end and restarts

I've been poking my nose into working with arrays in c++ and I've been writing a 1D Euler solver code that I wrote in matlab and converting it to c++ as a practice exercise.
This issue is that this for loop is supposed to run until the counter i reaches N_cells-1 but no matter how high I set the number, it always gets to 57, then restarts from 2 and continues doing this until I click on the output screen. I also ran the code with an N_cells number less than 57 and I get an error code which I've included below.
I'm pretty new to arrays and header files in c++ so I'm sure it's something simple, but I just can't find it. I know it's related to the fqL array but I don't know what.
Error when number <57 is used:
#include "stdafx.h"
#include "Flux.h"
#include <iostream>
#include <chrono>
using namespace std;
void Flux(double * q, double y, double R, int N_cells,double * Flux)
{
double qL[3];
double qR[3];
for (int i = 0; i < N_cells - 1; i++) {
//Initialize left and right sides
//-------------------
qL[0] = q[0, i];
qL[1] = q[1, i];
qL[2] = q[2, i];
qR[0] = q[0, i + 1];
qR[1] = q[1, i + 1];
qR[2] = q[2, i + 1];
//-------------------
//Calculate Left side Parameters
//-------------------
double PL;
//double fqL[3];
double cL2;
double HL;
double uL;
PL = (y - 1)*(qL[2] - 0.5 / qL[0] * (qL[1] * qL[1]));
double fqL[3] = { qL[1],
(3 - y)*0.5*(qL[1] * qL[1]) / qL[0] + (y - 1)*qL[2],
y*qL[1] * qL[2] / qL[0] - (y - 1)*0.5*(qL[1] * qL[1] * qL[1]) / (qL[0] * qL[0]) };
cL2 = y * (y - 1)*(qL[2] / qL[0] - 0.5*(qL[1] / qL[0])*(qL[1] / qL[0]));
HL = 0.5*(qL[1] / qL[0])*(qL[1] / qL[0]) + cL2 / (y - 1);
uL = qL[1] / qL[0];
//Calculate Right side Parameters
//-------------------
double PR;
//double fqR[3];
double cR2;
double HR;
double uR;
PR = (y - 1)*(qR[2] - 0.5 / qR[0] * (qR[1] * qR[1]));
double fqR[3] = { qR[1],
(3 - y)*0.5*(qR[1] * qR[1]) / qR[0] + (y - 1)*qR[2],
y*qR[1] * qR[2] / qR[0] - (y - 1)*0.5*(qR[1] * qR[1] * qR[1]) / (qR[0] * qR[0]) };
cR2 = y * (y - 1)*(qR[2] / qR[0] - 0.5*(qR[1] / qR[0])*(qR[1] / qR[0]));
HR = 0.5*(qR[1] / qR[0])*(qR[1] / qR[0]) + cR2 / (y - 1);
uR = qR[1] / qR[0];
//-------------------
//Calculate Roe's Variables
//-------------------------------- -
double u;
double H;
double c;
double rho;
u = (sqrt(qL[1])*qL[2] / qL[1] + sqrt(qR[1])*qR[2] / qR[1]) / (sqrt(qL[1]) + sqrt(qR[1]));
H = (sqrt(qL[1])*HL + sqrt(qR[1])*HR) / (sqrt(qL[1]) + sqrt(qR[1]));
c = sqrt((y - 1)*(H - 0.5*u *u));
rho = sqrt(qL[1] * qR[1]);
//-------------------------------- -
//-------------------------------- -
double g[3] = { u - c, u, u + c };
double v[3][3] = { {1, u - c, H - u * c},
{1, u, 0.5*u*u},
{1, u + c, H + u * c } };
double a[3] = { 0.5 / (c*c)*((PR - PL) - c * rho*(uR - uL)),
(qR[0] - qL[0]) - 1 * (PR - PL) / (c*c),
0.5 / (c*c)*((PR - PL) + c * rho*(uR - uL)) };
double SUM[3];
SUM[0] = g[0] * a[0] * v[0][0] + g[1] * a[1] * v[1][0] + g[2] * a[2] * v[2][0];
SUM[1] = g[0] * a[0] * v[0][1] + g[1] * a[1] * v[1][1] + g[2] * a[2] * v[2][1];
SUM[2] = g[0] * a[0] * v[0][2] + g[1] * a[1] * v[1][2] + g[2] * a[2] * v[2][2];
double Flux[3];
Flux[0,i] = 0.5*(fqL[0] + fqR[0]) - 0.5*SUM[0];
Flux[1,i] = 0.5*(fqL[1] + fqR[1]) - 0.5*SUM[1];
Flux[2,i] = 0.5*(fqL[2] + fqR[2]) - 0.5*SUM[2];
std::cout << i << endl;
}
}

C++/3D Terrain: std::vector pushback() crashes with c0000374

When attempted to push back a vector of UINT, the progrma crashes with Critical error detected c0000374. Below is the initial code:
void Terrain::CreateIndexList(UINT Width, UINT Height){
UINT sz_iList = (Width - 1)*(Height - 1) * 6;
UINT *iList = new UINT[sz_iList];
for (int i = 0; i < Width; i++){
for (int j = 0; j < Height; j++){
iList[(i + j * (Width - 1)) * 6] = ((UINT)(2 * i));
iList[(i + j * (Width - 1)) * 6 + 1] = (UINT)(2 * i + 1);
iList[(i + j * (Width - 1)) * 6 + 2] = (UINT)(2 * i + 2);
iList[(i + j * (Width - 1)) * 6 + 3] = (UINT)(2 * i + 2);
iList[(i + j * (Width - 1)) * 6 + 4] = (UINT)(2 * i + 1);
iList[(i + j * (Width - 1)) * 6 + 5] = (UINT)(2 * i + 3);
}
}
for (int i = 0; i < sz_iList; i++){
Geometry.IndexVertexData.push_back(iList[i]);
}
delete[] iList;
}
The goal is to take the generated indices from the iList array and fill the Geometry.IndexVertexData vector array. While debugging this, I've created several other implementations of this:
//After creating the iList array:
Geometry.IndexVertexData.resize(sz_iList); //Fails with "Vector subscript out of range?"
UINT in = 0;
for (int i = 0; i < Width; i++){
for (int j = 0; j < Height; j++){
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 1] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 2] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 3] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 4] = iList[in];
in++;
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 5] = iList[in];
in++;
}
}
And a final, direct to vector implementation:
Geometry.IndexVertexData.reserve(sz_iList);
for (int index = 0; index < sz_iList; index+=6) {
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6] = ((UINT)(2 * i));
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 1] = (UINT)(2 * i + 1);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 2] = (UINT)(2 * i + 2);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 3] = (UINT)(2 * i + 2);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 4] = (UINT)(2 * i + 1);
Geometry.IndexVertexData[(i + j*(Width - 1)) * 6 + 5] = (UINT)(2 * i + 3);
}
sz_iList has a final value of 2166, resultant from a grid of 20x20 (400 total points) and is used to initialize sizes. In all cases, the vector array would not fully fill, crashing with Critical error detected c0000374. Am I doing something wrong?
Your sz_iList doesn't appear to be big enough. Let's use a simple example of Width = Height = 2;, then sz_iList = (2 - 1) * (2 - 1) * 6 = 6, right? But in your nested loops, the last iteration occurs when i = j = 1 (i is one less than Width and j is one less than Height), where (in the last line of your loop), you try to access element (i + j * (Width - 1)) * 6 + 5 = (1 + 1 * (2 - 1)) * 6 + 5 = (1 + 1 * 1) * 6 + 5 = 2 * 6 + 5 = 17, which is bigger than the size of your array. This results in undefined behavior.

ListView items do not appear when added

I want to insert some items to my ListCtrl. This is my Code. I can't seem to get errors in the program. Unfortunately, not All items show in List, I can't know why it is. Please help me.
LVITEM lItem;
lItem.mask=LVIF_TEXT;
lItem.iItem = 1 + (nColNum - 1) * 3;
lItem.iSubItem=0;
lItem.pszText = W2T(L"");
pDlgSportNews->m_List.InsertItem( 1 + (nColNum-1 ) * 3 , lItem.pszText);
WCHAR szzText[MAX_PATH];
lItem.iItem = 1 + (nColNum - 1) * 3;
lItem.iSubItem = 1;
lItem.pszText = W2T(m_arData[nColNum - 1].szGameName);
pDlgSportNews->m_List.SetItemText(1 + (nColNum - 1) * 3 , 1 , lItem.pszText);
lItem.mask=LVIF_TEXT;
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 0;
lItem.pszText = W2T(m_arData[nColNum - 1].szGameTime);
pDlgSportNews->m_List.InsertItem(2 + + (nColNum - 1) * 3 , lItem.pszText);
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 1;
lItem.pszText = W2T(m_arData[nColNum - 1].szA_Team);
pDlgSportNews->m_List.SetItemText( 2 + (nColNum - 1) * 3 , 1 , lItem.pszText);
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 2;
if (m_arData[nColNum - 1].bFT_HDP)
{
swprintf(szzText , L"%s %s" , m_arData[nColNum - 1].szFT_HDP , m_arData[nColNum - 1].szFT_A_HDP);
lItem.pszText = W2T(szzText);
}
else
lItem.pszText = W2T(m_arData[nColNum - 1].szFT_A_HDP);
pDlgSportNews->m_List.SetItemText( 2 + (nColNum - 1) * 3 , 2 , lItem.pszText);
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 3;
swprintf(szzText , L"%s %s" , m_arData[nColNum - 1].szFT_UO , m_arData[nColNum - 1].szFT_A_UO);
lItem.pszText=W2T(szzText);
pDlgSportNews->m_List.SetItemText( 2 + (nColNum - 1) * 3 , 3 , lItem.pszText);
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 4;
lItem.pszText = W2T(m_arData[nColNum - 1].szFT_S_Odd);
pDlgSportNews->m_List.SetItemText( 2 + (nColNum - 1) * 3 , 4 , lItem.pszText);
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 5;
if (m_arData[nColNum - 1].bHT_HDP)
{
swprintf(szzText , L"%s %s" , m_arData[nColNum - 1].szHT_HDP , m_arData[nColNum - 1].szHT_A_HDP);
lItem.pszText=W2T(szzText);
}
else
lItem.pszText = W2T(m_arData[nColNum - 1].szHT_A_HDP);
pDlgSportNews->m_List.SetItemText( 2 + (nColNum - 1) * 3 , 5 , lItem.pszText);
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 6;
swprintf(szzText , L"%s %s" , m_arData[nColNum - 1].szHT_UO , m_arData[nColNum - 1].szHT_A_UO);
lItem.pszText=W2T(szzText);
pDlgSportNews->m_List.SetItemText( 2 + (nColNum - 1) * 3 , 6 , lItem.pszText);
lItem.iItem = 2 + (nColNum - 1) * 3;
lItem.iSubItem = 7;
lItem.pszText = W2T(m_arData[nColNum - 1].szHT_S_Odd);
pDlgSportNews->m_List.SetItemText( 2 + (nColNum - 1) * 3 , 7 , lItem.pszText);
lItem.pszText = W2T(L" ");
pDlgSportNews->m_List.InsertItem( 3 + (nColNum-1 ) * 3 , lItem.pszText);
lItem.iItem = 3 + (nColNum - 1) * 3;
lItem.iSubItem = 1;
lItem.pszText = W2T(m_arData[nColNum - 1].szB_Team);
pDlgSportNews->m_List.SetItemText( 3 + (nColNum - 1) * 3 , 1 , lItem.pszText);
lItem.iItem = 3 + (nColNum - 1) * 3;
lItem.iSubItem = 2;
if (!m_arData[nColNum - 1].bFT_HDP)
{
swprintf(szzText , L"%s %s" , m_arData[nColNum - 1].szFT_HDP , m_arData[nColNum - 1].szFT_B_HDP);
lItem.pszText=W2T(szzText);
}
else
lItem.pszText = W2T(m_arData[nColNum - 1].szFT_A_HDP);
pDlgSportNews->m_List.SetItemText( 3 + (nColNum - 1) * 3 , 2 , lItem.pszText);
lItem.iItem = 3 + (nColNum - 1) * 3;
lItem.iSubItem = 3;
lItem.pszText = W2T(m_arData[nColNum - 1].szFT_B_UO);
pDlgSportNews->m_List.SetItemText( 3 + (nColNum - 1) * 3 , 3 , lItem.pszText);
lItem.iItem = 3 + (nColNum - 1) * 3;
lItem.iSubItem = 4;
lItem.pszText = W2T(m_arData[nColNum - 1].szFT_D_Odd);
pDlgSportNews->m_List.SetItemText( 3 + (nColNum - 1) * 3 , 4 , lItem.pszText);
lItem.iItem = 3 + (nColNum - 1) * 3;
lItem.iSubItem = 5;
if (!m_arData[nColNum - 1].bHT_HDP)
{
swprintf(szzText , L"%s %s" , m_arData[nColNum - 1].szHT_HDP , m_arData[nColNum - 1].szHT_B_HDP);
lItem.pszText=W2T(szzText);
}
else
lItem.pszText = W2T(m_arData[nColNum - 1].szHT_B_HDP);
pDlgSportNews->m_List.SetItemText( 3 + (nColNum - 1) * 3 , 5 , lItem.pszText);
lItem.iItem = 3 + (nColNum - 1) * 3;
lItem.iSubItem = 6;
lItem.pszText = W2T(m_arData[nColNum - 1].szHT_B_UO);
pDlgSportNews->m_List.SetItemText( 3 + (nColNum - 1) * 3 , 6 , lItem.pszText);
lItem.iItem = 3 + (nColNum - 1) * 3;
lItem.iSubItem = 7;
lItem.pszText = W2T(m_arData[nColNum - 1].szHT_D_Odd);
pDlgSportNews->m_List.SetItemText( 3 + (nColNum - 1) * 3 , 7 , lItem.pszText);
pDlgSportNews->m_List.UpdateData();
UpdateData(FALSE);
Possibly going out on a limb here, but:
lItem.iItem = 1 + (nColNum - 1) * 3;
Why are you calculating the item number this way? This could likely be a reason not all of your items are being added.
Boilerplate for adding an item to a CListCtrl:
// First item (first row)
VITEM item;
item.mask = LVIF_TEXT;
item.pszText = "Column Text";
item.iItem = 0; // Item number
item.iSubItem = 0; // Sub item number (column number)
m_CListCtrl.InsertItem(&item);
// Second item (second row)
//...
item.iItem = 1;
//...
As somebody suggested, you should use a loop.