Macaulay2 Engine
Loading...
Searching...
No Matches
GivaroTest.cpp
Go to the documentation of this file.
1// Copyright (c) 994-2009 by The Givaro group
2// This file is part of Givaro.
3// Givaro is governed by the CeCILL-B license under French law
4// and abiding by the rules of distribution of free software.
5// see the COPYRIGHT file for more details.
6
32
33#include <iostream>
34#include <givaro/modular.h>
35#include <givaro/montgomery.h>
36#include <givaro/gfq.h>
37#include <givaro/gfqext.h>
38using namespace Givaro;
39template <class Field>
40void TestField(const Field& F)
41{
42 std::cerr << "Within ";
43 F.write(std::cerr);
44 std::cerr << " : " << std::flush;
45 typename Field::Element a, b, c, d;
46 F.init(a, 7U);
47 F.init(b, -29.3);
48 F.init(c); // empty constructor
49 F.init(d); // empty constructor
50 F.add(c, a, b); // c = a+b
51 // Separate output writing
52 F.write(std::cout, a) << " + " << std::flush;
53 F.write(std::cout, b) << " = " << std::flush;
54 F.write(std::cerr, c) << std::endl;
55 F.mul(c, a, b); // c = a*b
56 F.axpy(d, a, b, c); // d = a*b + c;
57 // Writing all outputs in a single command line
58 F.write(std::cerr << "Within ") << " : " << std::flush;
59 F.write(F.write(F.write(F.write(std::cout, c) << " + ", a) << " * ", b)
60 << " = ",
61 d)
62 << std::endl;
63 {
64 typename Field::Element e;
65 F.init(e);
66 F.assign(e, d);
67 F.maxpy(e, a, b, d); // e = d-a*b
68 // Writing all outputs in a single command line
69 F.write(std::cerr << "Within ") << " : " << std::flush;
70 F.write(F.write(F.write(F.write(std::cout, d) << " - ", a) << " * ", b)
71 << " = ",
72 e)
73 << std::endl;
74 }
75 {
76 typename Field::Element e;
77 F.init(e);
78 F.assign(e, d);
79 F.maxpyin(e, a, b); // e = d - a*b;
80 // Writing all outputs in a single command line
81 F.write(std::cerr << "Within ") << " : " << std::flush;
82 F.write(F.write(F.write(F.write(std::cout, d) << " - ", a) << " * ", b)
83 << " = ",
84 e)
85 << std::endl;
86 }
87 {
88 typename Field::Element e;
89 F.init(e);
90 F.assign(e, d);
91 F.axmy(e, a, b, d); // e = a*b -d;
92 // Writing all outputs in a single command line
93 F.write(std::cerr << "Within ") << " : " << std::flush;
94 F.write(F.write(F.write(F.write(std::cout, a) << " * ", b) << " - ", d)
95 << " = ",
96 e)
97 << std::endl;
98 }
99 {
100 typename Field::Element e;
101 F.init(e);
102 F.assign(e, d);
103 F.maxpyin(e, a, b); // e = d - a*b;
104 // Writing all outputs in a single command line
105 F.write(std::cerr << "Within ") << " : " << std::flush;
106 F.write(F.write(F.write(F.write(std::cout, d) << " - ", a) << " * ", b)
107 << " = ",
108 e)
109 << std::endl;
110 }
111 // Four operations
112 F.write(F.write(std::cout, a) << " += ", b) << " is ";
113 F.write(std::cout, F.addin(a, b)) << " ; ";
114 F.write(F.write(std::cout, a) << " -= ", b) << " is ";
115 F.write(std::cout, F.subin(a, b)) << " ; ";
116 F.write(F.write(std::cout, a) << " *= ", b) << " is ";
117 F.write(std::cout, F.mulin(a, b)) << " ; ";
118 F.write(F.write(std::cout, a) << " /= ", b) << " is ";
119 F.write(std::cout, F.divin(a, b)) << std::endl;
120 F.init(a, 22996);
121 F.inv(b, a);
122 F.write(F.write(std::cout << "1/", a) << " is ", b) << std::endl;
123 F.mul(c, b, a);
124 F.write(std::cout << "1 is ", c) << std::endl;
125 F.init(a, 22996);
126 F.init(b, 22996);
127 F.write(std::cout << "1/", a) << " is ";
128 F.invin(a);
129 F.write(std::cout, a) << std::endl;
130 F.mulin(a, b);
131 F.write(std::cout << "1 is ", a) << std::endl;
132 F.init(a, 37403);
133 F.inv(b, a);
134 F.write(F.write(std::cout << "1/", a) << " is ", b) << std::endl;
135 F.mul(c, b, a);
136 F.write(std::cout << "1 is ", c) << std::endl;
137 F.init(a, 37403);
138 F.init(b, 37403);
139 F.write(std::cout << "1/", a) << " is ";
140 F.invin(a);
141 F.write(std::cout, a) << std::endl;
142 F.mulin(a, b);
143 F.write(std::cout << "1 is ", a) << std::endl;
144}
145extern "C" {
146#include <sys/time.h>
147#include <sys/resource.h>
148}
149int main(int argc, char** argv)
150{
151 // modulo 13 over 16 bits
152 Modular<int16_t> C13(13);
153 TestField(C13);
154 // modulo 13 over 32 bits
155 Modular<int32_t> Z13(13);
156 TestField(Z13);
157 // modulo 13 over unsigned 32 bits
158 Modular<uint32_t> U13(13);
159 TestField(U13);
160#ifdef __USE_Givaro_SIXTYFOUR__
161 // modulo 13 over 64 bits
162 Modular<int64_t> LL13(13U);
163 TestField(LL13);
164#endif
165 // modulo 13 fully tabulated
166 Modular<Log16> L13(13);
167 TestField(L13);
168 // modulo 13 over 32 bits with Montgomery reduction
169 Montgomery<int32_t> M13(13);
170 TestField(M13);
171 Montgomery<int32_t> M3(39989);
172 TestField(M3);
173 // modulo 13 with primitive root representation
174 GFqDom<int> GF13(13);
175 TestField(GF13);
176 // modulo 13 over arbitrary size
177 Modular<Integer> IntZ13(13);
178 TestField(IntZ13);
179 // Zech log finite field with 5^4 elements
180 GFqDom<int> GF625(5, 4);
181 TestField(GF625);
182#if 0 // Possibly related: https://github.com/cr-marcstevens/m4gb/issues/8
183 // Zech log finite field with 256 elements
184 // and prescribed irreducible polynomial
185 std::vector<GFqDom<unsigned long long>::Residu_t> Irred(9);
186 Irred[0] = 1;
187 Irred[1] = 1;
188 Irred[2] = 0;
189 Irred[3] = 1;
190 Irred[4] = 1;
191 Irred[5] = 0;
192 Irred[6] = 0;
193 Irred[7] = 0;
194 Irred[8] = 1;
195 GFqDom<unsigned long long> F256(2, 8, Irred);
196 TestField(F256);
197#endif
198 // Zech log finite field with 3^4 elements
199 // Using the Q-adic Transform
200 GFqExt<int32_t> GF81(3, 4);
201 TestField(GF81);
202 // Zech log finite field with 2Mb tables
203 struct rusage tmp1;
204 getrusage(RUSAGE_SELF, &tmp1);
205 // user time
206 double tim = (double)tmp1.ru_utime.tv_sec +
207 ((double)tmp1.ru_utime.tv_usec) / (1000000.0);
208 ;
209 getrusage(RUSAGE_SELF, &tmp1);
210 tim = (double)tmp1.ru_utime.tv_sec +
211 ((double)tmp1.ru_utime.tv_usec) / (1000000.0) - tim;
212 std::cerr << "Initialization took " << tim
213 << " cpu seconds and : " << std::endl;
214 std::cerr << tmp1.ru_maxrss << " maximum resident set size" << std::endl
215 << tmp1.ru_ixrss << " integral shared memory size" << std::endl
216 << tmp1.ru_idrss << " integral unshared data size" << std::endl
217 << tmp1.ru_isrss << " integral unshared stack size" << std::endl
218 << tmp1.ru_minflt << " page reclaims" << std::endl
219 << tmp1.ru_majflt << " page faults" << std::endl
220 << tmp1.ru_nswap << " swaps" << std::endl
221 << tmp1.ru_inblock << " block input operations" << std::endl
222 << tmp1.ru_oublock << " block output operations" << std::endl;
223 return 0;
224}
void TestField(const Field &F)
int main()