Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
NMF/generate_test_matrices.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
99 lines (66 sloc)
1.53 KB
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
// | |
// generate_test_matrices.cpp | |
// non-neg-mat-fac | |
// | |
// Created by Peter Arndt on 12/04/2016. | |
// Copyright © 2016 Peter Arndt. All rights reserved. | |
// | |
#include <stdio.h> | |
#include "common.h" | |
#include "random.h" | |
#include "matrix.h" | |
using namespace std; | |
MatDoub generate_random_matrix(int I,int J,int max) | |
{ | |
Ran ran(initrand()); | |
MatDoub M(I,J,0.0); | |
for (int i=0;i<I;i++) | |
for(int j=0; j<J; j++) | |
M[i][j]=1+ran.int64() % (max); | |
return M; | |
} | |
int main(int argc, const char * argv[]) { | |
StartUp(argc,argv); | |
cout << "data dir:" << data_dir(parent_dir(__FILE__)+"data") << endl; | |
int N = 20; | |
int M = 1000; | |
int K = 2; | |
int A=1; | |
MatDoub W=generate_random_matrix(N, K,10); | |
MatDoub H=generate_random_matrix(K, M,10); | |
for (int j=0; j<W.ncols();j++){ | |
double h=0; | |
for (int i=0;i<W.nrows();i++) | |
h+=W[i][j]; | |
for (int i=0;i<W.nrows();i++) | |
W[i][j]/=h; | |
} | |
for (int j=0; j<H.ncols();j++){ | |
double h=0; | |
for (int i=0;i<H.nrows();i++) | |
h+=H[i][j]; | |
for (int i=0;i<H.nrows();i++) | |
H[i][j]/=h; | |
} | |
cout.precision(8); | |
cout << W << endl << H <<endl; | |
MatDoub V(N,M,0.0); | |
NRmatrix_dgemm(false,false,1,W,H,0,V); | |
if (0){ | |
Ran ran(initrand()); | |
for (int i=0;i<N;i++) | |
for(int j=0; j<M; j++) | |
V[i][j]+=(ran.doub()*0.1)-0.05; | |
} | |
fstream fout(data_dir()+"V"+toa(A)+".tsv",ios::out); | |
fout.precision(18); | |
fout << V; | |
fout.close(); | |
fout.open(data_dir()+"W"+toa(A)+".tsv",ios::out); | |
fout << W; | |
fout.close(); | |
fout.open(data_dir()+"H"+toa(A)+".tsv",ios::out); | |
fout << H; | |
fout.close(); | |
return 0; | |
} |