int test_pluq_solve_left(rci_t m, rci_t n, int offsetA, int offsetB) { mzd_t* Abase = mzd_init(2048, 2048); mzd_t* Bbase = mzd_init(2048, 2048); mzd_randomize(Abase); mzd_randomize(Bbase); mzd_t* A = mzd_init_window(Abase, 0, offsetA, m, m + offsetA); mzd_t* B = mzd_init_window(Bbase, 0, offsetB, m, n + offsetB); // copy B mzd_t* Bcopy = mzd_init(B->nrows, B->ncols); for (rci_t i = 0; i < B->nrows; ++i) for (rci_t j = 0; j < B->ncols; ++j) mzd_write_bit(Bcopy,i,j, mzd_read_bit (B,i,j)); for (rci_t i = 0; i < m; ++i) { mzd_write_bit(A,i,i, 1); } mzd_t *Acopy = mzd_copy(NULL, A); rci_t r = mzd_echelonize(Acopy,1); printf("solve_left m: %4d, n: %4d, r: %4d da: %4d db: %4d ", m, n, r, offsetA, offsetB); mzd_free(Acopy); Acopy = mzd_copy(NULL, A); int consistency = mzd_solve_left(A, B, 0, 1); //copy B mzd_t *X = mzd_init(B->nrows,B->ncols); for (rci_t i = 0; i < B->nrows; ++i) for (rci_t j = 0; j < B->ncols; ++j) mzd_write_bit(X,i,j, mzd_read_bit (B,i,j)); mzd_t *B1 = mzd_mul(NULL, Acopy, X, 0); mzd_t *Z = mzd_add(NULL, Bcopy, B1); int status = 0; if(consistency == 0) { status = 1 - mzd_is_zero(Z); if (status == 0) { printf("passed\n"); } else { printf("FAILED\n"); } } else { printf("skipped (no solution)\n"); } mzd_free(Bcopy); mzd_free(B1); mzd_free(Z); mzd_free_window(A); mzd_free_window(B); mzd_free(Acopy); mzd_free(Abase); mzd_free(Bbase); mzd_free(X); return status; }
int test_kernel_left_pluq(size_t m, size_t n) { mzd_t* A = mzd_init(m, n); mzd_randomize(A); mzd_t *Acopy = mzd_copy(NULL, A); size_t r = mzd_echelonize_m4ri(A, 0, 0); printf("kernel_left m: %4zu, n: %4zu, r: %4zu ",m, n, r); mzd_free(Acopy); Acopy = mzd_copy(NULL, A); mzd_t *X = mzd_kernel_left_pluq(A, 0); if (X == NULL) { printf("passed\n"); mzd_free(A); mzd_free(Acopy); return 0; } mzd_t *Z = mzd_mul(NULL, Acopy, X, 0); int status = 1 - mzd_is_zero(Z); if (!status) printf("passed\n"); else printf("FAILED\n"); mzd_free(A); mzd_free(Acopy); mzd_free(X); mzd_free(Z); return status; }
int test_trsm_upper_right (int m, int n, int offset, const char* description){ printf("upper_right: %s m: %4d n: %4d offset: %4d ... ",description, m, n, offset); mzd_t* Ubase = mzd_init (2048,2048); mzd_t* Bbase = mzd_init (2048,2048); mzd_randomize (Ubase); mzd_randomize (Bbase); mzd_t* Bbasecopy = mzd_copy (NULL, Bbase); mzd_t* U = mzd_init_window (Ubase, 0, offset, n, offset + n); mzd_t* B = mzd_init_window (Bbase, 0, offset, m, offset + n); mzd_t* W = mzd_copy (NULL, B); size_t i,j; for (i=0; i<n; ++i){ for (j=0; j<i;++j) mzd_write_bit(U,i,j, 0); mzd_write_bit(U,i,i, 1); } mzd_trsm_upper_right (U, B, 2048); mzd_addmul(W, B, U, 2048); int status = 0; for ( i=0; i<m; ++i) for ( j=0; j<n; ++j){ if (mzd_read_bit (W,i,j)){ status = 1; } } // Verifiying that nothing has been changed around the submatrices mzd_addmul(W, B, U, 2048); mzd_copy (B, W); for ( i=0; i<2048; ++i) for ( j=0; j<2048/RADIX; ++j){ if (Bbase->rows[i][j] != Bbasecopy->rows[i][j]){ status = 1; } } mzd_free_window (U); mzd_free_window (B); mzd_free (W); mzd_free(Ubase); mzd_free(Bbase); mzd_free(Bbasecopy); if (!status) printf("passed\n"); else printf("FAILED\n"); return status; }
int addmul_test_equality(rci_t m, rci_t l, rci_t n, int k, int cutoff) { int ret = 0; mzd_t *A, *B, *C, *D, *E, *F; printf("addmul: m: %4d, l: %4d, n: %4d, k: %2d, cutoff: %4d", m, l, n, k, cutoff); /* we create two random matrices */ A = mzd_init(m, l); B = mzd_init(l, n); C = mzd_init(m, n); mzd_randomize(A); mzd_randomize(B); mzd_randomize(C); /* D = C + A*B via M4RM, temporary buffers are managed internally */ D = mzd_copy(NULL, C); D = mzd_addmul_m4rm(D, A, B, k); /* E = C + A*B via naiv cubic multiplication */ E = mzd_mul_m4rm(NULL, A, B, k); mzd_add(E, E, C); /* F = C + A*B via naiv cubic multiplication */ F = mzd_copy(NULL, C); F = mzd_addmul(F, A, B, cutoff); mzd_free(A); mzd_free(B); mzd_free(C); if (mzd_equal(D, E) != TRUE) { printf(" M4RM != add,mul"); ret -=1; } if (mzd_equal(E, F) != TRUE) { printf(" add,mul = addmul"); ret -=1; } if (mzd_equal(F, D) != TRUE) { printf(" M4RM != addmul"); ret -=1; } if (ret==0) printf(" ... passed\n"); else printf(" ... FAILED\n"); mzd_free(D); mzd_free(E); mzd_free(F); return ret; }
int test_trsm_lower_left (int m, int n, int offsetL, int offsetB){ mzd_t* Lbase = mzd_init (2048,2048); mzd_t* Bbase = mzd_init (2048,2048); mzd_randomize (Lbase); mzd_randomize (Bbase); mzd_t* Bbasecopy = mzd_copy (NULL, Bbase); mzd_t* L = mzd_init_window (Lbase, 0, offsetL, m, offsetL + m); mzd_t* B = mzd_init_window (Bbase, 0, offsetB, m, offsetB + n); mzd_t* W = mzd_copy (NULL, B); size_t i,j; for (i=0; i<m; ++i){ for (j=i+1; j<m;++j) mzd_write_bit(L,i,j, 0); mzd_write_bit(L,i,i, 1); } mzd_trsm_lower_left(L, B, 2048); mzd_addmul(W, L, B, 2048); int status = 0; for ( i=0; i<m; ++i) for ( j=0; j<n; ++j){ if (mzd_read_bit (W,i,j)){ status = 1; } } // Verifiying that nothing has been changed around the submatrices mzd_addmul(W, L, B, 2048); mzd_copy (B, W); for ( i=0; i<2048; ++i) for ( j=0; j<2048/RADIX; ++j){ if (Bbase->rows[i][j] != Bbasecopy->rows[i][j]){ status = 1; } } mzd_free_window (L); mzd_free_window (B); mzd_free_window (W); mzd_free(Lbase); mzd_free(Bbase); mzd_free(Bbasecopy); if (!status) printf(" ... passed\n"); else printf(" ... FAILED\n"); return status; }
mzd_t *mzd_kernel_left_pluq(mzd_t *A, int const cutoff) { mzp_t *P = mzp_init(A->nrows); mzp_t *Q = mzp_init(A->ncols); rci_t r = mzd_pluq(A, P, Q, cutoff); if (r == A->ncols) { mzp_free(P); mzp_free(Q); __M4RI_DD_MZD(A); return NULL; } mzd_t *U = mzd_init_window(A, 0, 0, r, r); mzd_t *B = mzd_init_window(A, 0, r, r, A->ncols); mzd_trsm_upper_left(U, B, cutoff); mzd_t *R = mzd_init(A->ncols, A->ncols - r); mzd_t *RU = mzd_init_window(R, 0, 0, r, R->ncols); mzd_copy(RU, B); for(rci_t i = 0; i < R->ncols; ++i) { mzd_write_bit(R, r + i, i, 1); } mzd_apply_p_left_trans(R, Q); mzp_free(P); mzp_free(Q); mzd_free_window(RU); mzd_free_window(U); mzd_free_window(B); __M4RI_DD_MZD(A); __M4RI_DD_MZD(R); return R; }
gf2matrix *invert_matrix(gf2matrix *dest, const gf2matrix *m) { if (!m) return NULL; mzd_t *temp = mzd_inv_m4ri(NULL, (mzd_t*) m, 0); if(!temp) { dest = NULL; } else { mzd_t *I = make_identity_matrix(m->nrows); mzd_t *prod = mul_matrices(NULL, m, temp); if(comp_matrices(prod, I) == 0) { // inversion was successful if (dest) { mzd_copy(dest, temp); mzd_free(temp); } else { dest = temp; } } else { dest = NULL; } mzd_free(prod); mzd_free(I); } return dest; }
int test_pluq_structured(rci_t m, rci_t n) { printf("pluq: testing structured m: %5d, n: %5d", m, n); mzd_t* A = mzd_init(m, n); mzd_t* L = mzd_init(m, m); mzd_t* U = mzd_init(m, n); for(rci_t i = 0; i < m; i += 2) for (rci_t j = i; j < n; ++j) mzd_write_bit(A, i, j, 1); mzd_t* Acopy = mzd_copy (NULL,A); mzp_t* P = mzp_init(m); mzp_t* Q = mzp_init(n); rci_t r = mzd_pluq(A, P, Q, 0); printf(", rank: %5d ",r); for (rci_t i = 0; i < r; ++i){ for (rci_t j = 0; j < i; ++j) mzd_write_bit(L, i, j, mzd_read_bit(A,i,j)); for (rci_t j = i + 1; j < n; ++j) mzd_write_bit(U, i, j, mzd_read_bit(A,i,j)); } for (rci_t i = r; i < m; ++i) for (rci_t j = 0; j < r; ++j) mzd_write_bit(L, i, j, mzd_read_bit(A,i,j)); for (rci_t i = 0; i < r; ++i){ mzd_write_bit(L,i,i, 1); mzd_write_bit(U,i,i, 1); } mzd_apply_p_left(Acopy, P); mzd_apply_p_right_trans(Acopy, Q); mzd_addmul(Acopy, L, U, 0); int status = 0; for (rci_t i = 0; i < m; ++i) for (rci_t j = 0; j < n; ++j){ if (mzd_read_bit (Acopy,i,j)){ status = 1; break; } } if (status) { printf("\n"); printf(" ... FAILED\n"); } else printf (" ... passed\n"); mzd_free(U); mzd_free(L); mzd_free(A); mzd_free(Acopy); mzp_free(P); mzp_free(Q); return status; }
/* * Class: m4rjni_Mzd * Method: mzd_copy * Signature: (J)J */ JNIEXPORT jlong JNICALL Java_m4rjni_Mzd_mzd_1copy(JNIEnv *env, jobject obj, jlong ptr) { mzd_t *M = (mzd_t*)ptr; if (M==NULL) return 0; mzd_t* D = mzd_copy(NULL,M); return (jlong) D; }
gf2matrix *dup_matrix(const gf2matrix *m) { if (!m) return NULL; return mzd_copy(NULL, m); }
int test_lqup_half_rank(size_t m, size_t n) { printf("pluq: testing half rank m: %5zd, n: %5zd",m,n); mzd_t* U = mzd_init(m, n); mzd_t* L = mzd_init(m, m); mzd_t* U2 = mzd_init(m, n); mzd_t* L2 = mzd_init(m, m); mzd_t* A = mzd_init(m, n); mzd_randomize (U); mzd_randomize (L); size_t i,j; for (i=0; i<m && i<n; ++i){ mzd_write_bit(U,i,i, 1); for (j=0; j<i;++j) mzd_write_bit(U,i,j, 0); if (i%2) for (j=i; j<n;++j) mzd_write_bit(U,i,j, 0); for (j=i+1; j<m;++j) mzd_write_bit(L,i,j, 0); mzd_write_bit(L,i,i, 1); } mzd_mul(A, L, U, 0); mzd_t* Acopy = mzd_copy (NULL,A); mzp_t* Pt = mzp_init(m); mzp_t* Q = mzp_init(n); int r = mzd_pluq(A, Pt, Q, 0); for (i=0; i<r; ++i){ for (j=0; j<i;++j) mzd_write_bit (L2, i, j, mzd_read_bit(A,i,j)); for (j=i+1; j<n;++j) mzd_write_bit (U2, i, j, mzd_read_bit(A,i,j)); } for (i=r; i<m; i++) for (j=0; j<r;++j) mzd_write_bit (L2, i, j, mzd_read_bit(A,i,j)); for (i=0; i<r; ++i){ mzd_write_bit(L2,i,i, 1); mzd_write_bit(U2,i,i, 1); } mzd_apply_p_left(Acopy, Pt); mzd_apply_p_right_trans(Acopy, Q); mzd_addmul(Acopy,L2,U2,0); int status = 0; for ( i=0; i<m; ++i) { for ( j=0; j<n; ++j){ if (mzd_read_bit(Acopy,i,j)){ status = 1; } } if(status) break; } if (status) printf(" ... FAILED\n"); else printf (" ... passed\n"); mzd_free(U); mzd_free(L); mzd_free(U2); mzd_free(L2); mzd_free(A); mzd_free(Acopy); mzp_free(Pt); mzp_free(Q); return status; }
int test_lqup_full_rank (size_t m, size_t n){ printf("pluq: testing full rank m: %5zu, n: %5zu",m,n); mzd_t* U = mzd_init (m,n); mzd_t* L = mzd_init (m,m); mzd_t* U2 = mzd_init (m,n); mzd_t* L2 = mzd_init (m,m); mzd_t* A = mzd_init (m,n); mzd_randomize (U); mzd_randomize (L); size_t i,j; for (i=0; i<m; ++i){ for (j=0; j<i && j<n;++j) mzd_write_bit(U,i,j, 0); for (j=i+1; j<m;++j) mzd_write_bit(L,i,j, 0); if(i<n) mzd_write_bit(U,i,i, 1); mzd_write_bit(L,i,i, 1); } mzd_mul(A, L, U, 2048); mzd_t* Acopy = mzd_copy (NULL,A); mzp_t* P = mzp_init(m); mzp_t* Q = mzp_init(n); mzd_pluq(A, P, Q, 2048); for (i=0; i<m; ++i){ for (j=0; j<i && j <n;++j) mzd_write_bit (L2, i, j, mzd_read_bit(A,i,j)); for (j=i+1; j<n;++j) mzd_write_bit (U2, i, j, mzd_read_bit(A,i,j)); } for (i=0; i<n && i<m; ++i){ mzd_write_bit(L2,i,i, 1); mzd_write_bit(U2,i,i, 1); } mzd_addmul(Acopy,L2,U2,0); int status = 0; for ( i=0; i<m; ++i) for ( j=0; j<n; ++j){ if (mzd_read_bit (Acopy,i,j)){ status = 1; } } if (status){ printf(" ... FAILED\n"); } else printf (" ... passed\n"); mzd_free(U); mzd_free(L); mzd_free(U2); mzd_free(L2); mzd_free(A); mzd_free(Acopy); mzp_free(P); mzp_free(Q); return status; }