GY-63_MS5611/libraries/SparseMatrix
2022-08-03 21:56:07 +02:00
..
.github add funding.yml 2022-08-03 21:56:07 +02:00
examples 0.1.3 SparseMatrix 2022-07-16 13:42:58 +02:00
test 0.1.3 SparseMatrix 2022-07-16 13:42:58 +02:00
.arduino-ci.yml 0.1.2 SparseMatrix 2022-07-14 17:28:24 +02:00
keywords.txt 0.1.2 SparseMatrix 2022-07-14 17:28:24 +02:00
library.json 0.1.3 SparseMatrix 2022-07-16 13:42:58 +02:00
library.properties 0.1.3 SparseMatrix 2022-07-16 13:42:58 +02:00
LICENSE 0.1.0 SparseMatrix 2022-07-12 20:20:20 +02:00
README.md 0.1.3 SparseMatrix 2022-07-16 13:42:58 +02:00
SparseMatrix.cpp 0.1.3 SparseMatrix 2022-07-16 13:42:58 +02:00
SparseMatrix.h 0.1.3 SparseMatrix 2022-07-16 13:42:58 +02:00

Arduino CI Arduino-lint JSON check License: MIT GitHub release

SparseMatrix

Arduino library for sparse matrices.

Description

SparseMatrix is an experimental library to implement two dimensional sparse matrices (of floats) on an Arduino. A sparse matrix is a matrix with mostly zeros and a low percentage non-zero values. The purpose of this library is efficient storage in memory.

The maximum matrix that can be represented is 255 x 255 with a theoretical maximum of 65535 non-zero elements. In practice the library limits this to 1000 non-zero elements. Note: 255 elements would still fit in an UNO's 2K memory.

Note: the library does not do matrix math operations.

Note: the library does not hold the dimensions of the matrix and cannot check these.

Relates somewhat to https://github.com/RobTillaart/distanceTable

Implementation

The implementation is based on 3 arrays holding x, y, value where value is float, and x and y are uint8_t. That are 6 bytes per element. The number of elements that the sparse matrix object can hold are given as parameter to the constructor. If the space cannot be allocated the size is set to zero.

In the future other data types should be possible.

Performance

The elements are not kept sorted or indexed so optimizations might be possible but are not investigated yet. There is however a test sketch to monitor the performance of the most important functions.

Accessing elements internally is done with a linear search, which becomes (much) slower if the number of elements is increasing. This means that although in theory there can be 65535 elements, in practice a few 100 can already become annoyingly slow. To keep performance a bit the library has a limit build in. Check the .h file for SPARSEMATRIX_MAX_SIZE 1000

Interface

#include "SparseMatrix.h"

Constructor + meta

  • SparseMatrix(uint16_t size) constructor. Parameter is the maximum number of elements in the sparse matrix. Note this number is limited to SPARSEMATRIX_MAX_SIZE 1000. If the space requested cannot be allocated size will be set to 0.
  • uint16_t size() maximum number of elements. If this is zero, a problem occurred with allocation happened.
  • uint16_t count() current number of elements in the matrix. Should be between 0 and size.
  • float sum() sum of all elements ( > 0 ) in the matrix.
  • void clear() resets the matrix to all zero's again.

Access

  • bool set(uint8_t x, uint8_t y, float value) gives an element in the matrix a value. If the value is set to zero, it is removed from the internal store. Returns false if the internal store is full, true otherwise.
  • float get(uint8_t x, uint8_t y) returns the value in the matrix.
  • bool add(uint8_t x, uint8_t y, float value) adds value to an element in the matrix. If needed a new internal element is created. If the sum is zero, the element is removed from the internal store. Returns false if the internal store is full, true otherwise.
  • void boundingBox(uint8_t &minX, uint8_t &maxX, uint8_t &minY, uint8_t &maxY) Returns the bounding box in which all values != 0 are located. This can be useful for printing or processing the non zero elements.

Future

  • documentation
  • test
  • template version to store other data types
    • 1, 2, 3 (RGB), 4 byte integer or 8 byte doubles
    • struct, complex number
    • etc
  • investigate performance optimizations
    • sort
    • linked list, tree, hashing?

Functions

  • walk through the elements?
    • in storage order.
    • bool first(uint8_t &x, uint8_t &y, float &value);
    • bool next(uint8_t &x, uint8_t &y, float &value);
    • bool prev(uint8_t &x, uint8_t &y, float &value);
    • bool last(uint8_t &x, uint8_t &y, float &value);
    • returns false if there is no element anymore
    • if count = 0 or current == -1 or end of list.

won't

  • should set() and add() return the number of free places?
    • more informative than just a bool.
    • One looses the info that the operation was successful
  • set a zero threshold ?
    • if (abs(x) < TH) element is considered zero => remove
    • not portable to template version (sum() is not either!)
    • user can do this.
  • math
    • determinant?
    • M x M
    • diagonal?
  • add examples
    • N queens game.
    • battleship game
    • minesweeper game