Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ashishtest #51

Merged
merged 10 commits into from
Mar 10, 2014
10 changes: 9 additions & 1 deletion nmod_mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

Copyright (C) 2010 William Hart
Copyright (C) 2010,2011 Fredrik Johansson
Copyright (C) 2014 Ashish Kedia

******************************************************************************/

Expand Down Expand Up @@ -73,14 +74,16 @@ _nmod_mat_set_mod(nmod_mat_t mat, mp_limb_t n)
void nmod_mat_init(nmod_mat_t mat, slong rows, slong cols, mp_limb_t n);
void nmod_mat_init_set(nmod_mat_t mat, const nmod_mat_t src);
void nmod_mat_clear(nmod_mat_t mat);
void nmod_mat_one(nmod_mat_t mat);
void nmod_mat_swap(nmod_mat_t mat1, nmod_mat_t mat2);

void nmod_mat_window_init(nmod_mat_t window, const nmod_mat_t mat, slong r1, slong c1, slong r2, slong c2);
void nmod_mat_window_clear(nmod_mat_t window);

/* Random matrix generation */
void nmod_mat_randtest(nmod_mat_t mat, flint_rand_t state);
void nmod_mat_randfull(nmod_mat_t mat, flint_rand_t state);
int nmod_mat_randpermdiag(nmod_mat_t mat, flint_rand_t state,
int nmod_mat_randpermdiag(nmod_mat_t mat, flint_rand_t state,
mp_srcptr diag, slong n);
void nmod_mat_randrank(nmod_mat_t, flint_rand_t state, slong rank);
void nmod_mat_randops(nmod_mat_t mat, slong count, flint_rand_t state);
Expand Down Expand Up @@ -138,6 +141,11 @@ void nmod_mat_addmul(nmod_mat_t D, const nmod_mat_t C,
void nmod_mat_submul(nmod_mat_t D, const nmod_mat_t C,
const nmod_mat_t A, const nmod_mat_t B);

/* Exponent */

void _nmod_mat_pow(nmod_mat_t dest, const nmod_mat_t mat, ulong pow);
void nmod_mat_pow(nmod_mat_t dest, const nmod_mat_t mat, ulong pow);

/* Trace */

mp_limb_t nmod_mat_trace(const nmod_mat_t mat);
Expand Down
21 changes: 21 additions & 0 deletions nmod_mat/doc/nmod_mat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
/******************************************************************************

Copyright (C) 2010 Fredrik Johansson
Copyright (C) 2014 Ashish Kedia

******************************************************************************/

Expand Down Expand Up @@ -51,6 +52,10 @@ void nmod_mat_set(nmod_mat_t mat, nmod_mat_t src)
Sets \code{mat} to a copy of \code{src}. It is assumed
that \code{mat} and \code{src} have identical dimensions.

void nmod_mat_swap(nmod_mat_t mat1, nmod_mat_t mat2)

Exchanges \code{mat1} and \code{mat2}.

*******************************************************************************

Basic properties and manipulation
Expand Down Expand Up @@ -244,6 +249,22 @@ void nmod_mat_submul(nmod_mat_t D, const nmod_mat_t C,
Sets $D = C + AB$. $C$ and $D$ may be aliased with each other but
not with $A$ or $B$.

*******************************************************************************

Matrix Exponentiation

*******************************************************************************

void _nmod_mat_pow(nmod_mat_t dest, const nmod_mat_t mat, ulong pow)

Sets $dest = mat^pow$. \code{dest} and \code{mat} cannot be aliased.
Implements exponentiation by squaring.

void nmod_mat_pow(nmod_mat_t dest, nmod_mat_t mat, ulong pow)

Sets $dest = mat^pow$. \code{dest} and \code{mat} may be aliased. Implements
exponentiation by squaring.

*******************************************************************************

Trace
Expand Down
41 changes: 41 additions & 0 deletions nmod_mat/one.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*=============================================================================

This file is part of FLINT.

FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

=============================================================================*/
/******************************************************************************

Copyright (C) 2014 Ashish Kedia

******************************************************************************/

#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "nmod_mat.h"

/*Function to convert a square matrix to an identity matrix
The matrix is assumed to be a square one*/
void
nmod_mat_one(nmod_mat_t mat)
{
slong i,j;
for(i = 0; i < mat->r; i++)
for(j = 0; j < mat->c; j++)
if(i==j) nmod_mat_entry(mat, i, j) = 1;
else nmod_mat_entry(mat, i, j) = 0;
}
98 changes: 98 additions & 0 deletions nmod_mat/pow.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*=============================================================================

This file is part of FLINT.

FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

=============================================================================*/
/******************************************************************************

Copyright (C) 2014 Ashish Kedia

******************************************************************************/

#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "nmod_mat.h"

void
_nmod_mat_pow(nmod_mat_t dest, const nmod_mat_t mat, ulong pow)
{
nmod_mat_t temp1, temp2;
if (mat->r == 0)
{
return;
}
if (pow == 0)
{
nmod_mat_one(dest);
return;
}
if (pow == 1)
{
nmod_mat_set(dest, mat);
return;
}
if (pow == 2)
{
nmod_mat_mul(dest, mat, mat);
return;
}

nmod_mat_init(temp1, mat->r, mat->c, mat->mod.n);

if(pow == 3)
{
nmod_mat_mul(temp1, mat, mat);
nmod_mat_mul(dest, temp1, mat);
nmod_mat_clear(temp1);
return;
}

nmod_mat_one(dest);
nmod_mat_init_set(temp2, mat);
while(pow > 0)
{
if(pow%2 == 1)
{
nmod_mat_mul(temp1, dest, temp2);
nmod_mat_swap(temp1, dest);
}
if (pow > 1)
{
nmod_mat_mul(temp1, temp2, temp2);
nmod_mat_swap(temp1, temp2);
}
pow /= 2;
}
nmod_mat_clear(temp1);
nmod_mat_clear(temp2);
}
void
nmod_mat_pow(nmod_mat_t dest, const nmod_mat_t mat, ulong pow)
{
nmod_mat_t temp;
if (mat == dest)
{
nmod_mat_init_set(temp, mat);
_nmod_mat_pow(dest, temp, pow);
nmod_mat_clear(temp);
}
else
{
_nmod_mat_pow(dest, mat, pow);
}
}
38 changes: 38 additions & 0 deletions nmod_mat/swap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*=============================================================================

This file is part of FLINT.

FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

=============================================================================*/
/******************************************************************************

Copyright (C) 2014 Ashish Kedia

******************************************************************************/

#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "nmod_mat.h"

void
nmod_mat_swap(nmod_mat_t mat1, nmod_mat_t mat2)
{
nmod_mat_t temp;
*temp = *mat1;
*mat1 = *mat2;
*mat2 = *temp;
}
4 changes: 2 additions & 2 deletions nmod_mat/test/t-mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ main(void)
{
slong i;
FLINT_TEST_INIT(state);


flint_printf("mul....");
fflush(stdout);
Expand Down Expand Up @@ -132,7 +132,7 @@ main(void)
}

FLINT_TEST_CLEANUP(state);

flint_printf("PASS\n");
return 0;
}
88 changes: 88 additions & 0 deletions nmod_mat/test/t-pow.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*=============================================================================

This file is part of FLINT.

FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

=============================================================================*/
/******************************************************************************

Copyright (C) 2014 Ashish Kedia

******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <gmp.h>
#include "flint.h"
#include "nmod_mat.h"
#include "ulong_extras.h"

int
main()
{
slong i;
FLINT_TEST_INIT(state);

flint_printf("pow....");
fflush(stdout);

for (i = 0; i < 1000 * flint_test_multiplier(); i++)
{
nmod_mat_t A, B, C, D;
mp_limb_t mod;
slong m, j;
ulong exp;

mod = n_randtest_not_zero(state);
m = n_randint(state, 20);
exp = n_randint(state, 50);

nmod_mat_init(A, m, m, mod);
nmod_mat_init(B, m, m, mod);
nmod_mat_init(C, m, m, mod);
nmod_mat_init(D, m, m, mod);
nmod_mat_randtest(A, state);

nmod_mat_pow(B, A, exp);
nmod_mat_one(C);
for(j = 1; j <= exp; j++)
{
nmod_mat_mul(D, C, A);
nmod_mat_swap(D, C);
}
nmod_mat_pow(A, A, exp);

if (!(nmod_mat_equal(C, B) && nmod_mat_equal(C, A)))
{
flint_printf("FAIL: results not equal\n");fflush(stdout);
nmod_mat_print_pretty(A);
nmod_mat_print_pretty(B);
nmod_mat_print_pretty(C);
abort();
}

nmod_mat_clear(A);
nmod_mat_clear(B);
nmod_mat_clear(C);
nmod_mat_clear(D);
}

FLINT_TEST_CLEANUP(state);

flint_printf("PASS\n");
return 0;
}
7 changes: 7 additions & 0 deletions nmod_poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
Copyright (C) 2010, 2011 William Hart
Copyright (C) 2011 Sebastian Pancratz
Copyright (C) 2011 Fredrik Johansson
Copyright (C) 2014 Ashish Kedia

******************************************************************************/

Expand Down Expand Up @@ -791,6 +792,12 @@ void _nmod_poly_evaluate_nmod_vec_fast(mp_ptr ys, mp_srcptr coeffs, slong len,
void nmod_poly_evaluate_nmod_vec_fast(mp_ptr ys,
const nmod_poly_t poly, mp_srcptr xs, slong n);

void _nmod_poly_evaluate_mat(nmod_mat_t dest,
const mp_srcptr poly, slong len, const nmod_mat_t c);

void nmod_poly_evaluate_mat(nmod_mat_t dest,
const nmod_poly_t poly, const nmod_mat_t c);

/* Subproduct tree **********************************************************/

mp_ptr * _nmod_poly_tree_alloc(slong len);
Expand Down