Skip to content

Commit 84b8c6b

Browse files
committed
0.1.1 SparseMatrix
1 parent 02d63b3 commit 84b8c6b

File tree

10 files changed

+168
-24
lines changed

10 files changed

+168
-24
lines changed

libraries/SparseMatrix/README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Relates to https://github.com/RobTillaart/distanceTable
3232
The implementation is based on 3 arrays holding ``` x, y, value``` where value is float.
3333
In the future other datatypes should be possible.
3434

35+
#### Performance
36+
3537
The elements are not kept sorted or indexed so optimizations are possible
3638
but not investigated yet.
3739

@@ -43,10 +45,15 @@ Parameter is the maximum number of elements in the sparse matrix.
4345
- **uint8_t size()** maximum number of elements.
4446
- **uint8_t count()** current number of elements in the matrix.
4547
- **float sum()** sum of all elements ( > 0 ) in the matrix.
48+
- **void clear()** resets the matrix to all zero's again.
4649
- **bool set(uint8_t x, uint8_t y, float value)** gives an element in the matrix a value.
4750
If the value is set to zero, it is removed from the internal store.
4851
Returns false if the internal store is full, true otherwise.
4952
- **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.
54+
If needed a new internal element is created.
55+
If the sum is zero, the element is removed from the internal store.
56+
Returns false if the internal store is full, true otherwise.
5057

5158

5259
## Future
@@ -61,23 +68,19 @@ Returns false if the internal store is full, true otherwise.
6168
- 2D histogram e.g. temperature vs humidity
6269
- N queens game.
6370
- investigate optimizations.
64-
- should **set()** return the number of free places?
71+
- should **set()** and **add()** return the number of free places?
6572
- no hard code and more informative than just a bool.
73+
- can **set()** and **add()** be merged?
6674
- add link in distanceTable repo
6775
- uint16_t size for larger platforms.
6876
- max matrix still 255 x 255 but more elements <> 0.
6977

7078

7179
#### new functions
7280

73-
- **float add(uint8_t x, uint8_t y, float value)** adds value to the x,y position.
74-
- add or remove an internal element if needed,
75-
- functional **set(x,y, get(x,y) + value)**
76-
- **void clear()** sets all elements to zero again.
77-
- \_count = 0; should be sufficient.
78-
- walking through the elements?
81+
- walk through the elements?
7982
- first -> next; last -> prev.
80-
83+
8184

8285
#### won't
8386

libraries/SparseMatrix/SparseMatrix.cpp

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
//
22
// FILE: SparseMatrix.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.0
4+
// VERSION: 0.1.1
55
// DATE: 2022-07-12
66
// PURPOSE: Arduino library for sparse matrices
77
//
88
// HISTORY:
99
// 0.1.0 2022-07-12 initial version
10+
// 0.1.1 2022-07-13 add clear();
11+
// add add(x, y, value);
12+
// fix set(x, y, 0);
1013

1114

1215
#include "SparseMatrix.h"
@@ -21,6 +24,7 @@ SparseMatrix::SparseMatrix(uint8_t sz)
2124
_value = (float *) malloc(sz * sizeof(float));
2225
// catch malloc error
2326
if (_x && _y && _value) return;
27+
// if malloc error set size to zero.
2428
_size = 0;
2529
}
2630

@@ -44,6 +48,13 @@ uint8_t SparseMatrix::count()
4448
return _count;
4549
}
4650

51+
52+
void SparseMatrix::clear()
53+
{
54+
_count = 0;
55+
}
56+
57+
4758
float SparseMatrix::sum()
4859
{
4960
float _sum = 0;
@@ -70,17 +81,54 @@ bool SparseMatrix::set(uint8_t x, uint8_t y, float value)
7081
_count--;
7182
// move last element
7283
// efficiency is not a requirement yet.
73-
_x[pos] = _x[_count];
74-
_y[pos] = _y[_count];
75-
_value[pos] = _value[_count];
84+
if (_count > 0)
85+
{
86+
_x[pos] = _x[_count];
87+
_y[pos] = _y[_count];
88+
_value[pos] = _value[_count];
89+
}
90+
}
91+
return true;
92+
}
93+
94+
// does not exist => new element ?
95+
if (value == 0) return true;
96+
if (_count >= _size) return false;
97+
_x[_count] = x;
98+
_y[_count] = y;
99+
_value[_count] = value;
100+
_count++;
101+
return true;
102+
}
103+
104+
105+
bool SparseMatrix::add(uint8_t x, uint8_t y, float value)
106+
{
107+
int pos = findPos(x, y);
108+
// existing element
109+
if (pos > -1)
110+
{
111+
_value[pos] += value;
112+
if (_value[pos] == 0.0)
113+
{
114+
_count--;
115+
// move last element
116+
// efficiency is not a requirement yet.
117+
if (_count > 0)
118+
{
119+
_x[pos] = _x[_count];
120+
_y[pos] = _y[_count];
121+
_value[pos] = _value[_count];
122+
}
76123
}
77124
return true;
78125
}
79126

80-
// new element
127+
// does not exist => new element ?
128+
if (value == 0) return true;
81129
if (_count >= _size) return false;
82-
_x[_count] = x;
83-
_y[_count] = y;
130+
_x[_count] = x;
131+
_y[_count] = y;
84132
_value[_count] = value;
85133
_count++;
86134
return true;

libraries/SparseMatrix/SparseMatrix.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
//
33
// FILE: SparseMatrix.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.0
5+
// VERSION: 0.1.1
66
// DATE: 2022-07-12
77
// PURPOSE: Arduino library for sparse matrices
88
//
99

1010

1111
#include "Arduino.h"
1212

13-
#define SPARSEMATRIX_LIB_VERSION (F("0.1.0"))
13+
#define SPARSEMATRIX_LIB_VERSION (F("0.1.1"))
1414

1515

1616
class SparseMatrix
@@ -22,10 +22,14 @@ class SparseMatrix
2222
uint8_t size();
2323
uint8_t count();
2424
float sum();
25+
void clear();
26+
2527

2628
// returns false if no slots free
2729
// could return # free slots?
2830
bool set(uint8_t x, uint8_t y, float value);
31+
// adds value to element x,y
32+
bool add(uint8_t x, uint8_t y, float value);
2933
float get(uint8_t x, uint8_t y);
3034

3135

libraries/SparseMatrix/examples/sparse_matrix_demo/sparse_matrix_demo.ino

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ void setup()
2929
bool b = sm.set(x, y, value); // check full
3030
if (b) Serial.print('.');
3131
}
32-
3332
Serial.println();
3433
dump(10, 10);
3534

@@ -38,6 +37,32 @@ void setup()
3837
sm.set(3, y, 0);
3938
}
4039
dump(10, 10);
40+
41+
for (int y = 0; y < 10; y++)
42+
{
43+
bool b = sm.add(0, y, -1);
44+
if (b == false) Serial.print('e');
45+
}
46+
Serial.println();
47+
dump(10, 10);
48+
49+
sm.clear();
50+
Serial.println();
51+
dump(10, 10);
52+
53+
for (int y = 0; y < 10; y++)
54+
{
55+
sm.add(y, y, 5);
56+
}
57+
Serial.println();
58+
dump(10, 10);
59+
60+
for (int y = 0; y < 10; y++)
61+
{
62+
sm.add(y, y, -5);
63+
}
64+
Serial.println();
65+
dump(10, 10);
4166
}
4267

4368

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
sparse_matrix_performance.ino
2+
20
3+
0
4+
5+
set 20x : 272
6+
redo 20x : 224
7+
full 20x : 368
8+
add 20x : 444
9+
get 20x : 232
10+
9.00
11+
sum 20x : 156
12+
clr 20x : 4
13+

libraries/SparseMatrix/examples/sparse_matrix_performance/sparse_matrix_performance.ino

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: sparse_matrix_performance.ino
33
// AUTHOR: Rob Tillaart
4-
// PURPOSE: demo
4+
// PURPOSE: performance measurement functions
55

66

77
#include "SparseMatrix.h"
@@ -54,6 +54,16 @@ void setup()
5454
Serial.println(stop - start);
5555
delay(100);
5656

57+
start = micros();
58+
for (int i = 0; i < 20; i++)
59+
{
60+
sm.add(i, i, 5);
61+
}
62+
stop = micros();
63+
Serial.print("add 20x :\t");
64+
Serial.println(stop - start);
65+
delay(100);
66+
5767
volatile float f;
5868
start = micros();
5969
for (int i = 0; i < 20; i++)
@@ -72,6 +82,16 @@ void setup()
7282
Serial.print("sum 20x :\t");
7383
Serial.println(stop - start);
7484
delay(100);
85+
86+
start = micros();
87+
for (int i = 0; i < 20; i++)
88+
{
89+
sm.clear();
90+
}
91+
stop = micros();
92+
Serial.print("clr 20x :\t");
93+
Serial.println(stop - start);
94+
delay(100);
7595
}
7696

7797

libraries/SparseMatrix/keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ SparseMatrix KEYWORD1
77
size KEYWORD2
88
count KEYWORD2
99
sum KEYWORD2
10+
clear KEYWORD2
1011

1112
get KEYWORD2
1213
set KEYWORD2
14+
add KEYWORD2
1315

1416

1517
# Constants (LITERAL1)

libraries/SparseMatrix/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/SparseMatrix.git"
1717
},
18-
"version": "0.1.0",
18+
"version": "0.1.1",
1919
"license": "MIT",
2020
"frameworks": "arduino",
2121
"platforms": "*",

libraries/SparseMatrix/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparseMatrix
2-
version=0.1.0
2+
version=0.1.1
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for sparse matrices.

libraries/SparseMatrix/test/unit_test_001.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,17 @@ unittest(test_set)
5252
for (int i = 0; i < 10; i++)
5353
{
5454
assertTrue(sm.set(i, i, 1.0 * i * i));
55-
assertEqual(i+1, sm.count());
55+
assertEqual(i, sm.count());
5656
}
57-
assertFalse(sm.set(3,4,5)); // don't fit any more...
57+
assertTrue(sm.set(3, 4, 5));
58+
assertFalse(sm.set(5, 4, 5)); // don't fit any more...
59+
60+
// do not set new element to zero
61+
sm.clear();
62+
assertEqual(0, sm.count());
63+
64+
sm.set(1, 2, 0);
65+
assertEqual(0, sm.count());
5866
}
5967

6068

@@ -66,7 +74,6 @@ unittest(test_get)
6674
assertTrue(sm.set(i, i, 1.0 * i * i));
6775
assertEqualFloat(1.0 * i * i, sm.get(i, i), 0.001);
6876
}
69-
assertFalse(sm.set(3,4,5)); // don't fit any more...
7077
}
7178

7279

@@ -81,6 +88,28 @@ unittest(test_sum)
8188
}
8289

8390

91+
unittest(test_add)
92+
{
93+
SparseMatrix sm(10);
94+
for (int i = 0; i < 10; i++)
95+
{
96+
assertTrue(sm.add(i, i, 1.0 * i * i));
97+
assertEqualFloat(1.0 * i * i, sm.get(i, i), 0.001);
98+
}
99+
for (int i = 0; i < 10; i++)
100+
{
101+
assertTrue(sm.add(i, i, 1.0 * i * i));
102+
assertEqualFloat(2.0 * i * i, sm.get(i, i), 0.001);
103+
}
104+
for (int i = 0; i < 10; i++)
105+
{
106+
assertTrue(sm.add(i, i, -2.0 * i * i));
107+
assertEqualFloat(0, sm.get(i, i), 0.001);
108+
}
109+
assertEqual(0, sm.count());
110+
}
111+
112+
84113
unittest_main()
85114

86115

0 commit comments

Comments
 (0)