Skip to content
Extraits de code Groupes Projets
Valider 2e758036 rédigé par Hugo TRACHINO's avatar Hugo TRACHINO
Parcourir les fichiers

TD1 part1

parent 49e94f60
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -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
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter