@@ -13,44 +13,78 @@ Arduino library for sparse matrices.
1313
1414## Description
1515
16- SparseMatrix is an ** experimental** library to implement sparse matrices on an Arduino.
16+ SparseMatrix is an ** experimental** library to implement
17+ two dimensional sparse matrices (of floats) on an Arduino.
18+ A sparse matrix is a matrix with mostly zeros and a low percentage non-zero values.
19+ The purpose of this library is efficient storage in memory.
1720
1821The maximum matrix that can be represented is 255 x 255
19- with a maximum of 255 non-zero elements.
20- This would just fit in an UNO's 2K memory.
22+ with a theoretical maximum of 65535 non-zero elements.
23+ In practice the library limits this to 1000 non-zero elements.
24+ Note: 255 elements would still fit in an UNO's 2K memory.
2125
22- The library does not hold the dimensions of the matrix (at least in 0.1.0)
26+ Note: the library does not do matrix math operations.
2327
24- The purpose of the library is efficient storage in memory.
25- It does not do math operations except sum() .
28+ Note: the library does not hold the dimensions of the matrix
29+ and cannot check these .
2630
27- Relates to https://github.com/RobTillaart/distanceTable
31+ Relates somewhat to https://github.com/RobTillaart/distanceTable
2832
2933
3034#### Implementation
3135
32- The implementation is based on 3 arrays holding ``` x, y, value ``` where value is float.
33- In the future other datatypes should be possible.
36+ The implementation is based on 3 arrays holding ``` x, y, value ```
37+ where value is float, and x and y are uint8_t.
38+ That are 6 bytes per element.
39+ The number of elements that the sparse matrix object can hold are
40+ given as parameter to the constructor.
41+ If the space cannot be allocated the size is set to zero.
42+
43+ In the future other data types should be possible.
44+
3445
3546#### Performance
3647
37- The elements are not kept sorted or indexed so optimizations are possible
38- but not investigated yet.
48+ The elements are not kept sorted or indexed so optimizations might be
49+ possible but are not investigated yet.
50+ There is however a test sketch to monitor the performance of
51+ the most important functions.
52+
53+ Accessing elements internally is done with a linear search,
54+ which becomes (much) slower if the number of elements is increasing.
55+ This means that although in theory there can be 65535 elements,
56+ in practice a few 100 can already become annoyingly slow.
57+ To keep performance a bit the library has a limit build in.
58+ Check the .h file for ** SPARSEMATRIX_MAX_SIZE 1000**
3959
4060
4161## Interface
4262
43- - ** SparseMatrix(uint8_t size)** constructor.
63+ ``` cpp
64+ #include " SparseMatrix.h"
65+ ```
66+
67+ ### Constructor + meta
68+
69+ - ** SparseMatrix(uint16_t size)** constructor.
4470Parameter is the maximum number of elements in the sparse matrix.
45- - ** uint8_t size()** maximum number of elements.
46- - ** uint8_t count()** current number of elements in the matrix.
71+ Note this number is limited to ** SPARSEMATRIX_MAX_SIZE 1000** .
72+ If the space requested cannot be allocated size will be set to 0.
73+ - ** uint16_t size()** maximum number of elements.
74+ If this is zero, a problem occurred with allocation happened.
75+ - ** uint16_t count()** current number of elements in the matrix.
76+ Should be between 0 and size.
4777- ** float sum()** sum of all elements ( > 0 ) in the matrix.
4878- ** void clear()** resets the matrix to all zero's again.
79+
80+
81+ ### Access
82+
4983- ** bool set(uint8_t x, uint8_t y, float value)** gives an element in the matrix a value.
5084If the value is set to zero, it is removed from the internal store.
5185Returns false if the internal store is full, true otherwise.
5286- ** float get(uint8_t x, uint8_t y)** returns the value in the matrix.
53- - ** bool add(uint8_t x, uint8_t y, float value)** adds a value to an element in the matrix.
87+ - ** bool add(uint8_t x, uint8_t y, float value)** adds value to an element in the matrix.
5488If needed a new internal element is created.
5589If the sum is zero, the element is removed from the internal store.
5690Returns false if the internal store is full, true otherwise.
@@ -64,32 +98,33 @@ Returns false if the internal store is full, true otherwise.
6498 - 1, 2, 3 (RGB), 4 byte integer or 8 byte doubles
6599 - struct, complex number
66100 - etc
67- - add examples
68- - 2D histogram e.g. temperature vs humidity
69- - N queens game.
70- - investigate optimizations.
71- - should ** set()** and ** add()** return the number of free places?
72- - no hard code and more informative than just a bool.
101+ - investigate performance optimizations
102+ - sort
103+ - linked list, tree, hashing?
73104- can ** set()** and ** add()** be merged?
74- - add link in distanceTable repo
75- - uint16_t size for larger platforms.
76- - max matrix still 255 x 255 but more elements <> 0.
77105
78106
79- #### new functions
107+ #### Functions
80108
81109- walk through the elements?
82- - first -> next; last -> prev.
83-
110+ - first() -> next() ; optional last() -> prev() .
111+
84112
85113#### won't
86114
115+ - should ** set()** and ** add()** return the number of free places?
116+ - more informative than just a bool.
117+ - One looses the info that the operation was successful
118+ - set a zero threshold ?
119+ - if (abs(x) < TH) element is considered zero => remove
120+ - not portable to template version (sum() is not either!)
121+ - user can do this.
87122- math
88123 - determinant?
89124 - M x M
90125 - diagonal?
91126- add examples
127+ - N queens game.
92128 - battleship game
93129 - minesweeper game
94- - nice exercise
95130
0 commit comments