Skip to main content

AffineTransform

Struct AffineTransform 

Source
pub struct AffineTransform { /* private fields */ }
Expand description

Invertible i32 affine map y' = (y * gain + offset) / scale.

Arithmetic uses i64 intermediates and nearest, ties-away-from-zero rounding. Identity (modulo rounding when |scale| ≠ 1) is gain = scale and offset = 0. A negative gain or scale is allowed and flips sense.

This type never reads NVM, wraps a transfer, or writes TransferMetadata. AffineCalibration contains one of these and delegates its gain/offset/scale arithmetic here.

§Inverse

unapply solves y = (y' * scale - offset) / gain with the same rounding. Because a zero gain collapses every input onto offset / scale, new rejects gain == 0 rather than deferring the failure to unapply.

§Numerical scope

For any i32 y, gain, and offset, the product/sum y * gain + offset always fits in i64; the same holds for y' * scale - offset on the inverse path. Both directions still report AffineOverflow::Overflow when the rounded result does not fit i32.

Both directions round, so unapply(apply(y)) is bounded rather than exact. A transform that compresses the scale cannot restore what the forward quantization discarded. At i32 extremes, inverse rounding of a forward result can land just outside i32, in which case unapply reports overflow.

Implementations§

Source§

impl AffineTransform

Source

pub const fn new( gain: i32, offset: i32, scale: i32, ) -> Result<Self, AffineTransformError>

Construct an invertible affine transform.

§Errors

Returns AffineTransformError::ZeroScale if scale == 0, or AffineTransformError::ZeroGain if gain == 0. A zero gain maps every input onto the single value offset / scale and has no inverse.

Examples found in repository?
examples/no_std_generated_fixtures.rs (line 36)
23pub fn runtime_api_smoke(code: u16, sample: u32) -> (i32, u16, u32, bool) {
24    let transfer = PiecewiseLinearTransfer::new(
25        &SMOKE_INPUTS,
26        &SMOKE_OUTPUTS,
27        MonotonicDirection::Increasing,
28    );
29    let calibrated = match AffineCalibration::new(transfer, 1_001, 0, 1_000) {
30        Ok(value) => value,
31        Err(_) => unreachable!(),
32    };
33    let physical = calibrated.convert(code).unwrap_or_default();
34    let inverse = calibrated.invert(physical).unwrap_or_default();
35
36    let affine = match AffineTransform::new(1_001, 0, 1_000) {
37        Ok(value) => value,
38        Err(_) => unreachable!(),
39    };
40    let _ = affine
41        .apply(physical)
42        .and_then(|value| affine.unapply(value));
43
44    let mut average = MovingAverage::<u32, 1>::new();
45    let filtered = match average.update(sample) {
46        FilterOutput::Ready(value) => value,
47        FilterOutput::WarmingUp { .. } => unreachable!(),
48    };
49    let mut latch = Hysteresis::<u32>::new(100, 200);
50    (physical, inverse, filtered, latch.update(filtered))
51}
Source

pub const fn gain(&self) -> i32

Return the gain coefficient.

Source

pub const fn offset(&self) -> i32

Return the offset term.

Source

pub const fn scale(&self) -> i32

Return the nonzero scale divisor.

Source

pub fn apply(&self, value: i32) -> Result<i32, AffineOverflow>

Apply y' = (y * gain + offset) / scale.

Rounding is nearest, ties away from zero.

§Errors

Returns AffineOverflow::Overflow when the rounded result does not fit in i32.

Examples found in repository?
examples/no_std_generated_fixtures.rs (line 41)
23pub fn runtime_api_smoke(code: u16, sample: u32) -> (i32, u16, u32, bool) {
24    let transfer = PiecewiseLinearTransfer::new(
25        &SMOKE_INPUTS,
26        &SMOKE_OUTPUTS,
27        MonotonicDirection::Increasing,
28    );
29    let calibrated = match AffineCalibration::new(transfer, 1_001, 0, 1_000) {
30        Ok(value) => value,
31        Err(_) => unreachable!(),
32    };
33    let physical = calibrated.convert(code).unwrap_or_default();
34    let inverse = calibrated.invert(physical).unwrap_or_default();
35
36    let affine = match AffineTransform::new(1_001, 0, 1_000) {
37        Ok(value) => value,
38        Err(_) => unreachable!(),
39    };
40    let _ = affine
41        .apply(physical)
42        .and_then(|value| affine.unapply(value));
43
44    let mut average = MovingAverage::<u32, 1>::new();
45    let filtered = match average.update(sample) {
46        FilterOutput::Ready(value) => value,
47        FilterOutput::WarmingUp { .. } => unreachable!(),
48    };
49    let mut latch = Hysteresis::<u32>::new(100, 200);
50    (physical, inverse, filtered, latch.update(filtered))
51}
Source

pub fn unapply(&self, value: i32) -> Result<i32, AffineOverflow>

Undo y' = (y * gain + offset) / scale, recovering y.

Solves y = (y' * scale - offset) / gain with the same nearest, ties-away rounding. Offset is subtracted in i64 rather than negated as i32, so offset == i32::MIN is representable.

Both directions round, so unapply(apply(y)) is bounded rather than exact: a transform that compresses the scale cannot restore what the forward quantization discarded.

§Errors

Returns AffineOverflow::Overflow when the rounded result does not fit in i32.

Examples found in repository?
examples/no_std_generated_fixtures.rs (line 42)
23pub fn runtime_api_smoke(code: u16, sample: u32) -> (i32, u16, u32, bool) {
24    let transfer = PiecewiseLinearTransfer::new(
25        &SMOKE_INPUTS,
26        &SMOKE_OUTPUTS,
27        MonotonicDirection::Increasing,
28    );
29    let calibrated = match AffineCalibration::new(transfer, 1_001, 0, 1_000) {
30        Ok(value) => value,
31        Err(_) => unreachable!(),
32    };
33    let physical = calibrated.convert(code).unwrap_or_default();
34    let inverse = calibrated.invert(physical).unwrap_or_default();
35
36    let affine = match AffineTransform::new(1_001, 0, 1_000) {
37        Ok(value) => value,
38        Err(_) => unreachable!(),
39    };
40    let _ = affine
41        .apply(physical)
42        .and_then(|value| affine.unapply(value));
43
44    let mut average = MovingAverage::<u32, 1>::new();
45    let filtered = match average.update(sample) {
46        FilterOutput::Ready(value) => value,
47        FilterOutput::WarmingUp { .. } => unreachable!(),
48    };
49    let mut latch = Hysteresis::<u32>::new(100, 200);
50    (physical, inverse, filtered, latch.update(filtered))
51}

Trait Implementations§

Source§

impl Clone for AffineTransform

Source§

fn clone(&self) -> AffineTransform

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for AffineTransform

Source§

impl Debug for AffineTransform

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for AffineTransform

Source§

impl PartialEq for AffineTransform

Source§

fn eq(&self, other: &AffineTransform) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for AffineTransform

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<Src, Dst> LosslessTryInto<Dst> for Src
where Dst: LosslessTryFrom<Src>,

Source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
Source§

impl<Src, Dst> LossyInto<Dst> for Src
where Dst: LossyFrom<Src>,

Source§

fn lossy_into(self) -> Dst

Performs the conversion.
Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.