pub struct Instruction {
pub instr_type: InstrType,
pub args: HashMap<String, f64>,
}
Expand description
Struct for a general instruction, consisting of type and arguments.
Different instruction types expects different fields in their argument dictionary.
Behavior for minimally expected keys are defined in Instruction::new
, behavior of
default values are defined in Instruction::eval_inplace
.
§Implemented instruction types and their expected fields:
InstrType::CONST
:const
InstrType::SINE
:freq
amplitude
: Default is1.0
offset
: Default is0.0
phase
: Default is0.0
Fields§
§instr_type: InstrType
§args: HashMap<String, f64>
Implementations§
Source§impl Instruction
impl Instruction
Sourcepub fn new(instr_type: InstrType, args: HashMap<String, f64>) -> Instruction
pub fn new(instr_type: InstrType, args: HashMap<String, f64>) -> Instruction
Constructs an Instruction
object.
This method serves as the foundational constructor upon which custom constructor
wrappers for new instructions should be built. For each instruction type,
it ensures that the args
dictionary contains the required keys.
Missing keys will cause a panic.
§Examples
Constructing a new CONST
instruction
(this is effectively the underlying implementation for Instruction::new_const
,
the more convenient constructor):
use nicompiler_backend::instruction::*;
use std::collections::HashMap;
let mut const_args = InstrArgs::new();
const_args.insert("value".to_string(), 1.0);
let const_instr = Instruction::new(InstrType::CONST, const_args);
If you fail to provide the required argument fields, it will panic:
let mut const_args = InstrArgs::new();
let const_instr = Instruction::new(InstrType::CONST, const_args);
The panic message will be:
thread 'main' panicked at 'Expected instr type CONST to contain key value'
.
Constructing a new SINE
instruction:
let mut sine_args = InstrArgs::new();
sine_args.insert("freq".to_string(), 10.0);
sine_args.insert("offset".to_string(), 1.0); // amplitude and phase will use default values
let sine_instr = Instruction::new(InstrType::SINE, sine_args);
Sourcepub fn eval_inplace(
&self,
t_arr: &mut ArrayBase<ViewRepr<&mut f64>, Dim<[usize; 1]>>,
)
pub fn eval_inplace( &self, t_arr: &mut ArrayBase<ViewRepr<&mut f64>, Dim<[usize; 1]>>, )
Evaluates the instruction and populates the given array view with float-point values.
This method takes a mutable array view (t_arr
) and modifies its values in-place based on the instruction type and its arguments.
- For
InstrType::CONST
, the array will be filled with the constant value specified by thevalue
argument. - For
InstrType::SINE
, a sinusoidal waveform is generated using the argumentsfreq
,amplitude
,offset
, andphase
. Default values are used if certain arguments are not provided.
§Arguments
t_arr
- A mutable 1D array view that will be populated with the evaluated values.
§Examples
Given an instruction set with a constant and a sine instruction, and an array representing time values from 0 to 1:
use ndarray::{Array2, Array1};
use nicompiler_backend::instruction::*;
let t_row = ndarray::Array1::linspace(0.0, 1.0, 10);
let mut t_values = ndarray::stack(ndarray::Axis(0), &[t_row.view(), t_row.view()]).unwrap();
// Use wrappers to create sine and constant instructions same as the examples above
let const_instr = Instruction::new_const(1.0);
const_instr.eval_inplace(&mut t_values.row_mut(0));
let sine_instr = Instruction::new_sine(10.0, None, None, Some(1.0));
sine_instr.eval_inplace(&mut t_values.row_mut(1));
assert!(t_values[[0, 0]] == 1. && t_values[[0, 1]] == 1.);
Sourcepub fn new_const(value: f64) -> Instruction
pub fn new_const(value: f64) -> Instruction
Wrapper for conveniently creating new constant instructions. Example usage equivalent to the constant example above:
let const_instr = Instruction::new_const(1.0);
Sourcepub fn new_sine(
freq: f64,
amplitude: Option<f64>,
phase: Option<f64>,
dc_offset: Option<f64>,
) -> Instruction
pub fn new_sine( freq: f64, amplitude: Option<f64>, phase: Option<f64>, dc_offset: Option<f64>, ) -> Instruction
Constructs a new sine instruction with provided parameters.
Allows for convenient creation of sine instructions by specifying the frequency and optionally, amplitude, phase, and DC offset. Unspecified parameters will not be included in the instruction’s argument dictionary, allowing for default values to be used elsewhere if necessary.
§Arguments
freq
: The frequency of the sine wave.amplitude
: Optional amplitude of the sine wave. IfNone
, it will not be set in the instruction.phase
: Optional phase offset of the sine wave in radians. IfNone
, it will not be set in the instruction.dc_offset
: Optional DC offset for the sine wave. IfNone
, it will not be set in the instruction.
§Examples
Constructing a sine instruction with a specified frequency, and DC offset. Amplitude and phase will use any default values defined elsewhere:
§use nicompiler_backend::instruction::*;
§let sine_instr = Instruction::new_sine(10.0, None, None, Some(1.0));
Trait Implementations§
Source§impl Clone for Instruction
impl Clone for Instruction
Source§fn clone(&self) -> Instruction
fn clone(&self) -> Instruction
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Display for Instruction
impl Display for Instruction
Source§impl PartialEq for Instruction
impl PartialEq for Instruction
impl StructuralPartialEq for Instruction
Auto Trait Implementations§
impl Freeze for Instruction
impl RefUnwindSafe for Instruction
impl Send for Instruction
impl Sync for Instruction
impl Unpin for Instruction
impl UnwindSafe for Instruction
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more