ProteoWizard
ParametrizedFunction.hpp
Go to the documentation of this file.
1 //
2 // $Id: ParametrizedFunction.hpp 1191 2009-08-14 19:33:05Z chambm $
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
6 //
7 // Copyright 2006 Louis Warschaw Prostate Cancer Center
8 // Cedars Sinai Medical Center, Los Angeles, California 90048
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //
22 
23 
24 #ifndef _PARAMETRIZEDFUNCTION_HPP_
25 #define _PARAMETRIZEDFUNCTION_HPP_
26 
27 
30 
31 
32 #include "boost/numeric/ublas/vector.hpp"
33 #include "boost/numeric/ublas/matrix.hpp"
34 #include "boost/numeric/ublas/io.hpp"
35 #include "boost/numeric/ublas/matrix_proxy.hpp"
36 namespace ublas = boost::numeric::ublas;
37 
38 
39 #include <complex>
40 
41 
42 namespace pwiz {
43 namespace frequency {
44 
45 
46 template<typename value_type>
48 {
49  public:
50  virtual unsigned int parameterCount() const = 0;
51  virtual value_type operator()(double x, const ublas::vector<double>& p) const = 0;
52  virtual ublas::vector<value_type> dp(double x, const ublas::vector<double>& p) const = 0;
53  virtual ublas::matrix<value_type> dp2(double x, const ublas::vector<double>& p) const = 0;
55 
56  class ErrorFunction;
57 };
58 
59 
60 template<typename value_type>
62 {
63  public:
64 
66  typedef std::vector<Datum> Data;
67 
69  : f_(f), data_(data)
70  {}
71 
72  int parameterCount() const {return f_.parameterCount();}
73 
74  double operator()(const ublas::vector<double>& p) const
75  {
76  double result = 0;
77  for (typename Data::const_iterator it=data_.begin(); it!=data_.end(); ++it)
78  result += norm(std::complex<double>(f_(it->x,p) - it->y));
79  return result;
80  }
81 
82  ublas::vector<double> dp(const ublas::vector<double>& p) const
83  {
84  ublas::vector<double> result(parameterCount());
85  result.clear();
86  for (typename Data::const_iterator it=data_.begin(); it!=data_.end(); ++it)
87  {
88  std::complex<double> diffconj = conj(std::complex<double>(f_(it->x,p) - it->y));
89  result += 2 * real(diffconj*f_.dp(it->x,p));
90  }
91  return result;
92  }
93 
94  ublas::matrix<double> dp2(const ublas::vector<double>& p) const
95  {
96  ublas::matrix<double> result(parameterCount(), parameterCount());
97  result.clear();
98  for (typename Data::const_iterator it=data_.begin(); it!=data_.end(); ++it)
99  {
100  std::complex<double> diffconj = conj(std::complex<double>(f_(it->x, p) - it->y));
101  ublas::vector<value_type> dp = f_.dp(it->x,p);
102  ublas::matrix<value_type> dp2 = f_.dp2(it->x,p);
103  result += 2 * real(diffconj*dp2 + outer_prod(conj(dp),dp));
104  }
105  return result;
106  }
107 
108  private:
110  const Data& data_;
111 };
112 
113 
114 } // namespace frequency
115 } // namespace pwiz
116 
117 
118 #endif // _PARAMETRIZEDFUNCTION_HPP_
119