Arithmetic Scalar Operators
The ArithmeticScalar class serves as a base for operators that perform mathematical operations on a single numeric input stream. Each derived class implements a specific mathematical function that is applied to each input value.
Basic Operation
All ArithmeticScalar operators:
- Have one input port (port 0) for numeric data
- Have one output port (port 0) for numeric results
- Process messages immediately (no buffering)
- Preserve message timestamps
- Apply their mathematical operation to each input value
Available Operators
Arithmetic Operators
Add (
y = x + C)- Additional parameter:
value(constant to add)
{
"id": "add1",
"type": "Add",
"value": 5.0
}- Additional parameter:
Scale (
y = C × x)- Additional parameter:
value(scaling factor)
{
"id": "scale1",
"type": "Scale",
"value": 2.0
}- Additional parameter:
Power (
y = x^C)- Additional parameter:
value(exponent)
{
"id": "pow1",
"type": "Power",
"value": 2.0
}- Additional parameter:
Trigonometric Functions
Sin (
y = sin(x)){
"id": "sin1",
"type": "Sin"
}Cos (
y = cos(x)){
"id": "cos1",
"type": "Cos"
}Tan (
y = tan(x)){
"id": "tan1",
"type": "Tan"
}
Exponential and Logarithmic Functions
Exp (
y = e^x){
"id": "exp1",
"type": "Exp"
}Log (
y = ln(x)){
"id": "log1",
"type": "Log"
}Log10 (
y = log₁₀(x)){
"id": "log10_1",
"type": "Log10"
}
Absolute Value and Sign Functions
Abs (
y = |x|){
"id": "abs1",
"type": "Abs"
}Sign (
y = sign(x)){
"id": "sign1",
"type": "Sign"
}
Rounding Functions
Floor (
y = ⌊x⌋){
"id": "floor1",
"type": "Floor"
}Ceil (
y = ⌈x⌉){
"id": "ceil1",
"type": "Ceil"
}Round (
y = round(x)){
"id": "round1",
"type": "Round"
}
Example Usage
Message Flow Example
Consider an Add operator with value = 2.0:
| Time | Input | Output |
|---|---|---|
| 1 | 1.0 | 3.0 |
| 2 | 2.5 | 4.5 |
| 4 | -1.0 | 1.0 |
| 5 | 3.0 | 5.0 |
Note how:
- Time gaps are preserved (no message at t=3)
- Output timestamps match input timestamps
- The constant value (2.0) is added to each input
Code Example
// Create an Add operator
auto add = make_add("add1", 2.0);
// Create a Sine operator
auto sine = make_sin("sin1");
// Process some messages
add->receive_data(create_message<NumberData>(1, NumberData{1.0}), 0);
add->execute();
sine->receive_data(create_message<NumberData>(1, NumberData{M_PI/2}), 0);
sine->execute();
Error Handling
Operators will throw exceptions for:
- Invalid message types
- Invalid input port indices
- Mathematical errors (e.g., log of negative numbers)
Implementation Notes
Stateless Operation
- No buffering of messages
- Each output depends only on current input
- Immediate processing
Performance
- O(1) processing per message
- Constant memory usage
- No allocation during normal operation
Numerical Considerations
- Uses IEEE 754 floating-point arithmetic
- Handles special values (NaN, Inf) according to standard rules
- Preserves numeric precision of underlying math functions
Factory Functions
Each operator type has an associated factory function:
auto add = make_add("add1", 2.0);
auto sin = make_sin("sin1");
auto exp = make_exp("exp1");
// etc.