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