imate
C++/CUDA Reference
Loading...
Searching...
No Matches
log_gaussian_der.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: Copyright 2021, Siavash Ameli <sameli@berkeley.edu>
3 * SPDX-License-Identifier: BSD-3-Clause
4 * SPDX-FileType: SOURCE
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the license found in the LICENSE.txt file in the root
8 * directory of this source tree.
9 */
10
11
12// =======
13// Headers
14// =======
15
16// Before including cmath, define _USE_MATH_DEFINES. This is only required to
17// define the math constants like M_PI, etc, in win32 operating system.
18#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && \
19 !defined(__CYGWIN__)
20 #define _USE_MATH_DEFINES
21#endif
22
23#include <cmath> // exp, M_SQRT1_2, M_2_SQRTPI
24#include "./log_gaussian_der.h"
25
26
27// ================
28// Log Gaussian Der
29// ================
30
33
34LogGaussianDer::LogGaussianDer(double mu_, double sigma_)
35{
36 this->mu = mu_;
37 this->sigma = sigma_;
38}
39
40// =========================
41// Log Gaussian Der function (float)
42// =========================
43
47
48float LogGaussianDer::function(const float lambda_) const
49{
50 float mu_ = static_cast<float>(this->mu);
51 float sigma_ = static_cast<float>(this->sigma);
52 float sigma2 = sigma_ * sigma_;
53 float sigma3 = sigma2 * sigma_;
54 float x = (lambda_ - mu_) / sigma_;
55 return -(0.5f * M_SQRT1_2 * M_2_SQRTPI / sigma3) * \
56 std::exp(-0.5f * x * x) * \
57 (std::log(lambda_) - std::log(mu) + sigma2) / (lambda_ * lambda_);
58}
59
60
61// =========================
62// Log Gaussian Der function (double)
63// =========================
64
68
69double LogGaussianDer::function(const double lambda_) const
70{
71 double x = (lambda_ - this->mu) / this->sigma;
72 double sigma2 = this->sigma * this->sigma;
73 double sigma3 = sigma2 * this->sigma;
74 return -(0.5 * M_SQRT1_2 * M_2_SQRTPI / sigma3) * \
75 std::exp(-0.5 * x * x) * \
76 (std::log(lambda_) - std::log(mu) + sigma2) / (lambda_ * lambda_);
77}
78
79
80// =========================
81// Log Gaussian Der function (long double)
82// =========================
83
87
88long double LogGaussianDer::function(const long double lambda_) const
89{
90 long double mu_ = static_cast<long double>(this->mu);
91 long double sigma_ = static_cast<long double>(this->sigma);
92 long double sigma2 = sigma_ * sigma_;
93 long double sigma3 = sigma2 * sigma_;
94 long double x = (lambda_ - mu_) / sigma_;
95 return -(0.5l * M_SQRT1_2 * M_2_SQRTPI / sigma3) * \
96 std::exp(-0.5l * x * x) * \
97 (std::log(lambda_) - std::log(mu) + sigma2) / (lambda_ * lambda_);
98}
virtual float function(const float lambda_) const
LogGaussianDer(double mu_, double sigma_)
Sets the default for the parameter mu to 0.0 and for the parameter sigma to 1.0.