/usr/include/linbox/blackbox/pascal.h is in liblinbox-dev 1.4.2-5build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | #ifndef __LINBOX_pascal_H
#define __LINBOX_pascal_H
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
#ifdef __LINBOX_USE_OPENMP
#include <omp.h>
#endif
#include "linbox/util/debug.h"
#include "linbox/matrix/sliced3.h"
#define PASCAL_BASECASE_THRESH 81
namespace LinBox {
template <class Field>
class ModularNChooseK {
public:
typedef typename Field::Element Element;
int reduceZeros(int x)
{
while ((x>0)&&(x%q_==0)) {
x/=q_;
}
return x;
}
void initFactorials()
{
factList_.resize(maxN_+1);
Element d;
F_.assign(factList_[0],F_.one);
for (int i=1;i<maxN_+1;++i) {
F_.init(d,reduceZeros(i));
if (d==0) {
F_.assign(factList_[i],factList_[i-1]);
} else {
F_.mul(factList_[i],factList_[i-1],d);
}
}
}
void initPowerList()
{
powerList_.resize(maxN_+1);
for (int i=0;i<maxN_+1;++i) {
powerList_[i]=0;
}
for (int k=q_;k<maxN_;k*=q_) {
for (int i=k-1;i<maxN_;i+=k) {
++powerList_[i+1];
}
}
int sum=0;
for (int i=0;i<maxN_+1;++i) {
sum+=powerList_[i];
powerList_[i]=sum;
}
}
ModularNChooseK(Field& F, int q, int maxN) :
F_(F), q_(q), maxN_(maxN)
{
initPowerList();
initFactorials();
}
Element& compute(Element& d,int n,int k)
{
int denomPower=powerList_[k]+powerList_[n-k];
int numerPower=powerList_[n];
if (numerPower>denomPower) {
F_.assign(d,F_.zero);
} else {
linbox_check(numerPower==denomPower);
F_.mul(d,factList_[n-k],factList_[k]);
F_.div(d,factList_[n],d);
}
return d;
}
protected:
Field F_;
int q_;
std::vector<int> powerList_;
std::vector<Element> factList_;
int maxN_;
};
template <class Field>
class PascalBlackbox {
public:
typedef typename Field::Element Element;
template <class Vector>
PascalBlackbox(int r,
int c,
const Vector& polyCoeffs,
Field& F) :
polyCoeffs_(polyCoeffs),
rowdim_(r),coldim_(c),
F_(F)
{
initPowersOfThree();
initBaseCase();
}
void initBaseCase()
{
ModularNChooseK<Field> Choose(F_,3,std::max(rowdim_+coldim_,2*PASCAL_BASECASE_THRESH)+1);
nonZeroRows_.clear();
nonZeroCols_.clear();
nonZerosUnflipped_.clear();
nonZerosFlipped_.clear();
int r=PASCAL_BASECASE_THRESH;
Element two;
F_.add(two,F_.one,F_.one);
for (int k=0;k<(2*r)-1;++k) {
for (int j=(k<r)?0:(1+k-4);j<std::min(k+1,r);++j) {
Element d;
int i=k-j;
Choose.compute(d,k,j);
if (!F_.isZero(d)) {
nonZeroRows_.push_back(i);
nonZeroCols_.push_back(j);
nonZerosUnflipped_.push_back(d);
F_.mulin(d,two);
nonZerosFlipped_.push_back(d);
}
}
}
}
template <class Mat1, class Mat2>
Mat1& applyLeft(Mat1& lhs,const Mat2& rhs) const
{
int r=std::max(PASCAL_BASECASE_THRESH,
nextPowerOfThree(std::max(rowdim_,coldim_)));
lhs.zero();
applyLeft(0,0,r,lhs,const_cast<Mat2&>(rhs),false,1);
return lhs;
}
template<class Mat1, class Mat2>
void applyLeft(int i0, int j0, int r, Mat1& lhs,Mat2& rhs, bool flip, int numThreads) const
{
if (i0 > rowdim_ || j0 > coldim_) return;
if (r<=PASCAL_BASECASE_THRESH) {
if (flip) {
applyHelper(i0,j0,r,lhs,rhs,nonZerosFlipped_);
} else {
applyHelper(i0,j0,r,lhs,rhs,nonZerosUnflipped_);
}
} else {
int r3=r/3;
//CP: removing all explicit OMP calls
// to be replaced by calls to FFLAS-FFPACK's DSL
// #ifdef __LINBOX_USE_OPENMP
// int threadLimit=omp_get_max_threads();
// //int threadLimit=4;
// #endif
// #pragma omp parallel sections if (numThreads+2<=threadLimit) shared(lhs,rhs)
// {
// #pragma omp section
applyLeft(i0,j0,r3,lhs,rhs,flip,numThreads+2);
// #pragma omp section
applyLeft(i0+r3,j0,r3,lhs,rhs,flip,numThreads+2);
// #pragma omp section
applyLeft(i0+2*r3,j0,r3,lhs,rhs,flip,numThreads+2);
// }
// #pragma omp parallel sections if (numThreads+1<=threadLimit) shared(lhs,rhs)
// {
// #pragma omp section
applyLeft(i0,j0+r3,r3,lhs,rhs,flip,numThreads+1);
// #pragma omp section
applyLeft(i0+r3,j0+r3,r3,lhs,rhs,!flip,numThreads);
// }
applyLeft(i0,j0+2*r3,r3,lhs,rhs,flip,numThreads+1);
}
}
template<class SlicedDom>
void applyHelper(int i0,int j0,int r,Sliced<SlicedDom>& lhs,Sliced<SlicedDom>& rhs,
const std::vector<uint16_t>& nonZeros) const
{
int nnz=nonZeroRows_.size();
for (int l=0;l<nnz;++l) {
int i=nonZeroRows_[l]+i0;
int j=nonZeroCols_[l]+j0;
int k=i+j;
if (i>=rowdim_ || j>=coldim_) {
continue;
}
if (F_.isZero(polyCoeffs_[k])) {
continue;
}
Element d;
d=nonZeros[l];
F_.mul(d,d,polyCoeffs_[k]);
typename Sliced<SlicedDom>::RawIterator Ab(lhs.rowBegin(i)),
Ae(lhs.rowEnd(i)), Bb(rhs.rowBegin(j));
lhs.axpyin(Ab,Ae,d,Bb);
}
}
template<class Mat1, class Mat2>
void applyHelper(int i0,int j0,int r,Mat1& lhs,Mat2& rhs,const std::vector<uint16_t>& nonZeros) const
{
int nnz=nonZeroRows_.size();
int w=rhs.coldim();
for (int l=0;l<nnz;++l) {
int i=nonZeroRows_[l]+i0;
int j=nonZeroCols_[l]+j0;
int k=i+j;
if (i>=rowdim_ || j>=coldim_) {
continue;
}
if (F_.isZero(polyCoeffs_[k])) {
continue;
}
Element d;
d=nonZeros[l];
F_.mul(d,d,polyCoeffs_[k]);
typename Mat2::constSubMatrixType Xr(rhs,j,0,1,w);
typename Mat1::subMatrixType Yr(lhs,i,0,1,w);
lhs._MD.saxpyin(Yr,d,Xr);
}
}
size_t rowdim() const {return rowdim_;}
size_t coldim() const {return coldim_;}
const Field& field() const {
return F_;
}
protected:
std::vector<Element> polyCoeffs_;
int rowdim_,coldim_;
Field F_;
std::vector<int> powersOfThree_;
std::vector<uint16_t> nonZeroRows_,nonZeroCols_,nonZerosFlipped_,nonZerosUnflipped_;
void initPowersOfThree()
{
int x=1;
while (x<std::numeric_limits<int>::max()/3) {
powersOfThree_.push_back(x);
x*=3;
}
powersOfThree_.push_back(x);
}
int nextPowerOfThree(int x) const
{
int i=0;
while (powersOfThree_[i] < x) {++i;}
return powersOfThree_[i];
}
};
}
#endif //__LINBOX_pascal_H
|