STK++ 0.9.13
STK_ArrayByVectorProduct.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
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: 30 déc. 2012
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
36#ifndef STK_ARRAYBYVECTORPRODUCT_H
37#define STK_ARRAYBYVECTORPRODUCT_H
38
39namespace STK
40{
41
42namespace hidden
43{
44
45// point by array product
46const int pointByArrayRowSize_ = 256;
48
51
52
56template<typename Type>
58{
60 static Type vectorByVector(Type const* p_lhs, Type const* p_rhs)
61 {
62 Type sum = Type(0);
63 for (int k=0; k< vectorSize; ++k) sum += p_lhs[k] * p_rhs[k];
64 return(sum);
65 }
67 static Type PanelByVector(Type const* p_lhs, Type const* p_rhs)
68 {
69 Type sum = Type(0);
70 for (int k=0; k< vectorSize; ++k) sum += p_lhs[k] * p_rhs[k];
71 return(sum);
72 }
73};
74
80template<typename Lhs, typename Rhs, typename Result>
81struct bv
82{
83 typedef typename Result::Type Type;
88 static void run(Lhs const& lhs, Rhs const& rhs, Result& res)
89 {
90 for (int j= lhs.beginCols(); j<lhs.endCols(); ++j)
91 {
92 for (int i= lhs.beginRows(); i< lhs.endRows(); ++i)
93 { res.elt(i) += lhs(i,j) * rhs[j];}
94 }
95 return;
96 }
97}; // struct bv
98
104template<typename Lhs, typename Rhs, typename Result>
105struct vb
106{
107 typedef typename Result::Type Type;
112 static void run(Lhs const& lhs, Rhs const& rhs, Result& res)
113 {
114 for (int j= rhs.beginCols(); j<rhs.endCols(); ++j)
115 {
116 for (int i= rhs.beginRows(); i< rhs.endRows(); ++i)
117 { res.elt(j) += lhs[i] * rhs(i,j);}
118 }
119 }
120}; // struct pb
121
125template<typename Lhs, typename Rhs, typename Result>
127{
128 typedef typename Result::Type Type;
132 template<class SubLhs, class SubRhs>
133 static void mult(SubLhs const& l, SubRhs const& r, Result& res)
134 {
135 // loop over the column of the right hand side
136 for (int j=r.beginCols(); j< r.endCols(); ++j)
137 {
138 Type sum(0); // compute dot product
139 for (int i=l.begin(); i<l.end(); ++i) { sum += l.elt(i) * r.elt(i,j); }
140 res.elt(j) += sum;
141 }
142 }
146 static void run( ExprBase<Lhs> const& l, ExprBase<Rhs> const& r, Result& res)
147 {
149 for(;rowRange.end()<r.endRows(); rowRange += pointByArrayRowSize_)
150 {
151 PointByArrayColRange colRange(r.beginCols(), pointByArrayColSize_); // create fixed size col range
152 for(; colRange.end() < r.endCols(); colRange += pointByArrayColSize_)
153 {
154 mult(l.sub(rowRange), r.sub(rowRange, colRange), res);
155 }
156 Range lastColRange(colRange.begin(), r.lastIdxCols(), 0);
157 mult(l.sub(rowRange), r.sub(rowRange, lastColRange ), res);
158 }
159 // end
160 Range lastRowRange(rowRange.begin(), r.lastIdxRows(), 0);
161 PointByArrayColRange colRange(r.beginCols(), pointByArrayColSize_); // create fixed size col range
162 for(; colRange.end() < r.endCols(); colRange += pointByArrayColSize_)
163 { mult(l.sub(lastRowRange), r.sub(lastRowRange, colRange), res);}
164 // last term
165 Range lastColRange(colRange.begin(), r.lastIdxCols(), 0);
167 }
168};
169
170} // namespace hidden
171
172
173
174} // namespace STK
175
176#endif /* STK_ARRAYBYVECTORPRODUCT_H */
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
Arrays::SumOp< Lhs, Rhs >::result_type sum(Lhs const &lhs, Rhs const &rhs)
convenience function for summing two arrays
TRange< pointByArrayRowSize_ > pointByArrayRowRange
TRange< pointByArrayColSize_ > PointByArrayColRange
The namespace STK is the main domain space of the Statistical ToolKit project.
const int vectorSize
this structure regroup all the methods using only pointers on the Type
static Type vectorByVector(Type const *p_lhs, Type const *p_rhs)
multiplication of two vectors
static Type PanelByVector(Type const *p_lhs, Type const *p_rhs)
multiplication of two vectors
This structure regroup the products between a point and different kind of array.
static void run(ExprBase< Lhs > const &l, ExprBase< Rhs > const &r, Result &res)
Compute the product res = l*r with l a point (a row-vector) and r a matrix using column decomposition...
static void mult(SubLhs const &l, SubRhs const &r, Result &res)
Compute the product res = l*r with l a point (a row-vector) and r a matrix using the standard formula...
Methods to use for C=AV with A a general matrix and V a vector.
static void run(Lhs const &lhs, Rhs const &rhs, Result &res)
Main method for Matrices by vector multiplication implementation res have been resized and initialize...
Methods to use for C=PB with P a point and B a matrix.
static void run(Lhs const &lhs, Rhs const &rhs, Result &res)
Main method for point by Matrices multiplication implementation.