/************************************************************************* Inversion of a general complex matrix. Input parameters: A - matrix. Array whose indexes range within [0..N-1, 0..N-1]. N - size of matrix A. Output parameters: A - inverse of matrix A. Array whose indexes range within [0..N-1, 0..N-1]. Result: True, if the matrix is not singular. False, if the matrix is singular. -- ALGLIB -- Copyright 2005 by Bochkanov Sergey *************************************************************************/ bool cmatrixinverse(ap::complex_2d_array& a, int n) { bool result; ap::integer_1d_array pivots; cmatrixlu(a, n, n, pivots); result = cmatrixluinverse(a, pivots, n); return result; }
/************************************************************************* Problem testing *************************************************************************/ static void testproblem(const ap::complex_2d_array& a, int n) { ap::complex_2d_array b; ap::complex_2d_array blu; ap::complex_2d_array t1; ap::integer_1d_array p; int i; int j; ap::complex v; // // Decomposition // makeacopy(a, n, n, b); cmatrixinverse(b, n); makeacopy(a, n, n, blu); cmatrixlu(blu, n, n, p); cmatrixluinverse(blu, p, n); // // Test // t1.setbounds(0, n-1, 0, n-1); internalmatrixmatrixmultiply(a, 0, n-1, 0, n-1, false, b, 0, n-1, 0, n-1, false, t1, 0, n-1, 0, n-1); for(i = 0; i <= n-1; i++) { for(j = 0; j <= n-1; j++) { v = t1(i,j); if( i==j ) { v = v-1; } inverrors = inverrors||ap::abscomplex(v)>threshold; } } internalmatrixmatrixmultiply(a, 0, n-1, 0, n-1, false, blu, 0, n-1, 0, n-1, false, t1, 0, n-1, 0, n-1); for(i = 0; i <= n-1; i++) { for(j = 0; j <= n-1; j++) { v = t1(i,j); if( i==j ) { v = v-1; } inverrors = inverrors||ap::abscomplex(v)>threshold; } } }