[CSC 335] Complex matrix operations using GSL and C
Andrew J. Pounds
pounds_aj at mercer.edu
Fri Nov 15 22:36:44 EST 2013
Okay gentlemen - this one took me a while to unravel. But here is the
code...
#include <stdio.h>
#include <gsl/gsl_complex.h>
#include <gsl/gsl_linalg.h>
/* Solving complex linear system
* (3+i)x - 2y = 3-4i
* -3x + (1-2i) = -1+.5i
*
* using the Gnu Scientific Library. There is some strange
* packing that has to be done to set up the matrices. I have
* not yet been successful using the gsl_complex type, which would
* make all this really easy.
*/
int
main (void)
{
double a_data[] = { 3,1, -2,0,
-3,0, 1,-2 };
double b_data[] = { 3,-4,
-1,0.5 };
gsl_matrix_complex_view m = gsl_matrix_complex_view_array (a_data,2,2);
gsl_vector_complex_view b = gsl_vector_complex_view_array(b_data, 2);
gsl_vector_complex *x = gsl_vector_complex_alloc (2);
int s;
gsl_permutation * p = gsl_permutation_alloc (2);
gsl_linalg_complex_LU_decomp (&m.matrix, p, &s);
gsl_linalg_complex_LU_solve (&m.matrix, p, &b.vector, x);
printf ("x = \n");
gsl_vector_complex_fprintf(stdout, x, "%g");
gsl_permutation_free (p);
return 0;
}
compile on cobra with...
gcc cmatrix.c -lgsl -lgslcblas -lm
I've got some more work to do -- but this should help you get started
with some of the homework and the project.
--
Andrew J. Pounds, Ph.D. (pounds_aj at mercer.edu)
Professor of Chemistry and Computer Science
Mercer University, Macon, GA 31207 (478) 301-5627
http://faculty.mercer.edu/pounds_aj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://theochem.mercer.edu/pipermail/csc335/attachments/20131115/05ef8d50/attachment.html>
More information about the csc335
mailing list