Created
May 21, 2014 13:01
-
-
Save jorgecarleitao/a7ef61d4d918ee8059d6 to your computer and use it in GitHub Desktop.
Generates a unitary random vector in D dimensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::vector<double> unitaryVector(unsigned int dimension) | |
{ | |
std::vector<double> vector(dimension); | |
double norm = 0; | |
for (unsigned int d = 0; d < dimension; d++) | |
{ | |
vector[d] = grandom(); // RNG call for a gaussian with mean=0 and sigma=1 | |
norm += vector[d]*vector[d]; | |
} | |
norm = sqrt(norm); | |
for (size_t d = 0; d < dimension; d++) | |
{ | |
vector[d] /= norm; | |
} | |
return vector; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment