Macro integer_array::declare_array_real [−][src]
macro_rules! declare_array_real {
($name : ident, $N : expr) => { ... };
}Expand description
Create an i32 array type of size N. Complete with the traits shown below.
Traits implemented with the macro:
The implemented traits are focumented below. See the source for a complete list.
Basic use:
arrays types of fixed size is defined with traits through a macro as follows:
use integer_array as ia;
use integer_array::trait_definitions::*;
integer_array::declare_array_real!( Arr11, 11);Initialization
The array can be initalized with a single value across the array, or by creating a ramp.
Generate an array of zeros.
zeros is made as separate trait for convenience.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr2, 2);
let x = Arr2::zeros();
assert_eq!{x.len(), 2};Generate an array of ones.
ones is made as separate trait for convenience.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr2, 2);
let x = Arr2::ones();
assert_eq!{x[0], 1};Generate an array of a specific value.
An array of a single value is generated using the new keyword.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr2, 2);
let x = Arr2::new(200);
assert_eq!{x.data, [200, 200]};Generate a ramp of increasing value.
startThe starting value (of item 0).stepThe incrimental value.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr4, 4);
let x = Arr4::ramp(100,20);
assert_eq!{x.data, [100, 120, 140, 160] };Indexing:
Front and back:
Access the first element of the array using the .front() attribute.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr2, 2);
let x = Arr2::new(200);
assert_eq!{x.front(), 200};Access the last element of the array using the .back() attribute.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr32, 32);
let x = Arr32::ramp(100,20);
assert_eq!{x.back(), 720};At
A specific item can be accessed through either using square bracket notation, or .at( index ).
indexThe index of the item, in the range 0..N-1.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr2, 2);
let x = Arr2::zeros();
assert_eq!{x.at(1), 0};Bracket indexing
Square bracket indexing can be used both for reading and writing from/to an item.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr8, 8);
let x = Arr8::ramp(0,22);
assert_eq!{x[2], 44i32 };use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr8, 8);
let mut x = Arr8::ramp(0,22);
x[2] = 56;
assert_eq!{x[2], 56i32 };Scalar-array arithemetic:
Bias
The bias attribute adds a scalar bias to every element in the array.
valueThe bias amount.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr2, 2);
let x = Arr2::new(200);
let y = x.bias(5);
assert_eq!{y.front(), 205};The bias can also be implemented by adding or subtracting the array with a scalar.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr4, 8);
let mut x = Arr4::ramp(0,22);
x = x+3;
assert_eq!{x[1], 25i32 };
let mut x = Arr4::ramp(0,22);
x = x-3;
assert_eq!{x[1], 19i32 };Scale
The scale attribute scales every element in the array with a scalar value.
valueThe scaling factor.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr2, 2);
let x = Arr2::new(100);
let y = x.scale(5);
assert_eq!{y.front(), 500};Scaling can also be used by simply multiplying the array with a scalar value.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr8, 8);
let mut x = Arr8::ramp(0,22);
x = x*3;
assert_eq!{x[1], 66i32 };or through a division.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr4, 4);
let mut x = Arr4::ramp(0,22);
x = 1000/x;
assert_eq!{x.data, [2147483647, 45, 22, 15] };Sqrt
The sqrt attribute finds the square root of every element in the array.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr4, 4);
let x = Arr4::ramp(10000,1000);
assert_eq!{x.sqrt().data, [100, 104, 109, 114] };Inter-array arithemetic:
Operations can also be performed on an inter-array-basis. The arrays must be of the same size. The operations are written similarly as one would for scalars in Rust.
Addition and subtraction
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr4, 4);
let mut x = Arr4::ramp(0,22);
let y = Arr4::new(10);
x = x+y;
assert_eq!{x.data, [10,32,54,76] };
x = x-y;
assert_eq!{x.data, [0,22,44,66] };Multiplication and division
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr4, 4);
let mut x = Arr4::ramp(10,22);
let y = Arr4::new(10);
x = x*y;
assert_eq!{x.data, [100,320,540,760] };
x = Arr4::ramp(0,22);
x = x/y;
assert_eq!{x.data, [0,2,4,6] };In a divide-by-zero case, the maximum int value is returned.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr4, 4);
let mut x = Arr4::ramp(0,22);
let y = Arr4::new(1000);
x = y/x;
assert_eq!{x.data, [i32::MAX,45,22,15] };Trigometric functions:
The following trigometric functions are implemented based on a taylor approximation.
Wrap phase
Wrap array to a fixed-point -π=<x<π range.
piThe integer level which represents π in the input data.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr8, 8);
let x = Arr8::ramp(0,22);
assert_eq!{x.wrap_phase( 50 ).data, [0,22,44,-34,-12,10,32,-46] };Sin
Take the elemtent-wise sine using a Taylor approximation of sine x. Self must be wrapped to the -π=<x<π range.
piThe integer level which represents π in the input data.normThe integer level which represents 1 in the output data.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr8, 8);
let mut x = Arr8::ramp(0,60);
x = x.wrap_phase( 180 );
assert_eq!{x.sin( 180, 100).data, [0, 86, 86, 0, -86, -86, 0, 86] };Tan
Take the element-wise tan using a Taylor approximation of tan x.
- ‘pi’ The integer level which represents π in the input data.
- ‘norm’ The integer level which represents 1 in the output data.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr8, 8);
let x = Arr8::ramp(0,20);
assert_eq!{x.tan( 180, 100).data, [0, 36, 83, 155, 289, 630, 1794, 5875] };Estimator utilites:
Max and min:
The maimum and minimum value in the array can be found through the .max() and .min() traits respectively.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr32, 32);
let x = Arr32::ramp(100,20);
assert_eq!{x.max(), 720};use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr32, 32);
let x = Arr32::ramp(100,20);
assert_eq!{x.min(), 100};Argmax and argmin:
The index of the maximum and minimum items in the array can be found through the .argmax() and .argmin() traits respectively.
use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr32, 32);
let x = Arr32::ramp(0,1);
assert_eq!{x.argmax(), 31};use integer_array as ia;
use ia::trait_definitions::*;
ia::declare_array_real!( Arr32, 32);
let x = Arr32::ramp(100,20);
assert_eq!{x.argmin(), 0};