-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2189 from lgoettgens/lg/fmpz_mod_mat_pow
Add `gr_mat_pow_ui` and `gr_mat_pow_fmpz`, use it in added `fmpz_mod_mat_pow_ui`
- Loading branch information
Showing
11 changed files
with
363 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Copyright (C) 2025 Lars Göttgens | ||
This file is part of FLINT. | ||
FLINT is free software: you can redistribute it and/or modify it under | ||
the terms of the GNU Lesser General Public License (LGPL) as published | ||
by the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. See <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "longlong.h" | ||
#include "fmpz_mod.h" | ||
#include "fmpz_mod_mat.h" | ||
#include "gr.h" | ||
#include "gr_mat.h" | ||
|
||
void | ||
fmpz_mod_mat_pow_ui(fmpz_mod_mat_t B, const fmpz_mod_mat_t A, ulong exp, | ||
const fmpz_mod_ctx_t ctx) | ||
{ | ||
gr_ctx_t gr_ctx; | ||
|
||
_gr_ctx_init_fmpz_mod_from_ref(gr_ctx, ctx); | ||
GR_MUST_SUCCEED(gr_mat_pow_ui | ||
((gr_mat_struct *) B, (const gr_mat_struct *) A, exp, | ||
gr_ctx)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
Copyright (C) 2025 Lars Göttgens | ||
This file is part of FLINT. | ||
FLINT is free software: you can redistribute it and/or modify it under | ||
the terms of the GNU Lesser General Public License (LGPL) as published | ||
by the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. See <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "test_helpers.h" | ||
#include "fmpz_mod_mat.h" | ||
|
||
TEST_FUNCTION_START(fmpz_mod_mat_pow, state) | ||
{ | ||
slong i; | ||
|
||
for (i = 0; i < 100 * flint_test_multiplier(); i++) | ||
{ | ||
fmpz_mod_mat_t A, B, C; | ||
slong n, i; | ||
ulong e; | ||
fmpz_mod_ctx_t ctx; | ||
|
||
n = n_randint(state, 10); | ||
e = n_randint(state, 20); | ||
|
||
fmpz_mod_ctx_init_rand_bits(ctx, state, 200); | ||
|
||
fmpz_mod_mat_init(A, n, n, ctx); | ||
fmpz_mod_mat_init(B, n, n, ctx); | ||
fmpz_mod_mat_init(C, n, n, ctx); | ||
|
||
fmpz_mod_mat_randtest(A, state, ctx); | ||
fmpz_mod_mat_randtest(B, state, ctx); | ||
|
||
/* Make sure noise in the output is ok */ | ||
fmpz_mod_mat_randtest(B, state, ctx); | ||
|
||
fmpz_mod_mat_pow_ui(B, A, e, ctx); | ||
|
||
fmpz_mod_mat_one(C, ctx); | ||
for (i = 0; i < e; i++) | ||
fmpz_mod_mat_mul(C, C, A, ctx); | ||
|
||
if (!fmpz_mod_mat_equal(C, B, ctx)) | ||
{ | ||
flint_printf("FAIL: results not equal\n"); | ||
fflush(stdout); | ||
flint_abort(); | ||
} | ||
|
||
fmpz_mod_mat_pow_ui(A, A, e, ctx); | ||
|
||
if (!fmpz_mod_mat_equal(A, B, ctx)) | ||
{ | ||
flint_printf("FAIL: aliasing failed\n"); | ||
fflush(stdout); | ||
flint_abort(); | ||
} | ||
|
||
fmpz_mod_mat_clear(A, ctx); | ||
fmpz_mod_mat_clear(B, ctx); | ||
fmpz_mod_mat_clear(C, ctx); | ||
fmpz_mod_ctx_clear(ctx); | ||
} | ||
|
||
TEST_FUNCTION_END(state); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
Copyright (C) 2025 Lars Göttgens | ||
This file is part of FLINT. | ||
FLINT is free software: you can redistribute it and/or modify it under | ||
the terms of the GNU Lesser General Public License (LGPL) as published | ||
by the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. See <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "longlong.h" | ||
#include "fmpz.h" | ||
#include "gr.h" | ||
#include "gr_mat.h" | ||
|
||
int | ||
gr_mat_pow_ui(gr_mat_t res, const gr_mat_t mat, ulong exp, gr_ctx_t ctx) | ||
{ | ||
int status; | ||
slong sz = ctx->sizeof_elem; | ||
slong d; | ||
|
||
d = gr_mat_nrows(res, ctx); | ||
|
||
if (d != gr_mat_ncols(res, ctx) || d != gr_mat_nrows(mat, ctx) | ||
|| d != gr_mat_ncols(mat, ctx)) | ||
{ | ||
return GR_DOMAIN; | ||
} | ||
|
||
status = GR_SUCCESS; | ||
|
||
if (exp <= 2 || d <= 1) | ||
{ | ||
if (exp == 0 || d == 0) | ||
{ | ||
status |= gr_mat_one(res, ctx); | ||
} | ||
else if (d == 1) | ||
{ | ||
status |= gr_pow_ui(GR_MAT_ENTRY(res, 0, 0, sz), | ||
GR_MAT_ENTRY(mat, 0, 0, sz), exp, ctx); | ||
} | ||
else if (exp == 1) | ||
{ | ||
status |= gr_mat_set(res, mat, ctx); | ||
} | ||
else if (exp == 2) | ||
{ | ||
status |= gr_mat_sqr(res, mat, ctx); | ||
} | ||
} | ||
else | ||
{ | ||
gr_ctx_t mctx; | ||
|
||
gr_ctx_init_matrix_ring(mctx, ctx, d); | ||
status |= gr_pow_ui(res, mat, exp, mctx); | ||
gr_ctx_clear(mctx); | ||
} | ||
|
||
return status; | ||
} | ||
|
||
int | ||
gr_mat_pow_fmpz(gr_mat_t res, const gr_mat_t mat, fmpz_t exp, gr_ctx_t ctx) | ||
{ | ||
int status; | ||
slong sz = ctx->sizeof_elem; | ||
slong d; | ||
|
||
d = gr_mat_nrows(res, ctx); | ||
|
||
if (d != gr_mat_ncols(res, ctx) || d != gr_mat_nrows(mat, ctx) | ||
|| d != gr_mat_ncols(mat, ctx)) | ||
{ | ||
return GR_DOMAIN; | ||
} | ||
|
||
status = GR_SUCCESS; | ||
|
||
|
||
if (fmpz_is_zero(exp) || d == 0) | ||
{ | ||
status |= gr_mat_one(res, ctx); | ||
} | ||
else if (d == 1) | ||
{ | ||
status |= gr_pow_fmpz(GR_MAT_ENTRY(res, 0, 0, sz), | ||
GR_MAT_ENTRY(mat, 0, 0, sz), exp, ctx); | ||
} | ||
else if (fmpz_is_one(exp)) | ||
{ | ||
status |= gr_mat_set(res, mat, ctx); | ||
} | ||
else | ||
{ | ||
gr_ctx_t mctx; | ||
|
||
gr_ctx_init_matrix_ring(mctx, ctx, d); | ||
status |= gr_pow_fmpz(res, mat, exp, mctx); | ||
gr_ctx_clear(mctx); | ||
} | ||
|
||
return status; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.