1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
/* ---------------------------------------------------------------------
*
* -- Integer Matrix Library (IML)
* (C) Copyright 2004, 2006 All Rights Reserved
*
* -- IML routines -- Version 1.0.1 -- November, 2006
*
* Author : Zhuliang Chen
* Contributor(s) : Arne Storjohann
* University of Waterloo -- School of Computer Science
* Waterloo, Ontario, N2L3G1 Canada
*
* ---------------------------------------------------------------------
*
* -- Copyright notice and Licensing terms:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions, and the following disclaimer in
* the documentation and/or other materials provided with the distri-
* bution.
* 3. The name of the University, the IML group, or the names of its
* contributors may not be used to endorse or promote products deri-
* ved from this software without specific written permission.
*
* -- Disclaimer:
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEO-
* RY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (IN-
* CLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/* this example shows how to call the certified solving function to solve
* a randomly generated nonsingular system
*/
#include <stdio.h>
#include <stdlib.h>
#include "gmp.h"
#include "iml.h"
long * randomLongMat (const long n, const long m, const long bd);
mpz_t * randomMPMat (const long n, const long m, const mpz_t mp_bd);
int main(void)
{
long i, j, n, m, certflag, bd, nullcol, ret;
long *A;
mpz_t mp_bd, mp_D, mp_DZ;
mpz_t *mp_B, *mp_N, *mp_NZ;
/* generate a n x m random left hand side matrix A */
n = 5; m = 8; /* set the input matrix dimension */
bd = 7; /* any entry e in A satisfying -2^bd < e < 2^bd */
A = randomLongMat(n, m, bd);
/* generate a n x 1 random right hand side matrix mp_B */
mpz_init(mp_bd);
mpz_ui_pow_ui(mp_bd, 2, bd); /* any entry e in mp_B satisfying */
mp_B = randomMPMat(n, 1, mp_bd); /* -2^bd < e < 2^bd */
/* print the input system */
fprintf(stdout, "Input Matrices:\n");
fprintf(stdout, "A:\n");
for (i = 0; i < n; i++)
{
fprintf(stdout, " ");
for (j = 0; j < m; j++)
fprintf(stdout, "%ld\t", A[i*m+j]);
fprintf(stdout, "\n");
}
fprintf(stdout, "\n B:\n");
for (i = 0; i < n; i++)
gmp_fprintf(stdout, " %Zd\n", mp_B[i]);
/* allocate space for numerator vector mp_N and denominator mp_D */
mpz_init(mp_D);
mp_N = (mpz_t *) malloc(m*sizeof(mpz_t));
for (i = 0; i < m; i++) { mpz_init(mp_N[i]); }
certflag = 1; /* compute the certificate vector */
/* allocate space for certificate vector */
mpz_init(mp_DZ);
mp_NZ = (mpz_t *) malloc(n*sizeof(mpz_t));
for (i = 0; i < n; i++) { mpz_init(mp_NZ[i]); }
/* solve system AX = mp_B without reduce the solution */
ret = certSolveLong(certflag, n, m, A, mp_B, mp_N, mp_D, mp_NZ, mp_DZ);
/* print the computation result */
if (ret != 3)
{
if (ret == 1)
fprintf(stdout, "\nSystem has more than one solution\n");
if (ret == 2)
fprintf(stdout, "\nSystem has a unique solution\n");
fprintf(stdout, "\nNon-reduced solution: \n");
fprintf(stdout, "\n Denominator: \n");
gmp_fprintf(stdout, " %Zd\n", mp_D);
fprintf(stdout, "\n Numerator: \n");
for (i = 0; i < m; i++)
gmp_fprintf(stdout, " %Zd\n", mp_N[i]);
}
else
{
fprintf(stdout, "\nSystem has no solution\n");
}
/* solve system AX = mp_B with solution reduced by a factor of 10 */
nullcol = 10;
ret = certSolveRedLong(certflag, nullcol, n, m, A, mp_B, mp_N, mp_D, \
mp_NZ, mp_DZ);
/* print the computation result */
if (ret != 3)
{
fprintf(stdout, "\nReduced solution: \n");
fprintf(stdout, "\n Denominator: \n");
gmp_fprintf(stdout, " %Zd\n", mp_D);
fprintf(stdout, "\n Numerator: \n");
for (i = 0; i < m; i++)
gmp_fprintf(stdout, " %Zd\n", mp_N[i]);
}
else
{
fprintf(stdout, "\nSystem has no solution\n");
}
/* free the memory */
for (i = 0; i < n; i++) { mpz_clear(mp_B[i]); mpz_clear(mp_NZ[i]); }
for (i = 0; i < m; i++) { mpz_clear(mp_N[i]); }
{ free(mp_N); free(mp_B); free(A); }
{ mpz_clear(mp_D); mpz_clear(mp_DZ); }
return 0;
}
/* generate a n x m random dense signed long matrix with entries lying in
* (-2^bd, 2^bd)
*/
long *
randomLongMat (const long n, const long m, const long bd)
{
long i, j;
mpz_t mp_rand, mp_sign;
gmp_randstate_t state;
unsigned long seed;
FILE *devrandom;
long *M;
static unsigned long inc = 0;
M = (long *) malloc (n * m * sizeof (long));
mpz_init (mp_sign);
mpz_init (mp_rand);
gmp_randinit_default (state);
seed = 387439;
/* generate random seed using /dev/random */
if ((devrandom = fopen ("/dev/urandom", "r")) != NULL)
{
fread (&seed, sizeof (seed), 1, devrandom);
fclose (devrandom);
}
seed += inc;
inc += 1;
gmp_randseed_ui (state, seed);
for (i = 0; i < n * m; i++)
{
mpz_urandomb (mp_rand, state, bd);
mpz_urandomb (mp_sign, state, 1);
if (mpz_sgn (mp_sign) == 0)
mpz_neg (mp_rand, mp_rand);
M[i] = mpz_get_si (mp_rand);
}
mpz_clear (mp_rand);
gmp_randclear (state);
mpz_clear (mp_sign);
return M;
}
/* generate a n x m random dense matrix of type mpz_t with entries lying in
* (-2^bd, 2^bd)
*/
mpz_t *
randomMPMat (const long n, const long m, const mpz_t mp_bd)
{
long i, j;
mpz_t mp_rand, mp_sign, * mp_B;
gmp_randstate_t state;
unsigned long seed;
FILE *devrandom;
static unsigned long inc = 0;
mp_B = (mpz_t *) malloc(n*m*sizeof(mpz_t));
mpz_init (mp_sign);
mpz_init (mp_rand);
gmp_randinit_default (state);
seed = 387439;
/* generate random seed using /dev/random */
if ((devrandom = fopen ("/dev/urandom", "r")) != NULL)
{
fread (&seed, sizeof (seed), 1, devrandom);
fclose (devrandom);
}
seed += inc;
inc += 1;
gmp_randseed_ui (state, seed);
for (i = 0; i < n * m; i++)
{
mpz_urandomm (mp_rand, state, mp_bd);
mpz_urandomb (mp_sign, state, 1);
if (mpz_sgn (mp_sign) == 0)
mpz_neg (mp_rand, mp_rand);
mpz_init_set(mp_B[i], mp_rand);
}
mpz_clear (mp_rand);
gmp_randclear (state);
mpz_clear (mp_sign);
return mp_B;
}
|