TEST(FastUpdate, BlockMatrixAdd) { typedef double Scalar; typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> matrix_t; std::vector<size_t> N_list, M_list; N_list.push_back(0); N_list.push_back(10); N_list.push_back(2); M_list.push_back(10); M_list.push_back(20); M_list.push_back(4); for (size_t n=0; n<N_list.size(); ++n) { for (size_t m=0; m<M_list.size(); ++m) { const size_t N = N_list[n]; const size_t M = M_list[m]; matrix_t A(N,N), B(N,M), C(M,N), D(M,M); matrix_t E(N,N), F(N,M), G(M,N), H(M,M); alps::ResizableMatrix<Scalar> invA(N,N), BigMatrix(N+M, N+M, 0);//, invBigMatrix2(N+M, N+M, 0); randomize_matrix(A, 100);//100 is a seed randomize_matrix(B, 200); randomize_matrix(C, 300); randomize_matrix(D, 400); if (N>0) { invA = A.inverse(); } else { invA.destructive_resize(0,0); } copy_block(A,0,0,BigMatrix,0,0,N,N); copy_block(B,0,0,BigMatrix,0,N,N,M); copy_block(C,0,0,BigMatrix,N,0,M,N); copy_block(D,0,0,BigMatrix,N,N,M,M); const Scalar det_rat = compute_det_ratio_up<Scalar>(B, C, D, invA); ASSERT_TRUE(std::abs(det_rat-determinant(BigMatrix)/A.determinant())<1E-8) << "N=" << N << " M=" << M << " " << std::abs(det_rat-determinant(BigMatrix)) << "/" << std::abs(det_rat)<<"=" << std::abs(det_rat-determinant(BigMatrix)/A.determinant()); const Scalar det_rat2 = compute_inverse_matrix_up(B, C, D, invA); ASSERT_TRUE(std::abs(det_rat-det_rat2)<1E-8) << "N=" << N << " M=" << M; ASSERT_TRUE(norm_square(inverse(BigMatrix)-invA)<1E-8) << "N=" << N << " M=" << M; } } }
int main(void) { srand(time(NULL)); TYPE **a = alloc_matrix(N); TYPE **b = alloc_matrix(N); TYPE **c = alloc_matrix(N); randomize_matrix(a, N); randomize_matrix(b, N); clock_t start = clock(); mul_matrix(c, a, b, N); double working_time = (double)(clock() - start) / CLOCKS_PER_SEC; printf("Working time: %g s\n", working_time); free_matrix(a, N); free_matrix(b, N); free_matrix(c, N); return 0; }
int main(void) { struct sigaction ding; struct weston_matrix M; struct inverse_matrix Q; int ret; double errsup; double det; ding.sa_handler = stopme; sigemptyset(&ding.sa_mask); ding.sa_flags = 0; sigaction(SIGALRM, &ding, NULL); srandom(13); M.d[0] = 3.0; M.d[4] = 17.0; M.d[8] = 10.0; M.d[12] = 0.0; M.d[1] = 2.0; M.d[5] = 4.0; M.d[9] = -2.0; M.d[13] = 0.0; M.d[2] = 6.0; M.d[6] = 18.0; M.d[10] = -12; M.d[14] = 0.0; M.d[3] = 0.0; M.d[7] = 0.0; M.d[11] = 0.0; M.d[15] = 1.0; ret = matrix_invert(Q.LU, Q.perm, &M); printf("ret = %d\n", ret); printf("det = %g\n\n", determinant(&M)); if (ret != 0) return 1; print_inverse_data_matrix(&Q); printf("P * A = L * U\n"); print_permutation_matrix(&Q); print_LU_decomposition(&Q); printf("a random matrix:\n"); randomize_matrix(&M); det = determinant(&M); print_matrix(&M); errsup = test_inverse(&M); printf("\nThe matrix multiplied by its inverse, error:\n"); print_matrix(&M); printf("max abs error: %g, original determinant %g\n", errsup, det); test_loop_precision(); test_loop_speed_matrixvector(); test_loop_speed_inversetransform(); test_loop_speed_invert(); test_loop_speed_invert_explicit(); return 0; }
static int test(void) { struct weston_matrix m; double det, errsup; randomize_matrix(&m); det = determinant(&m); errsup = test_inverse(&m); if (errsup < 1e-6) return TEST_OK; if (fabs(det) < 1e-5 && isinf(errsup)) return TEST_NOT_INVERTIBLE_OK; printf("test fail, det: %g, error sup: %g\n", det, errsup); return TEST_FAIL; }
TEST(FastUpdate, BlockMatrixRemove) { typedef double Scalar; typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> matrix_t; std::vector<int> N_list, M_list; N_list.push_back(10); M_list.push_back(10); M_list.push_back(20); M_list.push_back(30); for (int n=0; n<N_list.size(); ++n) { for (int m=0; m<M_list.size(); ++m) { const int N = N_list[n]; const int M = M_list[m]; matrix_t BigMatrix(N+M, N+M, 0), invBigMatrix(N+M, N+M, 0);//G, G^{-1} matrix_t SmallMatrix(N,N,0);//G' std::vector<std::pair<int,int> > swapped_rows_in_G, swapped_cols_in_G; randomize_matrix(BigMatrix, 100);//100 is a seed invBigMatrix = inverse(BigMatrix); //which rows and cols are to be removed std::vector<int> rows_removed(N+M); std::vector<int> rows_remain(N); for (int i=0; i<N+M; ++i) { rows_removed[i] = i; } std::random_shuffle(rows_removed.begin(), rows_removed.end()); for (int i=0; i<N; ++i) { rows_remain[i] = rows_removed[i+M]; } rows_removed.resize(M); std::sort(rows_removed.begin(), rows_removed.end()); std::sort(rows_remain.begin(), rows_remain.end()); for (int j=0; j<N; ++j) { for (int i=0; i<N; ++i) { SmallMatrix(i,j) = BigMatrix(rows_remain[i], rows_remain[j]); } } //testing compute_det_ratio_down double det_rat = compute_det_ratio_down(M,rows_removed,invBigMatrix); ASSERT_TRUE(std::abs(det_rat-alps::numeric::determinant(SmallMatrix)/alps::numeric::determinant(BigMatrix))<1E-8) << "N=" << N << " M=" << M; matrix_t invSmallMatrix2(invBigMatrix); double det_rat2 = compute_inverse_matrix_down(M,rows_removed,invSmallMatrix2, swap_list); ASSERT_TRUE(std::abs(det_rat-det_rat2)<1E-8) << "N=" << N << " M=" << M; matrix_t SmallMatrix3(BigMatrix); for (int s=0; s<swap_list.size(); ++s) { SmallMatrix3.swap_cols(swap_list[s].first, swap_list[s].second); SmallMatrix3.swap_rows(swap_list[s].first, swap_list[s].second); } SmallMatrix3.resize(N,N); ASSERT_TRUE(alps::numeric::norm_square(inverse(SmallMatrix3)-invSmallMatrix2)<1E-8) << "N=" << N << " M=" << M; } } }