pub trait Amplitude:
DynClone
+ Send
+ Sync
+ Serialize
+ Deserialize {
// Required methods
fn register(
&mut self,
resources: &mut Resources,
) -> Result<AmplitudeID, LadduError>;
fn compute(
&self,
parameters: &Parameters<'_>,
event: &Event,
cache: &Cache,
) -> Complex<Float>;
// Provided methods
fn precompute(&self, event: &Event, cache: &mut Cache) { ... }
fn precompute_all(&self, dataset: &Dataset, resources: &mut Resources) { ... }
fn compute_gradient(
&self,
parameters: &Parameters<'_>,
event: &Event,
cache: &Cache,
gradient: &mut DVector<Complex<Float>>,
) { ... }
fn central_difference_with_indices(
&self,
indices: &[usize],
parameters: &Parameters<'_>,
event: &Event,
cache: &Cache,
gradient: &mut DVector<Complex<Float>>,
) { ... }
}Expand description
This is the only required trait for writing new amplitude-like structures for this
crate. Users need only implement the register
method to register parameters, cached values, and the amplitude itself with an input
Resources struct and the compute method to actually carry
out the calculation. Amplitude-implementors are required to implement Clone and can
optionally implement a precompute method to calculate and
cache values which do not depend on free parameters.
Required Methods§
Sourcefn register(
&mut self,
resources: &mut Resources,
) -> Result<AmplitudeID, LadduError>
fn register( &mut self, resources: &mut Resources, ) -> Result<AmplitudeID, LadduError>
This method should be used to tell the Resources manager about all of
the free parameters and cached values used by this Amplitude. It should end by
returning an AmplitudeID, which can be obtained from the
Resources::register_amplitude method.
Sourcefn compute(
&self,
parameters: &Parameters<'_>,
event: &Event,
cache: &Cache,
) -> Complex<Float>
fn compute( &self, parameters: &Parameters<'_>, event: &Event, cache: &Cache, ) -> Complex<Float>
This method constitutes the main machinery of an Amplitude, returning the actual
calculated value for a particular Event and set of Parameters. See those
structs, as well as Cache, for documentation on their available methods. For the
most part, Events can be interacted with via
Variables, while Parameters and the
Cache are more like key-value storage accessed by
ParameterIDs and several different types of cache
IDs.
Provided Methods§
Sourcefn precompute(&self, event: &Event, cache: &mut Cache)
fn precompute(&self, event: &Event, cache: &mut Cache)
Sourcefn precompute_all(&self, dataset: &Dataset, resources: &mut Resources)
fn precompute_all(&self, dataset: &Dataset, resources: &mut Resources)
Evaluates Amplitude::precompute over ever Event in a Dataset.
Sourcefn compute_gradient(
&self,
parameters: &Parameters<'_>,
event: &Event,
cache: &Cache,
gradient: &mut DVector<Complex<Float>>,
)
fn compute_gradient( &self, parameters: &Parameters<'_>, event: &Event, cache: &Cache, gradient: &mut DVector<Complex<Float>>, )
This method yields the gradient of a particular Amplitude at a point specified
by a particular Event and set of Parameters. See those structs, as well as
Cache, for documentation on their available methods. For the most part,
Events can be interacted with via Variables,
while Parameters and the Cache are more like key-value storage accessed by
ParameterIDs and several different types of cache
IDs. If the analytic version of the gradient is known, this method can be overwritten to
improve performance for some derivative-using methods of minimization. The default
implementation calculates a central finite difference across all parameters, regardless of
whether or not they are used in the Amplitude.
In the future, it may be possible to automatically implement this with the indices of
registered free parameters, but until then, the Amplitude::central_difference_with_indices
method can be used to conveniently only calculate central differences for the parameters
which are used by the Amplitude.
Sourcefn central_difference_with_indices(
&self,
indices: &[usize],
parameters: &Parameters<'_>,
event: &Event,
cache: &Cache,
gradient: &mut DVector<Complex<Float>>,
)
fn central_difference_with_indices( &self, indices: &[usize], parameters: &Parameters<'_>, event: &Event, cache: &Cache, gradient: &mut DVector<Complex<Float>>, )
A helper function to implement a central difference only on indices which correspond to
free parameters in the Amplitude. For example, if an Amplitude contains free
parameters registered to indices 1, 3, and 5 of the its internal parameters array, then
running this with those indices will compute a central finite difference derivative for
those coordinates only, since the rest can be safely assumed to be zero.