[CSC 335] ztest (armadillo version)
Andrew J. Pounds
pounds_aj at mercer.edu
Thu Nov 30 16:32:51 EST 2023
I was asked to send out an example of using C++ and Armadillo using
complex numbers. I am including here my ztestarma.cpp source, which
solves the same problem that ztest.f90 and gslztest.c solved. Basically
a complex matrix is created, I take the inverse of the matrix, and them
multiply the inverse and the original matrix. The solution should be a
matrix with all 1 along the diagonal -- but due to numerical precision
errors this is rarely the case. At the end I sum up the diagonal
elements to see if it matches the matrix dimension.
#include <iostream>
#include <armadillo>
// Compile with gcc gslztest.c -lgsl -lm
using namespace std;
int main (void)
{
const int N = 10;
int i, j;
double pi, NCUBE, alpha;
complex<double> sumdiag;
// Create pointers and allocate space for complex matrices
arma::cx_dmat A(N,N);
arma::cx_dmat B(N,N);
arma::cx_dmat C(N,N);
arma::cx_dmat INV(N,N);
pi = acos(-1.0);
// Set the value of N^3 since we use is a lot
NCUBE = (double) N * (double) N * (double) N;
// Now fill the matrices with the same info
for (i=1; i<=N; i++)
for (j=1; j<=N; j++) {
alpha = sqrt( pow(cos ( (double) ( i*i*j) * pi / NCUBE ),2) +
pow(sin ( (double) (2*i*i*j) * pi / NCUBE ),2));
alpha = (double) 1.0 / alpha;
A( i-1, j-1) = complex<double>(
alpha * cos( (double) ( i*i*j) * pi / NCUBE ),
alpha * sin( (double) (2*i*i*j) * pi / NCUBE ));
B( i-1, j-1) = A( i-1, j-1);
}
inv(INV,B);
C = INV*A;
sumdiag = complex<double>(0.0,0.0);
for (i=0;i<N;i++) sumdiag = sumdiag + C(i,i);
printf("Sum of diagonal is %g + i %g\n", sumdiag);
}
--
*Andrew J. Pounds, Ph.D.*
/Professor of Chemistry and Computer Science/
/Director of the Computational Science Program/
/Mercer University, Macon, GA, 31207 (478) 301-5627 /
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://theochem.mercer.edu/pipermail/csc335/attachments/20231130/7c78b5d6/attachment.html>
More information about the csc335
mailing list