STK++ 0.9.13
STK_Array2D_Functors.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Arrays
27 * created on: 26 nov. 2012
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
37#ifndef STK_ARRAY2D_FUNCTORS_H
38#define STK_ARRAY2D_FUNCTORS_H
39
40#define BINARY_RETURN_TYPE(Lhs,Rhs) \
41typename hidden::BinaryReturnArray2DType< typename hidden::Promote< typename hidden::Traits<Lhs>::Type \
42 , typename hidden::Traits<Rhs>::Type >::result_type \
43 , hidden::Traits<Lhs>::structure_ \
44 , hidden::Traits<Rhs>::structure_ \
45 >::result_type
46
47namespace STK
48{
49
50// forward declarations
51template<typename> class Array2D;
52template<typename> class Array2DSquare;
53template<typename> class Array2DUpperTriangular;
54template<typename> class Array2DLowerTriangular;
55template<typename> class Array2DDiagonal;
56template<typename> class Array2DPoint;
57template<typename> class Array2DVector;
58template<typename> class Array2DNumber;
59
60namespace hidden
61{
68template<typename Type, int LhsStructure_, int RhsStructure_> struct BinaryReturnArray2DType;
69
70// Lhs is array2D_
71template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::array2D_, Arrays::array2D_>
73template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::array2D_, Arrays::square_>
75template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::array2D_, Arrays::lower_triangular_>
77template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::array2D_, Arrays::upper_triangular_>
79template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::array2D_, Arrays::diagonal_>
81
82template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::square_, Arrays::array2D_>
84template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::square_, Arrays::square_>
86template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::square_, Arrays::lower_triangular_>
88template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::square_, Arrays::upper_triangular_>
90template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::square_, Arrays::diagonal_>
92
93template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::lower_triangular_, Arrays::array2D_>
95template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::lower_triangular_, Arrays::square_>
97template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::lower_triangular_, Arrays::lower_triangular_>
99template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::lower_triangular_, Arrays::upper_triangular_>
101template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::lower_triangular_, Arrays::diagonal_>
103
104template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::upper_triangular_, Arrays::array2D_>
106template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::upper_triangular_, Arrays::square_>
108template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::upper_triangular_, Arrays::lower_triangular_>
110template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::upper_triangular_, Arrays::upper_triangular_>
112template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::upper_triangular_, Arrays::diagonal_>
114
115template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::diagonal_, Arrays::array2D_>
117template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::diagonal_, Arrays::square_>
119template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::diagonal_, Arrays::diagonal_>
121template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::diagonal_, Arrays::lower_triangular_>
123template<typename Type> struct BinaryReturnArray2DType<Type, Arrays::diagonal_, Arrays::upper_triangular_>
125
130template<typename Type, int LStructure_, int RStructure_> struct ProductArray2DReturnType;
131
132template<typename Type, int RStructure_> struct ProductArray2DReturnType<Type, Arrays::array2D_, RStructure_>
134template<typename Type> struct ProductArray2DReturnType<Type, Arrays::array2D_, Arrays::vector_>
136template<typename Type> struct ProductArray2DReturnType<Type, Arrays::array2D_, Arrays::point_>
137{}; // not possible
138
139template<typename Type> struct ProductArray2DReturnType<Type, Arrays::square_, Arrays::array2D_>
141template<typename Type> struct ProductArray2DReturnType<Type, Arrays::square_, Arrays::square_>
143template<typename Type> struct ProductArray2DReturnType<Type, Arrays::square_, Arrays::diagonal_>
145template<typename Type> struct ProductArray2DReturnType<Type,Arrays::square_, Arrays::lower_triangular_>
147template<typename Type> struct ProductArray2DReturnType<Type,Arrays::square_, Arrays::upper_triangular_>
149template<typename Type> struct ProductArray2DReturnType<Type, Arrays::square_, Arrays::vector_>
151template<typename Type> struct ProductArray2DReturnType<Type, Arrays::square_, Arrays::point_>
152{}; // not possible
153
154template<typename Type, int RStructure_> struct ProductArray2DReturnType<Type, Arrays::lower_triangular_, RStructure_>
156template<typename Type> struct ProductArray2DReturnType<Type, Arrays::lower_triangular_, Arrays::lower_triangular_>
158template<typename Type> struct ProductArray2DReturnType<Type, Arrays::lower_triangular_, Arrays::diagonal_>
160template<typename Type> struct ProductArray2DReturnType<Type, Arrays::lower_triangular_, Arrays::vector_>
162template<typename Type> struct ProductArray2DReturnType<Type, Arrays::lower_triangular_, Arrays::point_>
163{}; // not possible
164
165template<typename Type, int RStructure_> struct ProductArray2DReturnType<Type, Arrays::upper_triangular_, RStructure_>
167template<typename Type> struct ProductArray2DReturnType<Type, Arrays::upper_triangular_, Arrays::upper_triangular_>
169template<typename Type> struct ProductArray2DReturnType<Type, Arrays::upper_triangular_, Arrays::diagonal_>
171template<typename Type> struct ProductArray2DReturnType<Type, Arrays::upper_triangular_, Arrays::vector_>
173template<typename Type> struct ProductArray2DReturnType<Type, Arrays::upper_triangular_, Arrays::point_>
174{}; // not possible
175
176template<typename Type> struct ProductArray2DReturnType<Type, Arrays::diagonal_, Arrays::array2D_>
178template<typename Type> struct ProductArray2DReturnType<Type, Arrays::diagonal_, Arrays::square_>
180template<typename Type> struct ProductArray2DReturnType<Type, Arrays::diagonal_, Arrays::lower_triangular_>
182template<typename Type> struct ProductArray2DReturnType<Type, Arrays::diagonal_, Arrays::upper_triangular_>
184template<typename Type> struct ProductArray2DReturnType<Type, Arrays::diagonal_, Arrays::diagonal_>
186template<typename Type> struct ProductArray2DReturnType<Type, Arrays::diagonal_, Arrays::vector_>
188template<typename Type> struct ProductArray2DReturnType<Type, Arrays::diagonal_, Arrays::point_>
189{}; // not possible
190
191template<typename Type, int RStructure_> struct ProductArray2DReturnType<Type, Arrays::vector_, RStructure_>
192{}; // not possible
193template<typename Type> struct ProductArray2DReturnType<Type, Arrays::vector_, Arrays::point_>
195
196template<typename Type> struct ProductArray2DReturnType<Type, Arrays::point_, Arrays::array2D_>
198template<typename Type> struct ProductArray2DReturnType<Type, Arrays::point_, Arrays::square_>
200template<typename Type> struct ProductArray2DReturnType<Type, Arrays::point_, Arrays::diagonal_>
202template<typename Type> struct ProductArray2DReturnType<Type, Arrays::point_, Arrays::lower_triangular_>
204template<typename Type> struct ProductArray2DReturnType<Type, Arrays::point_, Arrays::upper_triangular_>
206template<typename Type> struct ProductArray2DReturnType<Type, Arrays::point_, Arrays::vector_>
208template<typename Type> struct ProductArray2DReturnType<Type, Arrays::point_, Arrays::point_>
209{}; // not possible
210
211
212} // namespace hidden
213
214namespace Arrays
215{
216
222template<typename Lhs, typename Rhs>
223struct SumOp
224{
225 enum { NbParam_ = 2 };
226 typedef Lhs const& param1_type ;
227 typedef Rhs const& param2_type ;
230
231 SumOp( ExprBase<Lhs> const& lhs, ExprBase<Rhs> const& rhs )
232 : lhs_(lhs.asDerived()), rhs_(rhs.asDerived())
233 , rows_(inf(lhs_.rows(), rhs_.rows()))
234 , cols_(inf(lhs_.cols(), rhs_.cols()))
235 , res_()
236 {}
238 {
239 res_.resize(rows_, cols_); res_ = Type();
240 Range cols = inf(cols_, lhs_.cols());
241 for (int j= cols.begin(); j < cols.end(); ++j)
242 {
243 Range rows = inf(rows_, lhs_.rangeRowsInCol(j));
244 for (int i= rows.begin(); i< rows.end(); ++i)
245 { res_(i,j) += lhs_(i,j);}
246 }
247 cols = inf(cols_, rhs_.cols());
248 for (int j= cols.begin(); j < cols.end(); ++j)
249 {
250 Range rows = inf(rows_, rhs_.rangeRowsInCol(j));
251 for (int i= rows.begin(); i< rows.end(); ++i)
252 { res_(i,j) += rhs_(i,j);}
253 }
254 return res_;
255 }
256 protected:
262};
263
269template<typename Lhs, typename Rhs>
271{
272 enum { NbParam_ = 2 };
273 typedef Lhs param1_type ;
274 typedef Rhs param2_type ;
277
278 DifferenceOp( ExprBase<Lhs> const& lhs, ExprBase<Rhs> const& rhs )
279 : lhs_(lhs.asDerived()), rhs_(rhs.asDerived())
280 , rows_(inf(lhs_.rows(), rhs_.rows()))
281 , cols_(inf(lhs_.cols(), rhs_.cols()))
282 , res_()
283 {}
285 {
286 res_.resize(rows_, cols_); res_ = Type();
287 Range cols = inf(cols_, lhs_.cols());
288 for (int j= cols.begin(); j< cols.end(); ++j)
289 {
290 Range rows = inf(rows_, lhs_.rangeRowsInCol(j));
291 for (int i= rows.begin(); i< rows.end(); ++i)
292 { res_(i,j) += lhs_(i,j);}
293 }
294 cols = inf(cols_, rhs_.cols());
295 for (int j= cols.begin(); j< cols.end(); ++j)
296 {
297 Range rows = inf(rows_, rhs_.rangeRowsInCol(j));
298 for (int i= rows.begin(); i< rows.end(); ++i)
299 { res_(i,j) -= rhs_(i,j);}
300 }
301 return res_;
302 }
303 protected:
309};
310
316template<typename Lhs, typename Rhs>
318{
319 enum { NbParam_ = 2 };
320 typedef Lhs param1_type ;
321 typedef Rhs param2_type ;
324
325 Product( ExprBase<Lhs> const& lhs, ExprBase<Rhs> const& rhs )
326 : lhs_(lhs.asDerived()), rhs_(rhs.asDerived())
327 , rows_(inf(lhs_.rows(), rhs_.rows()))
328 , cols_(inf(lhs_.cols(), rhs_.cols()))
329 , res_()
330 {}
332 {
333 res_.resize(rows_, cols_); res_ = Type();
334 for (int j= cols_.begin(); j< cols_.end(); ++j)
335 {
336 Range rows = inf(lhs_.rangeRowsInCol(j), rhs_.rangeRowsInCol(j));
337 for (int i= rows.begin(); i< rows.end(); ++i)
338 { res_(i,j) = lhs_(i,j) * rhs_(i,j);}
339 }
340 return res_;
341 }
342 protected:
348};
349
356template<typename Lhs, typename Rhs>
357struct DivOp
358{
359 enum { NbParam_ = 2 };
360 typedef Lhs param1_type ;
361 typedef Rhs param2_type ;
364
365 DivOp( ExprBase<Lhs> const& lhs, ExprBase<Rhs> const& rhs )
366 : lhs_(lhs.asDerived()), rhs_(rhs.asDerived())
367 , rows_(inf(lhs_.rows(), rhs_.rows()))
368 , cols_(inf(lhs_.cols(), rhs_.cols()))
369 , res_()
370 {}
372 {
373 res_.resize(rows_, cols_); res_ = Type();
374 for (int j= cols_.begin(); j< cols_.end(); ++j)
375 {
376 Range rows = inf(lhs_.rangeRowsInCol(j), rhs_.rangeRowsInCol(j));
377 for (int i= rows.begin(); i< rows.end(); ++i)
378 { res_(i,j) = lhs_(i,j) / rhs_(i,j);}
379 }
380 return res_;
381 }
382 protected:
388};
389
394template<typename Lhs, typename Rhs>
395struct MultOp
396{
397 enum { NbParam_ = 2 };
398 typedef Lhs param1_type ;
399 typedef Rhs param2_type ;
402
403 MultOp( ExprBase<Lhs> const& lhs, ExprBase<Rhs> const& rhs )
404 : lhs_(lhs.asDerived()), rhs_(rhs.asDerived())
405 , rows_(lhs_.rows())
406 , cols_(rhs_.cols())
407 , res_()
408 {}
409
411 {
412 res_.resize(rows_, cols_);
413 // for all cols and for all rows
414 for (int j=res_.beginCols(); j<res_.endCols(); j++)
415 {
416 Integer const end = res_.rangeRowsInCol(j).end();
417 for (int i=res_.rangeRowsInCol(j).begin(); i<end; i++)
418 { res_(i, j) = dot(lhs_.row(i), rhs_.col(j));}
419 }
420 return res_;
421 }
422 template<typename Weights>
424 {
425 res_.resize(rows_, cols_);
426 // for all cols and for all rows
427 for (int j=res_.beginCols(); j<res_.endCols(); j++)
428 {
429 Integer const end = res_.rangeRowsInCol(j).end();
430 for (int i=res_.rangeRowsInCol(j).begin(); i<end; i++)
431 { res_(i, j) = weightedDot(lhs_.row(i), rhs_.col(j), weights);}
432 }
433 return res_;
434 }
435
436 protected:
442};
443
448template<typename Lhs, typename Rhs>
450{
451 enum { NbParam_ = 2 };
452 typedef Lhs param1_type ;
453 typedef Rhs param2_type ;
456
458 : lhs_(lhs.asDerived()), rhs_(rhs.asDerived())
459 , rows_(lhs_.cols())
460 , cols_(rhs_.cols())
461 , res_()
462 {}
463
465 {
466 res_.resize(rows_, cols_);
467 for (int j=res_.beginCols(); j<res_.endCols(); j++)
468 {
469 Integer const end = res_.rangeRowsInCol(j).end();
470 for (int i=res_.rangeRowsInCol(j).begin(); i<end; i++)
471 { res_(i, j) = dot(lhs_.col(i), rhs_.col(j));}
472 }
473 return res_;
474 }
475 template<typename Weights>
477 {
478 res_.resize(rows_, cols_);
479 for (int j=rhs_.beginCols(); j<rhs_.endCols(); j++)
480 {
481 Integer const end = res_.rangeRowsInCol(j).end();
482 for (int i=res_.rangeRowsInCol(j).begin(); i<end; i++)
483 { res_(i, j) = weightedDot(lhs_.col(i), rhs_.col(j), weights);}
484 }
485 return res_;
486 }
487
488 protected:
494};
495
500template<typename Lhs, typename Rhs>
502{
503 enum { NbParam_ = 2 };
504 typedef Lhs param1_type ;
505 typedef Rhs param2_type ;
508
510 : lhs_(lhs.asDerived()), rhs_(rhs.asDerived())
511 , rows_(lhs_.rows())
512 , cols_(rhs_.rows())
513 , res_()
514 {}
515
517 {
518 res_.resize(rows_, cols_);
519 for (int j=res_.beginCols(); j<res_.endCols(); j++)
520 {
521 Integer const end = res_.rangeRowsInCol(j).end();
522 for (int i=res_.rangeRowsInCol(j).begin(); i<end; i++)
523 { res_(i, j) = dot(lhs_.row(i), rhs_.row(j));}
524 }
525 return res_;
526 }
527 template<typename Weights>
529 {
530 res_.resize(rows_, cols_);
531 for (int j=res_.beginCols(); j<res_.endCols(); j++)
532 {
533 Integer const end = res_.rangeRowsInCol(j).end();
534 for (int i=res_.rangeRowsInCol(j).begin(); i<end; i++)
535 { res_(i, j) = weightedDot(lhs_.row(i), rhs_.row(j), weights);}
536 }
537 return res_;
538 }
539
540 protected:
546};
547
548} // namespace Arrays
549
552template<typename Lhs, typename Rhs>
554 sum(Lhs const& lhs, Rhs const& rhs)
555{ return Arrays::SumOp<Lhs, Rhs>(lhs, rhs)();}
556
559template<typename Lhs, typename Rhs>
561 difference(Lhs const& lhs, Rhs const& rhs)
562{ return Arrays::DifferenceOp<Lhs, Rhs>(lhs, rhs)();}
563
566template<typename Lhs, typename Rhs>
568 product(Lhs const& lhs, Rhs const& rhs)
569{ return Arrays::Product<Lhs, Rhs>(lhs, rhs)();}
570
573template<typename Lhs, typename Rhs>
575 div(Lhs const& lhs, Rhs const& rhs)
576{ return Arrays::DivOp<Lhs, Rhs>(lhs, rhs)();}
577
580template<typename Lhs, typename Rhs>
582 mult(Lhs const& lhs, Rhs const& rhs)
583{ return Arrays::MultOp<Lhs, Rhs>(lhs, rhs)();}
584
587template<typename Lhs, typename Rhs, typename Weights>
589 wmult(Lhs const& lhs, Rhs const& rhs, Weights const& weights)
590{ return Arrays::MultOp<Lhs, Rhs>(lhs, rhs)(weights);}
591
594template<typename Lhs, typename Rhs>
596 multLeftTranspose(Lhs const& lhs, Rhs const& rhs)
597{ return Arrays::MultLeftTransposeOp<Lhs, Rhs>(lhs, rhs)();}
598
601template<typename Lhs, typename Rhs, typename Weights>
603 wmultLeftTranspose(Lhs const& lhs, Rhs const& rhs, Weights const& weights)
605
608template<typename Lhs, typename Rhs>
610 multRightTranspose(Lhs const& lhs, Rhs const& rhs)
611{ return Arrays::MultRightTransposeOp<Lhs, Rhs>(lhs, rhs)();}
612
615template<typename Lhs, typename Rhs, typename Weights>
617 wmultRightTranspose(Lhs const& lhs, Rhs const& rhs, Weights const& weights)
619
632template<class Container1D1, class Container1D2>
634{
635 // compute the valid range
636 const int first = std::max(x.begin(), y.begin()) , end = std::min(x.end(), y.end());
637 // compute the sum product
638 Real sum=0.0;
639 for (int i = first; i<end; ++i) sum += x[i] * y[i];
640 return (sum);
641}
642
656template<class Container1D1, class Container1D2, class Container1D3>
660 )
661{
662 // compute the valid range
663 const int first = std::max(x.begin(), y.begin()) , last = std::min(x.lastIdx(), y.lastIdx());
664#ifdef STK_DEBUG
665 if (!Range(first,last,0).isIn(w.range()))
666 { STKRUNTIME_ERROR_2ARG(In weightedDot(x,y,w),Range(first,last,0),w.range(),first:last not include in w.range());}
667#endif
668 // compute the sum product
669 Real sum=0.0;
670 int i;
671 for (i = first; i<last; i+=2)
672 sum += w[i]*x[i] * y[i] + w[i+1]*x[i+1] * y[i+1];
673 // check if last is odd
674 if (i == last) sum += w[last]*x[last]*y[last];
675 return (sum);
676}
677
691template<class Container1D1, class Container1D2>
693{
694 // compute the valid range
695 const int first = std::max(x.begin(), y.begin()) , last = std::min(x.lastIdx(), y.lastIdx());
696 // compute the std::maximal difference
697 Real scale = 0.;
698 for (int i = first; i<=last; i++)
699 scale = std::max(scale, std::abs(x[i] - y[i]));
700 // Compute the norm
701 Real norm2 = 0.;
702 if (scale)
703 { // comp the norm^2
704 for (int i = first; i<=last; i++)
705 {
706 const Real aux = (x[i]-y[i])/scale;
707 norm2 += aux * aux;
708 }
709 }
710 // rescale sum
711 return (Real(sqrt(double(norm2)))*scale);
712}
713
728template<class Container1D1, class Container1D2, class Container1D3>
732 )
733{
734 // compute the valid range
735 const int first = std::max(x.begin(), y.begin()) , last = std::min(x.lastIdx(), y.lastIdx());
736#ifdef STK_DEBUG
737 if (!Range(first,last).isIn(w.range()))
738 throw runtime_error("In weightedDist(x, w) "
739 "Range(first,last) not include in w.range()");
740#endif
741 // compute the std::maximal difference
742 Real scale = 0., norm2= 0.;
743 for (int i = first; i<=last; i++)
744 scale = std::max(scale, std::abs(w[i]*(x[i] - y[i])));
745 // Compute the norm
746 if (scale)
747 { // comp the norm^2
748 for (int i = first; i<=last; i++)
749 {
750 const Real aux = (x[i]-y[i])/scale;
751 norm2 += w[i]*aux * aux;
752 }
753 }
754 // rescale sum
755 return (Real(sqrt(double(norm2)))*scale);
756}
757
765template<typename Derived>
767{
768 typedef typename Derived::Type Type;
769 Array2DSquare<Type> res(A.cols(), Type(0));
770 for (int i=A.beginCols(); i<=A.lastIdxCols(); i++)
771 {
772 // diagonal
773 for (int k = A.beginRows(); k< A.endRows(); k++) res(i, i) += A(k, i) * A(k, i);
774 // outside diagonal
775 for (int j=A.beginCols(); j<i; j++)
776 {
777 for (int k = A.beginRows(); k< A.endRows(); k++)
778 res(i, j) += A(k, i) * A(k, j);
779 res(j, i) = res(i, j);
780 }
781 }
782 return res;
783}
784
792template<typename Derived>
794{
795 typedef typename Derived::Type Type;
796 Array2DSquare<Type> res(A.rows(), Type(0));
797 for (int i=A.beginRows(); i<=A.lastIdxRows(); i++)
798 {
799 // compute diagonal
800 for (int k = A.beginCols(); k< A.endCols(); k++)
801 res(i, i) += A(i, k) * A(i, k);
802 // compute outside diagonal
803 for (int j=A.beginRows(); j<i; j++)
804 {
805 for (int k = A.beginCols(); k< A.endCols(); k++)
806 res(i, j) += A(i, k) * A(j, k);
807 res(j, i) = res(i, j);
808 }
809 }
810 return res;
811}
812
821template<typename Derived, typename Weights>
822Array2DSquare<typename Derived::Type>
824{
825 typedef typename Derived::Type Type;
826 Array2DSquare<Type> res(A.cols(), Type(0));
827 for (int i=A.beginCols(); i<=A.lastIdxCols(); i++)
828 {
829 // diagonal
830 for (int k = A.beginRows(); k< A.endRows(); k++)
831 res(i, i) += weights[k] * A(k, i) * A(k, i);
832 // outside diagonal
833 for (int j=A.beginCols(); j<i; j++)
834 {
835 for (int k = A.beginRows(); k< A.endRows(); k++)
836 res(i, j) += weights[k] * A(k, i) * A(k, j);
837 res(j, i) = res(i, j);
838 }
839 }
840 return res;
841}
842
851template<typename Derived, typename Weights>
852Array2DSquare<typename Derived::Type>
854{
855 typedef typename Derived::Type Type;
856 Array2DSquare<Type> res(A.cols(), Type(0));
857 for (int i=A.beginRows(); i<=A.lastIdxRows(); i++)
858 {
859 // compute diagonal
860 for (int k = A.beginCols(); k< A.endCols(); k++)
861 res(i, i) += weights[k] *A(i, k) * A(i, k);
862 // compute outside diagonal
863 for (int j=A.beginRows(); j<i; j++)
864 {
865 for (int k = A.beginCols(); k< A.endCols(); k++)
866 res(i, j) += weights[k] * A(i, k) * A(j, k);
867 res(j, i) = res(i, j);
868 }
869 }
870 return res;
871}
872
873
874} // namespace STK
875
876#undef BINARY_RETURN_TYPE
877
878#endif /* STK_ARRAY2D_FUNCTORS_H_ */
#define BINARY_RETURN_TYPE(Lhs, Rhs)
#define STKRUNTIME_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:120
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
int begin() const
get the first index of the TRange.
Definition STK_Range.h:93
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
int end() const
get the ending index of the TRange.
Definition STK_Range.h:299
Runtime errors represent problems outside the scope of a program; they cannot be easily predicted and...
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).
Definition STK_Range.h:477
double Real
STK fundamental type of Real values.
The namespace STK is the main domain space of the Statistical ToolKit project.
TRange< UnknownSize > Range
Definition STK_Range.h:59
Array difference (by coefficient) Perform the matrix difference coefficient by coefficient of the Arr...
hidden::BinaryReturnArray2DType< typenamehidden::Promote< typenamehidden::Traits< Lhs >::Type, typenamehidden::Traits< Rhs >::Type >::result_type, hidden::Traits< Lhs >::structure_, hidden::Traits< Rhs >::structure_ >::result_type result_type
DifferenceOp(ExprBase< Lhs > const &lhs, ExprBase< Rhs > const &rhs)
hidden::Promote< typenamehidden::Traits< Lhs >::Type, typenamehidden::Traits< Rhs >::Type >::result_type Type
Matricial Division.
param1_type const & lhs_
param2_type const & rhs_
hidden::BinaryReturnArray2DType< typenamehidden::Promote< typenamehidden::Traits< Lhs >::Type, typenamehidden::Traits< Rhs >::Type >::result_type, hidden::Traits< Lhs >::structure_, hidden::Traits< Rhs >::structure_ >::result_type result_type
DivOp(ExprBase< Lhs > const &lhs, ExprBase< Rhs > const &rhs)
hidden::Promote< typenamehidden::Traits< Lhs >::Type, typenamehidden::Traits< Rhs >::Type >::result_type Type
Array multiplication Perform the matrix product of the transposed Array lhs by the Array rhs.
result_type operator()(ExprBase< Weights > const &weights)
MultLeftTransposeOp(ExprBase< Lhs > const &lhs, ExprBase< Rhs > const &rhs)
hidden::Promote< typenamehidden::Traits< Lhs >::Type, typenamehidden::Traits< Rhs >::Type >::result_type Type
hidden::ProductArray2DReturnType< Type, Lhs::structure_, Rhs::structure_ >::result_type result_type
Array multiplication Perform the matrix product of the Array lhs by the Array rhs.
result_type operator()(ExprBase< Weights > const &weights)
hidden::Promote< typenamehidden::Traits< Lhs >::Type, typenamehidden::Traits< Rhs >::Type >::result_type Type
hidden::ProductArray2DReturnType< Type, Lhs::structure_, Rhs::structure_ >::result_type result_type
MultOp(ExprBase< Lhs > const &lhs, ExprBase< Rhs > const &rhs)
param2_type const & rhs_
param1_type const & lhs_
Array multiplication Perform the matrix product of the Array lhs by the transposed Array rhs.
hidden::ProductArray2DReturnType< Type, Lhs::structure_, Rhs::structure_ >::result_type result_type
MultRightTransposeOp(ExprBase< Lhs > const &lhs, ExprBase< Rhs > const &rhs)
hidden::Promote< typenamehidden::Traits< Lhs >::Type, typenamehidden::Traits< Rhs >::Type >::result_type Type
result_type operator()(ExprBase< Weights > const &weights)
Array product (by coefficient) Perform the matrix product coefficient by coefficient of the Array lhs...
Product(ExprBase< Lhs > const &lhs, ExprBase< Rhs > const &rhs)
hidden::BinaryReturnArray2DType< typenamehidden::Promote< typenamehidden::Traits< Lhs >::Type, typenamehidden::Traits< Rhs >::Type >::result_type, hidden::Traits< Lhs >::structure_, hidden::Traits< Rhs >::structure_ >::result_type result_type
hidden::Promote< typenamehidden::Traits< Lhs >::Type, typenamehidden::Traits< Rhs >::Type >::result_type Type
param1_type const & lhs_
param2_type const & rhs_
Array sum (by coefficient) Perform the matrix sum coefficient by coefficient of the Array lhs by the ...
hidden::BinaryReturnArray2DType< typenamehidden::Promote< typenamehidden::Traits< Lhs >::Type, typenamehidden::Traits< Rhs >::Type >::result_type, hidden::Traits< Lhs >::structure_, hidden::Traits< Rhs >::structure_ >::result_type result_type
hidden::Promote< typenamehidden::Traits< Lhs >::Type, typenamehidden::Traits< Rhs >::Type >::result_type Type
SumOp(ExprBase< Lhs > const &lhs, ExprBase< Rhs > const &rhs)
Helper class to get the correct returned Structure of Functors.
Helper class to get the correct returned Type for Array2D* classes when a product is performed betwee...
Convenient struct to Promote the result Type of some binary functors.