| 1 |
/* Copyright (c) FFLAS-FFPACK |
| 2 |
* Written by ZHU Hongguang <[email protected]> |
| 3 |
* ========LICENCE======== |
| 4 |
* This file is part of the library FFLAS-FFPACK. |
| 5 |
* |
| 6 |
* FFLAS-FFPACK is free software: you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* ========LICENCE======== |
| 20 |
*/ |
| 21 |
|
| 22 |
|
| 23 |
#include <fflas-ffpack/fflas/fflas.h> |
| 24 |
#include <givaro/modular.h> |
| 25 |
#include <givaro/modular-balanced.h> |
| 26 |
#include "fflas-ffpack/utils/fflas_io.h" |
| 27 |
#include <fflas-ffpack/ffpack/ffpack.h> |
| 28 |
#include <iostream> |
| 29 |
|
| 30 |
using namespace FFLAS; |
| 31 |
using namespace FFPACK; |
| 32 |
|
| 33 |
int main(int argc, char** argv) { |
| 34 |
|
| 35 |
typedef Givaro::Modular<float> Field; |
| 36 |
Field F(17); |
| 37 |
|
| 38 |
|
| 39 |
// Let A be a M times M square matrix of coefficients |
| 40 |
const size_t M = 4, lda = M; |
| 41 |
|
| 42 |
|
| 43 |
Field::Element_ptr A; |
| 44 |
A = fflas_new(F,M,M); |
| 45 |
|
| 46 |
|
| 47 |
// Fulfill the square matrix A so that A is invertible |
| 48 |
F.assign(A[0], F.one); |
| 49 |
F.assign (A[1],F.zero); |
| 50 |
F.assign(A[2],F.one); |
| 51 |
F.assign (A[3],F.zero); |
| 52 |
F.assign(A[4],F.zero); |
| 53 |
F.assign (A[5],F.one); |
| 54 |
F.assign(A[6],F.zero); |
| 55 |
F.assign (A[7],F.one); |
| 56 |
F.assign(A[8],F.zero); |
| 57 |
F.assign (A[9],F.zero); |
| 58 |
F.assign(A[10],F.one); |
| 59 |
F.assign (A[11],F.zero); |
| 60 |
F.assign(A[12],F.zero); |
| 61 |
F.assign (A[13],F.zero); |
| 62 |
F.assign(A[14],(F.zero)); |
| 63 |
F.assign (A[15],F.one); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
WriteMatrix(std::cout<<"A:="<<std::endl,F,M,M,A,lda)<<std::endl; |
| 68 |
|
| 69 |
// Let X be a M times 2 matrix of variables |
| 70 |
const size_t ldx = 2; |
| 71 |
Field::Element_ptr x; |
| 72 |
x = fflas_new(F,M,2); |
| 73 |
fiszero (F, M, 2, x, ldx); //initialize all elements to zero |
| 74 |
|
| 75 |
WriteMatrix(std::cout<<"x:="<<std::endl,F,M, 2, x, ldx)<<std::endl; |
| 76 |
|
| 77 |
// Let b be a M times 2 matrix of solutions |
| 78 |
const size_t ldb = 2; |
| 79 |
Field::Element_ptr b; |
| 80 |
b = fflas_new(F,M,2); |
| 81 |
|
| 82 |
|
| 83 |
// Fulfill the matrix b with desired values |
| 84 |
F.init(b[0],4); |
| 85 |
F.init(b[1],4); |
| 86 |
F.init(b[2],6); |
| 87 |
F.init(b[3],3); |
| 88 |
F.init(b[4],3); |
| 89 |
F.init(b[5],6); |
| 90 |
F.init(b[6],4); |
| 91 |
F.init(b[7],4); |
| 92 |
|
| 93 |
WriteMatrix(std::cout<<"b:="<<std::endl,F,M, 2, b, ldb)<<std::endl; |
| 94 |
|
| 95 |
// make a copy of b into x |
| 96 |
fassign(F,M,2,b,ldb,x,2); |
| 97 |
WriteMatrix(std::cout<<"copied b:="<<std::endl,F,M, 2, x, ldx)<<std::endl; |
| 98 |
|
| 99 |
//Solve the system |
| 100 |
int state; |
| 101 |
size_t rank = fgesv(F, FflasLeft, M, 2, A, lda, x, ldx, &state); |
| 102 |
if(rank!=M)std::cout<<"Results are incorrect after the fgesv()!"<<std::endl; |
| 103 |
WriteMatrix(std::cout<<"x:="<<std::endl,F,M, 2, x, ldx)<<std::endl; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
// Let res be a M times 2 matrix |
| 108 |
const size_t ldres = 2; |
| 109 |
Field::Element_ptr res; |
| 110 |
res = fflas_new(F,M,2); |
| 111 |
fiszero (F, M, 2, res, ldres); //initialize all elements to zero |
| 112 |
|
| 113 |
// Verify if A*x == b to confirm the found the solution |
| 114 |
std::cout<<"Verification:"<<std::endl; |
| 115 |
fgemm(F, FflasNoTrans, FflasNoTrans, M, 2, M, F.one, A, lda, x, ldx, F.zero, res, ldres); |
| 116 |
WriteMatrix(std::cout<<"A*x:="<<std::endl,F,M,2,res,ldres)<<std::endl; |
| 117 |
|
| 118 |
if( !fequal (F, M, 2, res, ldres, b, ldb) ) { |
| 119 |
std::cout<<"Results are incorrect!"<<std::endl; |
| 120 |
} |
| 121 |
else |
| 122 |
{ |
| 123 |
std::cout<<"Results are correct!"<<std::endl; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
// Clearing up the memory |
| 128 |
fflas_delete(A); |
| 129 |
fflas_delete(x); |
| 130 |
fflas_delete(b); |
| 131 |
fflas_delete(res); |
| 132 |
|
| 133 |
return 0; |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 139 |
// vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s |