Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/build/pkgs/iml/patches/examples.patch
8820 views
1
diff -ruN iml-1.0.1-sage/examples/exam-nullspace.c src/examples/exam-nullspace.c
2
--- iml-1.0.1-sage/examples/exam-nullspace.c 2006-11-23 22:45:00.000000000 +0100
3
+++ src/examples/exam-nullspace.c 2007-03-01 06:25:06.000000000 +0100
4
@@ -46,6 +46,7 @@
5
6
#include <stdio.h>
7
#include <stdlib.h>
8
+#include <time.h>
9
#include "gmp.h"
10
#include "iml.h"
11
12
@@ -57,29 +58,41 @@
13
{
14
long i, j, n, m, bd, s, *A;
15
mpz_t *mp_B, *mp_N;
16
+ clock_t c;
17
18
/* generate a n x m random left hand side matrix A */
19
n = 5;
20
- m = 10;
21
- bd = 34; /* entris of A satisfying -2^bd < e < 2^bd */
22
+ m = 6;
23
+ bd = 3; /* entris of A satisfying -2^bd < e < 2^bd */
24
A = randomLongMat (n, m, bd);
25
+ c = clock();
26
+ s = nullspaceLong (n, m, A, &mp_N);
27
+ printf("Time to compute nullspace: %f\n", (clock() - c)/((float)CLOCKS_PER_SEC));
28
+ fprintf (stdout, "Dimension of nullspace: ");
29
+ fprintf (stdout, " %ld\n", s);
30
+
31
fprintf (stdout, "Input system:\n");
32
+ fprintf (stdout, "[");
33
for (i = 0; i < n; i++)
34
{
35
- fprintf (stdout, " ");
36
- for (j = 0; j < m; j++)
37
- fprintf (stdout, "%ld\t", A[i * m + j]);
38
- fprintf (stdout, "\n");
39
+ for (j = 0; j < m; j++) {
40
+ fprintf (stdout, "%ld", A[i * m + j]);
41
+ if (!(i==n-1 && j == m-1))
42
+ fprintf(stdout, ",");
43
+ }
44
}
45
- s = nullspaceLong (n, m, A, &mp_N);
46
- fprintf (stdout, "Dimension of nullspace: ");
47
- fprintf (stdout, " %ld\n", s);
48
+ fprintf(stdout, "]\n");
49
+
50
+ fprintf (stdout, "[");
51
for (i = 0; i < m; i++)
52
{
53
- for (j = 0; j < s; j++)
54
- gmp_fprintf (stdout, " %Zd", mp_N[i * s + j]);
55
- fprintf (stdout, "\n");
56
+ for (j = 0; j < s; j++) {
57
+ gmp_fprintf (stdout, "%Zd", mp_N[i * s + j]);
58
+ if (!(i==m-1 && j == s-1))
59
+ fprintf(stdout, ",");
60
+ }
61
}
62
+ fprintf(stdout, "]\n");
63
free (A);
64
for (i = 0; i < m * s; i++)
65
mpz_clear (mp_N[i]);
66
diff -ruN iml-1.0.1-sage/examples/exam-nullspace2.c src/examples/exam-nullspace2.c
67
--- iml-1.0.1-sage/examples/exam-nullspace2.c 1970-01-01 01:00:00.000000000 +0100
68
+++ src/examples/exam-nullspace2.c 2007-03-01 06:25:06.000000000 +0100
69
@@ -0,0 +1,146 @@
70
+/* ---------------------------------------------------------------------
71
+ *
72
+ * -- Integer Matrix Library (IML)
73
+ * (C) Copyright 2004, 2006 All Rights Reserved
74
+ *
75
+ * -- IML routines -- Version 1.0.1 -- November, 2006
76
+ *
77
+ * Author : Zhuliang Chen
78
+ * Contributor(s) : Arne Storjohann
79
+ * University of Waterloo -- School of Computer Science
80
+ * Waterloo, Ontario, N2L3G1 Canada
81
+ *
82
+ * ---------------------------------------------------------------------
83
+ *
84
+ * -- Copyright notice and Licensing terms:
85
+ *
86
+ * Redistribution and use in source and binary forms, with or without
87
+ * modification, are permitted provided that the following conditions
88
+ * are met:
89
+ *
90
+ * 1. Redistributions of source code must retain the above copyright
91
+ * notice, this list of conditions and the following disclaimer.
92
+ * 2. Redistributions in binary form must reproduce the above copyright
93
+ * notice, this list of conditions, and the following disclaimer in
94
+ * the documentation and/or other materials provided with the distri-
95
+ * bution.
96
+ * 3. The name of the University, the IML group, or the names of its
97
+ * contributors may not be used to endorse or promote products deri-
98
+ * ved from this software without specific written permission.
99
+ *
100
+ * -- Disclaimer:
101
+ *
102
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
103
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
104
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
105
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY
106
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
107
+ * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
108
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
109
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEO-
110
+ * RY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (IN-
111
+ * CLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
112
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
113
+ *
114
+ */
115
+
116
+#include <stdio.h>
117
+#include <stdlib.h>
118
+#include <time.h>
119
+#include "gmp.h"
120
+#include "iml.h"
121
+
122
+long *randomLongMat (const long n, const long m, const long bd);
123
+mpz_t *randomMPMat (const long n, const long m, const mpz_t mp_bd);
124
+
125
+int
126
+main (void)
127
+{
128
+ long i, j, n, m, bd, s, *A;
129
+ mpz_t *mp_B, *mp_N;
130
+ clock_t c;
131
+
132
+ /* generate a n x m random left hand side matrix A */
133
+ printf("1\n");
134
+ n = 20;
135
+ m = 21;
136
+ bd = 3;
137
+ /* entris of A satisfying -2^bd < e < 2^bd */
138
+ A = randomLongMat (n, m, bd);
139
+ printf("2\n");
140
+ c = clock();
141
+ printf("3\n");
142
+ fprintf (stdout, "Input system:\n");
143
+ for (i = 0; i < n; i++)
144
+ {
145
+ fprintf (stdout, " ");
146
+ for (j = 0; j < m; j++)
147
+ fprintf (stdout, "%ld\t", A[i * m + j]);
148
+ fprintf (stdout, "\n");
149
+ }
150
+ s = nullspaceLong (n, m, A, &mp_N);
151
+ printf("4\n");
152
+ printf("Time to compute nullspace: %f\n", (clock() - c)/((float)CLOCKS_PER_SEC));
153
+ fprintf (stdout, "Dimension of nullspace: ");
154
+ fprintf (stdout, " %ld\n", s);
155
+
156
+
157
+ for (i = 0; i < m; i++)
158
+ {
159
+ for (j = 0; j < s; j++)
160
+ gmp_fprintf (stdout, " %Zd", mp_N[i * s + j]);
161
+ fprintf (stdout, "\n");
162
+ }
163
+ free (A);
164
+ for (i = 0; i < m * s; i++)
165
+ mpz_clear (mp_N[i]);
166
+ free (mp_N);
167
+ return 0;
168
+}
169
+
170
+
171
+/* generate a n x m random dense signed long matrix with entries lying in
172
+ * (-2^bd, 2^bd)
173
+ */
174
+long *
175
+randomLongMat (const long n, const long m, const long bd)
176
+{
177
+ long i, j;
178
+ mpz_t mp_rand, mp_sign;
179
+ gmp_randstate_t state;
180
+ unsigned long seed;
181
+ FILE *devrandom;
182
+ long *M;
183
+ static unsigned long inc = 0;
184
+
185
+ M = (long *) malloc (n * m * sizeof (long));
186
+ mpz_init (mp_sign);
187
+ mpz_init (mp_rand);
188
+ gmp_randinit_default (state);
189
+ seed = 387439;
190
+
191
+
192
+ /* generate random seed using /dev/random */
193
+ if ((devrandom = fopen ("/dev/urandom", "r")) != NULL)
194
+ {
195
+ fread (&seed, sizeof (seed), 1, devrandom);
196
+ fclose (devrandom);
197
+ }
198
+ seed += inc;
199
+ inc += 1;
200
+ gmp_randseed_ui (state, seed);
201
+
202
+
203
+ for (i = 0; i < n * m; i++)
204
+ {
205
+ mpz_urandomb (mp_rand, state, bd);
206
+ mpz_urandomb (mp_sign, state, 1);
207
+ if (mpz_sgn (mp_sign) == 0)
208
+ mpz_neg (mp_rand, mp_rand);
209
+ M[i] = mpz_get_si (mp_rand);
210
+ }
211
+ mpz_clear (mp_rand);
212
+ gmp_randclear (state);
213
+ mpz_clear (mp_sign);
214
+ return M;
215
+}
216
217