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
|
@q $Id: sparse_tensor.cweb 1258 2007-05-11 13:59:10Z kamenik $ @>
@q Copyright 2004, Ondra Kamenik @>
@ Start of {\tt sparse\_tensor.cpp} file.
@c
#include "sparse_tensor.h"
#include "fs_tensor.h"
#include "tl_exception.h"
#include <cmath>
@<|SparseTensor::insert| code@>;
@<|SparseTensor::isFinite| code@>;
@<|SparseTensor::getFoldIndexFillFactor| code@>;
@<|SparseTensor::getUnfoldIndexFillFactor| code@>;
@<|SparseTensor::print| code@>;
@<|FSSparseTensor| constructor code@>;
@<|FSSparseTensor| copy constructor code@>;
@<|FSSparseTensor::insert| code@>;
@<|FSSparseTensor::multColumnAndAdd| code@>;
@<|FSSparseTensor::print| code@>;
@<|GSSparseTensor| slicing constructor@>;
@<|GSSparseTensor::insert| code@>;
@<|GSSparseTensor::print| code@>;
@ This is straightforward. Before we insert anything, we do a few
checks. Then we reset |first_nz_row| and |last_nz_row| if necessary.
@<|SparseTensor::insert| code@>=
void SparseTensor::insert(const IntSequence& key, int r, double c)
{
TL_RAISE_IF(r < 0 || r >= nr,
"Row number out of dimension of tensor in SparseTensor::insert");
TL_RAISE_IF(key.size() != dimen(),
"Wrong length of key in SparseTensor::insert");
TL_RAISE_IF(! std::isfinite(c),
"Insertion of non-finite value in SparseTensor::insert");
iterator first_pos = m.lower_bound(key);
@<check that pair |key| and |r| is unique@>;
m.insert(first_pos, Map::value_type(key, Item(r,c)));
if (first_nz_row > r)
first_nz_row = r;
if (last_nz_row < r)
last_nz_row = r;
}
@
@<check that pair |key| and |r| is unique@>=
iterator last_pos = m.upper_bound(key);
for (iterator it = first_pos; it != last_pos; ++it)
if ((*it).second.first == r) {
TL_RAISE("Duplicate <key, r> insertion in SparseTensor::insert");
return;
}
@ This returns true if all items are finite (not Nan nor Inf).
@<|SparseTensor::isFinite| code@>=
bool SparseTensor::isFinite() const
{
bool res = true;
const_iterator run = m.begin();
while (res && run != m.end()) {
if (! std::isfinite((*run).second.second))
res = false;
++run;
}
return res;
}
@ This returns a ratio of a number of non-zero columns in folded
tensor to the total number of columns.
@<|SparseTensor::getFoldIndexFillFactor| code@>=
double SparseTensor::getFoldIndexFillFactor() const
{
int cnt = 0;
const_iterator start_col = m.begin();
while (start_col != m.end()) {
cnt++;
const IntSequence& key = (*start_col).first;
start_col = m.upper_bound(key);
}
return ((double)cnt)/ncols();
}
@ This returns a ratio of a number of non-zero columns in unfolded
tensor to the total number of columns.
@<|SparseTensor::getUnfoldIndexFillFactor| code@>=
double SparseTensor::getUnfoldIndexFillFactor() const
{
int cnt = 0;
const_iterator start_col = m.begin();
while (start_col != m.end()) {
const IntSequence& key = (*start_col).first;
Symmetry s(key);
cnt += Tensor::noverseq(s);
start_col = m.upper_bound(key);
}
return ((double)cnt)/ncols();
}
@ This prints the fill factor and all items.
@<|SparseTensor::print| code@>=
void SparseTensor::print() const
{
printf("Fill: %3.2f %%\n", 100*getFillFactor());
const_iterator start_col = m.begin();
while (start_col != m.end()) {
const IntSequence& key = (*start_col).first;
printf("Column: ");key.print();
const_iterator end_col = m.upper_bound(key);
int cnt = 1;
for (const_iterator run = start_col; run != end_col; ++run, cnt++) {
if ((cnt/7)*7 == cnt)
printf("\n");
printf("%d(%6.2g) ", (*run).second.first, (*run).second.second);
}
printf("\n");
start_col = end_col;
}
}
@
@<|FSSparseTensor| constructor code@>=
FSSparseTensor::FSSparseTensor(int d, int nvar, int r)
: SparseTensor(d, r, FFSTensor::calcMaxOffset(nvar, d)),
nv(nvar), sym(d)
{}
@
@<|FSSparseTensor| copy constructor code@>=
FSSparseTensor::FSSparseTensor(const FSSparseTensor& t)
: SparseTensor(t),
nv(t.nvar()), sym(t.sym)
{}
@
@<|FSSparseTensor::insert| code@>=
void FSSparseTensor::insert(const IntSequence& key, int r, double c)
{
TL_RAISE_IF(!key.isSorted(),
"Key is not sorted in FSSparseTensor::insert");
TL_RAISE_IF(key[key.size()-1] >= nv || key[0] < 0,
"Wrong value of the key in FSSparseTensor::insert");
SparseTensor::insert(key, r, c);
}
@ We go through the tensor |t| which is supposed to have single
column. If the item of |t| is nonzero, we make a key by sorting the
index, and then we go through all items having the same key (it is its
column), obtain the row number and the element, and do the
multiplication.
The test for non-zero is |a != 0.0|, since there will be items which
are exact zeros.
I have also tried to make the loop through the sparse tensor outer, and
find index of tensor |t| within the loop. Surprisingly, it is little
slower (for monomial tests with probability of zeros equal 0.3). But
everything depends how filled is the sparse tensor.
@<|FSSparseTensor::multColumnAndAdd| code@>=
void FSSparseTensor::multColumnAndAdd(const Tensor& t, Vector& v) const
{
@<check compatibility of input parameters@>;
for (Tensor::index it = t.begin(); it != t.end(); ++it) {
int ind = *it;
double a = t.get(ind, 0);
if (a != 0.0) {
IntSequence key(it.getCoor());
key.sort();
@<check that |key| is within the range@>;
const_iterator first_pos = m.lower_bound(key);
const_iterator last_pos = m.upper_bound(key);
for (const_iterator cit = first_pos; cit != last_pos; ++cit) {
int r = (*cit).second.first;
double c = (*cit).second.second;
v[r] += c * a;
}
}
}
}
@
@<check compatibility of input parameters@>=
TL_RAISE_IF(v.length() != nrows(),
"Wrong size of output vector in FSSparseTensor::multColumnAndAdd");
TL_RAISE_IF(t.dimen() != dimen(),
"Wrong dimension of tensor in FSSparseTensor::multColumnAndAdd");
TL_RAISE_IF(t.ncols() != 1,
"The input tensor is not single-column in FSSparseTensor::multColumnAndAdd");
@
@<check that |key| is within the range@>=
TL_RAISE_IF(key[0] < 0 || key[key.size()-1] >= nv,
"Wrong coordinates of index in FSSparseTensor::multColumnAndAdd");
@
@<|FSSparseTensor::print| code@>=
void FSSparseTensor::print() const
{
printf("FS Sparse tensor: dim=%d, nv=%d, (%dx%d)\n", dim, nv, nr, nc);
SparseTensor::print();
}
@ This is the same as |@<|FGSTensor| slicing from |FSSparseTensor|@>|.
@<|GSSparseTensor| slicing constructor@>=
GSSparseTensor::GSSparseTensor(const FSSparseTensor& t, const IntSequence& ss,
const IntSequence& coor, const TensorDimens& td)
: SparseTensor(td.dimen(), t.nrows(), td.calcFoldMaxOffset()),
tdims(td)
{
@<set |lb| and |ub| to lower and upper bounds of slice indices@>;
FSSparseTensor::const_iterator lbi = t.getMap().lower_bound(lb);
FSSparseTensor::const_iterator ubi = t.getMap().upper_bound(ub);
for (FSSparseTensor::const_iterator run = lbi; run != ubi; ++run) {
if (lb.lessEq((*run).first) && (*run).first.lessEq(ub)) {
IntSequence c((*run).first);
c.add(-1, lb);
insert(c, (*run).second.first, (*run).second.second);
}
}
}
@ This is the same as |@<set |lb| and |ub| to lower and upper bounds
of indices@>| in {\tt gs\_tensor.cpp}, see that file for details.
@<set |lb| and |ub| to lower and upper bounds of slice indices@>=
IntSequence s_offsets(ss.size(), 0);
for (int i = 1; i < ss.size(); i++)
s_offsets[i] = s_offsets[i-1] + ss[i-1];
IntSequence lb(coor.size());
IntSequence ub(coor.size());
for (int i = 0; i < coor.size(); i++) {
lb[i] = s_offsets[coor[i]];
ub[i] = s_offsets[coor[i]] + ss[coor[i]] - 1;
}
@
@<|GSSparseTensor::insert| code@>=
void GSSparseTensor::insert(const IntSequence& s, int r, double c)
{
TL_RAISE_IF(! s.less(tdims.getNVX()),
"Wrong coordinates of index in GSSparseTensor::insert");
SparseTensor::insert(s, r, c);
}
@
@<|GSSparseTensor::print| code@>=
void GSSparseTensor::print() const
{
printf("GS Sparse tensor: (%dx%d)\nSymmetry: ", nr, nc);
tdims.getSym().print();
printf("NVS: ");
tdims.getNVS().print();
SparseTensor::print();
}
@ End of {\tt sparse\_tensor.cpp} file.
|