STK++ 0.9.13
STK_AdditiveBSplineCoefficients.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::Regress
27 * created on: 25 juin 2010
28 * Purpose: Compute the coefficients of an additive B-spline manifold.
29 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
30 **/
31
36#ifndef STK_ADDITIVEBSPLINECOEFFICIENTS_H
37#define STK_ADDITIVEBSPLINECOEFFICIENTS_H
38
40
41namespace STK
42{
43
55template<class Array>
57{
58 public:
105 : IRunnerBase(coefs)
106 , p_data_(coefs.p_data_)
107 , nbKnots_(coefs.nbKnots_)
109 , degree_(coefs.degree_)
110 , position_(coefs.position_)
112 {}
123 void setData( Array const* p_data
124 , int nbControlPoints
125 , int degree = 3
127 );
128
130 bool run();
131
133 inline int degree() const { return degree_;}
135 inline int nbKnots() const { return nbKnots_;}
137 inline int nbControlPoints() const { return nbControlPoints_;}
141 inline ArrayXX const& coefficients() const { return coefficients_;}
145 ArrayXX extrapolate(Array const& x) const;
146
147 protected:
149 Array const* p_data_;
162};
163
164/* run the computations. */
165template<class Array>
167{
168#ifdef STK_REGRESS_VERBOSE
169 stk_cout << _T("In AdditiveBSplineCoefficients::run()\n");
170#endif
171 // check if there exists data
172 if (!p_data_)
173 {
175 return false;
176 }
177 try
178 {
179 // resize the Array with the marginal coefficients
180 coefs_.clear(); // necesssary as coefficient_ is a reference at second call
181 coefs_.resize(p_data_->cols());
182
183 // resize the matrix of all coefficient
184 coefficients_.resize(p_data_->rows(), Range());
185 // get dimensions
186 for (int i=p_data_->beginCols(); i<p_data_->endCols(); i++)
187 {
188 // create a reference on the ith column of the data
189 ColVector colData(p_data_->col(i), true);
190 // set data to the i-th coefficient
191 // WARNING: colData will be invalidate
192 coefs_[i].setData(colData, nbControlPoints_, degree_, position_);
193 // run computation for the i-th coefficient
194 if (!coefs_[i].run())
195 {
196 msg_error_ =coefs_[i].error();
197 return false;
198 }
199 // get coefficients
200 coefficients_.merge(coefs_[i].coefficients());
201 }
202 }
203 catch ( Exception const& e)
204 {
205 msg_error_ = e.error();
206 return false;
207 }
208#ifdef STK_REGRESS_VERBOSE
209 stk_cout << _T("AdditiveBSplineCoefficients::run() done\n");
210#endif
211 return true;
212}
213
214/* run the computations. */
215template<class Array>
217{
218#ifdef STK_REGRESS_VERBOSE
219 stk_cout << _T("in AdditiveBSplineCoefficients::extrapolate()\n");
220#endif
221 if (x.cols() != coefs_.range())
222 { STKRUNTIME_ERROR_NO_ARG(AdditiveBSplineBSplineCoefficients::extrapolate(x),x.cols() != coefs_.range());}
223 // resize the matrix of coefficient
224 ArrayXX coefficients(x.rows(), Range());
225 for (int j= coefs_.begin(); j<coefs_.end(); j++)
226 { coefficients.merge(coefs_[j].extrapolate(x.col(j)));}
227#ifdef STK_REGRESS_VERBOSE
228 stk_cout << _T("AdditiveBSplineCoefficients::run() done\n");
229#endif
230 return coefficients;
231}
232
233
234/* Compute the coefficients of the B-spline curve for the given values.
235 * @param p_data the input data values
236 * @param nbControlPoints number of control points
237 * @param degree degree of the B-spline curves
238 * @param position method to use for positioning the knots
239 **/
240template<class Array>
242 , int nbControlPoints
243 , int degree
245 )
246{
247 p_data_ =p_data;
248 nbKnots_ = nbControlPoints + degree +1;
249 nbControlPoints_ = nbControlPoints;
250 degree_ = degree;
251 position_ = position;
252}
253
254
255} // namespace STK
256
257#endif /* STK_BSPLINECOEFFICIENTS_H */
In this file we define the BSplineCoefficients class.
#define STKERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:49
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
Compute the regression splines coefficients of an additive model.
AdditiveBSplineCoefficients * clone() const
clone pattern implementation
int degree() const
give the degree of the B-spline curves.
ArrayXX const & coefficients() const
give the computed coefficients of the B-spline curves.
ArrayXX coefficients_
Array of the coefficients.
Regress::KnotsPosition position_
Method used in order to position the knots.
AdditiveBSplineCoefficients(Array const *p_data, int nbControlPoints, int degree=3, Regress::KnotsPosition position=Regress::uniformKnotsPositions_)
Constructor : initialize the data members.
void setData(Array const *p_data, int nbControlPoints, int degree=3, Regress::KnotsPosition position=Regress::uniformKnotsPositions_)
Compute the coefficients of the B-spline curve for the given values.
int nbControlPoints_
number of control points of the B-spline curves.
Array1D< BSplineCoefficients< ColVector > > coefs_
Array with the knots and coefficients in each dimension.
int nbKnots() const
give the number of knots of the B-spline curves.
int degree_
degree of the B-splines curves.
AdditiveBSplineCoefficients(Array const &data, int nbControlPoints, int degree=3, Regress::KnotsPosition position=Regress::uniformKnotsPositions_)
Constructor : initialize the data members.
int nbKnots_
number of knots of the B-spline curves.
AdditiveBSplineCoefficients(AdditiveBSplineCoefficients const &coefs)
Copy constructor.
int nbControlPoints() const
give the number of control points of the B-spline curves.
Sdk class for all library Exceptions.
Derived & resize(Range const &I, Range const &J)
resize the array.
void merge(IArray2D< OtherDerived > const &other)
Append the container other to this without copying the data explicitly.
void clear()
clear the object.
virtual bool run()
run the computations.
Abstract base class for all classes having a.
Definition STK_IRunner.h:65
String msg_error_
String with the last error message.
Definition STK_IRunner.h:96
String const & error() const
get the last error message.
Definition STK_IRunner.h:82
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
virtual Array extrapolate(Array const &x) const
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
KnotsPosition
Method to use for positioning the knots for BSpline basis.
@ uniformKnotsPositions_
uniform knots
The namespace STK is the main domain space of the Statistical ToolKit project.