26 #ifndef __SPARSE_ALGORITHM_H
27 #define __SPARSE_ALGORITHM_H
39 template<
typename Iterator,
typename Type,
typename BinaryFunction1,
typename BinaryFunction2>
42 BinaryFunction1 f1, BinaryFunction2 f2) {
46 while(rhs != rhs_end) {
47 result = f1(result, f2(lhs[rhs.
key()], rhs.
val()));
53 template<
typename Iterator,
typename Type,
typename BinaryFunction1,
typename BinaryFunction2>
55 Iterator rhs, Type init,
56 BinaryFunction1 f1, BinaryFunction2 f2) {
59 while (lhs != lhs_end) {
60 result = f1(result, f2(lhs.
val(), rhs[lhs.
key()]));
66 template<
typename Type,
typename BinaryFunction1,
typename BinaryFunction2>
69 BinaryFunction1 f1, BinaryFunction2 f2)
74 while (lhs != lhs_end && rhs != rhs_end) {
76 if (lhs.
key() < rhs.
key() && lhs != lhs_end) {
78 }
else if (lhs.
key() == rhs.
key()) {
79 result = f1(result, f2(lhs.
val(), rhs.
val()));
82 }
else if(lhs.
key() > rhs.
key()) {
94 template<
typename Iterator,
typename Type,
95 typename BinaryFunction1,
typename BinaryFunction2>
98 BinaryFunction1 f1, BinaryFunction2 f2) {
103 while(rhs != rhs_end) {
104 int rhs_pos = rhs.
key();
106 for(; pos < rhs_pos; ++pos, ++lhs) {
107 result = f1(result, f2(*lhs, static_cast<Type>(0)));
109 result = f1(result, f2(*lhs, rhs.
val()));
117 while(lhs != lhs_end) {
118 result = f1(result, f2(*lhs, static_cast<Type>(0)));
125 template<
typename Iterator,
typename Type,
126 typename BinaryFunction1,
typename BinaryFunction2>
128 Iterator rhs, Type init,
129 BinaryFunction1 f1, BinaryFunction2 f2)
132 Iterator rhs_end = rhs + lhs.
max();
135 while(lhs != lhs_end) {
136 int lhs_pos = lhs.
key();
138 for(; pos < lhs_pos; ++pos, ++rhs) {
139 result = f1(result, f2(static_cast<Type>(0), *rhs));
141 result = f1(result, f2(lhs.
val(), *rhs));
150 while(rhs != rhs_end) {
151 result = f1(result, f2(static_cast<Type>(0), *rhs));
158 template<
typename Type,
159 typename BinaryFunction1,
typename BinaryFunction2>
162 BinaryFunction1 f1, BinaryFunction2 f2) {
166 while(lhs != lhs_end && rhs != rhs_end) {
167 if(lhs.
key() < rhs.
key()) {
168 result = f1(result, f2(lhs.
val(),
static_cast<Type
>(0)));
170 }
else if(lhs.
key() == rhs.
key()) {
171 result = f1(result, f2(lhs.
val(), rhs.
val()));
174 }
else if(lhs.
key() > rhs.
key()) {
175 result = f1(result, f2(static_cast<Type>(0), rhs.
val()));
180 while(lhs != lhs_end) {
181 result = f1(result, f2(lhs.
val(),
static_cast<Type
>(0)));
184 while(rhs != rhs_end) {
185 result = f1(result, f2(static_cast<Type>(0), rhs.
val()));
194 template<
typename Iterator1,
typename Iterator2,
typename Type,
195 typename BinaryFunction1,
typename BinaryFunction2>
197 Iterator2 rhs, Type init,
198 BinaryFunction1 f1, BinaryFunction2 f2) {
205 template<
typename Iterator,
typename Type,
typename BinaryFunction1,
typename BinaryFunction2>
208 BinaryFunction1 f1, BinaryFunction2 f2) {
216 while (rhs != rhs_end) {
217 result = f1(result, f2(lhs[rhs.
key()], rhs.
val()));
218 lLen = f1(lLen, f2(lhs[rhs.
key()], rhs.
val()));
219 rLen += rhs.
val() * rhs.
val();
223 if (lLen * rLen > 0) result /= sqrt(lLen * rLen);
227 template<
typename Iterator,
typename Type,
typename BinaryFunction1,
typename BinaryFunction2>
229 Iterator rhs, Type init,
230 BinaryFunction1 f1, BinaryFunction2 f2) {
236 while (lhs != lhs_end) {
237 result = f1(result, f2(lhs.
val(), rhs[lhs.
key()]));
238 lLen += lhs.
val() * lhs.
val();
239 rLen += rhs[lhs.
key()] * rhs[lhs.
key()];
243 if (lLen * rLen > 0) result /= sqrt(lLen * rLen);
248 template<
typename Type,
typename BinaryFunction1,
typename BinaryFunction2>
251 BinaryFunction1 f1, BinaryFunction2 f2) {
259 while (lhs != lhs_end && rhs != rhs_end) {
261 if (lhs.
key() < rhs.
key() && lhs != lhs_end) {
263 }
else if (lhs.
key() == rhs.
key()) {
264 result = f1(result, f2(lhs.
val(), rhs.
val()));
265 lLen += lhs.
val() * lhs.
val();
266 rLen += rhs.
val() * rhs.
val();
270 }
else if(lhs.
key() > rhs.
key()) {
275 if (lLen * rLen > 0) result /= sqrt(lLen * rLen);
281 template<
typename Iterator1,
typename Iterator2,
typename Type,
typename BinaryFunction1,
typename BinaryFunction2>
283 Iterator2 rhs, Type init,
284 BinaryFunction1 f1, BinaryFunction2 f2) {
290 while (lhs != lhs_end) {
291 result = f1(result, f2(*lhs, *rhs));
298 if (lLen * rLen > 0) result /= sqrt(lLen * rLen);
304 template<
typename T1,
typename BinaryFunction>
310 for(; l != l_end; ++l, ++out) {
311 while(r != r_end && r.
key() < l.
key()) {
314 if(r != r_end && r.
key() == l.
key()) {
322 template<
typename Type,
typename BinaryFunction,
typename Iterator>
325 Iterator out, BinaryFunction f)
328 memset(out, 0, l.
max() *
sizeof(Type));
330 for(; l != l_end; ++l, ++r) {
331 while(r != r_end && r.
key() < l.
key()) {
334 if(r != r_end && r.
key() == l.
key()) {
341 template<
typename Iterator,
typename Ti,
342 typename To,
typename BinaryFunction>
348 for(
int i = 0; l != l_end; ++i, ++l) {
350 out.
val() = f(*l, r.
val());
357 template<
typename Iterator,
typename Type,
typename BinaryFunction,
358 typename OutputIterator>
361 OutputIterator out, BinaryFunction f)
363 for(
int i = 0; l != l_end; ++i, ++l, ++out) {
365 *out = f(*l, r.
val());
374 template<
typename Type,
typename Iterator,
typename BinaryFunction>
376 Iterator r, Iterator r_end,
380 *out = make_pair(l.
key(), f(l.
val(), r[l.
key()]));
385 template<
typename Type,
typename Iterator,
typename BinaryFunction,
typename OutputIterator>
387 Iterator r, Iterator r_end,
388 OutputIterator out, BinaryFunction f)
392 if(index == l.
key()) {
393 *out = f(l.
val(), *r);
404 template<
typename Iterator1,
typename Iterator2,
405 typename OutputIterator,
typename BinaryFunction>
407 Iterator2 r, Iterator2 r_end,
408 OutputIterator out, BinaryFunction f)
425 template<
typename T1,
typename BinaryFunction>
429 for(; l != l_end; ++l, ++out) {
430 out.
val() = f(l.
val(), val);
434 template<
typename Iterator,
typename Type,
typename BinaryFunction>
446 template<
typename Iterator,
typename OutputIterator,
typename BinaryFunction>
448 Iterator r, Iterator r_end,
449 OutputIterator out, BinaryFunction f)
456 template<
typename Type,
typename OutputIterator,
typename BinaryFunction>
459 OutputIterator out, BinaryFunction f)
461 memset(out, 0, l.
max() *
sizeof(Type));
471 template<
typename Iterator,
typename Type,
472 typename OutputIterator,
typename BinaryFunction>
475 OutputIterator out, BinaryFunction f)
477 OutputIterator start = out;
484 start[r.
key()] = f(start[r.
key()], r.
val());
488 template<
typename Iterator,
typename Type,
489 typename OutputIterator,
typename BinaryFunction>
491 Iterator r, Iterator r_end,
492 OutputIterator out, BinaryFunction f)
498 *out = f(l.
val(), *r);
501 *out = f(static_cast<Type>(0), *r);
508 template<
typename Type,
typename BinaryFunction>
512 BinaryFunction f,
float fEps1,
float fEps2)
514 throw CException(
"Adaptation of sparse vectors is not supported.");
517 template<
typename Type,
typename Iterator,
typename BinaryFunction,
typename OutputIterator>
519 Iterator r, Iterator r_end,
520 OutputIterator out, BinaryFunction f,
float fEps1,
float fEps2)
522 throw CException(
"Adaptation of sparse vectors is not supported.");
525 template<
typename Iterator,
typename Type,
typename BinaryFunction,
typename OutputIterator>
528 OutputIterator out, BinaryFunction f,
float fEps1,
float fEps2)
530 OutputIterator start = out;
540 start[r.
key()] = f(start[r.
key()], fEps1 * r.
val());
545 template<
typename Iterator1,
typename Iterator2,
typename OutputIterator,
typename BinaryFunction>
547 Iterator2 r, Iterator2 r_end,
548 OutputIterator out, BinaryFunction f,
float fEps1,
float fEps2)
552 *out = f(*l, fEps1 * *r);
557 *out = f(fEps2 * *l, fEps1 * *r);
564 template<
typename Type>
568 return (v2 - v1)*(v2 - v1);
578 #endif // #ifndef __SPARSE_ALGORITHM_H
int key() const
Definition: keyed_iterator.hpp:107
difference_type max() const
Definition: keyed_iterator.hpp:111
int size() const
Definition: keyed_iterator.hpp:110
Type coCosine_distance(Iterator lhs, Iterator lhs_end, keyed_iterator< Type > rhs, Type init, BinaryFunction1 f1, BinaryFunction2 f2)
Definition: sparse_algorithm.h:206
Definition: sparse_algorithm.h:565
void element_add(Iterator l, Iterator l_end, Iterator r, Iterator r_end, OutputIterator out, BinaryFunction f)
Definition: sparse_algorithm.h:447
void element_product(keyed_iterator< T1 > l, keyed_iterator< T1 > l_end, keyed_iterator< T1 > r, keyed_iterator< T1 > r_end, keyed_iterator< T1 > out, BinaryFunction f)
Definition: sparse_algorithm.h:305
Definition: CDenseVector.h:35
Type inner_distance(Iterator lhs, Iterator lhs_end, keyed_iterator< Type > rhs, Type init, BinaryFunction1 f1, BinaryFunction2 f2)
Definition: sparse_algorithm.h:96
Type operator()(const Type v1, const Type v2) const
Definition: sparse_algorithm.h:566
Type inner_product(keyed_iterator< Type > lhs, keyed_iterator< Type > lhs_end, keyed_iterator< Type > rhs, Type init, BinaryFunction1 f1, BinaryFunction2 f2)
Definition: sparse_algorithm.h:67
Type val() const
Definition: keyed_iterator.hpp:108
Type inner_product(Iterator lhs, Iterator lhs_end, keyed_iterator< Type > rhs, Type init, BinaryFunction1 f1, BinaryFunction2 f2)
Definition: sparse_algorithm.h:40
Definition: CException.h:40
void element_adapt(keyed_iterator< Type > l, keyed_iterator< Type > l_end, keyed_iterator< Type > r, keyed_iterator< Type > r_end, keyed_iterator< Type > out, BinaryFunction f, float fEps1, float fEps2)
Definition: sparse_algorithm.h:509