Created
June 23, 2022 07:18
-
-
Save dvsseed/c3645890d51cfe337ab1addb51cdfba0 to your computer and use it in GitHub Desktop.
To get SVD with ALGLIB
This file contains 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
#include <iostream> | |
#include <iomanip> | |
#include <fstream> | |
#include <sys/time.h> | |
#include "ap.h" | |
#include "linalg.h" | |
using namespace std; | |
using namespace alglib; | |
using std::cout; | |
using std::endl; | |
int main(int argc, char **argv) { | |
struct timeval start, end; | |
// start timer. | |
gettimeofday(&start, NULL); | |
std::string filename = "/home/d1075102/tadnn/pytorch/LBPhistogram_BRGC_20220609.csv"; | |
alglib::real_2d_array hmat; | |
// void read_csv(const char *filename, char separator, int flags, alglib::real_2d_array &out); | |
read_csv(filename.c_str(), ',', alglib::CSV_SKIP_HEADERS, hmat); | |
/* | |
bool alglib::rmatrixsvd( | |
real_2d_array a, | |
ae_int_t m, | |
ae_int_t n, | |
ae_int_t uneeded, | |
ae_int_t vtneeded, | |
ae_int_t additionalmemory, | |
real_1d_array& w, | |
real_2d_array& u, | |
real_2d_array& vt, | |
const xparams _params = alglib::xdefault); | |
*/ | |
// alglib::real_2d_array hmat; | |
// double hmat0[] = { .0, .0, .0, .0, .0, .0, .0, .0, .0 }; | |
// hmat.setcontent( 3, 3, hmat0 ); // rows, cols | |
alglib::real_1d_array W; | |
alglib::real_2d_array U; | |
alglib::real_2d_array VT; | |
int rows = 5000; | |
int cols = 131072; | |
W.setlength( rows ); | |
U.setlength( rows, cols ); | |
VT.setlength( rows, cols ); | |
/* | |
U -- if UNeeded=0, U isn't changed, the left singular vectors are not calculated. | |
VT -- if VTNeeded=0, VT isn't changed, the right singular vectors are not calculated. | |
AdditionalMemory - | |
If the parameter: | |
* equals 0, the algorithm doesn't use additional | |
memory (lower requirements, lower performance). | |
* equals 1, the algorithm uses additional | |
memory of size min(M,N)*min(M,N) of real numbers. | |
It often speeds up the algorithm. | |
* equals 2, the algorithm uses additional | |
memory of size M*min(M,N) of real numbers. | |
It allows to get a maximum performance. | |
The recommended value of the parameter is 2. | |
*/ | |
alglib::rmatrixsvd( hmat, rows, cols, 0, 0, 2, W, U, VT ); | |
// cout << "Warr: " << W.tostring( 3 ) << endl; | |
// cout << "Umat: " << U.tostring( 3 ) << endl; | |
// cout << "VTmat:" << VT.tostring( 3 ) << endl; | |
// write to csv | |
std::ofstream eigenfile; | |
eigenfile.open ("Eigenvalue_BRGC_20220615.csv"); | |
for (int i = 0; i < W.length(); i++) { | |
eigenfile << W[i] << "\n"; | |
} | |
eigenfile.close(); | |
// stop timer. | |
gettimeofday(&end, NULL); | |
// Calculating total time taken by the program. | |
double time_taken; | |
time_taken = (end.tv_sec - start.tv_sec) * 1e6; | |
time_taken = (time_taken + (end.tv_usec - start.tv_usec)) * 1e-6; | |
cout << "Time taken by program is: " << fixed << setprecision(6) << time_taken << " seconds." << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment