Debian Codegraph @

tutorials/fflas_101_lvl1.C

raw
1 /* Copyright (c) FFLAS-FFPACK
2 * Written by Philippe LEDENT
3 * [email protected]
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 /* ====================================
26 | FFLAS Crash Course Level 1 |
27 ====================================
28 The puspose of this file is to be able to handle the first level
29 of the linear algebra over finite fields used in fflas.
30 In this example the finite field will be the Z over 101 Z set.
31 This set is created using the givaro library.
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 // For information on the inclusions check the file 101-fflas.C
39 #include <fflas-ffpack/fflas/fflas.h>
40 #include <givaro/modular.h>
41 #include <givaro/modular-balanced.h>
42 #include <iostream>
43 #include "fflas-ffpack/utils/fflas_io.h"
44
45 using namespace FFLAS;
46
47 int main(int argc, char** argv) {
48 std::cout << "" << std::endl;
49
50 typedef Givaro::Modular<float> Float_Ring;
51 Float_Ring F(101);
52 Float_Ring::Element alpha,beta; // scalars
53 Float_Ring::Element * X, * Y, * Z; // vectors
54 const size_t xsize = 2, ysize = 2 , zsize = 2, one = 1, ld = 1;
55
56 X = fflas_new(F,xsize,one);
57 Y = fflas_new(F,ysize,one);
58 Z = fflas_new(F,zsize,one);
59
60 // ===== manual initialisation =====
61
62 F.init(*(X+0), 52); // X[0] = 52
63 F.init( X[1], 183); // X[1] = 82
64
65 std::cout << "\nInitialisation" << std::endl;
66 WriteMatrix(std::cout << "X:=", F, xsize, one, X, one) << std::endl;
67 WriteMatrix(std::cout << "Y:=", F, ysize, one, Y, one) << std::endl;
68
69
70 // ===== initialisation from a vector ===== ???
71 /* finit(F,2,Y,1,X,1);
72 WriteMatrix(F, std::cout << "X:=", X, 2, 1, 1) << std::endl;
73 WriteMatrix(F, std::cout << "Y:=", Y, ysize, one, one) << std::endl;
74 */
75
76
77
78
79 // ===== modular reduction =====
80 std::cout << "\n=== Modular reductions ===" << std::endl;
81
82 typedef Givaro::Modular<float> Ring;
83 Ring F2(2);
84 freduce(F2,2,Y,1); // reduce Y modulo F2 inplace
85 freduce(F2,2,X,1,Y,1); // reduce X modulo F2 and store in Y
86
87
88 std::cout << "\nY mod 2 inplace" << std::endl;
89 WriteMatrix(std::cout << "Y:=", F, ysize, one, Y, ld) << std::endl;
90 std::cout << "\nY := X mod 2" << std::endl;
91 WriteMatrix(std::cout << "Y:=", F, ysize, one, Y, ld) << std::endl;
92
93
94 // ===== negation =====
95 fnegin(F,2,X,1); // X := -X
96 fneg(F,2,X,1,Y,1); // Y := -X
97
98 std::cout << "\n=== Negations ===" << std::endl;
99 std::cout << "\nX := -X" << std::endl;
100 WriteMatrix(std::cout << "X:=", F, xsize, one, X, ld) << std::endl;
101 std::cout << "\nY := -X" << std::endl;
102 WriteMatrix(std::cout << "Y:=", F, ysize, one, Y, ld) << std::endl;
103
104
105 // ===== addition / substraction =====
106 // both of these exist inplace with the suffix "in"
107 F.init(X[0],3); F.init(X[1],4); // X := (3;4)
108 F.init(Y[0],2); F.assign(Y[1],F.one); // Y := (2;1)
109
110 std::cout << "\n=== Additions / Substractions ===" << std::endl;
111 WriteMatrix(std::cout << "X:=", F, xsize, one, X, ld) << std::endl;
112 WriteMatrix(std::cout << "Y:=", F, ysize, one, Y, ld) << std::endl;
113 WriteMatrix(std::cout << "Z:=", F, zsize, one, Z, ld) << std::endl;
114
115 fadd(F,2,X,1,Y,1,Z,1); // Z := X + Y
116 fsub(F,2,Z,1,Y,1,X,1); // X := Z - Y
117
118 std::cout << "\nZ := X + Y" << std::endl;
119 WriteMatrix(std::cout << "Z:=", F, zsize, one, Z,ld) << std::endl;
120 std::cout << "\nX := Z - Y" << std::endl;
121 WriteMatrix(std::cout << "X:=", F, xsize, one, X, ld) << std::endl;
122
123 // ===== scalar times vector =====
124 F.init(alpha,42); // the scalar is scalar with respect to F
125 fscal(F,2,alpha,X,1,Y,1); // Y = alpha * X
126
127 std::cout << "\n=== Scalar times vector ===" << std::endl;
128 WriteMatrix(std::cout << "X:=", F, 2, 1, X, 1) << std::endl;
129 WriteMatrix(std::cout << "Y:=", F, ysize, one, Y, one) << std::endl;
130
131
132
133 // ===== linar combinations =====
134 // Although fadd, fsub, fdot and fscal exist, it's often better
135 // to use faxpy due to processor optimisation
136
137 std::cout << "\n=== Linear Combinations ===" << std::endl;
138 WriteMatrix(std::cout << "X:=", F, 2, 1, X, 1) << std::endl;
139 WriteMatrix(std::cout << "Y:=", F, ysize, one, Y, one) << std::endl;
140
141 faxpy(F,2,alpha,X,1,Y,1); // Y := alpha * X + Y
142
143 std::cout << "\nY := alpha * X + Y" << std::endl;
144 WriteMatrix(std::cout << "Y:=", F, ysize, one, Y, one) << std::endl;
145
146 // faxpby is a more general form of faxpy
147
148 //F.init(beta,14);
149 //faxpby(F,2,alpha,X,1,beta,Y,1); // Y = alpha * X + beta * Y
150
151 // ===== dot product (scalar product) =====
152
153 std::cout << "\n=== Dot Product ===" << std::endl;
154 WriteMatrix(std::cout << "Z:=", F, zsize, one, Z,ld) << std::endl;
155 WriteMatrix(std::cout << "X:=", F, xsize, one, X, ld) << std::endl;
156
157 fdot(F,2,Z,1,X,1); // X := Z^T dot X
158
159 std::cout << "\nX := Z^T dot X" << std::endl;
160 WriteMatrix(std::cout << "X:=", F, xsize, one, X, ld) << std::endl;
161
162 // ===== tests =====
163 bool b;
164 F.init(Z[0], 0); F.init(Z[1], 120);
165
166 std::cout << "\n=== Tests ===" << std::endl;
167 WriteMatrix(std::cout << "Z:=", F, zsize, one, Z,ld) << std::endl;
168 WriteMatrix(std::cout << "X:=", F, xsize, one, X, ld) << std::endl;
169
170
171 b = fiszero(F,2,Z,1); // TRUE if X is zero vector in F
172
173 std::cout << "Z == zero_vector : " << b << std::endl;
174
175 b = fequal(F,2,Z,1,X,1); // TRUE if X and Y are equivalent in F
176
177 std::cout << "Z == X : " << b << std::endl;
178
179 // ===== swap =====
180
181 std::cout << "\n=== Swap ===" << std::endl;
182 WriteMatrix(std::cout << "X:=", F, xsize, one, X, ld) << std::endl;
183 WriteMatrix(std::cout << "Y:=", F, ysize, one, Y, ld) << std::endl;
184
185 fswap(F,2,X,1,Y,1); // exchanges X and Y;
186
187 std::cout << "\nX <-> Y" << std::endl;
188 WriteMatrix(std::cout << "X:=", F, xsize, one, X, ld) << std::endl;
189 WriteMatrix(std::cout << "Y:=", F, ysize, one, Y, ld) << std::endl;
190
191
192 } // End main
193 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
194 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s