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.

  • start The starting value (of item 0).
  • step The 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 ).

  • index The 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.

  • value The 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.

  • value The 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.

  • pi The 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.

Sin is calculated using the following polynomial: sinx = x -( x^3/6.0 )+( x^5/120.0 )-( x^7/5040.0 )+( x^9/362880.0 )

Self must be wrapped to the -π=<x<π range.

  • 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 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] };

Below is the the taylor approximation for sine compared to the Julia native sin function. The below plot is generated with double presicion floationg point for demonstrative purposes. In the figure it is apparent that there is greater error in the Taylor approximation further from origo. Therefore, Alt version To counter these, the fact that all quarters of the sine(x) function are mirrored versions of each other. Therefore the first quarters, having the least error, which can be seen in the time domain plot above, are used for all values of x. Resulting in a practically ideal sine function, as can be seen in the frequency domain comparison below. Alt version

Cos

Take the elemtent-wise cosine using a Taylor approximation of cos x.

Cos is calculated using the following polynomial: cosx = 1 -( x^2/2 )+( x^4/24.0 )-( x^6/720.0 )+( x^8/40320.0 )

Self must be wrapped to the -π=<x<π range.

  • 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 mut x = Arr8::ramp(0,60);
x = x.wrap_phase( 180 );
assert_eq!{x.cos( 180, 100).data, [100, 50, -50, -100, -50, 50, 100, 50] };

A first-quarter method as described for the sine implementation is also used on cosine. The pure Taylor approximation is displayed below. Alt version With the first-quarter method, the resulting cosine power spectrum is displayed below. Alt version

Tan

Take the element-wise tan using a Taylor approximation of tan x.

Tan is calculated using the following polynomial: tanx = x+( x^3/3 )+( x^5*2/15.0 )+( x^7*17/315.0 )+( x^9*62/2835.0 )+( x^11*1382/155925.0 )+( x^13*21844/6081075.0 )+( x^15*929569/638512875.0 )

Self must be wrapped to the -π/2=<x<π/2 range. The function is based on a Taylor expansion. Its error increases as |x| approaches π/2.

  • 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, 158, 373, 2155, 19696, 158268] };

Below is the the taylor approximation for tan compared to the Julia native tan function. The below plot is generated with double presicion floationg point for demonstrative purposes. Alt version

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};