Just convert each double
to long double
on graphical codes
+ Gets rid of some trailing white-spaces
This commit is contained in:
parent
9dbc5cbd5d
commit
5dac85c8db
LinearRegression
Modules/Graphics
PolynomialRegression
@ -15,11 +15,11 @@ using namespace std;
|
||||
#define INTERVALWIDTH 5
|
||||
|
||||
|
||||
const double leadingCoeficient[] = {1.0, 2.0};
|
||||
const long double leadingCoeficient[] = {1.0, 2.0};
|
||||
|
||||
|
||||
void getRandomCoordinates(double[], double[]);
|
||||
void getGradient(const double[], const double[], const double[], double[]);
|
||||
void getRandomCoordinates(long double[], long double[]);
|
||||
void getGradient(const long double[], const long double[], const long double[], long double[]);
|
||||
|
||||
|
||||
|
||||
@ -30,20 +30,20 @@ int main(int argc, char const *argv[])
|
||||
|
||||
srand(time(nullptr));
|
||||
|
||||
double x[NBEXAMPLES] = {0.0};
|
||||
double y[NBEXAMPLES] = {0.0};
|
||||
long double x[NBEXAMPLES] = {0.0};
|
||||
long double y[NBEXAMPLES] = {0.0};
|
||||
|
||||
/* Coordinates generation */
|
||||
getRandomCoordinates(x, y);
|
||||
|
||||
double theta[2] = {0.0};
|
||||
double alpha(FIRSTALPHA);
|
||||
double gradOld[2];
|
||||
long double theta[2] = {0.0};
|
||||
long double alpha(FIRSTALPHA);
|
||||
long double gradOld[2];
|
||||
|
||||
getGradient(x, y, theta, gradOld);
|
||||
|
||||
double grad[2];
|
||||
double deltaGrad[2];
|
||||
long double grad[2];
|
||||
long double deltaGrad[2];
|
||||
|
||||
/* Training ! */
|
||||
int i;
|
||||
@ -59,7 +59,7 @@ int main(int argc, char const *argv[])
|
||||
deltaGrad[0] = grad[0] - gradOld[0];
|
||||
deltaGrad[1] = grad[1] - gradOld[1];
|
||||
|
||||
alpha = (-alpha * (gradOld[0] * deltaGrad[0] + gradOld[1] * deltaGrad[1]))
|
||||
alpha = (-alpha * (gradOld[0] * deltaGrad[0] + gradOld[1] * deltaGrad[1]))
|
||||
/ ((deltaGrad[0] * deltaGrad[0]) + (deltaGrad[1] * deltaGrad[1]));
|
||||
}
|
||||
|
||||
@ -86,17 +86,17 @@ int main(int argc, char const *argv[])
|
||||
|
||||
|
||||
|
||||
void getRandomCoordinates(double x[], double y[])
|
||||
void getRandomCoordinates(long double x[], long double y[])
|
||||
{
|
||||
for(int i(0); i < NBEXAMPLES; i++)
|
||||
{
|
||||
x[i] = i;
|
||||
y[i] = (i * leadingCoeficient[1]) + leadingCoeficient[0] + (rand() / (double)RAND_MAX) * (2 * INTERVALWIDTH) - INTERVALWIDTH;
|
||||
y[i] = (i * leadingCoeficient[1]) + leadingCoeficient[0] + (rand() / (long double)RAND_MAX) * (2 * INTERVALWIDTH) - INTERVALWIDTH;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void getGradient(const double x[], const double y[], const double theta[], double gradient[])
|
||||
void getGradient(const long double x[], const long double y[], const long double theta[], long double gradient[])
|
||||
{
|
||||
gradient[0] = 0;
|
||||
gradient[1] = 0;
|
||||
|
@ -15,11 +15,11 @@ using namespace std;
|
||||
#define INTERVALWIDTH 5
|
||||
|
||||
|
||||
const double leadingCoefficient = 2.0;
|
||||
const long double leadingCoefficient = 2.0;
|
||||
|
||||
|
||||
void getRandomCoordinates(double[], double[]);
|
||||
double getGradient(const double[], const double[], const double[]);
|
||||
void getRandomCoordinates(long double[], long double[]);
|
||||
long double getGradient(const long double[], const long double[], const long double[]);
|
||||
|
||||
|
||||
|
||||
@ -30,16 +30,16 @@ int main(int argc, char const *argv[])
|
||||
|
||||
srand(time(0));
|
||||
|
||||
double x[NBEXAMPLES] = {0.0};
|
||||
double y[NBEXAMPLES] = {0.0};
|
||||
long double x[NBEXAMPLES] = {0.0};
|
||||
long double y[NBEXAMPLES] = {0.0};
|
||||
|
||||
/* Coordinates generation */
|
||||
getRandomCoordinates(x, y);
|
||||
|
||||
double theta[1] = {0.0};
|
||||
double alpha(FIRSTALPHA);
|
||||
double gradOld(getGradient(x, y, theta));
|
||||
double grad;
|
||||
long double theta[1] = {0.0};
|
||||
long double alpha(FIRSTALPHA);
|
||||
long double gradOld(getGradient(x, y, theta));
|
||||
long double grad;
|
||||
|
||||
/* Training ! */
|
||||
int i;
|
||||
@ -73,19 +73,19 @@ int main(int argc, char const *argv[])
|
||||
|
||||
|
||||
|
||||
void getRandomCoordinates(double x[], double y[])
|
||||
void getRandomCoordinates(long double x[], long double y[])
|
||||
{
|
||||
for(int i(0); i < NBEXAMPLES; i++)
|
||||
{
|
||||
x[i] = i;
|
||||
y[i] = (i * leadingCoefficient) + (rand() / (double)RAND_MAX) * (2 * INTERVALWIDTH) - INTERVALWIDTH;
|
||||
y[i] = (i * leadingCoefficient) + (rand() / (long double)RAND_MAX) * (2 * INTERVALWIDTH) - INTERVALWIDTH;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
double getGradient(const double x[], const double y[], const double theta[])
|
||||
long double getGradient(const long double x[], const long double y[], const long double theta[])
|
||||
{
|
||||
double value = 0.0;
|
||||
long double value = 0.0;
|
||||
|
||||
for(short int i(0); i < NBEXAMPLES; i++)
|
||||
{
|
||||
|
@ -92,9 +92,9 @@ void getRandomCoordinates(Matrix<long double, 1, NBPARAMETERS> x[], long double
|
||||
{
|
||||
x[i](0, 0) = 1.0;
|
||||
|
||||
for(int j(1); j < NBPARAMETERS; j++)
|
||||
for(int j(1); j < NBPARAMETERS; j++)
|
||||
{
|
||||
x[i](j) = rand() / ((long double)RAND_MAX) * (INTERVALVALUESSUP - INTERVALVALUESINF) + INTERVALVALUESINF;
|
||||
x[i](j) = rand() / ((long double)RAND_MAX) * (INTERVALVALUESSUP - INTERVALVALUESINF) + INTERVALVALUESINF;
|
||||
}
|
||||
|
||||
y[i] = leadingCoefficient.dot(x[i]) + (rand() / (long double)RAND_MAX) * (2 * INTERVALWIDTH) - INTERVALWIDTH;
|
||||
|
@ -4,7 +4,7 @@
|
||||
using namespace sf;
|
||||
|
||||
|
||||
void sfmlDisplay(const char* const string, const double x[], const double y[], const int nbPoints, const double theta[], const int nbDimensions)
|
||||
void sfmlDisplay(const char* const string, const long double x[], const long double y[], const int nbPoints, const long double theta[], const int nbDimensions)
|
||||
{
|
||||
/* Let's create a new window ! */
|
||||
RenderWindow window(VideoMode(SIZE_X, SIZE_Y), string);
|
||||
@ -21,8 +21,8 @@ void sfmlDisplay(const char* const string, const double x[], const double y[], c
|
||||
const int window_width = window.getSize().x;
|
||||
const int window_height = window.getSize().y;
|
||||
|
||||
const double ratio_x = window_width / (double)nbPoints;
|
||||
const double ratio_y = window_height / std::abs(getMax(y, nbPoints) - getMin(y, nbPoints));
|
||||
const long double ratio_x = window_width / (long double)nbPoints;
|
||||
const long double ratio_y = window_height / std::abs(getMax(y, nbPoints) - getMin(y, nbPoints));
|
||||
|
||||
/* Dots for generated coordinates */
|
||||
int i;
|
||||
@ -32,11 +32,11 @@ void sfmlDisplay(const char* const string, const double x[], const double y[], c
|
||||
shape[i].setRadius(2);
|
||||
shape[i].setFillColor(Color(255, 0, 0));
|
||||
|
||||
shape[i].setPosition(x[i] * ratio_x, window_height - y[i] * ratio_y);
|
||||
shape[i].setPosition(x[i] * ratio_x, window_height - y[i] * ratio_y);
|
||||
}
|
||||
|
||||
/* Dots for our computed regression */
|
||||
for(double temp_y = 0.0; i < 2 * nbPoints; i++, temp_y = 0.0)
|
||||
for(long double temp_y(0.0); i < 2 * nbPoints; i++, temp_y = 0.0)
|
||||
{
|
||||
/* The dot will be blue, and with a radius of 20 ! */
|
||||
shape[i].setRadius(2);
|
||||
@ -47,7 +47,7 @@ void sfmlDisplay(const char* const string, const double x[], const double y[], c
|
||||
{
|
||||
for(int j(0); j < (int)nbDimensions; j++)
|
||||
{
|
||||
temp_y += pow(x[i - nbPoints - 1], (double)j) * theta[j];
|
||||
temp_y += pow(x[i - nbPoints - 1], (long double)j) * theta[j];
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,9 +90,9 @@ void sfmlDisplay(const char* const string, const double x[], const double y[], c
|
||||
}
|
||||
|
||||
|
||||
double getMin(const double tab[], const int n)
|
||||
long double getMin(const long double tab[], const int n)
|
||||
{
|
||||
double minimum = tab[0];
|
||||
long double minimum = tab[0];
|
||||
|
||||
for(int i(1); i < n; i++)
|
||||
{
|
||||
@ -106,9 +106,9 @@ double getMin(const double tab[], const int n)
|
||||
}
|
||||
|
||||
|
||||
double getMax(const double tab[], const int n)
|
||||
long double getMax(const long double tab[], const int n)
|
||||
{
|
||||
double maximum = tab[0];
|
||||
long double maximum = tab[0];
|
||||
|
||||
for(int i(1); i < n; i++)
|
||||
{
|
||||
|
@ -11,7 +11,7 @@
|
||||
#define SIZE_Y 1024
|
||||
|
||||
|
||||
void sfmlDisplay(const char* const, const double[], const double[], const int, const double[], const int);
|
||||
void sfmlDisplay(const char* const, const long double[], const long double[], const int, const long double[], const int);
|
||||
|
||||
double getMin(const double[], const int);
|
||||
double getMax(const double[], const int);
|
||||
long double getMin(const long double[], const int);
|
||||
long double getMax(const long double[], const int);
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <cstdlib>
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
|
||||
#include <eigen3/Eigen/Dense>
|
||||
|
||||
using namespace Eigen;
|
||||
@ -13,7 +14,7 @@ using namespace Eigen;
|
||||
#define NBITERATIONS 1000
|
||||
#define INTERVALWIDTH 0
|
||||
#define NBPARAMETERS 7 /* It needs to be greater or equal than 2 (if lower: affine regression) */
|
||||
#define INTERVALVALUESINF -10
|
||||
#define INTERVALVALUESINF -10
|
||||
#define INTERVALVALUESSUP 10
|
||||
|
||||
|
||||
@ -108,7 +109,7 @@ void getRandomCoordinates(Matrix<long double, 1, NBPARAMETERS> x[], long double
|
||||
x[i](0, 1) = rand() / ((long double)RAND_MAX) * (INTERVALVALUESSUP - INTERVALVALUESINF) + INTERVALVALUESINF;
|
||||
|
||||
/* Let's put this input to each powers of itself */
|
||||
for(int j(2); j < NBPARAMETERS; j++)
|
||||
for(int j(2); j < NBPARAMETERS; j++)
|
||||
{
|
||||
x[i](0, j) = pow(x[i](0, 1), (long double)j);
|
||||
}
|
||||
@ -150,7 +151,7 @@ void meanNormalization(Matrix<long double, 1, NBPARAMETERS> x[], long double ran
|
||||
/* Computes the inverse of the range of each feature in range */
|
||||
void featuresRange(long double range[])
|
||||
{
|
||||
range[0] = 1; /* In order not to modify the x0 feature in meanNormalization */
|
||||
range[0] = 1; /* In order not to modify the x0 feature in meanNormalization */
|
||||
for(int i(1); i < NBPARAMETERS; i += 2)
|
||||
{
|
||||
range[i] = std::abs(pow(INTERVALVALUESSUP, i) - pow(INTERVALVALUESINF, i));
|
||||
@ -169,7 +170,7 @@ void featuresRange(long double range[])
|
||||
/* Computes the average of each feature in average*/
|
||||
void featuresAverage(long double average[])
|
||||
{
|
||||
average[0] = 0; /* In order not to modify the x0 feature in meanNormalization */
|
||||
average[0] = 0; /* In order not to modify the x0 feature in meanNormalization */
|
||||
|
||||
for(int i(1); i < NBPARAMETERS; i += 2)
|
||||
{
|
||||
|
Reference in New Issue
Block a user