Problem for svd.h
ekeen
03-09-2010, 11:30 AM
I used svd.h for svd analysis.
My matrix is 38 rows and 1354245 cols.
But for 1354245 rows and 38 cols, it is OK.
The progame cannot run. It has a error of segmentation fault.
Why?
ekeen
03-09-2010, 11:36 AM
// Whatever you need for the nr3 include files
#include "../code/nr3.h"
#include "../code/svd.h"
int main(void)
{
int i, j, k, m, n;
string txt;
char *inname = "svdtest.dat";
ifstream fp(inname);
if (!fp) {
cerr << "*****File " << inname
<< " could not be opened for reading*****" << endl;
throw("Exiting program"); // Or could simply exit(EXIT_FAILURE)
}
cout << "Opened file " << inname << " for reading." << endl;
getline(fp, txt); // First line is any kind of comment
cout << txt << endl; // Echo the first line
fp >> m >> n; // Read rows, columns from second line
getline(fp, txt); // Eat the rest of this line
if (!fp) {
cerr << "*****Could not read rows and columns from second line*****"
<< endl;
throw("Exiting program"); // Or could simply exit(EXIT_FAILURE)
}
cout << "m = " << m << ", n = " << n << endl;
MatDoub a(m, n);
MatDoub uvw(m, n);
// Get matrix coefficients
for (i = 0; i < m && fp; i++) {
for (j = 0; j < n && fp; j++) {
fp >> a[i][j];
if (!fp) {
cerr << "***** Error reading coefficient["
<< i << "][" << j << "] *****" << endl;
throw("Exiting program"); // Or exit(EXIT_FAILURE);
}
}
}
fp.close();
// The constructor performs the decomposition
SVD mySVD(a);
cout << fixed << setprecision(6);
cout << "**********Decomposition Matrices***********" << endl;
cout << "Matrix U" << endl;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
cout << setw(12) << mySVD.u[i][j];
}
cout << endl;
}
cout << "Diagonal of matrix W" << endl;
for (i = 0; i < n; i++) {
cout << setw(12) << mySVD.w[i];
}
cout << endl << "Matrix V-Transpose" << endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cout << setw(12) << mySVD.v[j][i];
}
cout << endl;
}
cout << endl;
cout << "***Check product against original matrix***" << endl;
cout << "Original Matrix" << endl;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
cout << setw(12) << a[i][j];
}
cout << endl;
}
cout << "Product [U] x [W] x [V-transpose]" << endl;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
uvw[i][j] = 0.0;
for (k = 0; k < n; k++) {
uvw[i][j] += mySVD.u[i][k] * mySVD.w[k] * mySVD.v[j][k];
}
cout << setw(12) << uvw[i][j];
}
cout << endl;
}
cout << endl;
//This gives a small number for an ill-conditioned system.
cout << "Inverse Condition number = "
<< scientific << mySVD.inv_condition() << endl;
return 0;
}
ekeen
03-09-2010, 11:41 AM
I modified my code from here.
Thanks.
#include "...NR Headers\nr3.h"
#include "...svd.h"
ifstream svtest; // Input file name
string filename; // user entered file name
Int nrows,ncols,n,m;
Doub pause; // dummy variable used to keep the output window visible
int main(void)
{
cout << "Enter the name of the input file " << endl;
cin >> filename;
svtest.open(filename.c_str());
if(svtest.fail()) throw ("Error opening input file");
cout << "Enter the number of rows " << endl;
cin >> m;
cout << "Enter the number of columns " << endl;
cin >> n;
MatDoub a(m, n);
MatDoub uvw(m, n);
// Read in the matrix
for(Int i=0;i<m;i++){
for(Int j=0;j<n;j++) {
svtest >> a[i][j];
}
}
// Print matrix out to output screen
cout << fixed << setprecision(6);
cout << "Loaded Matrix from Data File" << endl << endl;
for(Int i=0;i<m;i++) {
for(Int j=0;j<n;j++) {
cout << setw(12) << a[i][j];
}
cout << endl;
}
// Perform SVD
SVD mySVD(a);
cout << fixed << setprecision(6);
cout << endl << "Matrix U" << endl << endl;
for (Int i = 0; i < m; i++) {
for (Int j = 0; j < n; j++) {
cout << setw(12) << mySVD.u[i][j];
}
cout << endl;
}
cout << endl;
cout << "Diagonal of matrix W" << endl << endl;
for(Int i=0;i<n;i++){
cout << setw(12) << mySVD.w[i];
}
cout << endl;
cout << "Matrix V-Transpose" << endl;
for(Int i=0;i<n;i++){
for(Int j=0;j<n;j++){
cout << setw(12) << mySVD.v[j][i];
}
cout << endl;
}
cout << endl;
cout << "Product [u] x [w] x [V-Transpose]" << endl;
for(Int i=0; i<m; i++){
for(Int j=0; j<n; j++){
uvw[i][j] = 0.0;
for(Int k=0;k<n;k++){
uvw[i][j] +=mySVD.u[i][k] * mySVD.w[k] * mySVD.v[j][k];
}
cout << setw(12) << uvw[i][j];
}
cout << endl;
}
cout << endl;
cout << endl << "Inverse Condition number = "
<< scientific << mySVD.inv_condition() << endl;
cin >> pause;
return 0;
}
MPD78
03-09-2010, 11:47 AM
Why?
You have 1,354,245 columns. That is a lot.
A segmentation error is indicating that you are running out of memory.
See link below.
http://en.wikipedia.org/wiki/Segmentation_fault
Thanks
Matt