Skip to content

Commit a7a68e7

Browse files
committed
0.3.4 DistanceTable
1 parent 29f6bfa commit a7a68e7

File tree

13 files changed

+397
-51
lines changed

13 files changed

+397
-51
lines changed

libraries/DistanceTable/CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.3.4] - 2025-03-06
10+
- add sumOfColumn(), minimumOfColumn(), maximumOfColumn() and averageOfColumn()
11+
- add geometricMedian(), minColumn(), maxColumn()
12+
- update readme.md
13+
- update unit test
14+
- minor edits
15+
916
## [0.3.3] - 2023-09-25
1017
- update changelog
1118
- update readme.md
1219

13-
1420
## [0.3.2] - 2022-11.02
1521
- add changelog.md
1622
- add rp2040 to build-CI

libraries/DistanceTable/DistanceTable.cpp

Lines changed: 127 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: DistanceTable.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.3.3
4+
// VERSION: 0.3.4
55
// PURPOSE: Arduino library to store a symmetrical distance table in less memory
66
// URL: https://github.com/RobTillaart/DistanceTable
77

@@ -68,7 +68,7 @@ bool DistanceTable::set(uint8_t x, uint8_t y, float value )
6868
};
6969

7070

71-
float DistanceTable::get (uint8_t x, uint8_t y)
71+
float DistanceTable::get(uint8_t x, uint8_t y)
7272
{
7373
// comment next line to skip range check (squeeze performance)
7474
if ( (x >= _dimension) || (y >= _dimension)) return -1; // NAN ?
@@ -88,23 +88,11 @@ float DistanceTable::get (uint8_t x, uint8_t y)
8888
};
8989

9090

91-
// triangular dump
92-
void DistanceTable::dump(Print * stream)
93-
{
94-
stream->println();
95-
for (uint8_t i = 0; i < _dimension; i++)
96-
{
97-
for (uint8_t j = 0; j <_dimension; j++)
98-
{
99-
stream->print(get(i, j));
100-
stream->print("\t");
101-
}
102-
stream->println();
103-
}
104-
stream->println();
105-
};
106-
10791

92+
/////////////////////////////////////////////////////
93+
//
94+
// STATISTICS
95+
//
10896
float DistanceTable::minimum(uint8_t &x, uint8_t &y)
10997
{
11098
float mi = _distanceTable[0];
@@ -188,7 +176,106 @@ float DistanceTable::average()
188176
}
189177

190178

179+
/////////////////////////////////////////////////////
180+
//
181+
// COLUMN FUNCTIONS
182+
//
183+
float DistanceTable::sumOfColumn(uint8_t x)
184+
{
185+
float sum = 0;
186+
for (int y = 0; y < _dimension; y++)
187+
{
188+
sum += get(x, y);
189+
}
190+
return sum;
191+
}
192+
193+
float DistanceTable::averageOfColumn(uint8_t x, bool skipSelf)
194+
{
195+
if (skipSelf) return sumOfColumn(x) / (_dimension - 1);
196+
return sumOfColumn(x) / _dimension;
197+
}
198+
199+
float DistanceTable::minimumOfColumn(uint8_t x, bool skipSelf)
200+
{
201+
float minimum = get(x, 0);
202+
for (uint8_t y = 1; y < _dimension; y++)
203+
{
204+
if ((x == y) && skipSelf) continue;
205+
float value = get(x,y);
206+
if (value < minimum)
207+
{
208+
minimum = value;
209+
}
210+
}
211+
return minimum;
212+
}
213+
214+
215+
float DistanceTable::maximumOfColumn(uint8_t x, bool skipSelf)
216+
{
217+
float maximum = get(x, 0);
218+
for (uint8_t y = 1; y < _dimension; y++)
219+
{
220+
if ((x == y) && skipSelf) continue;
221+
float value = get(x,y);
222+
if (value > maximum)
223+
{
224+
maximum = value;
225+
}
226+
}
227+
return maximum;
228+
}
229+
230+
231+
/////////////////////////////////////////////////////
232+
//
233+
// GEOMETRIC MEDIAN - EXPERIMENTAL
234+
//
235+
float DistanceTable::geometricMedian(uint8_t &x)
236+
{
237+
return minColumn(x);
238+
}
239+
240+
float DistanceTable::minColumn(uint8_t &x)
241+
{
242+
x = 0;
243+
float minSum = sumOfColumn(x);
244+
245+
for (uint8_t _x = 0; _x < _dimension; _x++)
246+
{
247+
float sum = sumOfColumn(_x);
248+
if (sum < minSum)
249+
{
250+
x = _x;
251+
minSum = sum;
252+
}
253+
}
254+
return minSum;
255+
}
256+
257+
float DistanceTable::maxColumn(uint8_t &x)
258+
{
259+
x = 0;
260+
float maxSum = sumOfColumn(x);
261+
262+
for (uint8_t _x = 0; _x < _dimension; _x++)
263+
{
264+
float sum = sumOfColumn(_x);
265+
if (sum > maxSum)
266+
{
267+
x = _x;
268+
maxSum = sum;
269+
}
270+
}
271+
return maxSum;
272+
}
273+
191274

275+
/////////////////////////////////////////////////////
276+
//
277+
// COUNT
278+
//
192279
uint16_t DistanceTable::count(float value, float epsilon)
193280
{
194281
uint16_t cnt = 0;
@@ -240,5 +327,27 @@ uint16_t DistanceTable::countBelow(float value)
240327
}
241328

242329

330+
/////////////////////////////////////////////////////
331+
//
332+
// DEBUG
333+
//
334+
335+
// triangular dump
336+
void DistanceTable::dump(Print * stream)
337+
{
338+
stream->println();
339+
for (uint8_t i = 0; i < _dimension; i++)
340+
{
341+
for (uint8_t j = 0; j <_dimension; j++)
342+
{
343+
stream->print(get(i, j));
344+
stream->print("\t");
345+
}
346+
stream->println();
347+
}
348+
stream->println();
349+
};
350+
351+
243352
// --- END OF FILE ---
244353

libraries/DistanceTable/DistanceTable.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: DistanceTable.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.3.3
5+
// VERSION: 0.3.4
66
// PURPOSE: Arduino library to store a symmetrical distance table in less memory
77
// URL: https://github.com/RobTillaart/DistanceTable
88
//
@@ -11,7 +11,7 @@
1111
#include "Arduino.h"
1212

1313

14-
#define DISTANCETABLE_LIB_VERSION (F("0.3.3"))
14+
#define DISTANCETABLE_LIB_VERSION (F("0.3.4"))
1515

1616

1717
class DistanceTable
@@ -38,6 +38,24 @@ class DistanceTable
3838
float average();
3939

4040

41+
// column functions
42+
float sumOfColumn(uint8_t x);
43+
float averageOfColumn(uint8_t x, bool skipSelf = false);
44+
float minimumOfColumn(uint8_t x, bool skipSelf = false);
45+
float maximumOfColumn(uint8_t x, bool skipSelf = false);
46+
47+
48+
// GeoMetric Median - EXPERIMENTAL
49+
// see Wikipedia.
50+
// this is a sort of GM search, there can be more than one
51+
// returns the first occurrence of the minimum column.
52+
// this is a point "closest" to all other points.
53+
// these three functions sweep over the whole table.
54+
float geometricMedian(uint8_t &x);
55+
float minColumn(uint8_t &x); // is same as geometricMedian()
56+
float maxColumn(uint8_t &x); // furthest away on average.
57+
58+
4159
// epsilon allows 'almost equal' searches
4260
// if (invert == false) count counts both (x,y) and (y,x) => always even nr.
4361
// if (invert == true) count counts (x,y) and (y,x) separately.

libraries/DistanceTable/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2015-2024 Rob Tillaart
3+
Copyright (c) 2015-2025 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

libraries/DistanceTable/examples/distanceTable/distanceTable.ino

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ uint32_t stop;
3333

3434
void setup()
3535
{
36+
while(!Serial);
3637
Serial.begin(115200);
37-
Serial.print("DistanceTable: ");
38+
Serial.println(__FILE__);
39+
Serial.print("DISTANCETABLE_LIB_VERSION: ");
3840
Serial.println(DISTANCETABLE_LIB_VERSION);
41+
Serial.println();
42+
3943
Serial.println("DistanceTable test 20x20: ");
4044

4145
Serial.print("clear:\t");
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//
2+
// FILE: distanceTable_column_functions.ino
3+
// AUTHOR: Rob Tillaart
4+
// PURPOSE: demo
5+
// DATE: 2015-06-18
6+
// URL: https://github.com/RobTillaart/DistanceTable
7+
//
8+
9+
10+
#include "DistanceTable.h"
11+
12+
DistanceTable dt(20);
13+
14+
15+
void setup()
16+
{
17+
while (!Serial);
18+
Serial.begin(115200);
19+
Serial.println(__FILE__);
20+
Serial.print("DISTANCETABLE_LIB_VERSION: ");
21+
Serial.println(DISTANCETABLE_LIB_VERSION);
22+
Serial.println();
23+
24+
Serial.println("DistanceTable test 20x20: ");
25+
Serial.println("\n========================================\n");
26+
Serial.print("Size:\t\t");
27+
Serial.println(dt.dimension() * dt.dimension());
28+
Serial.print("elements:\t");
29+
Serial.println(dt.elements());
30+
Serial.print("memoryUsed:\t");
31+
Serial.println(dt.memoryUsed());
32+
Serial.println("\n========================================\n");
33+
34+
dt.clear();
35+
for (int i = 0; i < 20; i++)
36+
{
37+
for (int j = 0; j < 20; j++)
38+
{
39+
dt.set(i, j, random(1000) * 0.01);
40+
}
41+
}
42+
43+
dt.dump();
44+
45+
for (int i = 0; i < 20; i++)
46+
{
47+
Serial.print(dt.sumOfColumn(i));
48+
Serial.print("\t");
49+
}
50+
Serial.println();
51+
for (int i = 0; i < 20; i++)
52+
{
53+
Serial.print(dt.averageOfColumn(i));
54+
Serial.print("\t");
55+
}
56+
Serial.println();
57+
Serial.println();
58+
59+
uint8_t a = 0;
60+
float v1 = dt.minColumn(a);
61+
Serial.print("minColumn:\t");
62+
Serial.print(a);
63+
Serial.print("\t");
64+
Serial.println(v1);
65+
delay(100);
66+
67+
float v2 = dt.maxColumn(a);
68+
Serial.print("maxColumn:\t");
69+
Serial.print(a);
70+
Serial.print("\t");
71+
Serial.println(v2);
72+
delay(100);
73+
74+
Serial.print("Ratio:\t\t\t");
75+
Serial.println(v1 / v2);
76+
Serial.println();
77+
delay(100);
78+
79+
Serial.println();
80+
Serial.print("sumOfColumn(5):\t");
81+
Serial.println(dt.sumOfColumn(5));
82+
Serial.print("averageOfColumn(5):\t");
83+
Serial.println(dt.averageOfColumn(5));
84+
Serial.print("minimumOfColumn(5):\t");
85+
Serial.println(dt.minimumOfColumn(5));
86+
Serial.print("maximumOfColumn(5):\t");
87+
Serial.println(dt.maximumOfColumn(5));
88+
delay(100);
89+
90+
Serial.println("\ndone...");
91+
}
92+
93+
94+
void loop()
95+
{
96+
}
97+
98+
99+
// -- END OF FILE --

libraries/DistanceTable/examples/distanceTable_pascal/distanceTable_pascal.ino

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ DistanceTable dt(MAXSIZE);
1919

2020
void setup()
2121
{
22+
while(!Serial);
2223
Serial.begin(115200);
23-
Serial.print("\nDistanceTable: ");
24+
Serial.println(__FILE__);
25+
Serial.print("DISTANCETABLE_LIB_VERSION: ");
2426
Serial.println(DISTANCETABLE_LIB_VERSION);
27+
Serial.println();
28+
2529
Serial.println("Pascals triangle");
2630

2731
dt.clear();

libraries/DistanceTable/examples/distanceTable_test/distanceTable_test.ino

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ uint32_t stop;
1818

1919
void setup()
2020
{
21+
while(!Serial);
2122
Serial.begin(115200);
22-
Serial.print("DistanceTable: ");
23+
Serial.println(__FILE__);
24+
Serial.print("DISTANCETABLE_LIB_VERSION: ");
2325
Serial.println(DISTANCETABLE_LIB_VERSION);
24-
Serial.println("DistanceTable test 20x20: ");
26+
Serial.println();
2527

28+
Serial.println("DistanceTable test 20x20: ");
2629
Serial.println("\n========================================\n");
2730
Serial.println(dt.elements());
2831
Serial.println(dt.memoryUsed());

0 commit comments

Comments
 (0)