omdl  v0.6.1
OpenSCAD Mechanical Design Library
math_utility.scad
Go to the documentation of this file.
1 //! Miscellaneous mathematical utilities.
2 /***************************************************************************//**
3  \file math_utility.scad
4  \author Roy Allen Sutton
5  \date 2017
6 
7  \copyright
8 
9  This file is part of [omdl] (https://github.com/royasutton/omdl),
10  an OpenSCAD mechanical design library.
11 
12  The \em omdl is free software; you can redistribute it and/or modify
13  it under the terms of the [GNU Lesser General Public License]
14  (http://www.gnu.org/licenses/lgpl.html) as published by the Free
15  Software Foundation; either version 2.1 of the License, or (at
16  your option) any later version.
17 
18  The \em omdl is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with the \em omdl; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26  02110-1301, USA; or see <http://www.gnu.org/licenses/>.
27 
28  \details
29 
30  \ingroup math math_utility
31 *******************************************************************************/
32 
33 include <math_bitwise.scad>;
34 
35 //----------------------------------------------------------------------------//
36 /***************************************************************************//**
37  \addtogroup math
38  @{
39 
40  \defgroup math_utility Utilities
41  \brief Miscellaneous mathematical utilities.
42  @{
43 *******************************************************************************/
44 //----------------------------------------------------------------------------//
45 
46 //----------------------------------------------------------------------------//
47 // statistical / informational
48 //----------------------------------------------------------------------------//
49 
50 //! Generate a histogram for the elements of a list of values.
51 /***************************************************************************//**
52  \param v <data> A list of values.
53  \param m <integer> The output mode (a 5-bit encoded integer).
54 
55  \param cs <string-list-4> A list of strings [s1, s2, s3, fs]
56  (for custom field formatting).
57 
58  \returns <list|string> with the occurrence frequency of the elements
59  of \p v.
60 
61  \details
62 
63  The custom formatting strings are inserted in the output stream as
64  follows:
65 
66  \verbatim
67  s1, value, s2, value-frequency, s3, fs
68  \endverbatim
69 
70  See lstr_html() for description of the html formatting parameters
71  \p cb, \p cp, \p ca, \p cf, and \p d.
72 
73  Output mode selection:
74 
75  | bit | Description | 0 | 1 |
76  |:---:|:------------------------|:------------|:------------|
77  | 0 | output mode | numerical | string |
78  | 1-3 | string mode format | see table | see table |
79  | 4 | field separator mode | not at end | all |
80 
81  String output modes:
82 
83  | | B3 | B2 | B1 | B0 | Description |
84  |:---:|:---:|:---:|:---:|:---:|:------------------|
85  | 1 | 0 | 0 | 0 | 1 | list of strings |
86  | 3 | 0 | 0 | 1 | 1 | text format 1 |
87  | 9 | 1 | 0 | 0 | 1 | html format 1 |
88  | 15 | 1 | 1 | 1 | 1 | custom formating |
89 
90 *******************************************************************************/
91 function hist
92 (
93  v,
94  m = 0,
95  cs,
96  cb,
97  cp,
98  ca,
99  cf,
100  d = false
101 ) = let
102  (
103  hv = [for (i=unique(v)) [i, len(find(i, v, 0))]]
104  )
105  bitwise_is_equal(m, 0, 0) ? hv
106  : let
107  (
108  sm = bitwise_imi(m, 3, 1),
109  fm = bitwise_is_equal(m, 4, 1) ? true : false
110  )
111  (sm == 0) ? [for (i=hv) str(first(i), "x", second(i))]
112  : let
113  (
114  s1 = (sm==1) ? empty_str
115  : (sm==4) ? empty_str
116  : (len(cs) > 0 ? cs[0] : empty_str),
117  s2 = (sm==1) ? "<"
118  : (sm==4) ? "x"
119  : (len(cs) > 1 ? cs[1] : empty_str),
120  s3 = (sm==1) ? ">"
121  : (sm==4) ? empty_str
122  : (len(cs) > 2 ? cs[2] : empty_str),
123  fs = (sm==1) ? " "
124  : (sm==4) ? ", "
125  : (len(cs) > 3 ? cs[3] : empty_str),
126  fb = (sm==1) ? undef
127  : (sm==4) ? undef
128  : cb,
129  fp = (sm==1) ? undef
130  : (sm==4) ? [undef, undef, ["i", "sup"], ["b", "sup"], undef]
131  : cp,
132  fa = (sm==1) ? undef
133  : (sm==4) ? [undef, undef, undef, undef, undef, "br"]
134  : ca,
135  ff = (sm==1) ? undef
136  : (sm==4) ? undef
137  : cf
138  )
139  lstr
140  ([
141  for (i=[0:len(hv)-1])
142  lstr_html
143  (
144  [s1, first(hv[i]), s2, second(hv[i]), s3, if ((i<(len(hv)-1)) || fm) fs],
145  b=fb, p=fp, a=fa, f=ff, d=d
146  ),
147  ]);
148 
149 //! @}
150 //! @}
151 
152 //----------------------------------------------------------------------------//
153 // end of file
154 //----------------------------------------------------------------------------//
function second(v)
Return the second element of an iterable value.
function unique(v)
Return the unique elements of an iterable value.
function lstr(v)
Convert a list of values to a concatenated string.
function first(v)
Return the first element of an iterable value.
function hist(v, m=0, cs, cb, cp, ca, cf, d=false)
Generate a histogram for the elements of a list of values.
function bitwise_is_equal(v, b, t=1)
Test if a base-two bit position of an integer value equals a test bit.
function bitwise_imi(v, w, s)
Decode the integer in a value at a shifted base-two bit mask of width-w.
function lstr_html(v, b, p, a, f, d=false)
Convert a list of values to a concatenated HTML-formatted string.
function find(mv, v, c=1, i, i1=0, i2)
Find the occurrences of a match value in an iterable value.