1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LATINIME_NORMAL_DISTRIBUTION_H
18 #define LATINIME_NORMAL_DISTRIBUTION_H
19 
20 #include <cmath>
21 
22 #include "defines.h"
23 
24 namespace latinime {
25 
26 // Normal distribution N(u, sigma^2).
27 class NormalDistribution {
28  public:
NormalDistribution(const float u,const float sigma)29     NormalDistribution(const float u, const float sigma)
30             : mU(u),
31               mPreComputedNonExpPart(1.0f / sqrtf(2.0f * M_PI_F
32                       * GeometryUtils::SQUARE_FLOAT(sigma))),
33               mPreComputedExponentPart(-1.0f / (2.0f * GeometryUtils::SQUARE_FLOAT(sigma))) {}
34 
getProbabilityDensity(const float x)35     float getProbabilityDensity(const float x) const {
36         const float shiftedX = x - mU;
37         return mPreComputedNonExpPart
38                 * expf(mPreComputedExponentPart * GeometryUtils::SQUARE_FLOAT(shiftedX));
39     }
40 
41  private:
42     DISALLOW_IMPLICIT_CONSTRUCTORS(NormalDistribution);
43 
44     const float mU; // mean value
45     const float mPreComputedNonExpPart; // = 1 / sqrt(2 * PI * sigma^2)
46     const float mPreComputedExponentPart; // = -1 / (2 * sigma^2)
47 };
48 } // namespace latinime
49 #endif // LATINIME_NORMAL_DISTRIBUTION_H
50