Skip to content

Commit 7aefcd0

Browse files
committed
0.1.2 SparseMatrix
1 parent 84b8c6b commit 7aefcd0

File tree

13 files changed

+230
-54
lines changed

13 files changed

+230
-54
lines changed

libraries/SparseMatrix/.arduino-ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ compile:
88
- m4
99
- esp32
1010
- esp8266
11-
# - mega2560
11+
# - mega2560
12+
libraries:
13+
- "SHT85"

libraries/SparseMatrix/README.md

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1821
The 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.
4470
Parameter 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.
5084
If the value is set to zero, it is removed from the internal store.
5185
Returns 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.
5488
If needed a new internal element is created.
5589
If the sum is zero, the element is removed from the internal store.
5690
Returns 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

libraries/SparseMatrix/SparseMatrix.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
//
22
// FILE: SparseMatrix.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.1
4+
// VERSION: 0.1.2
55
// DATE: 2022-07-12
66
// PURPOSE: Arduino library for sparse matrices
7+
// URL: https://github.com/RobTillaart/SparseMatrix
78
//
89
// HISTORY:
910
// 0.1.0 2022-07-12 initial version
1011
// 0.1.1 2022-07-13 add clear();
1112
// add add(x, y, value);
1213
// fix set(x, y, 0);
14+
// 0.1.2 2022-07-14 increase size to uint16_t
15+
// add SPARSEMATRIX_MAX_SIZE
16+
// improve documentation
1317

1418

1519
#include "SparseMatrix.h"
1620

1721

18-
SparseMatrix::SparseMatrix(uint8_t sz)
22+
SparseMatrix::SparseMatrix(uint16_t sz)
1923
{
2024
_count = 0;
2125
_size = sz;
26+
if ( _size > SPARSEMATRIX_MAX_SIZE)
27+
{
28+
_size = SPARSEMATRIX_MAX_SIZE;
29+
}
2230
_x = (uint8_t *) malloc(sz);
2331
_y = (uint8_t *) malloc(sz);
2432
_value = (float *) malloc(sz * sizeof(float));
@@ -37,13 +45,13 @@ SparseMatrix::~SparseMatrix()
3745
}
3846

3947

40-
uint8_t SparseMatrix::size()
48+
uint16_t SparseMatrix::size()
4149
{
4250
return _size;
4351
}
4452

4553

46-
uint8_t SparseMatrix::count()
54+
uint16_t SparseMatrix::count()
4755
{
4856
return _count;
4957
}
@@ -58,7 +66,7 @@ void SparseMatrix::clear()
5866
float SparseMatrix::sum()
5967
{
6068
float _sum = 0;
61-
for (int i = 0; i < _count; i++)
69+
for (uint16_t i = 0; i < _count; i++)
6270
{
6371
_sum += _value[i];
6472
}
@@ -68,7 +76,7 @@ float SparseMatrix::sum()
6876

6977
bool SparseMatrix::set(uint8_t x, uint8_t y, float value)
7078
{
71-
int pos = findPos(x, y);
79+
int32_t pos = findPos(x, y);
7280
// existing element
7381
if (pos > -1)
7482
{
@@ -104,7 +112,7 @@ bool SparseMatrix::set(uint8_t x, uint8_t y, float value)
104112

105113
bool SparseMatrix::add(uint8_t x, uint8_t y, float value)
106114
{
107-
int pos = findPos(x, y);
115+
int32_t pos = findPos(x, y);
108116
// existing element
109117
if (pos > -1)
110118
{
@@ -137,7 +145,7 @@ bool SparseMatrix::add(uint8_t x, uint8_t y, float value)
137145

138146
float SparseMatrix::get(uint8_t x, uint8_t y)
139147
{
140-
int pos = findPos(x, y);
148+
int32_t pos = findPos(x, y);
141149
if (pos > -1)
142150
{
143151
return _value[pos];
@@ -146,13 +154,18 @@ float SparseMatrix::get(uint8_t x, uint8_t y)
146154
}
147155

148156

149-
int SparseMatrix::findPos(uint8_t x, uint8_t y)
157+
//////////////////////////////////////////////////////
158+
//
159+
// PRIVATE
160+
//
161+
int32_t SparseMatrix::findPos(uint8_t x, uint8_t y)
150162
{
151-
for (int i = 0; i < _count; i++)
163+
// linear search - not optimized.
164+
for (uint16_t i = 0; i < _count; i++)
152165
{
153166
if ((_x[i] == x) && (_y[i] == y))
154167
{
155-
return i;
168+
return (int32_t)i;
156169
}
157170
}
158171
return -1;
@@ -161,3 +174,4 @@ int SparseMatrix::findPos(uint8_t x, uint8_t y)
161174

162175

163176
// -- END OF FILE --
177+

libraries/SparseMatrix/SparseMatrix.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,30 @@
22
//
33
// FILE: SparseMatrix.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.1
5+
// VERSION: 0.1.2
66
// DATE: 2022-07-12
77
// PURPOSE: Arduino library for sparse matrices
8+
// URL: https://github.com/RobTillaart/SparseMatrix
89
//
910

1011

1112
#include "Arduino.h"
1213

13-
#define SPARSEMATRIX_LIB_VERSION (F("0.1.1"))
14+
#define SPARSEMATRIX_LIB_VERSION (F("0.1.2"))
15+
16+
#ifndef SPARSEMATRIX_MAX_SIZE
17+
#define SPARSEMATRIX_MAX_SIZE 1000
18+
#endif
1419

1520

1621
class SparseMatrix
1722
{
1823
public:
19-
SparseMatrix(uint8_t sz);
24+
SparseMatrix(uint16_t sz);
2025
~SparseMatrix();
2126

22-
uint8_t size();
23-
uint8_t count();
27+
uint16_t size();
28+
uint16_t count();
2429
float sum();
2530
void clear();
2631

@@ -34,16 +39,16 @@ class SparseMatrix
3439

3540

3641
private:
37-
int _size = 0;
38-
int _count = 0;
42+
uint16_t _size = 0;
43+
uint16_t _count = 0;
3944

4045
uint8_t *_x = NULL;
4146
uint8_t *_y = NULL;
4247
float *_value = NULL;
4348

44-
// returns index of x,y if in set
49+
// returns index of x, y if in set
4550
// otherwise -1
46-
int findPos(uint8_t x, uint8_t y);
51+
int32_t findPos(uint8_t x, uint8_t y);
4752
};
4853

4954

0 commit comments

Comments
 (0)