fourier inverse of complex matrix
pompo
06-23-2011, 08:05 AM
i have complex matrix as follow
MatComplex_IO X (512,512)
i wanna to get the inverse fourier transform of it
can i use ' fourn ' ?
kutta
08-03-2011, 03:38 AM
This was earlier explained in forum
Neverthless for the benifitt of the reader the same is appended
below
Thanks
C.R.Muthukumar(Kutta)
#include "nr3.h"
//#define gaussj
#include "gaussj2.1.cpp"
int main(int argc, char **argv)
{
/*
* Find the inverse of an "nxn" matrix
*/
int n;
int i, j, k;
string str;
char *inname = "matrixn.dat";
if (argc > 1) {
inname = argv[1];
}
ifstream fp(inname);
if (fp) {
cout << "Can't open file " << inname << " for reading." << endl;
exit(EXIT_FAILURE);
}
cout << fixed << setprecision(6);
getline(fp,str); // The comment line
cout << str << endl << endl;
fp >> n;
if (fp) {
cerr << "Error reading the size of the matrix" << endl;
exit(EXIT_FAILURE);
}
cout << "Matrix size is " << n << " x " << n << endl;
getline(fp, str); // eat the rest of the matrix size line
MatDoub a(n, n);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
fp >> a[i][j];
if (!fp) {
cerr << "Error reading a[" << i << "][" << j << "]"
<< endl;
exit(EXIT_FAILURE);
}
}
}
cout << scientific << setprecision(6);
cout << endl << "The matrix [a]: " << endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cout << setw(14) << a[i][j];
}
cout << endl;
}
cout << endl;
MatDoub ai(a); // Copy of a will hold the inverse
// Now find the inverse
gaussj(ai);
cout << "The calculated inverse of the matrix [a]:" << endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cout << setw(14) << ai[i][j];
}
cout << endl;
}
cout << endl;
//Multiply [a] * [a-inverse]
MatDoub check(n, n);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
check[i][j] = 0.0;
for (k = 0; k < n; k++) {
check[i][j] += (a[i][k] * ai[k][j]);
}
}
}
cout << "The matrix [a] times the calculated inverse:"
<< endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cout << setw(14) << check[i][j];
}
cout << endl;
}
cout << endl;
fp.close();
return EXIT_SUCCESS;
}
kutta:
1. The question was about the *inverse Fourier transform* of the matrix, not the *inverse* of the matrix. The two concepts are entirely different.
2. The point of the question was not "which NR routine do I call?" nor "how do I get data into and out of my program?" but whether the NR routine "fourn", which expects a vector of real numbers in a particular order, can be persuaded to accept a matrix of complex numbers instead.
pompo:
I think you can use &X[0][0] as the first argument to fourn and the Right Thing will happen. I haven't actually tried, nor scrutinized the code, so if someone more expert contradicts me then you should probably believe them rather than me.