1# Vector operation example {#dsppp_vector_example} 2 3To compute: 4 5\f[ 6 7\overrightarrow{d} = \overrightarrow{a} + \overrightarrow{b} * \overrightarrow{c} 8 9\f] 10 11we need to: 121. Include the right header files 132. allocate the vectors 143. initialize the vectors 154. make the computation. 16 17# Include the headers 18 19The headers are not yet part of the CMSIS-DSP packs since they are experimental. You can get them from the [CMSIS-DSP github](https://github.com/ARM-software/CMSIS-DSP/tree/main/dsppp/Include/dsppp) 20 21```cpp 22#include <dsppp/memory_pool> 23#include <dsppp/matrix> 24 25using namespace arm_cmsis_dsp; 26``` 27 28If fixed point datatypes are required, `#include <dsppp/fixed_point>` should be used before `<dsppp/matrix>` 29 30Fixed point requires the use of CMSIS-DSP. 31 32# Creation of the vectors 33 34To create a vector `a` you would write: 35 36```cpp 37constexpr int NB = 32; 38 39Vector<float32_t,NB> a; 40Vector<float32_t,NB> b; 41Vector<float32_t,NB> c; 42``` 43 44`Vector<float32_t,NB>` is creating a vector of dimension `NB` (known at build time) and datatype `float32_t`. This creation is requiring some memory allocation and by default it is done with a `malloc`. 45 46It is possible to change the memory allocator for the vectors (and it is advised) to avoid using `malloc` and instead have deterministic allocation without fragmentation. 47 48See section @ref dsppp_memory_allocator "Memory allocation". 49 50Vectors of different dimensions are considered as being different types. 51 52If you don't know the dimension at build time, you can use a different type of vector with: 53 54```cpp 55Vector<float32_t> a(NB); 56``` 57 58For the trade-off between vector with build time dimension or runtime dimension please see the section @ref dsppp_memory_static_dynamic . 59 60# Initialization of the vectors 61 62You can index the vectors as normal C arrays. 63 64```cpp 65for(int i = 0;i< NB;i++) 66{ 67 a[i] = b[i] = c[i] = i; 68} 69``` 70 71# Computation 72 73The computation can be written normally as : 74 75```cpp 76Vector<float32_t,NB> d = a + b * c; 77``` 78 79Note that the computation can be parametrized with template arguments so the same computation could be used with any datatype or length. In that case you would have to define a template (and not just a normal function) and inside you would use something like: 80 81```cpp 82Vector<T,NB> d = a + b * c; 83``` 84 85where `T` is a type variable coming from the template. 86 87The operators `+`, `*` are computed in one pass with one loop : we have loop fusion and instead of having a loop per operator we have a loop for the whole computation. 88 89To understand fusion and how to extend it with new operators, see section @ref dsppp_fusion . 90 91For an overview of vector operators, see section @ref dsppp_vector . 92For an overview of matrix operators, see section @ref dsppp_matrix . 93 94# Displaying the result 95 96The vectors can be displayed on `stdout` for debug purpose. 97 98```cpp 99std::cout << "Result = " << d ; 100``` 101