47{
48
49
50template<typename> class Array2D;
51template<typename> class Array2DSquare;
52template<typename> class Array2DUpperTriangular;
53template<typename> class Array2DLowerTriangular;
54template<typename> class Array2DDiagonal;
55template<typename> class Array2DPoint;
56template<typename> class Array2DVector;
57template<typename> class Array2DNumber;
58
59namespace hidden
60{
67template<typename Type, int LhsStructure_, int RhsStructure_> struct BinaryReturnArray2DType;
68
69
70template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
array2D_, Arrays::array2D_>
71{ typedef Array2D<Type> result_type;};
72template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
array2D_, Arrays::square_>
73{ typedef Array2D<Type> result_type;};
74template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
array2D_, Arrays::lower_triangular_>
75{ typedef Array2D<Type> result_type;};
76template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
array2D_, Arrays::upper_triangular_>
77{ typedef Array2D<Type> result_type;};
78template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
array2D_, Arrays::diagonal_>
79{ typedef Array2D<Type> result_type;};
80
81template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
square_, Arrays::array2D_>
82{ typedef Array2D<Type> result_type;};
83template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
square_, Arrays::square_>
84{ typedef Array2DSquare<Type> result_type;};
85template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
square_, Arrays::lower_triangular_>
86{ typedef Array2D<Type> result_type;};
87template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
square_, Arrays::upper_triangular_>
88{ typedef Array2D<Type> result_type;};
89template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
square_, Arrays::diagonal_>
90{ typedef Array2DSquare<Type> result_type;};
91
92template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
lower_triangular_, Arrays::array2D_>
93{ typedef Array2D<Type> result_type;};
94template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
lower_triangular_, Arrays::square_>
95{ typedef Array2D<Type> result_type;};
96template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
lower_triangular_, Arrays::lower_triangular_>
97{ typedef Array2DLowerTriangular<Type> result_type;};
98template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
lower_triangular_, Arrays::upper_triangular_>
99{ typedef Array2D<Type> result_type;};
100template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
lower_triangular_, Arrays::diagonal_>
101{ typedef Array2DLowerTriangular<Type> result_type;};
102
103template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
upper_triangular_, Arrays::array2D_>
104{ typedef Array2D<Type> result_type;};
105template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
upper_triangular_, Arrays::square_>
106{ typedef Array2D<Type> result_type;};
107template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
upper_triangular_, Arrays::lower_triangular_>
108{ typedef Array2D<Type> result_type;};
109template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
upper_triangular_, Arrays::upper_triangular_>
110{ typedef Array2DUpperTriangular<Type> result_type;};
111template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
upper_triangular_, Arrays::diagonal_>
112{ typedef Array2DUpperTriangular<Type> result_type;};
113
114template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
diagonal_, Arrays::array2D_>
115{ typedef Array2D<Type> result_type;};
116template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
diagonal_, Arrays::square_>
117{ typedef Array2DSquare<Type> result_type;};
118template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
diagonal_, Arrays::diagonal_>
119{ typedef Array2DDiagonal<Type> result_type;};
120template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
diagonal_, Arrays::lower_triangular_>
121{ typedef Array2DLowerTriangular<Type> result_type;};
122template<
typename Type>
struct BinaryReturnArray2DType<Type, Arrays::
diagonal_, Arrays::upper_triangular_>
123{ typedef Array2DUpperTriangular<Type> result_type;};
124
129template<typename Type, int LStructure_, int RStructure_> struct ProductArray2DReturnType;
130
131template<
typename Type,
int RStructure_>
struct ProductArray2DReturnType<Type, Arrays::
array2D_, RStructure_>
132{ typedef Array2D<Type> result_type;};
133template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
array2D_, Arrays::vector_>
134{ typedef Array2DVector<Type> result_type;};
135template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
array2D_, Arrays::point_>
136{};
137
138template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
square_, Arrays::array2D_>
139{ typedef Array2D<Type> result_type;};
140template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
square_, Arrays::square_>
141{ typedef Array2DSquare<Type> result_type;};
142template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
square_, Arrays::diagonal_>
143{ typedef Array2DSquare<Type> result_type;};
144template<
typename Type>
struct ProductArray2DReturnType<Type,Arrays::
square_, Arrays::lower_triangular_>
145{ typedef Array2D<Type> result_type;};
146template<
typename Type>
struct ProductArray2DReturnType<Type,Arrays::
square_, Arrays::upper_triangular_>
147{ typedef Array2D<Type> result_type;};
148template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
square_, Arrays::vector_>
149{ typedef Array2DVector<Type> result_type;};
150template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
square_, Arrays::point_>
151{};
152
153template<
typename Type,
int RStructure_>
struct ProductArray2DReturnType<Type, Arrays::
lower_triangular_, RStructure_>
154{ typedef Array2D<Type> result_type;};
155template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
lower_triangular_, Arrays::lower_triangular_>
156{ typedef Array2DLowerTriangular<Type> result_type;};
157template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
lower_triangular_, Arrays::diagonal_>
158{ typedef Array2DLowerTriangular<Type> result_type;};
159template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
lower_triangular_, Arrays::vector_>
160{ typedef Array2DVector<Type> result_type;};
161template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
lower_triangular_, Arrays::point_>
162{};
163
164template<
typename Type,
int RStructure_>
struct ProductArray2DReturnType<Type, Arrays::
upper_triangular_, RStructure_>
165{ typedef Array2D<Type> result_type;};
166template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
upper_triangular_, Arrays::upper_triangular_>
167{ typedef Array2DUpperTriangular<Type> result_type;};
168template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
upper_triangular_, Arrays::diagonal_>
169{ typedef Array2DUpperTriangular<Type> result_type;};
170template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
upper_triangular_, Arrays::vector_>
171{ typedef Array2DVector<Type> result_type;};
172template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
upper_triangular_, Arrays::point_>
173{};
174
175template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
diagonal_, Arrays::array2D_>
176{ typedef Array2D<Type> result_type;};
177template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
diagonal_, Arrays::square_>
178{ typedef Array2DSquare<Type> result_type;};
179template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
diagonal_, Arrays::lower_triangular_>
180{ typedef Array2DLowerTriangular<Type> result_type;};
181template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
diagonal_, Arrays::upper_triangular_>
182{ typedef Array2DUpperTriangular<Type> result_type;};
183template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
diagonal_, Arrays::diagonal_>
184{ typedef Array2DDiagonal<Type> result_type;};
185template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
diagonal_, Arrays::vector_>
186{ typedef Array2DVector<Type> result_type;};
187template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
diagonal_, Arrays::point_>
188{};
189
190template<
typename Type,
int RStructure_>
struct ProductArray2DReturnType<Type, Arrays::
vector_, RStructure_>
191{};
192template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
vector_, Arrays::point_>
193{ typedef Array2D<Type> result_type;};
194
195template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
point_, Arrays::array2D_>
196{ typedef Array2DPoint<Type> result_type;};
197template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
point_, Arrays::square_>
198{ typedef Array2DPoint<Type> result_type;};
199template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
point_, Arrays::diagonal_>
200{ typedef Array2DPoint<Type> result_type;};
201template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
point_, Arrays::lower_triangular_>
202{ typedef Array2DPoint<Type> result_type;};
203template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
point_, Arrays::upper_triangular_>
204{ typedef Array2DPoint<Type> result_type;};
205template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
point_, Arrays::vector_>
206{ typedef Array2DNumber<Type> result_type;};
207template<
typename Type>
struct ProductArray2DReturnType<Type, Arrays::
point_, Arrays::point_>
208{};
209
210
211}
212
213namespace Arrays
214{
215
221template<typename Lhs, typename Rhs>
222struct SumOp
223{
224 enum { NbParam_ = 2 };
225 typedef Lhs const& param1_type ;
226 typedef Rhs const& param2_type ;
227 typedef typename hidden::Promote< typename hidden::Traits<Lhs>::Type, typename hidden::Traits<Rhs>::Type >::result_type Type;
229
230 SumOp( ExprBase<Lhs> const& lhs, ExprBase<Rhs> const& rhs )
231 : lhs_(lhs.asDerived()), rhs_(rhs.asDerived())
232 , rows_(
inf(lhs_.rows(), rhs_.rows()))
233 , cols_(
inf(lhs_.cols(), rhs_.cols()))
234 , res_()
235 {}
236 result_type operator()()
237 {
238 res_.resize(rows_, cols_); res_ = Type();
239 Range cols =
inf(cols_, lhs_.cols());
240 for (int j= cols.begin(); j < cols.end(); ++j)
241 {
242 Range rows =
inf(rows_, lhs_.rangeRowsInCol(j));
243 for (int i= rows.begin(); i< rows.end(); ++i)
244 { res_(i,j) += lhs_(i,j);}
245 }
246 cols =
inf(cols_, rhs_.cols());
247 for (int j= cols.begin(); j < cols.end(); ++j)
248 {
249 Range rows =
inf(rows_, rhs_.rangeRowsInCol(j));
250 for (int i= rows.begin(); i< rows.end(); ++i)
251 { res_(i,j) += rhs_(i,j);}
252 }
253 return res_;
254 }
255 protected:
256 param1_type lhs_;
257 param2_type rhs_;
260 result_type res_;
261};
262
268template<typename Lhs, typename Rhs>
269struct DifferenceOp
270{
271 enum { NbParam_ = 2 };
272 typedef Lhs param1_type ;
273 typedef Rhs param2_type ;
274 typedef typename hidden::Promote< typename hidden::Traits<Lhs>::Type, typename hidden::Traits<Rhs>::Type >::result_type Type;
276
277 DifferenceOp( ExprBase<Lhs> const& lhs, ExprBase<Rhs> const& rhs )
278 : lhs_(lhs.asDerived()), rhs_(rhs.asDerived())
279 , rows_(
inf(lhs_.rows(), rhs_.rows()))
280 , cols_(
inf(lhs_.cols(), rhs_.cols()))
281 , res_()
282 {}
283 result_type operator()()
284 {
285 res_.resize(rows_, cols_); res_ = Type();
286 Range cols =
inf(cols_, lhs_.cols());
287 for (int j= cols.begin(); j< cols.end(); ++j)
288 {
289 Range rows =
inf(rows_, lhs_.rangeRowsInCol(j));
290 for (int i= rows.begin(); i< rows.end(); ++i)
291 { res_(i,j) += lhs_(i,j);}
292 }
293 cols =
inf(cols_, rhs_.cols());
294 for (int j= cols.begin(); j< cols.end(); ++j)
295 {
296 Range rows =
inf(rows_, rhs_.rangeRowsInCol(j));
297 for (int i= rows.begin(); i< rows.end(); ++i)
298 { res_(i,j) -= rhs_(i,j);}
299 }
300 return res_;
301 }
302 protected:
303 param1_type const& lhs_;
304 param2_type const& rhs_;
307 result_type res_;
308};
309
315template<typename Lhs, typename Rhs>
316struct Product
317{
318 enum { NbParam_ = 2 };
319 typedef Lhs param1_type ;
320 typedef Rhs param2_type ;
321 typedef typename hidden::Promote< typename hidden::Traits<Lhs>::Type, typename hidden::Traits<Rhs>::Type >::result_type Type;
323
324 Product( ExprBase<Lhs> const& lhs, ExprBase<Rhs> const& rhs )
325 : lhs_(lhs.asDerived()), rhs_(rhs.asDerived())
326 , rows_(
inf(lhs_.rows(), rhs_.rows()))
327 , cols_(
inf(lhs_.cols(), rhs_.cols()))
328 , res_()
329 {}
330 result_type operator()()
331 {
332 res_.resize(rows_, cols_); res_ = Type();
333 for (int j= cols_.begin(); j< cols_.end(); ++j)
334 {
335 Range rows =
inf(lhs_.rangeRowsInCol(j), rhs_.rangeRowsInCol(j));
336 for (int i= rows.begin(); i< rows.end(); ++i)
337 { res_(i,j) = lhs_(i,j) * rhs_(i,j);}
338 }
339 return res_;
340 }
341 protected:
342 param1_type const& lhs_;
343 param2_type const& rhs_;
346 result_type res_;
347};
348
355template<typename Lhs, typename Rhs>
356struct DivOp
357{
358 enum { NbParam_ = 2 };
359 typedef Lhs param1_type ;
360 typedef Rhs param2_type ;
361 typedef typename hidden::Promote< typename hidden::Traits<Lhs>::Type, typename hidden::Traits<Rhs>::Type >::result_type Type;
363
364 DivOp( ExprBase<Lhs> const& lhs, ExprBase<Rhs> const& rhs )
365 : lhs_(lhs.asDerived()), rhs_(rhs.asDerived())
366 , rows_(
inf(lhs_.rows(), rhs_.rows()))
367 , cols_(
inf(lhs_.cols(), rhs_.cols()))
368 , res_()
369 {}
370 result_type operator()()
371 {
372 res_.resize(rows_, cols_); res_ = Type();
373 for (int j= cols_.begin(); j< cols_.end(); ++j)
374 {
375 Range rows =
inf(lhs_.rangeRowsInCol(j), rhs_.rangeRowsInCol(j));
376 for (int i= rows.begin(); i< rows.end(); ++i)
377 { res_(i,j) = lhs_(i,j) / rhs_(i,j);}
378 }
379 return res_;
380 }
381 protected:
382 param1_type const& lhs_;
383 param2_type const& rhs_;
386 result_type res_;
387};
388
393template<typename Lhs, typename Rhs>
394struct MultOp
395{
396 enum { NbParam_ = 2 };
397 typedef Lhs param1_type ;
398 typedef Rhs param2_type ;
399 typedef typename hidden::Promote< typename hidden::Traits<Lhs>::Type, typename hidden::Traits<Rhs>::Type >::result_type Type;
400 typedef typename hidden::ProductArray2DReturnType<Type, Lhs::structure_, Rhs::structure_>::result_type result_type;
401
402 MultOp( ExprBase<Lhs> const& lhs, ExprBase<Rhs> const& rhs )
403 : lhs_(lhs.asDerived()), rhs_(rhs.asDerived())
404 , rows_(lhs_.rows())
405 , cols_(rhs_.cols())
406 , res_()
407 {}
408
409 result_type operator()()
410 {
411 res_.resize(rows_, cols_);
412
413 for (int j=res_.beginCols(); j<res_.endCols(); j++)
414 {
415 Integer const end = res_.rangeRowsInCol(j).end();
416 for (int i=res_.rangeRowsInCol(j).begin(); i<end; i++)
417 { res_(i, j) =
dot(lhs_.row(i), rhs_.col(j));}
418 }
419 return res_;
420 }
421 template<typename Weights>
422 result_type operator()(ExprBase<Weights> const& weights)
423 {
424 res_.resize(rows_, cols_);
425
426 for (int j=res_.beginCols(); j<res_.endCols(); j++)
427 {
428 Integer const end = res_.rangeRowsInCol(j).end();
429 for (int i=res_.rangeRowsInCol(j).begin(); i<end; i++)
430 { res_(i, j) =
weightedDot(lhs_.row(i), rhs_.col(j), weights);}
431 }
432 return res_;
433 }
434
435 protected:
436 param1_type const& lhs_;
437 param2_type const& rhs_;
440 result_type res_;
441};
442
447template<typename Lhs, typename Rhs>
448struct MultLeftTransposeOp
449{
450 enum { NbParam_ = 2 };
451 typedef Lhs param1_type ;
452 typedef Rhs param2_type ;
453 typedef typename hidden::Promote< typename hidden::Traits<Lhs>::Type, typename hidden::Traits<Rhs>::Type >::result_type Type;
454 typedef typename hidden::ProductArray2DReturnType<Type, Lhs::structure_, Rhs::structure_>::result_type result_type;
455
456 MultLeftTransposeOp( ExprBase<Lhs> const& lhs, ExprBase<Rhs> const& rhs )
457 : lhs_(lhs.asDerived()), rhs_(rhs.asDerived())
458 , rows_(lhs_.cols())
459 , cols_(rhs_.cols())
460 , res_()
461 {}
462
463 result_type operator()()
464 {
465 res_.resize(rows_, cols_);
466 for (int j=res_.beginCols(); j<res_.endCols(); j++)
467 {
468 Integer const end = res_.rangeRowsInCol(j).end();
469 for (int i=res_.rangeRowsInCol(j).begin(); i<end; i++)
470 { res_(i, j) =
dot(lhs_.col(i), rhs_.col(j));}
471 }
472 return res_;
473 }
474 template<typename Weights>
475 result_type operator()(ExprBase<Weights> const& weights)
476 {
477 res_.resize(rows_, cols_);
478 for (int j=rhs_.beginCols(); j<rhs_.endCols(); j++)
479 {
480 Integer const end = res_.rangeRowsInCol(j).end();
481 for (int i=res_.rangeRowsInCol(j).begin(); i<end; i++)
482 { res_(i, j) =
weightedDot(lhs_.col(i), rhs_.col(j), weights);}
483 }
484 return res_;
485 }
486
487 protected:
488 param1_type const& lhs_;
489 param2_type const& rhs_;
492 result_type res_;
493};
494
499template<typename Lhs, typename Rhs>
500struct MultRightTransposeOp
501{
502 enum { NbParam_ = 2 };
503 typedef Lhs param1_type ;
504 typedef Rhs param2_type ;
505 typedef typename hidden::Promote< typename hidden::Traits<Lhs>::Type, typename hidden::Traits<Rhs>::Type >::result_type Type;
506 typedef typename hidden::ProductArray2DReturnType<Type, Lhs::structure_, Rhs::structure_>::result_type result_type;
507
508 MultRightTransposeOp( ExprBase<Lhs> const& lhs, ExprBase<Rhs> const& rhs )
509 : lhs_(lhs.asDerived()), rhs_(rhs.asDerived())
510 , rows_(lhs_.rows())
511 , cols_(rhs_.rows())
512 , res_()
513 {}
514
515 result_type operator()()
516 {
517 res_.resize(rows_, cols_);
518 for (int j=res_.beginCols(); j<res_.endCols(); j++)
519 {
520 Integer const end = res_.rangeRowsInCol(j).end();
521 for (int i=res_.rangeRowsInCol(j).begin(); i<end; i++)
522 { res_(i, j) =
dot(lhs_.row(i), rhs_.row(j));}
523 }
524 return res_;
525 }
526 template<typename Weights>
527 result_type operator()(ExprBase<Weights> const& weights)
528 {
529 res_.resize(rows_, cols_);
530 for (int j=res_.beginCols(); j<res_.endCols(); j++)
531 {
532 Integer const end = res_.rangeRowsInCol(j).end();
533 for (int i=res_.rangeRowsInCol(j).begin(); i<end; i++)
534 { res_(i, j) =
weightedDot(lhs_.row(i), rhs_.row(j), weights);}
535 }
536 return res_;
537 }
538
539 protected:
540 param1_type const& lhs_;
541 param2_type const& rhs_;
544 result_type res_;
545};
546
547}
548
551template<typename Lhs, typename Rhs>
552typename Arrays::SumOp<Lhs, Rhs>::result_type
553 sum(Lhs
const& lhs, Rhs
const& rhs)
554{ return Arrays::SumOp<Lhs, Rhs>(lhs, rhs)();}
555
558template<typename Lhs, typename Rhs>
559typename Arrays::DifferenceOp<Lhs, Rhs>::result_type
561{ return Arrays::DifferenceOp<Lhs, Rhs>(lhs, rhs)();}
562
565template<typename Lhs, typename Rhs>
566typename Arrays::Product<Lhs, Rhs>::result_type
567 product(Lhs
const& lhs, Rhs
const& rhs)
568{ return Arrays::Product<Lhs, Rhs>(lhs, rhs)();}
569
572template<typename Lhs, typename Rhs>
573typename Arrays::DivOp<Lhs, Rhs>::result_type
574 div(Lhs
const& lhs, Rhs
const& rhs)
575{ return Arrays::DivOp<Lhs, Rhs>(lhs, rhs)();}
576
579template<typename Lhs, typename Rhs>
580typename Arrays::MultOp<Lhs, Rhs>::result_type
581 mult(Lhs
const& lhs, Rhs
const& rhs)
582{ return Arrays::MultOp<Lhs, Rhs>(lhs, rhs)();}
583
586template<typename Lhs, typename Rhs, typename Weights>
587typename Arrays::MultOp<Lhs, Rhs>::result_type
588 wmult(Lhs
const& lhs, Rhs
const& rhs, Weights
const& weights)
589{ return Arrays::MultOp<Lhs, Rhs>(lhs, rhs)(weights);}
590
593template<typename Lhs, typename Rhs>
594typename Arrays::MultLeftTransposeOp<Lhs, Rhs>::result_type
596{ return Arrays::MultLeftTransposeOp<Lhs, Rhs>(lhs, rhs)();}
597
600template<typename Lhs, typename Rhs, typename Weights>
601typename Arrays::MultLeftTransposeOp<Lhs, Rhs>::result_type
603{ return Arrays::MultLeftTransposeOp<Lhs, Rhs>(lhs, rhs)(weights);}
604
607template<typename Lhs, typename Rhs>
608typename Arrays::MultRightTransposeOp<Lhs, Rhs>::result_type
610{ return Arrays::MultRightTransposeOp<Lhs, Rhs>(lhs, rhs)();}
611
614template<typename Lhs, typename Rhs, typename Weights>
615typename Arrays::MultRightTransposeOp<Lhs, Rhs>::result_type
617{ return Arrays::MultRightTransposeOp<Lhs, Rhs>(lhs, rhs)(weights);}
618
631template<class Container1D1, class Container1D2>
632Real dot( ExprBase< Container1D1>
const& x, ExprBase< Container1D2>
const& y)
633{
634
635 const int first = std::max(x.begin(), y.begin()) , end = std::min(x.end(), y.end());
636
638 for (int i = first; i<end; ++i) sum += x[i] * y[i];
639 return (sum);
640}
641
655template<class Container1D1, class Container1D2, class Container1D3>
657 , ExprBase< Container1D2> const& y
658 , ExprBase< Container1D3> const& w
659 )
660{
661
662 const int first = std::max(x.begin(), y.begin()) , last = std::min(x.lastIdx(), y.lastIdx());
663#ifdef STK_DEBUG
664 if (!
Range(first,last,0).isIn(w.range()))
666#endif
667
669 int i;
670 for (i = first; i<last; i+=2)
671 sum += w[i]*x[i] * y[i] + w[i+1]*x[i+1] * y[i+1];
672
673 if (i == last)
sum += w[last]*x[last]*y[last];
674 return (sum);
675}
676
690template<class Container1D1, class Container1D2>
691Real dist( ExprBase< Container1D1>
const& x, ExprBase< Container1D2>
const& y)
692{
693
694 const int first = std::max(x.begin(), y.begin()) , last = std::min(x.lastIdx(), y.lastIdx());
695
697 for (int i = first; i<=last; i++)
698 scale = std::max(scale, std::abs(x[i] - y[i]));
699
701 if (scale)
702 {
703 for (int i = first; i<=last; i++)
704 {
705 const Real aux = (x[i]-y[i])/scale;
706 norm2 += aux * aux;
707 }
708 }
709
710 return (
Real(sqrt(
double(norm2)))*scale);
711}
712
727template<class Container1D1, class Container1D2, class Container1D3>
729 , ExprBase< Container1D2> const& y
730 , ExprBase< Container1D3> const& w
731 )
732{
733
734 const int first = std::max(x.begin(), y.begin()) , last = std::min(x.lastIdx(), y.lastIdx());
735#ifdef STK_DEBUG
736 if (!
Range(first,last).isIn(w.range()))
737 throw runtime_error("In weightedDist(x, w) "
738 "Range(first,last) not include in w.range()");
739#endif
740
741 Real scale = 0., norm2= 0.;
742 for (int i = first; i<=last; i++)
743 scale = std::max(scale, std::abs(w[i]*(x[i] - y[i])));
744
745 if (scale)
746 {
747 for (int i = first; i<=last; i++)
748 {
749 const Real aux = (x[i]-y[i])/scale;
750 norm2 += w[i]*aux * aux;
751 }
752 }
753
754 return (
Real(sqrt(
double(norm2)))*scale);
755}
756
764template<typename Derived>
765Array2DSquare<typename Derived::Type>
multLeftTranspose( ExprBase<Derived>
const& A)
766{
767 typedef typename Derived::Type Type;
768 Array2DSquare<Type> res(A.cols(), Type(0));
769 for (int i=A.beginCols(); i<=A.lastIdxCols(); i++)
770 {
771
772 for (int k = A.beginRows(); k< A.endRows(); k++) res(i, i) += A(k, i) * A(k, i);
773
774 for (int j=A.beginCols(); j<i; j++)
775 {
776 for (int k = A.beginRows(); k< A.endRows(); k++)
777 res(i, j) += A(k, i) * A(k, j);
778 res(j, i) = res(i, j);
779 }
780 }
781 return res;
782}
783
791template<typename Derived>
793{
794 typedef typename Derived::Type Type;
795 Array2DSquare<Type> res(A.rows(), Type(0));
796 for (int i=A.beginRows(); i<=A.lastIdxRows(); i++)
797 {
798
799 for (int k = A.beginCols(); k< A.endCols(); k++)
800 res(i, i) += A(i, k) * A(i, k);
801
802 for (int j=A.beginRows(); j<i; j++)
803 {
804 for (int k = A.beginCols(); k< A.endCols(); k++)
805 res(i, j) += A(i, k) * A(j, k);
806 res(j, i) = res(i, j);
807 }
808 }
809 return res;
810}
811
820template<typename Derived, typename Weights>
821Array2DSquare<typename Derived::Type>
823{
824 typedef typename Derived::Type Type;
825 Array2DSquare<Type> res(A.cols(), Type(0));
826 for (int i=A.beginCols(); i<=A.lastIdxCols(); i++)
827 {
828
829 for (int k = A.beginRows(); k< A.endRows(); k++)
830 res(i, i) += weights[k] * A(k, i) * A(k, i);
831
832 for (int j=A.beginCols(); j<i; j++)
833 {
834 for (int k = A.beginRows(); k< A.endRows(); k++)
835 res(i, j) += weights[k] * A(k, i) * A(k, j);
836 res(j, i) = res(i, j);
837 }
838 }
839 return res;
840}
841
850template<typename Derived, typename Weights>
851Array2DSquare<typename Derived::Type>
853{
854 typedef typename Derived::Type Type;
855 Array2DSquare<Type> res(A.cols(), Type(0));
856 for (int i=A.beginRows(); i<=A.lastIdxRows(); i++)
857 {
858
859 for (int k = A.beginCols(); k< A.endCols(); k++)
860 res(i, i) += weights[k] *A(i, k) * A(i, k);
861
862 for (int j=A.beginRows(); j<i; j++)
863 {
864 for (int k = A.beginCols(); k< A.endCols(); k++)
865 res(i, j) += weights[k] * A(i, k) * A(j, k);
866 res(j, i) = res(i, j);
867 }
868 }
869 return res;
870}
871
872
873}
874
875#undef BINARY_RETURN_TYPE
876
877#endif
#define BINARY_RETURN_TYPE(Lhs, Rhs)
#define STKRUNTIME_ERROR_2ARG(Where, Arg1, Arg2, Error)
Real weightedDist(ExprBase< Container1D1 > const &x, ExprBase< Container1D2 > const &y, ExprBase< Container1D3 > const &w)
Compute the weighted distance between two vectors.
Arrays::DivOp< Lhs, Rhs >::result_type div(Lhs const &lhs, Rhs const &rhs)
convenience function for the division element by element of two arrays
Real weightedDot(ExprBase< Container1D1 > const &x, ExprBase< Container1D2 > const &y, ExprBase< Container1D3 > const &w)
Compute the dot product.
Arrays::Product< Lhs, Rhs >::result_type product(Lhs const &lhs, Rhs const &rhs)
convenience function for the product element by element of two arrays
Array2DSquare< typename Derived::Type > weightedMultRightTranspose(ExprBase< Derived > const &A, ExprBase< Weights > const &weights)
weighted Array multiplication by its transpose
Array2DSquare< typename Derived::Type > weightedMultLeftTranspose(ExprBase< Derived > const &A, ExprBase< Weights > const &weights)
Weighted matrix multiplication by its transpose.
Arrays::MultOp< Lhs, Rhs >::result_type wmult(Lhs const &lhs, Rhs const &rhs, Weights const &weights)
convenience function for the multiplication of two matrices
Arrays::MultOp< Lhs, Rhs >::result_type mult(Lhs const &lhs, Rhs const &rhs)
convenience function for the multiplication of two matrices
Arrays::MultLeftTransposeOp< Lhs, Rhs >::result_type wmultLeftTranspose(Lhs const &lhs, Rhs const &rhs, Weights const &weights)
convenience function for the multiplication of two matrices
Real dist(ExprBase< Container1D1 > const &x, ExprBase< Container1D2 > const &y)
Compute the distance between two vectors.
Real dot(ExprBase< Container1D1 > const &x, ExprBase< Container1D2 > const &y)
Compute the dot product.
Arrays::MultLeftTransposeOp< Lhs, Rhs >::result_type multLeftTranspose(Lhs const &lhs, Rhs const &rhs)
convenience function for the multiplication of two matrices
Arrays::DifferenceOp< Lhs, Rhs >::result_type difference(Lhs const &lhs, Rhs const &rhs)
convenience function for differencing two arrays
Arrays::MultRightTransposeOp< Lhs, Rhs >::result_type multRightTranspose(Lhs const &lhs, Rhs const &rhs)
convenience function for the multiplication of two matrices
Arrays::MultRightTransposeOp< Lhs, Rhs >::result_type wmultRightTranspose(Lhs const &lhs, Rhs const &rhs, Weights const &weights)
convenience function for the multiplication of two matrices
Arrays::SumOp< Lhs, Rhs >::result_type sum(Lhs const &lhs, Rhs const &rhs)
convenience function for summing two arrays
@ array2D_
general matrix/array/expression
@ point_
row oriented vector/array/expression
@ lower_triangular_
lower triangular matrix/array/expression
@ upper_triangular_
upper triangular matrix/array/expression
@ diagonal_
diagonal matrix/array/expression
@ vector_
column oriented vector/array/expression
@ square_
square matrix/array/expression
Range inf(TRange< SizeI_ > const &I, TRange< SizeJ_ > const &J)
compute inf(I,J).
double Real
STK fundamental type of Real values.
int Integer
STK fundamental type of integer values.
TRange< UnknownSize > Range