ProteoWizard
scale.hpp
Go to the documentation of this file.
1//
2// $Id: $
3//
4//
5// Original author: Witold Wolski <wewolski@gmail.com>
6//
7// Copyright : ETH Zurich
8//
9// Licensed under the Apache License, Version 2.0 (the "License");
10// you may not use this file except in compliance with the License.
11// You may obtain a copy of the License at
12//
13// http://www.apache.org/licenses/LICENSE-2.0
14//
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20//
21
22#ifndef SCALE_H
23#define SCALE_H
24
25#include <math.h>
26#include <algorithm>
27#include <vector>
28#include <functional>
29#include <numeric>
30#include <iterator>
32
33namespace ralab
34{
35 namespace base
36 {
37 namespace stats
38 {
39
40
41 template<int N, typename TReal>
42 struct NthPower : std::unary_function<TReal,TReal>
43 {
44 TReal operator()(const TReal & x)
45 {
46 TReal ret = x;
47 for (int i=1; i < N; ++i) {
48 ret *= x;
49 }
50 return ret;
51 }
52 };
53
54
55 /*!\brief The root-mean-square for a column is obtained by computing the square-root of the sum-of-squares of the non-missing values in the column divided by the number of non-missing values minus one. */
56 template<typename InputIterator>
57 typename std::iterator_traits<InputIterator>::value_type
59 const InputIterator begin, //!< [in] start iterator
60 const InputIterator end //!< [in] end iterator
61 )
62 {
63 typedef typename std::iterator_traits<InputIterator>::value_type TReal;
64 std::vector<TReal> x(begin,end);
65
66 std::transform( x.begin(), x.end(), x.begin(), NthPower<2,TReal>() ); //first sqaure all elements
67 TReal sum = std::accumulate(x.begin(), x.end() , TReal(0.));
68 sum = sum/static_cast<TReal>(x.size() - size_t(1));
69 return(sqrt(sum));
70 }
71
72
73 /**
74 scale centers and/or scales all values from begin in to end.
75 */
76 template<typename InputIterator>
77 void scale(
78 InputIterator begin,
79 InputIterator end,
80 std::pair<typename std::iterator_traits<InputIterator>::value_type,typename std::iterator_traits<InputIterator>::value_type> & scaled, //!<[out] scaled.first = center, scaled.second = scale
81 bool center = true,//!<[in] either a logical value or a numeric vector of length equal to the number of columns of x.
82 bool scale = true //!<[in] either a logical value or a numeric vector of length equal to the number of columns of x.
83 )
84 {
85 typedef typename std::iterator_traits<InputIterator>::value_type TReal;
86 std::vector<TReal> tmp;
87
88 if(center)
89 {
90 scaled.first = ralab::base::base::mean( begin , end);
91 std::transform(begin, end, begin, std::bind2nd( std::minus<TReal>(), scaled.first));
92 }
93 else
94 {
95 scaled.first = std::numeric_limits<TReal>::quiet_NaN();
96 }
97 if(scale)
98 {
99 scaled.second = rootMeanSquare( begin , end );
100 std::transform(begin, end, begin , std::bind2nd(std::divides<TReal>(), scaled.second) );
101 }
102 else
103 {
104 scaled.second = std::numeric_limits<TReal>::quiet_NaN();
105 }
106 }
107
108 }
109 }
110}
111
112#endif
113
114
N
Definition Chemistry.hpp:80
KernelTraitsBase< Kernel >::space_type::abscissa_type x
std::iterator_traits< InputIterator >::value_type mean(InputIterator begin, InputIterator end)
MEAN Trimmed arithmetic mean.
Definition base.hpp:187
void scale(InputIterator begin, InputIterator end, std::pair< typename std::iterator_traits< InputIterator >::value_type, typename std::iterator_traits< InputIterator >::value_type > &scaled, bool center=true, bool scale=true)
scale centers and/or scales all values from begin in to end.
Definition scale.hpp:77
std::iterator_traits< InputIterator >::value_type rootMeanSquare(const InputIterator begin, const InputIterator end)
The root-mean-square for a column is obtained by computing the square-root of the sum-of-squares of t...
Definition scale.hpp:58
EQUISPACEINTERPOL Interpolation on a equidistantly spaced grid.
Definition base.hpp:40
TReal operator()(const TReal &x)
Definition scale.hpp:44