Interior-point-optimisation  1.0-1
Interior-pointoptimisationlibrary
LinearCombination.cc
Go to the documentation of this file.
1 /*
2  * $Id: LinearCombination.cc 177 2013-07-02 16:59:05Z jdl3 $
3  * Copyright (C) 2013 John D Lamb
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 # include<config.h>
22 #endif
23 
24 #include"LinearCombination.hpp"
25 
26 using namespace ipo_function::concrete;
27 
29  : FunctionBase { size }, coefficients( size ){ coefficients.set_all( 0.0 ); }
30 
31 void
32 LinearCombination::resize( size_t const size ){
33  if( coefficients.size() != size ){
34  size_t const MIN { std::min( size, coefficients.size() ) };
35  auto oldCoefficients = coefficients;
36  coefficients = gsl::vector( size );
37  // copy old values
38  for( size_t index { 0 }; index < MIN; ++index )
39  coefficients[index] = oldCoefficients[index];
40  // zero new values
41  for( size_t index { MIN }; index < coefficients.size(); ++index )
42  coefficients[index] = 0;
43  }
44 }
45 
46 void
47 LinearCombination::setCoefficient( size_t const index, double const value ){
48  if( index > coefficients.size() ){
49  using namespace ipo;
50  throw IPOE( "ipo_function::concrete::LinearCombination::setCoefficient(): "
51  "index out of range." );
52  }
53  coefficients[index] = value;
54 }
55 
56 double
57 LinearCombination::getCoefficient( size_t const index ) const {
58  if( index > coefficients.size() ){
59  using namespace ipo;
60  throw IPOE( "ipo_function::concrete::LinearCombination::getCoefficient(): "
61  "index out of range." );
62  }
63  return coefficients[index];
64 }
65 
66 gsl::vector const&
68  return coefficients;
69 }
70 
71 void
72 LinearCombination::setCoefficients( gsl::vector const& vector ){
73  if( vector.size() != coefficients.size() ){
74  using namespace ipo;
75  std::ostringstream
76  ost { "ipo_function::concrete::LinearCombination::setCoefficients(): " };
77  ost << "vector size " << vector.size();
78  ost << " and coefficients size " << coefficients.size();
79  ost << " do not match.";
80  throw IPOE( ost.str() );
81  }
82  coefficients.memcpy( vector );
83 }
84 
85 double
86 LinearCombination::operator()( gsl::vector const& vector ){
87  if( vector.size() != coefficients.size() ){
88  using namespace ipo;
89  std::ostringstream ost { "ipo_function::concrete::LinearCombination::operator()(): " };
90  ost << "vector size " << vector.size();
91  ost << " and coefficients size " << coefficients.size();
92  ost << " do not match.";
93  throw IPOE( ost.str() );
94  }
95  double result { 0.0 };
96  gsl::blas::ddot( coefficients, vector, &result );
97  return result;
98 }
99 
100 gsl::vector
101 LinearCombination::gradient( gsl::vector const& vector ){
102  size_t const SIZE { vector.size() };
103  if( SIZE != coefficients.size() ){
104  using namespace ipo;
105  std::ostringstream ost { "ipo_function::concrete::LinearCombination::gradient(): " };
106  ost << "vector size " << vector.size();
107  ost << " and coefficients size " << coefficients.size();
108  ost << " do not match.";
109  throw IPOE( ost.str() );
110  }
111  // return a copy of coefficients
112  if( functionGradient.size() != SIZE )
113  functionGradient = gsl::vector( SIZE );
114  functionGradient.memcpy( coefficients );
115  return functionGradient;
116 }
117 
118 gsl::matrix
119 LinearCombination::hessian( gsl::vector const& vector ){
120  size_t const SIZE { vector.size() };
121  if( SIZE != coefficients.size() ){
122  using namespace ipo;
123  std::ostringstream ost { "ipo_function::concrete::LinearCombination::hessian(): " };
124  ost << "vector size " << vector.size();
125  ost << " and coefficients size " << coefficients.size();
126  ost << " do not match.";
127  throw IPOE( ost.str() );
128  }
129  if( functionHessian.size1() != SIZE or functionHessian.size2() != SIZE )
130  functionHessian = gsl::matrix { SIZE, SIZE };
131  functionHessian.set_all( 0.0 );
132  return functionHessian;
133 }
134 
135 void
136 LinearCombination::setVector( gsl::vector const& vector ){
137  size_t const SIZE { vector.size() };
138  if( SIZE != coefficients.size() ){
139  using namespace ipo;
140  std::ostringstream ost { "ipo_function::concrete::LinearCombination::setVector(): " };
141  ost << "vector size " << vector.size();
142  ost << " and coefficients size " << coefficients.size();
143  ost << " do not match.";
144  throw IPOE( ost.str() );
145  }
146  gsl::blas::ddot( coefficients, vector, &functionValue );
147  if( functionGradient.size() != SIZE )
148  functionGradient = gsl::vector( SIZE );
149  functionGradient.memcpy( coefficients );
150  if( functionHessian.size1() != SIZE or functionHessian.size2() != SIZE )
151  functionHessian = gsl::matrix( SIZE, SIZE );
152  functionHessian.set_all( 0 );
153 }
void setCoefficient(size_t const index, double const value)
Set the value of a coefficient.
double functionValue
The function value.
gsl::vector coefficients
The vector of coefficients;.
gsl::vector functionGradient
The gradient value.
virtual gsl::matrix hessian() const
virtual gsl::vector gradient() const
virtual double value() const
gsl::matrix functionHessian
The Hessian value.
virtual void setVector(gsl::vector const &vector)
Set a vector and compute function value, gradient and Hessian efficiently.
Namespace to hold concrete functions.
#define IPOE(message)
Macro to allow file and line names in exceptions.
virtual double operator()(gsl::vector const &vector)
The function operator: computes the sum of the vector entries.
double getCoefficient(size_t const index) const
Get the value of a coefficient.
void setCoefficients(gsl::vector const &vector)
Set coefficients from a vector.
void resize(size_t const size)
Resize the vector of coefficients.
LinearCombination(size_t const size=0)
The constructor sets up function as a linear combination of size entries.
size_t const size
Size of vector arguments to supply to subclass functions.
gsl::vector const & getCoefficients() const
Get the coefficients as a vector by reference.
This namespace holds all the interior-point optimisation classes.
Definition: Array.hpp:28