LSST Applications 29.1.1,g0fba68d861+94d977d4f8,g1fd858c14a+0a42b1a450,g21d47ad084+bae5d1592d,g35bb328faa+fcb1d3bbc8,g36ff55ed5b+4036fd6440,g4e0f332c67+abab7ee1ee,g53246c7159+fcb1d3bbc8,g60b5630c4e+4036fd6440,g67b6fd64d1+31de10a2f7,g72a202582f+7a25662ef1,g78460c75b0+2f9a1b4bcd,g786e29fd12+cf7ec2a62a,g86c591e316+1a75853d69,g8852436030+8220ab3cb6,g88f4e072da+7005418d1d,g89139ef638+31de10a2f7,g8b8da53e10+8f7b08dc1c,g9125e01d80+fcb1d3bbc8,g989de1cb63+31de10a2f7,g9f1445be69+4036fd6440,g9f33ca652e+fcef3ba435,ga9baa6287d+4036fd6440,ga9e4eb89a6+a41a34c2ba,gabe3b4be73+1e0a283bba,gb0b61e0e8e+d456af7c26,gb1101e3267+f17a9d70ea,gb58c049af0+f03b321e39,gb89ab40317+31de10a2f7,gce29eb0867+05ed69485a,gcf25f946ba+8220ab3cb6,gd6cbbdb0b4+11317e7a17,gd9a9a58781+fcb1d3bbc8,gde0f65d7ad+b4f50ea554,ge278dab8ac+50e2446c94,ge410e46f29+31de10a2f7,ge80e9994a3+32bb9bc1c9,gf5e32f922b+fcb1d3bbc8,gf67bdafdda+31de10a2f7
LSST Data Management Base Package
Loading...
Searching...
No Matches
transforms.h
Go to the documentation of this file.
1#ifndef LSST_GAUSS2D_FIT_TRANSFORMS_H
2#define LSST_GAUSS2D_FIT_TRANSFORMS_H
3
4#include <cmath>
5#include <iostream>
6#include <memory>
7
9
10namespace lsst::gauss2d::fit {
11#pragma GCC diagnostic push
12#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
13
14namespace parameters = lsst::modelfit::parameters;
16
17struct InverseTransform : public Transform {
18 std::string description() const override { return "Inverse transform"; }
19 std::string repr(bool name_keywords = false,
20 const std::string_view& namespace_separator
22 return parameters::type_name_str<InverseTransform>(false, namespace_separator) + "()";
23 }
24 std::string str() const override { return parameters::type_name_str<InverseTransform>(true) + "()"; }
25
26 inline double derivative(double x) const override { return 1 / (x * x); }
27 inline double forward(double x) const override { return 1 / x; }
28 inline double reverse(double x) const override { return 1 / x; }
29};
30
32 static inline const double f_nu_0 = 3630.780547701002879554236770479;
33
34 std::string description() const override { return "jansky to AB magnitude transform"; }
35 std::string repr(bool name_keywords = false,
36 const std::string_view& namespace_separator
38 return parameters::type_name_str<JanskyToABMagTransform>(false, namespace_separator) + "()";
39 }
40 std::string str() const override {
42 }
43
44 inline double derivative(double x) const override {
45 return -1.08573620475812959718098227313021197915 / x;
46 }
47 inline double forward(double x) const override { return -2.5 * log10(x / f_nu_0); }
48 inline double reverse(double x) const override { return f_nu_0 * pow(10.0, -0.4 * x); }
49};
50
52 std::string description() const override { return "nanojansky to AB magnitude transform"; }
53 std::string repr(bool name_keywords = false,
54 const std::string_view& namespace_separator
56 return parameters::type_name_str<NanojanskyToABMagTransform>(false, namespace_separator) + "()";
57 }
58 std::string str() const override {
60 }
61
62 inline double derivative(double x) const override { return JanskyToABMagTransform::derivative(x); }
63 inline double forward(double x) const override { return JanskyToABMagTransform::forward(x * 1e-9); }
64 inline double reverse(double x) const override { return 1e9 * JanskyToABMagTransform::reverse(x); }
65};
66
67struct LogTransform : public Transform {
68 std::string description() const override { return "Natural (base e) logarithmic transform"; }
69 std::string repr(bool name_keywords = false,
70 const std::string_view& namespace_separator
72 return parameters::type_name_str<LogTransform>(false, namespace_separator) + "()";
73 }
74 std::string str() const override { return parameters::type_name_str<LogTransform>(true) + "()"; }
75
76 inline double derivative(double x) const override { return 1 / x; }
77 inline double forward(double x) const override { return log(x); }
78 inline double reverse(double x) const override { return exp(x); }
79};
80
81struct Log10Transform : public Transform {
82 std::string description() const override { return "Base 10 logarithmic transform"; }
83 std::string repr(bool name_keywords = false,
84 const std::string_view& namespace_separator
86 return parameters::type_name_str<Log10Transform>(false, namespace_separator) + "()";
87 }
88 std::string str() const override { return parameters::type_name_str<Log10Transform>(true) + "()"; }
89
90 inline double derivative(double x) const override {
91 return 0.434294481903251827651128918916605082294397 / x;
92 }
93 inline double forward(double x) const override { return log10(x); }
94 inline double reverse(double x) const override { return pow(10., x); }
95};
96
97struct LogitTransform : public Transform {
98 std::string description() const override { return "Logit transform"; }
99 std::string repr(bool name_keywords = false,
100 const std::string_view& namespace_separator
102 return parameters::type_name_str<LogitTransform>(false, namespace_separator) + "()";
103 }
104 std::string str() const override { return parameters::type_name_str<LogitTransform>(true) + "()"; }
105
106 inline double derivative(double x) const override { return 1 / x + 1 / (1 - x); }
107 inline double forward(double x) const override { return log(x / (1 - x)); }
108 inline double reverse(double x) const override { return 1 / (1 + exp(-x)); }
109};
110
112public:
114 set_limits(std::move(limits));
115 set_factor(factor);
116 _set_range();
117 }
118
119 std::string description() const override { return "Logit limited (to finite range) transform"; }
120 std::string repr(bool name_keywords = false,
121 const std::string_view& namespace_separator
123 return parameters::type_name_str<LogitLimitedTransform>(false, namespace_separator) + "("
124 + (name_keywords ? "limits=" : "") + _limits->repr(name_keywords, namespace_separator) + ", "
125 + (name_keywords ? "factor=" : "") + std::to_string(_factor) + ")";
126 }
127 std::string str() const override {
128 return parameters::type_name_str<LogitLimitedTransform>(true) + "(limits=" + _limits->str()
129 + ", factor=" + std::to_string(_factor) + ")";
130 }
131
132 double get_factor() const { return _factor; }
133 parameters::Limits<double>& get_limits() const { return *_limits; }
134
135 double derivative(double x) const override;
136 double forward(double x) const override;
137 double reverse(double x) const override;
138
139 void set_factor(double factor) {
140 if (!(factor > 0))
141 throw std::invalid_argument("LogitLimitedTransform factor=" + std::to_string(factor) + " !>0");
142 _factor = factor;
143 }
144
146 _limits = (limits == nullptr) ? std::make_shared<parameters::Limits<double>>() : std::move(limits);
147 _set_range();
148 }
149
150private:
152 double _factor;
153 double _range;
154
155 inline void _set_range() { _range = _limits->get_max() - _limits->get_min(); }
156};
157
158template <class T>
160 static T transform_default{};
161 static std::shared_ptr<T> ptr{std::shared_ptr<T>{}, &transform_default};
162 return ptr;
163}
164#pragma GCC diagnostic pop
165} // namespace lsst::gauss2d::fit
166
167#endif // LSST_GAUSS2D_FIT_TRANSFORMS_H
std::string description() const override
Return a description of this transform.
Definition transforms.h:119
parameters::Limits< double > & get_limits() const
Definition transforms.h:133
double forward(double x) const override
Return the transformed value of x.
Definition transforms.cc:19
std::string repr(bool name_keywords=false, const std::string_view &namespace_separator=parameters::Object::CC_NAMESPACE_SEPARATOR) const override
Return a full, callable string representation of this.
Definition transforms.h:120
void set_limits(std::shared_ptr< parameters::Limits< double > > limits)
Definition transforms.h:145
std::string str() const override
Return a brief, human-readable string representation of this.
Definition transforms.h:127
LogitLimitedTransform(std::shared_ptr< parameters::Limits< double > > limits, double factor=1)
Definition transforms.h:113
double derivative(double x) const override
Return the derivative of this tranform at the value x.
Definition transforms.cc:10
double reverse(double x) const override
Return the original value of x given a transformed value.
Definition transforms.cc:31
Range-based limits for parameter values.
Definition limits.h:45
static constexpr std::string_view CC_NAMESPACE_SEPARATOR
The C++ namespace separator.
Definition object.h:42
A reversible transformation of a real scalar value.
Definition transform.h:45
T make_shared(T... args)
T move(T... args)
parameters::Transform< double > Transform
Definition transforms.h:15
std::shared_ptr< T > get_transform_default()
Definition transforms.h:159
std::string type_name_str(bool strip_namespace=false, const std::string_view &namespace_str=detail::NAMESPACE_SEPARATOR)
Get a string representation of an arbitrary C++ type, potentially modifying its namespace prefix.
Definition type_name.h:101
double reverse(double x) const override
Return the original value of x given a transformed value.
Definition transforms.h:28
std::string repr(bool name_keywords=false, const std::string_view &namespace_separator=parameters::Object::CC_NAMESPACE_SEPARATOR) const override
Return a full, callable string representation of this.
Definition transforms.h:19
std::string description() const override
Return a description of this transform.
Definition transforms.h:18
std::string str() const override
Return a brief, human-readable string representation of this.
Definition transforms.h:24
double derivative(double x) const override
Return the derivative of this tranform at the value x.
Definition transforms.h:26
double forward(double x) const override
Return the transformed value of x.
Definition transforms.h:27
double derivative(double x) const override
Return the derivative of this tranform at the value x.
Definition transforms.h:44
std::string description() const override
Return a description of this transform.
Definition transforms.h:34
std::string repr(bool name_keywords=false, const std::string_view &namespace_separator=parameters::Object::CC_NAMESPACE_SEPARATOR) const override
Return a full, callable string representation of this.
Definition transforms.h:35
double forward(double x) const override
Return the transformed value of x.
Definition transforms.h:47
double reverse(double x) const override
Return the original value of x given a transformed value.
Definition transforms.h:48
std::string str() const override
Return a brief, human-readable string representation of this.
Definition transforms.h:40
std::string repr(bool name_keywords=false, const std::string_view &namespace_separator=parameters::Object::CC_NAMESPACE_SEPARATOR) const override
Return a full, callable string representation of this.
Definition transforms.h:83
std::string description() const override
Return a description of this transform.
Definition transforms.h:82
std::string str() const override
Return a brief, human-readable string representation of this.
Definition transforms.h:88
double derivative(double x) const override
Return the derivative of this tranform at the value x.
Definition transforms.h:90
double forward(double x) const override
Return the transformed value of x.
Definition transforms.h:93
double reverse(double x) const override
Return the original value of x given a transformed value.
Definition transforms.h:94
double reverse(double x) const override
Return the original value of x given a transformed value.
Definition transforms.h:78
std::string description() const override
Return a description of this transform.
Definition transforms.h:68
std::string str() const override
Return a brief, human-readable string representation of this.
Definition transforms.h:74
std::string repr(bool name_keywords=false, const std::string_view &namespace_separator=parameters::Object::CC_NAMESPACE_SEPARATOR) const override
Return a full, callable string representation of this.
Definition transforms.h:69
double derivative(double x) const override
Return the derivative of this tranform at the value x.
Definition transforms.h:76
double forward(double x) const override
Return the transformed value of x.
Definition transforms.h:77
std::string str() const override
Return a brief, human-readable string representation of this.
Definition transforms.h:104
double derivative(double x) const override
Return the derivative of this tranform at the value x.
Definition transforms.h:106
std::string description() const override
Return a description of this transform.
Definition transforms.h:98
double reverse(double x) const override
Return the original value of x given a transformed value.
Definition transforms.h:108
double forward(double x) const override
Return the transformed value of x.
Definition transforms.h:107
std::string repr(bool name_keywords=false, const std::string_view &namespace_separator=parameters::Object::CC_NAMESPACE_SEPARATOR) const override
Return a full, callable string representation of this.
Definition transforms.h:99
std::string description() const override
Return a description of this transform.
Definition transforms.h:52
double reverse(double x) const override
Return the original value of x given a transformed value.
Definition transforms.h:64
std::string repr(bool name_keywords=false, const std::string_view &namespace_separator=parameters::Object::CC_NAMESPACE_SEPARATOR) const override
Return a full, callable string representation of this.
Definition transforms.h:53
double derivative(double x) const override
Return the derivative of this tranform at the value x.
Definition transforms.h:62
double forward(double x) const override
Return the transformed value of x.
Definition transforms.h:63
std::string str() const override
Return a brief, human-readable string representation of this.
Definition transforms.h:58
T to_string(T... args)