Austin Schuh | 9049e20 | 2022-02-20 17:34:16 -0800 | [diff] [blame^] | 1 | import numpy as np |
| 2 | from scipy import sparse |
| 3 | import scipy.sparse.linalg as sla |
| 4 | import utils.codegen_utils as cu |
| 5 | from numpy.random import Generator, PCG64 |
| 6 | |
| 7 | # Set random seed for reproducibility |
| 8 | rg = Generator(PCG64(2)) |
| 9 | |
| 10 | # Test sparse matrix construction vs dense |
| 11 | test_sp_matrix_Adns = np.around(.6*rg.random((5, 6))) + rg.standard_normal((5,6)) |
| 12 | test_sp_matrix_A = sparse.csc_matrix(test_sp_matrix_Adns) |
| 13 | |
| 14 | |
| 15 | # Test vector operations |
| 16 | test_vec_ops_n = 10 |
| 17 | test_vec_ops_v1 = rg.standard_normal(test_vec_ops_n) |
| 18 | test_vec_ops_v2 = rg.standard_normal(test_vec_ops_n) |
| 19 | test_vec_ops_sc = rg.standard_normal() |
| 20 | test_vec_ops_norm_inf = np.linalg.norm(test_vec_ops_v1, np.inf) |
| 21 | test_vec_ops_norm_inf_diff = np.linalg.norm(test_vec_ops_v1 - test_vec_ops_v2, |
| 22 | np.inf) |
| 23 | test_vec_ops_add_scaled = test_vec_ops_v1 + test_vec_ops_sc * test_vec_ops_v2 |
| 24 | test_vec_ops_ew_reciprocal = np.reciprocal(test_vec_ops_v1) |
| 25 | test_vec_ops_vec_prod = test_vec_ops_v1.dot(test_vec_ops_v2) |
| 26 | test_vec_ops_ew_max_vec = np.maximum(test_vec_ops_v1, test_vec_ops_v2) |
| 27 | test_vec_ops_ew_min_vec = np.minimum(test_vec_ops_v1, test_vec_ops_v2) |
| 28 | |
| 29 | |
| 30 | # Test matrix operations |
| 31 | test_mat_ops_n = 2 |
| 32 | test_mat_ops_A = sparse.random(test_mat_ops_n, test_mat_ops_n, density=0.8, format='csc', random_state=rg) |
| 33 | test_mat_ops_d = rg.standard_normal(test_mat_ops_n) |
| 34 | D = sparse.diags(test_mat_ops_d, format='csc') |
| 35 | test_mat_ops_prem_diag = D.dot(test_mat_ops_A).tocoo().tocsc() # Force matrix reordering |
| 36 | test_mat_ops_postm_diag = test_mat_ops_A.dot(D).tocoo().tocsc() # Force matrix reordering |
| 37 | test_mat_ops_inf_norm_cols = np.amax(np.abs( |
| 38 | np.asarray(test_mat_ops_A.todense())), axis=0) |
| 39 | test_mat_ops_inf_norm_rows = np.amax(np.abs( |
| 40 | np.asarray(test_mat_ops_A.todense())), axis=1) |
| 41 | |
| 42 | # Test matrix vector operations |
| 43 | m = 5 |
| 44 | n = 4 |
| 45 | p = 0.4 |
| 46 | |
| 47 | test_mat_vec_n = n |
| 48 | test_mat_vec_m = m |
| 49 | test_mat_vec_A = sparse.random(m, n, density=1.0, format='csc', random_state=rg) |
| 50 | test_mat_vec_P = sparse.random(n, n, density=0.8, format='csc', random_state=rg) |
| 51 | test_mat_vec_P = test_mat_vec_P + test_mat_vec_P.T |
| 52 | test_mat_vec_Pu = sparse.triu(test_mat_vec_P, format='csc') |
| 53 | test_mat_vec_x = rg.standard_normal(n) |
| 54 | test_mat_vec_y = rg.standard_normal(m) |
| 55 | test_mat_vec_Ax = test_mat_vec_A.dot(test_mat_vec_x) |
| 56 | test_mat_vec_Ax_cum = test_mat_vec_A.dot(test_mat_vec_x) + test_mat_vec_y |
| 57 | test_mat_vec_ATy = test_mat_vec_A.T.dot(test_mat_vec_y) |
| 58 | test_mat_vec_ATy_cum = test_mat_vec_A.T.dot(test_mat_vec_y) + test_mat_vec_x |
| 59 | test_mat_vec_Px = test_mat_vec_P.dot(test_mat_vec_x) |
| 60 | test_mat_vec_Px_cum = test_mat_vec_P.dot(test_mat_vec_x) + test_mat_vec_x |
| 61 | |
| 62 | |
| 63 | # Test extract upper triangular |
| 64 | test_mat_extr_triu_n = 5 |
| 65 | test_mat_extr_triu_P = sparse.random(test_mat_extr_triu_n, test_mat_extr_triu_n, density=0.8, format='csc', random_state=rg) |
| 66 | test_mat_extr_triu_P = test_mat_extr_triu_P + test_mat_extr_triu_P.T |
| 67 | test_mat_extr_triu_Pu = sparse.triu(test_mat_extr_triu_P, format='csc') |
| 68 | test_mat_extr_triu_P_inf_norm_cols = np.amax(np.abs( |
| 69 | np.asarray(test_mat_extr_triu_P.todense())), axis=0) |
| 70 | |
| 71 | |
| 72 | # Test compute quad form |
| 73 | test_qpform_n = 4 |
| 74 | test_qpform_P = sparse.random(test_qpform_n, test_qpform_n, density=0.8, format='csc', random_state=rg) |
| 75 | test_qpform_P = test_qpform_P + test_qpform_P.T |
| 76 | test_qpform_Pu = sparse.triu(test_qpform_P, format='csc') |
| 77 | test_qpform_x = rg.standard_normal(test_qpform_n) |
| 78 | test_qpform_value = .5 * test_qpform_x.T.dot(test_qpform_P.dot(test_qpform_x)) |
| 79 | |
| 80 | |
| 81 | # Generate test data and solutions |
| 82 | data = {'test_sp_matrix_A': test_sp_matrix_A, |
| 83 | 'test_sp_matrix_Adns': test_sp_matrix_Adns, |
| 84 | 'test_vec_ops_n': test_vec_ops_n, |
| 85 | 'test_vec_ops_v1': test_vec_ops_v1, |
| 86 | 'test_vec_ops_v2': test_vec_ops_v2, |
| 87 | 'test_vec_ops_sc': test_vec_ops_sc, |
| 88 | 'test_vec_ops_norm_inf': test_vec_ops_norm_inf, |
| 89 | 'test_vec_ops_norm_inf_diff': test_vec_ops_norm_inf_diff, |
| 90 | 'test_vec_ops_add_scaled': test_vec_ops_add_scaled, |
| 91 | 'test_vec_ops_ew_reciprocal': test_vec_ops_ew_reciprocal, |
| 92 | 'test_vec_ops_vec_prod': test_vec_ops_vec_prod, |
| 93 | 'test_vec_ops_ew_max_vec': test_vec_ops_ew_max_vec, |
| 94 | 'test_vec_ops_ew_min_vec': test_vec_ops_ew_min_vec, |
| 95 | 'test_mat_ops_n': test_mat_ops_n, |
| 96 | 'test_mat_ops_A': test_mat_ops_A, |
| 97 | 'test_mat_ops_d': test_mat_ops_d, |
| 98 | 'test_mat_ops_prem_diag': test_mat_ops_prem_diag, |
| 99 | 'test_mat_ops_postm_diag': test_mat_ops_postm_diag, |
| 100 | 'test_mat_ops_inf_norm_cols': test_mat_ops_inf_norm_cols, |
| 101 | 'test_mat_ops_inf_norm_rows': test_mat_ops_inf_norm_rows, |
| 102 | 'test_mat_vec_n': test_mat_vec_n, |
| 103 | 'test_mat_vec_m': test_mat_vec_m, |
| 104 | 'test_mat_vec_A': test_mat_vec_A, |
| 105 | 'test_mat_vec_Pu': test_mat_vec_Pu, |
| 106 | 'test_mat_vec_x': test_mat_vec_x, |
| 107 | 'test_mat_vec_y': test_mat_vec_y, |
| 108 | 'test_mat_vec_Ax': test_mat_vec_Ax, |
| 109 | 'test_mat_vec_Ax_cum': test_mat_vec_Ax_cum, |
| 110 | 'test_mat_vec_ATy': test_mat_vec_ATy, |
| 111 | 'test_mat_vec_ATy_cum': test_mat_vec_ATy_cum, |
| 112 | 'test_mat_vec_Px': test_mat_vec_Px, |
| 113 | 'test_mat_vec_Px_cum': test_mat_vec_Px_cum, |
| 114 | 'test_mat_extr_triu_n': test_mat_extr_triu_n, |
| 115 | 'test_mat_extr_triu_P': test_mat_extr_triu_P, |
| 116 | 'test_mat_extr_triu_Pu': test_mat_extr_triu_Pu, |
| 117 | 'test_mat_extr_triu_P_inf_norm_cols': |
| 118 | test_mat_extr_triu_P_inf_norm_cols, |
| 119 | 'test_qpform_n': test_qpform_n, |
| 120 | 'test_qpform_Pu': test_qpform_Pu, |
| 121 | 'test_qpform_x': test_qpform_x, |
| 122 | 'test_qpform_value': test_qpform_value, |
| 123 | } |
| 124 | |
| 125 | # Generate test data |
| 126 | cu.generate_data('lin_alg', data) |