| 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 |
|
| 24 |
|
| 25 |
#include <fflas-ffpack/fflas/fflas.h> |
| 26 |
#include <givaro/modular.h> |
| 27 |
#include <givaro/modular-balanced.h> |
| 28 |
#include "fflas-ffpack/utils/fflas_io.h" |
| 29 |
#include <fflas-ffpack/ffpack/ffpack.h> |
| 30 |
#include <iostream> |
| 31 |
|
| 32 |
using namespace FFLAS; |
| 33 |
using namespace FFPACK; |
| 34 |
|
| 35 |
int main(int argc, char** argv) { |
| 36 |
|
| 37 |
typedef Givaro::Modular<float> Field; |
| 38 |
Field F(17); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
// Let A be a M times M square matrix |
| 43 |
const size_t M = 4, lda = M; |
| 44 |
|
| 45 |
|
| 46 |
// Let A be a M times M random square matrix |
| 47 |
Field::Element_ptr A; |
| 48 |
A = fflas_new(F,M,M); |
| 49 |
Field::Element_ptr A2; |
| 50 |
A2 = fflas_new(F,M,M); |
| 51 |
|
| 52 |
// Fulfill the square matrix A so that A is invertible |
| 53 |
F.assign(A[0], F.one); |
| 54 |
F.assign(A[1],F.zero); |
| 55 |
F.assign(A[2],F.zero); |
| 56 |
F.assign(A[3],F.zero); |
| 57 |
F.init(A[4],15); |
| 58 |
F.assign(A[5],F.one); |
| 59 |
F.assign(A[6],F.zero); |
| 60 |
F.init(A[7],3); |
| 61 |
F.assign(A[8],F.zero); |
| 62 |
F.assign(A[9],F.zero); |
| 63 |
F.assign(A[10],F.one); |
| 64 |
F.init(A[11],2); |
| 65 |
F.init(A[12],10); |
| 66 |
F.assign(A[13],F.zero); |
| 67 |
F.assign(A[14],(F.zero)); |
| 68 |
F.assign(A[15],F.one); |
| 69 |
|
| 70 |
FFLAS::fassign (F, M, M, A, lda, A2, lda); |
| 71 |
|
| 72 |
|
| 73 |
// Print out matrix A to verify |
| 74 |
WriteMatrix(std::cout<<"A:="<<std::endl,F,M,M,A,lda)<<std::endl; |
| 75 |
|
| 76 |
// Let x be a M dimensional vector |
| 77 |
const size_t incx = 1; |
| 78 |
Field::Element_ptr x; |
| 79 |
x = fflas_new(F,M,1); |
| 80 |
fzero(F,M,x,incx);//initialize all elements to zero |
| 81 |
|
| 82 |
// Let b be a M dimensional vector |
| 83 |
const size_t incb = 1; |
| 84 |
Field::Element_ptr b; |
| 85 |
b = fflas_new(F,M,1); |
| 86 |
|
| 87 |
// Fulfill the vector b with desired values |
| 88 |
F.init(b[0],1); |
| 89 |
F.init(b[1],3); |
| 90 |
F.init(b[2],6); |
| 91 |
F.init(b[3],5); |
| 92 |
|
| 93 |
// Print out matrix A to verify |
| 94 |
WriteMatrix(std::cout<<"b:="<<std::endl,F,M, 1, b, incb)<<std::endl; |
| 95 |
|
| 96 |
|
| 97 |
//Solve the linear system Ax=b for x |
| 98 |
/** |
| 99 |
PS: the function Solve will modify the matrix A so here we used a duplicate matrix A2 otherwise A*x will not be equal to b for the later verification stage |
| 100 |
*/ |
| 101 |
Solve( F, M, A2, lda, x, incx, b, incb ); |
| 102 |
|
| 103 |
// Print out x to verify |
| 104 |
WriteMatrix(std::cout<<"x:="<<std::endl,F,M, 1, x, incx)<<std::endl; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
// Let res be a M times 1 vector |
| 110 |
const size_t incres = 1; |
| 111 |
Field::Element_ptr res; |
| 112 |
res = fflas_new(F,M,1); |
| 113 |
fzero(F,M,res,incres);//initialize all elements to zero |
| 114 |
|
| 115 |
// Verify if A*x == b to confirm the found the solution |
| 116 |
std::cout<<"Verification:"<<std::endl; |
| 117 |
fgemv(F, FflasNoTrans, M, M, F.one, A, lda, x, incx, F.zero, res, incres); |
| 118 |
WriteMatrix(std::cout<<"A*x:="<<std::endl,F,M,1,res,incres)<<std::endl; |
| 119 |
if( !fequal (F, M, res, incres, b, incb) ) |
| 120 |
{ |
| 121 |
std::cout<<"Results are incorrect!"<<std::endl; |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
std::cout<<"Results are correct!"<<std::endl; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
// Clearing up the memory |
| 131 |
fflas_delete(A); |
| 132 |
fflas_delete(x); |
| 133 |
fflas_delete(b); |
| 134 |
fflas_delete(res); |
| 135 |
fflas_delete(A2); |
| 136 |
return 0; |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 142 |
// vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s |