1 /* 2 * Copyright (C) FFLAS-FFPACK 3 * Written by Brice Boyer (briceboyer) <boyer.brice@gmail.com> 4 * This file is Free Software and part of FFLAS-FFPACK. 5 * 6 * ========LICENCE======== 7 * This file is part of the library FFLAS-FFPACK. 8 * 9 * FFLAS-FFPACK is free software: you can redistribute it and/or modify 10 * it under the terms of the GNU Lesser General Public 11 * License as published by the Free Software Foundation; either 12 * version 2.1 of the License, or (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 * ========LICENCE======== 23 *. 24 */ 25 26 /*! @file tests/test-utils.h 27 * @ingroup tests 28 * @brief Utilities to create matrices with prescribed shapes, properties,... 29 * To be used in the tests 30 */ 31 32 #ifndef __FFLASFFPACK_tests_test_utils_H 33 #define __FFLASFFPACK_tests_test_utils_H 34 35 #include "fflas-ffpack/fflas-ffpack-config.h" 36 #include "fflas-ffpack/utils/debug.h" 37 #include "fflas-ffpack/ffpack/ffpack.h" 38 #include "fflas-ffpack/utils/fflas_randommatrix.h" 39 #include <givaro/givinteger.h> 40 #include <givaro/givintprime.h> 41 #include <givaro/givranditer.h> 42 43 #include <random> 44 #include <functional> 45 46 namespace FFLAS { getSeed()47 uint64_t getSeed(){ 48 struct timeval tp; 49 gettimeofday(&tp, 0) ; 50 return static_cast<uint64_t> (tp.tv_usec + tp.tv_sec*1000000); 51 } 52 } 53 namespace FFPACK { 54 55 template<typename Field> maxFieldElt()56 Givaro::Integer maxFieldElt() {return (Givaro::Integer)Field::maxCardinality();} 57 template<> 58 Givaro::Integer maxFieldElt<Givaro::ZRing<Givaro::Integer>>() {return (Givaro::Integer)-1;} 59 60 /*** Field chooser for test according to characteristic q and bitsize b ***/ 61 /* if q=-1 -> field is chosen randomly with a charateristic of b bits 62 if b=0 -> bitsize is chosen randomly according to maxFieldElt 63 */ 64 template<typename Field> chooseField(Givaro::Integer q,uint64_t b,uint64_t seed)65 Field* chooseField(Givaro::Integer q, uint64_t b, uint64_t seed){ 66 Givaro::Integer maxV= maxFieldElt<Field>(); 67 //auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); 68 std::mt19937 mt_rand(seed); 69 Givaro::Integer::seeding(mt_rand()); 70 if (maxV>0 && (q> maxV || b> maxV.bitsize())) 71 return nullptr; 72 if (b<=1){ 73 auto bitrand = std::bind(std::uniform_int_distribution<uint64_t>(2,maxV.bitsize()-1), 74 mt_rand); 75 b = bitrand(); 76 } 77 Givaro::IntPrimeDom IPD; 78 Givaro::Integer p; 79 if (q==-1){ 80 // Choose characteristic as a random prime of b bits 81 do{ 82 Givaro::Integer _p; 83 Givaro::Integer::random_exact_2exp(_p,b); 84 IPD.prevprime(p, _p+1 ); 85 }while( (p < 2) ); 86 } 87 else p=q; 88 89 return new Field(p); 90 } 91 92 template<> Givaro::ZRing<int32_t>* chooseField<Givaro::ZRing<int32_t> >(Givaro::Integer q, uint64_t b, uint64_t seed){return new Givaro::ZRing<int32_t>();} 93 template<> Givaro::ZRing<int64_t>* chooseField<Givaro::ZRing<int64_t> >(Givaro::Integer q, uint64_t b, uint64_t seed){return new Givaro::ZRing<int64_t>();} 94 template<> Givaro::ZRing<float>* chooseField<Givaro::ZRing<float> >(Givaro::Integer q, uint64_t b, uint64_t seed){return new Givaro::ZRing<float>();} 95 template<> Givaro::ZRing<double>* chooseField<Givaro::ZRing<double> >(Givaro::Integer q, uint64_t b, uint64_t seed){return new Givaro::ZRing<double>();} 96 97 } // FFPACK 98 #endif 99 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 100 // vim:sts=4:sw=4:ts=4:et:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s 101