Interior-point-optimisation  1.0-1
Interior-pointoptimisationlibrary
Sum.cc
Go to the documentation of this file.
1 /*
2  * $Id: Sum.cc 138 2013-06-29 15:10:53Z 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"Sum.hpp"
25 
26 using namespace ipo_function::concrete;
27 
28 Sum::Sum( size_t const size )
29  : FunctionBase { size }
30 {
31  if( size != 0 ){
32  functionGradient = gsl::vector( size );
33  functionGradient.set_all( 1.0 );
34  functionHessian = gsl::matrix { size, size };
35  functionHessian.set_all( 0.0 );
36  }
37 }
38 
39 double
40 Sum::operator()( gsl::vector const& vector ){
41  // Attempt sane behaviour even if vector has wrong size
42  return std::accumulate( vector.begin(), vector.end(), 0.0 );
43 }
44 
45 gsl::vector
46 Sum::gradient( gsl::vector const& vector ){
47  size_t const SIZE { 0 == getSize() ? vector.size() : getSize() };
48  if( 0 == getSize() ){
49  if( functionGradient.size() != SIZE ){
50  functionGradient = gsl::vector( SIZE );
51  functionGradient.set_all( 1.0 );
52  }
53  }
54  return functionGradient;
55 }
56 
57 gsl::matrix
58 Sum::hessian( gsl::vector const& vector ){
59  size_t const SIZE { 0 == getSize() ? vector.size() : getSize() };
60  if( 0 == getSize() ){
61  if( functionHessian.size1() != SIZE or functionHessian.size2() != SIZE ){
62  functionHessian = gsl::matrix { SIZE, SIZE };
63  functionHessian.set_all( 0.0 );
64  }
65  }
66  return functionHessian;
67 }
68 
69 void
70 Sum::setVector( gsl::vector const& vector ){
71  if( 0 == getSize() ){
72  size_t const SIZE { vector.size() };
73  if( functionGradient.size() != SIZE ) functionGradient = gsl::vector( SIZE );
74  if( functionHessian.size1() != SIZE or functionHessian.size2() != SIZE )
75  functionHessian = gsl::matrix { SIZE, SIZE };
76  functionGradient.set_all( 1.0 );
77  functionHessian.set_all( 0.0 );
78  }
79  functionValue = std::accumulate( vector.begin(), vector.end(), 0.0 );
80 }
Sum(size_t const size=0)
Specify a constructor argument if you want to be able to check the size of the argument in a Model Ob...
Definition: Sum.cc:28
double operator()(gsl::vector const &vector)
The function operator: computes the sum of the vector entries.
Definition: Sum.cc:40
double functionValue
The function value.
gsl::vector functionGradient
The gradient value.
virtual gsl::matrix hessian() const
virtual gsl::vector gradient() const
gsl::matrix functionHessian
The Hessian value.
Namespace to hold concrete functions.
size_t getSize() const
Get size of vector for function arguments or zero for arbitrary size.
void setVector(gsl::vector const &vector)
Set a vector and compute function value, gradient and Hessian efficiently.
Definition: Sum.cc:70