25#ifndef LSST_MODELFIT_PARAMETERS_PARAMETER_H
26#define LSST_MODELFIT_PARAMETERS_PARAMETER_H
42namespace lsst::modelfit::parameters {
109 virtual void set_unit(std::shared_ptr<const Unit> unit =
nullptr) = 0;
111 static const UnitTransform<T>& transform_none() {
return UnitTransform<T>::get(); };
114 return &first == &second;
117 friend bool operator!=(
const ParameterBase<T>& first,
const ParameterBase<T>& second) {
118 return &first != &second;
121 friend bool operator<(
const ParameterBase<T>& first,
const ParameterBase<T>& second) {
122 return std::less<const ParameterBase<T>*>{}(&first, &second) ==
true;
125 virtual ~ParameterBase() =
default;
149template <
typename T,
class C>
158 Limiter(
const Limits<T>& limits_in) : limits(limits_in){};
164 Transformer(
const Transform<T>& transform_in) : transform(transform_in){};
168 static constexpr T _default = 0;
170 static constexpr T _min = -std::numeric_limits<T>::infinity();
172 static constexpr T _max = std::numeric_limits<T>::infinity();
174 static constexpr bool _linear =
false;
178 std::unique_ptr<Limiter> _limiter;
180 std::unique_ptr<Transformer> _transformer;
184 std::shared_ptr<const Limits<T>> _limits_ptr;
186 std::shared_ptr<const Transform<T>> _transform_ptr;
188 std::shared_ptr<const Unit> _unit_ptr;
191 void _set_value(T value) {
193 throw std::runtime_error(this->
str() +
"Value=" + std::to_string(value)
204 static const std::string _get_desc() {
return C::_desc; }
205 static constexpr bool _get_linear() {
return C::_linear; }
206 static constexpr T _get_min() {
return C::_min; }
207 static constexpr T _get_max() {
return C::_max; }
208 static const std::string _get_name() {
return C::_name; }
214 std::string
get_desc()
const override {
return _get_desc(); }
222 std::string
get_label()
const override {
return _label; }
226 =
Limits<T>(_get_min(), _get_max(), std::string(type_name<C>()) +
".limits_maximal");
227 return limits_maximal;
234 T
get_min()
const override {
return _get_min(); }
236 T
get_max()
const override {
return _get_max(); }
238 std::string
get_name()
const override {
return _get_name(); }
242 std::shared_ptr<const Transform<T>>
get_transform_ptr()
const override {
return _transform_ptr; }
248 static const std::string
get_type_name(
bool strip_namespace_separator =
false,
249 const std::string_view& namespace_separator
250 = detail::NAMESPACE_SEPARATOR) {
251 return type_name_str<C>(strip_namespace_separator, namespace_separator);
261 std::shared_ptr<C>
ptr() {
return this->shared_from_this(); }
264 void set_free(
bool free)
override { _free = free; }
265 void set_label(std::string label)
override { _label = std::move(label); }
270 if (limits ==
nullptr) {
271 _limiter = std::make_unique<Limiter>(limits_maximal);
273 if (!((limits->get_min() >= this->get_min()) && (limits->get_max() <= this->get_max()))) {
274 std::string error =
get_type_name() +
".set_limits(" + limits->str()
275 +
") sets limits that are less restrictive than the minimum="
276 + limits_maximal.str();
277 throw std::runtime_error(error);
279 _limits_ptr = std::move(limits);
280 _limiter = std::make_unique<Limiter>(*_limits_ptr);
284 if (transform ==
nullptr) {
289 _transformer = std::make_unique<Transformer>(this->transform_none());
291 _transform_ptr = std::move(transform);
292 _transformer = std::make_unique<Transformer>(*_transform_ptr);
300 _value_transformed = _transformer->transform.forward(value_new);
304 _set_value(_transformer->transform.reverse(value_transformed));
308 void set_unit(std::shared_ptr<const Unit> unit =
nullptr)
override {
309 _unit_ptr = unit ==
nullptr ? nullptr : std::move(unit);
312 std::string
repr(
bool name_keywords =
false,
const std::string_view& namespace_separator
314 return get_type_name(
false, namespace_separator) +
"(" + (name_keywords ?
"value=" :
"")
315 + std::to_string(
_value) +
", " + (name_keywords ?
"limits=" :
"") +
get_limits().repr() +
", "
316 + (name_keywords ?
"transform=" :
"") +
get_transform().repr() +
", "
317 + (name_keywords ?
"fixed=" :
"") + std::to_string(0 +
get_fixed()) +
", "
318 + (name_keywords ?
"label='" :
"'") + _label +
"')";
321 std::string
str()
const override {
329 + (!
get_fixed() ?
"" : (std::string(
"fixed=") + std::to_string(0 +
get_fixed()) +
", "))
344 const std::shared_ptr<
const Transform<T>> transform =
nullptr,
345 std::shared_ptr<const Unit> unit =
nullptr,
bool fixed =
false, std::string label =
"")
349 set_transform(transform ==
nullptr ?
nullptr : std::move(transform));
static constexpr std::string_view CC_NAMESPACE_SEPARATOR
The C++ namespace separator.
Definition object.h:42
Interface for parameters with values and metadata.
Definition parameter.h:58
virtual T get_max() const =0
Return the maximum value for this parameter instance.
virtual T get_default() const =0
Get the default value.
virtual std::string get_desc() const =0
Get a string description for this parameter class.
virtual void set_unit(std::shared_ptr< const Unit > unit=nullptr)=0
Set the unit for this parameter instance.
virtual T get_min() const =0
Return the minimum value for this parameter instance.
virtual void set_fixed(bool fixed)=0
Set the parameter to be fixed (or not).
virtual const Limits< T > & get_limits() const =0
Return the limits for the untransformed value.
virtual const Unit & get_unit() const =0
Return the unit of this parameter instance.
virtual void set_label(std::string label)=0
Set the string label for this parameter instance.
virtual T get_transform_derivative() const =0
Return the derivative of the transform for this parameter instance.
virtual void set_limits(std::shared_ptr< const Limits< T > > limits)=0
Set the limits for this parameter instance.
virtual void set_value(T value)=0
Set the untransformed value for this parameter instance.
virtual bool get_fixed() const =0
Return whether the parameter is fixed (not free).
virtual const Limits< T > & get_limits_maximal() const =0
Return limits representing the maximum/minimum untransformed value.
virtual void set_free(bool free)=0
Set the parameter to be free (or not).
virtual std::shared_ptr< const Transform< T > > get_transform_ptr() const =0
Return the transform pointer for this parameter instance.
virtual std::string get_name() const =0
Get a string name for this parameter class.
virtual bool get_free() const =0
Return whether the parameter is free (not fixed).
virtual std::string get_label() const =0
Return a string label for this parameter instance.
virtual const Transform< T > & get_transform() const =0
Return the transforming function for this parameter instance.
virtual bool get_linear() const =0
Return whether the parameter is linear.
virtual void set_value_transformed(T value_transformed)=0
Set the transformed value for this parameter instance.
virtual void set_transform(std::shared_ptr< const Transform< T > > transform)=0
Set the transforming function for this parameter instance.
virtual T get_value() const =0
Return the untransformed value of this parameter instance.
virtual T get_value_transformed() const =0
Return the transformed value of this parameter instance.
A parameter with values and metadata.
Definition parameter.h:150
std::string get_label() const override
Return a string label for this parameter instance.
Definition parameter.h:222
std::string get_desc() const override
Get a string description for this parameter class.
Definition parameter.h:214
const Unit & get_unit() const override
Return the unit of this parameter instance.
Definition parameter.h:254
bool get_fixed() const override
Return whether the parameter is fixed (not free).
Definition parameter.h:218
T _value
The untransformed value.
Definition parameter.h:200
static constexpr T _get_default()
Get the default value for the derived type of this.
Definition parameter.h:212
static const std::string get_type_name(bool strip_namespace_separator=false, const std::string_view &namespace_separator=detail::NAMESPACE_SEPARATOR)
Get the name of the derived type of this.
Definition parameter.h:248
T _value_transformed
The cached, transformed value.
Definition parameter.h:202
Parameter(T value=_get_default(), std::shared_ptr< const Limits< T > > limits=nullptr, const std::shared_ptr< const Transform< T > > transform=nullptr, std::shared_ptr< const Unit > unit=nullptr, bool fixed=false, std::string label="")
Definition parameter.h:343
std::shared_ptr< C > ptr()
Return a shared pointer to this.
Definition parameter.h:261
T get_value() const override
Return the untransformed value of this parameter instance.
Definition parameter.h:256
const Limits< T > & get_limits_maximal() const override
Return limits representing the maximum/minimum untransformed value.
Definition parameter.h:224
void set_transform(const std::shared_ptr< const Transform< T > > transform) override
Set the transforming function for this parameter instance.
Definition parameter.h:283
const Transform< T > & get_transform() const override
Return the transforming function for this parameter instance.
Definition parameter.h:240
T get_max() const override
Return the maximum value for this parameter instance.
Definition parameter.h:236
void set_label(std::string label) override
Set the string label for this parameter instance.
Definition parameter.h:265
std::string get_name() const override
Get a string name for this parameter class.
Definition parameter.h:238
void set_limits(std::shared_ptr< const Limits< T > > limits) override
Set the limits for this parameter instance.
Definition parameter.h:266
T get_transform_derivative() const override
Return the derivative of the transform for this parameter instance.
Definition parameter.h:244
T get_default() const override
Get the default value.
Definition parameter.h:216
std::shared_ptr< const Transform< T > > get_transform_ptr() const override
Return the transform pointer for this parameter instance.
Definition parameter.h:242
void set_free(bool free) override
Set the parameter to be free (or not).
Definition parameter.h:264
std::string str() const override
Return a brief, human-readable string representation of this.
Definition parameter.h:321
void set_unit(std::shared_ptr< const Unit > unit=nullptr) override
Set the unit for this parameter instance.
Definition parameter.h:308
std::string repr(bool name_keywords=false, const std::string_view &namespace_separator=Object::CC_NAMESPACE_SEPARATOR) const override
Definition parameter.h:312
bool get_free() const override
Return whether the parameter is free (not fixed).
Definition parameter.h:220
bool get_linear() const override
Return whether the parameter is linear.
Definition parameter.h:232
T get_min() const override
Return the minimum value for this parameter instance.
Definition parameter.h:234
void set_value_transformed(T value_transformed) override
Set the transformed value for this parameter instance.
Definition parameter.h:303
void set_fixed(bool fixed) override
Set the parameter to be fixed (or not).
Definition parameter.h:263
const Limits< T > & get_limits() const override
Return the limits for the untransformed value.
Definition parameter.h:230
T get_value_transformed() const override
Return the transformed value of this parameter instance.
Definition parameter.h:258
void set_value(T value) override
Set the untransformed value for this parameter instance.
Definition parameter.h:297