Skip to content

Commit df5a7e4

Browse files
committed
0.1.0 SparseMatrix
1 parent 8183313 commit df5a7e4

File tree

16 files changed

+741
-0
lines changed

16 files changed

+741
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
compile:
2+
# Choosing to run compilation tests on 2 different Arduino platforms
3+
platforms:
4+
- uno
5+
# - due
6+
# - zero
7+
# - leonardo
8+
- m4
9+
- esp32
10+
- esp8266
11+
# - mega2560
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
name: Arduino-lint
3+
4+
on: [push, pull_request]
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- uses: arduino/arduino-lint-action@v1
11+
with:
12+
library-manager: update
13+
compliance: strict
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
name: Arduino CI
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
runTest:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: ruby/setup-ruby@v1
13+
with:
14+
ruby-version: 2.6
15+
- run: |
16+
gem install arduino_ci
17+
arduino_ci.rb
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: JSON check
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.json'
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: json-syntax-check
15+
uses: limitusus/json-syntax-check@v1
16+
with:
17+
pattern: "\\.json$"
18+

libraries/SparseMatrix/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022-2022 Rob Tillaart
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

libraries/SparseMatrix/README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
[![Arduino CI](https://github.com/RobTillaart/SparseMatrix/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
3+
[![Arduino-lint](https://github.com/RobTillaart/SparseMatrix/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/SparseMatrix/actions/workflows/arduino-lint.yml)
4+
[![JSON check](https://github.com/RobTillaart/SparseMatrix/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/SparseMatrix/actions/workflows/jsoncheck.yml)
5+
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/SparseMatrix/blob/master/LICENSE)
6+
[![GitHub release](https://img.shields.io/github/release/RobTillaart/SparseMatrix.svg?maxAge=3600)](https://github.com/RobTillaart/SparseMatrix/releases)
7+
8+
9+
# SparseMatrix
10+
11+
Arduino library for sparse matrices.
12+
13+
14+
## Description
15+
16+
SparseMatrix is an **experimental** library to implement sparse matrices on an Arduino.
17+
18+
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.
21+
22+
The library does not hold the dimensions of the matrix (at least in 0.1.0)
23+
24+
The purpose of the library is efficient storage in memory.
25+
It does not do math operations except sum().
26+
27+
Relates to https://github.com/RobTillaart/distanceTable
28+
29+
30+
#### Implementation
31+
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.
34+
35+
The elements are not kept sorted or indexed so optimizations are possible
36+
but not investigated yet.
37+
38+
39+
## Interface
40+
41+
- **SparseMatrix(uint8_t size)** constructor.
42+
Parameter is the maximum number of elements in the sparse matrix.
43+
- **uint8_t size()** maximum number of elements.
44+
- **uint8_t count()** current number of elements in the matrix.
45+
- **float sum()** sum of all elements ( > 0 ) in the matrix.
46+
- **bool set(uint8_t x, uint8_t y, float value)** gives an element in the matrix a value.
47+
If the value is set to zero, it is removed from the internal store.
48+
Returns false if the internal store is full, true otherwise.
49+
- **float get(uint8_t x, uint8_t y)** returns the value in the matrix.
50+
51+
52+
## Future
53+
54+
- documentation
55+
- test
56+
- template version to store other data types
57+
- 1, 2, 3 (RGB), 4 byte integer or 8 byte doubles
58+
- struct, complex number
59+
- etc
60+
- add examples
61+
- 2D histogram e.g. temperature vs humidity
62+
- N queens game.
63+
- investigate optimizations.
64+
- should **set()** return the number of free places?
65+
- no hard code and more informative than just a bool.
66+
- add link in distanceTable repo
67+
- uint16_t size for larger platforms.
68+
- max matrix still 255 x 255 but more elements <> 0.
69+
70+
71+
#### new functions
72+
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?
79+
- first -> next; last -> prev.
80+
81+
82+
#### won't
83+
84+
- math
85+
- determinant?
86+
- M x M
87+
- diagonal?
88+
- add examples
89+
- battleship game
90+
- minesweeper game
91+
- nice exercise
92+
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//
2+
// FILE: SparseMatrix.cpp
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// DATE: 2022-07-12
6+
// PURPOSE: Arduino library for sparse matrices
7+
//
8+
// HISTORY:
9+
// 0.1.0 2022-07-12 initial version
10+
11+
12+
#include "SparseMatrix.h"
13+
14+
15+
SparseMatrix::SparseMatrix(uint8_t sz)
16+
{
17+
_count = 0;
18+
_size = sz;
19+
_x = (uint8_t *) malloc(sz);
20+
_y = (uint8_t *) malloc(sz);
21+
_value = (float *) malloc(sz * sizeof(float));
22+
// catch malloc error
23+
if (_x && _y && _value) return;
24+
_size = 0;
25+
}
26+
27+
28+
SparseMatrix::~SparseMatrix()
29+
{
30+
if (_x) free(_x);
31+
if (_y) free(_y);
32+
if (_value) free(_value);
33+
}
34+
35+
36+
uint8_t SparseMatrix::size()
37+
{
38+
return _size;
39+
}
40+
41+
42+
uint8_t SparseMatrix::count()
43+
{
44+
return _count;
45+
}
46+
47+
float SparseMatrix::sum()
48+
{
49+
float _sum = 0;
50+
for (int i = 0; i < _count; i++)
51+
{
52+
_sum += _value[i];
53+
}
54+
return _sum;
55+
}
56+
57+
58+
bool SparseMatrix::set(uint8_t x, uint8_t y, float value)
59+
{
60+
int pos = findPos(x, y);
61+
// existing element
62+
if (pos > -1)
63+
{
64+
if (value != 0.0)
65+
{
66+
_value[pos] = value;
67+
}
68+
else
69+
{
70+
_count--;
71+
// move last element
72+
// efficiency is not a requirement yet.
73+
_x[pos] = _x[_count];
74+
_y[pos] = _y[_count];
75+
_value[pos] = _value[_count];
76+
}
77+
return true;
78+
}
79+
80+
// new element
81+
if (_count >= _size) return false;
82+
_x[_count] = x;
83+
_y[_count] = y;
84+
_value[_count] = value;
85+
_count++;
86+
return true;
87+
}
88+
89+
90+
float SparseMatrix::get(uint8_t x, uint8_t y)
91+
{
92+
int pos = findPos(x, y);
93+
if (pos > -1)
94+
{
95+
return _value[pos];
96+
}
97+
return 0;
98+
}
99+
100+
101+
int SparseMatrix::findPos(uint8_t x, uint8_t y)
102+
{
103+
for (int i = 0; i < _count; i++)
104+
{
105+
if ((_x[i] == x) && (_y[i] == y))
106+
{
107+
return i;
108+
}
109+
}
110+
return -1;
111+
}
112+
113+
114+
115+
// -- END OF FILE --
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
//
3+
// FILE: SparseMatrix.h
4+
// AUTHOR: Rob Tillaart
5+
// VERSION: 0.1.0
6+
// DATE: 2022-07-12
7+
// PURPOSE: Arduino library for sparse matrices
8+
//
9+
10+
11+
#include "Arduino.h"
12+
13+
#define SPARSEMATRIX_LIB_VERSION (F("0.1.0"))
14+
15+
16+
class SparseMatrix
17+
{
18+
public:
19+
SparseMatrix(uint8_t sz);
20+
~SparseMatrix();
21+
22+
uint8_t size();
23+
uint8_t count();
24+
float sum();
25+
26+
// returns false if no slots free
27+
// could return # free slots?
28+
bool set(uint8_t x, uint8_t y, float value);
29+
float get(uint8_t x, uint8_t y);
30+
31+
32+
private:
33+
int _size = 0;
34+
int _count = 0;
35+
36+
uint8_t *_x = NULL;
37+
uint8_t *_y = NULL;
38+
float *_value = NULL;
39+
40+
// returns index of x,y if in set
41+
// otherwise -1
42+
int findPos(uint8_t x, uint8_t y);
43+
};
44+
45+
46+
// -- END OF FILE --
47+

0 commit comments

Comments
 (0)