Debian Codegraph @

tutorials/fflas_101.C

raw
1 /* Copyright (c) FFLAS-FFPACK
2 * Written by
3
4 * ========LICENCE========
5 * This file is part of the library FFLAS-FFPACK.
6 *
7 * FFLAS-FFPACK is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * ========LICENCE========
21 */
22
23
24 /* ===========================================
25 | Linar Algebra Crash Course Level 0 |
26 ===========================================
27 The puspose of this file is to be able to handle the element level
28 of the linear algebra over finite fields used in fflas.
29 In this example the finite field will be the Z over 101 Z modular ring.
30 This set is created using the givaro library although modular rings are
31 not the only possibilities.
32 There had to be a choice between having a clean file and a clean display.
33 The clean display has been adopted therefore the reader is invited
34 to pay attention to what lines are used for display and what lines
35 actually do the work.
36 */
37
38 // =========Information=======
39 // This first tutorial has a lot of commentaries to enable
40 // the user to understand how operations on basic elements
41 // are performed.
42
43 // Although the elements are usually manipulated in the machine
44 // as floats and doubles,the display is done with integers.
45
46 // The different functions are split along four levels :
47 // - Level 0 : Element operations
48 // - Level 1 : Vector-Vector operations
49 // - Level 2 : Matrix-Vector operations
50 // - Level 3 : Matrix-Matrix operations
51
52 // This first file has many comments. As the levels increase
53 // the amount of comments will deminish as it will be assumed
54 // That the reader knows about how the programs form lower
55 // levels work.
56
57 // ==========Includes=========
58
59 // fflas-ffpack modules
60 // #include <fflas-ffpack/fflas-ffpack-config.h>
61 #include <fflas-ffpack/fflas/fflas.h>
62
63 // Givaro provides some finite fields such as modular rings.
64 // Here we include two types of modular rings.
65 // 1. Normal modular rings with elements numbered from 0 to p
66 // 2. Balanced modular rings with elements numbered form -(p-1)/2 to (p-1)/2
67 // Typically p is a prime number but Givaro works with any interger n as well.
68 // For other ways of defining fields using Givaro please refer to
69 // Givaro's documentation.
70 #include <givaro/modular.h>
71 #include <givaro/modular-balanced.h>
72
73
74
75 #include <iostream>
76
77 // All of the methods used come from the FFLAS namespace.
78 using namespace FFLAS;
79
80 int main(int argc, char** argv) {
81
82 // ========== Defining finite fields =========
83
84 // Defining rings using Givaro.
85 typedef Givaro::Modular<float> Float_Ring;
86 Float_Ring F1(101);
87 // F1 is a ring of floats defined modulo 101.
88
89 typedef Givaro::ModularBalanced<double> Double_Balanced_Ring;
90 Double_Balanced_Ring F2(105);
91 // F2 is a balanced ring of doubles defined modulo 105;
92
93
94 // ===== Elements in finite fields ======
95
96
97 // Let a, b and c be three elements of F1
98 // and d, e and f be three elements of F2.
99
100 Float_Ring::Element a,b,c,h;
101 Double_Balanced_Ring::Element d,e,f,g;
102
103 // The elements are defined but not yet assigned.
104 // There are two ways of assigning values to elements
105 // that depend on wether these values are integers or
106 // already exixting elements.
107 // The first way is using init().
108
109 F1.init(a,2);
110 F1.init(b,205);
111 // a and b are initialised with the elements of F1 that
112 // correspond to 2 and 205.
113 // Since F1 is made of floats modulo 101, these are 2.0 and 3.0.
114
115 // This means the second parameter will be converted into an
116 // actual element of the field.
117
118 // The second way is using assign().
119 // A field has two elements that are neutral with respect to
120 // the addition and multiplication laws, ie : zero and one.
121
122 F2.assign(d, F2.zero);
123 F2.assign(e, F2.one);
124 F2.assign(f, e);
125
126 // Operations on elements
127 // Since the modular reduction is very expensive it's important
128 // to call it as rarely as possible. Therefore the following
129 // operations way yield a result outside of the fields.
130
131 F1.add(c,a,b); // c = a + b
132 F2.mul(g,e,-5); // g = e * (-5)
133 F1.mul(h,c,-3); // h = c * (-3)
134
135 // The opposite operations sub and div are used just the same.
136
137 // ===== Display =====
138 // The fields form Givaro provide a display method called
139 // write().
140 // It's important to use write() because of the fact that the
141 // opertations may get out of the field. Here's why :
142
143 std::cout << "\n===== Linear Algebra level 0 =====" << std::endl;
144 std::cout << "a := " << a << " = 2 % 101" << std::endl;
145 std::cout << "b := " << b << " = 205 % 101" << std::endl;
146 std::cout << "c := " << c << " = a + b % 101" << std::endl;
147 std::cout << "d := " << d << " = 1 % 105" << std::endl;
148 std::cout << "e := " << e << " = 0 % 105" << std::endl;
149 std::cout << "f := " << f << " = e % 105" << std::endl;
150 std::cout << "g := " << g <<" = e*(-5) % 105" << std::endl;
151 std::cout << "h := " << h << " = c*(-3) % 101" << std::endl;
152
153 std::cout << "\nThis last result is strange,"<< std::endl;
154 std::cout << "h is supposed to be defined %101."<< std::endl;
155 std::cout << "With the Field display :"<< std::endl;
156 std::cout << "h := " << std::flush;
157 F1.write(std::cout,h) << std::endl;
158
159 std::cout << "\nThis is strange as well."<< std::endl;
160 std::cout << "That's because -3 is not"<< std::endl;
161 std::cout << "an element of the field."<< std::endl;
162 std::cout << "Thus -3 ought to be converted first."<< std::endl;
163 F1.init(h,-3);
164 std::cout << "F1.init(h,-3);\nF1.mulin(h,c);"<< std::endl;
165 std::cout << "With the Field display :"<< std::endl;
166 std::cout << "h := " << std::flush;
167 F1.write(std::cout,h) << "\n" <<std::endl;
168 } //end main
169 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
170 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s