diff --git a/sgi2018/src/tools/matlab.h b/sgi2018/src/tools/matlab.h index 1bc93c3d4082c6d64ab4e11873503d6a6c2de113..27d163c6a99d83a3f11d77eae03367db11824da2 100644 --- a/sgi2018/src/tools/matlab.h +++ b/sgi2018/src/tools/matlab.h @@ -4,7 +4,7 @@ #include <iostream> #include <fstream> #include "../types.h" - +#include <complex> /* * Code translated from Matlab base functions * matlab translation guide : https://eigen.tuxfamily.org/dox/AsciiQuickReference.txt @@ -115,27 +115,71 @@ Vector polyval(const Polynomial& pol, const Vector& x) } //------------------------------------------------------------------------------------------------- + +// RANDOM HELPING FUNCTION + +const unsigned int n = 65535;//0xFFFF; +const unsigned int m = 16807; +const unsigned int b = 0; +static long seed = 43690;//0xAAAA; +// generate an uniformaly distributed random number sampled from [0;0x7FFFFFFF] +long uniform() { + long unsigned int hi = m * (seed & n); + long unsigned int lo = m * (seed >> 16); + + lo+= (hi & 32767) << 16; + lo+= hi >> 15; + + if (lo > 2147483647) //0x7FFFFFFF) + lo -= 2147483647; //0x7FFFFFFF; + seed=(long) lo; + return seed ; +} + + +//-------------------------------------------------------------------------------------------------- + // RANDOM // generate an uniformaly distributed random number sampled from [-1;1] inline number randu() { - throw std::logic_error("Function not yet implemented."); + //on le veut entre -1 et 1 / pour l'instant on l'a entre 0 et 1 + number result = uniform()/2147483647; //(double)0x7FFFFFFF; + // number result = (uniform() - (number)3xFFFFFFF.8)/(number)3xFFFFFFF.8); + return result; + // throw std::logic_error("Function not yet implemented."); } // generates a vector of the given size, the entries of the vector are all complex number // their real and imaginary parts are uniformaly distributed random number sampled from [-1;1] ComplexVector randu(int size) { - // NOTE: this can be built on the previous function - throw std::logic_error("Function not yet implemented."); + ComplexVector complexVector(size); + std::complex<number> mycomplex; + for(int i = 0; i < size; i++) + { + std::complex<number> mycomplex(randu(), randu()); + complexVector(i) = mycomplex; + } + return complexVector; + + //throw std::logic_error("Function not yet implemented."); } // generate a normally distributed random number // sampled from a distribution with mean 0 and std 1 inline number randn() { - throw std::logic_error("Function not yet implemented."); + + number res; + number U0 = uniform()/2147483647;//(double)0x7FFFFFFF; + number U1 = uniform()/2147483647;//(double)0x7FFFFFFF; + res = sqrt(-2 * log(U0)) * cos(2 * M_PI * U1); + + return res; + + // throw std::logic_error("Function not yet implemented."); } // generates a vector of the given size, the entries of the vector are all complex number @@ -143,9 +187,19 @@ inline number randn() ComplexVector randn(int size) { // NOTE: this can be built on the previous function - throw std::logic_error("Function not yet implemented."); + ComplexVector complexVector(size); + std::complex<number> mycomplex; + for(int i = 0; i < size; i++) + { + std::complex<number> mycomplex(randn(), randn()); + complexVector(i) = mycomplex; + } + return complexVector; + //throw std::logic_error("Function not yet implemented."); } + + //------------------------------------------------------------------------------------------------- // IMPORT @@ -184,4 +238,4 @@ ComplexVector loadComplexVector(const std::string& file) return output; } -//------------------------------------------------------------------------------------------------- \ No newline at end of file +//-------------------------------------------------------------------------------------------------