Coverage for src/foapy/characteristics/_volume.py: 100%
3 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-17 20:45 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-17 20:45 +0000
1import numpy as np
4def volume(intervals, dtype=None):
5 """
6 Calculates average geometric value of intervals lengths.
8 $$ V=\\prod_{i=1}^{n} \\Delta_{i}$$
10 where \\( \\Delta_{i} \\) represents each interval and \\( n \\)
11 is the total number of intervals.
13 Parameters
14 ----------
15 intervals : array_like
16 An array of intervals
17 dtype : dtype, optional
18 The dtype of the output.
20 Returns
21 -------
22 : float
23 The volume of the input array of intervals.
25 Examples
26 --------
28 Calculate the geometric mean of intervals of a sequence.
30 ``` py linenums="1"
31 import foapy
33 source = ['a', 'b', 'a', 'c', 'a', 'd']
34 intervals = foapy.intervals(source, foapy.binding.start, foapy.mode.normal)
35 result = foapy.characteristics.volume(intervals)
36 print(result)
37 # 192
38 ```
40 Improve precision and avoid overflow by specifying a dtype.
42 ``` py linenums="1"
43 import foapy
44 import numpy as np
46 alphabet = np.arange(0, 200)
47 source = np.random.choice(alphabet, 1000)
48 intervals = foapy.intervals(source, foapy.binding.start, foapy.mode.normal)
50 result_A = foapy.characteristics.volume(intervals)
51 result_B = foapy.characteristics.volume(intervals, dtype=np.float128)
52 print(result_A)
53 # 0
54 print(result_B)
55 # 5.0039140361650821106e+1951
56 ```
58 """ # noqa: W605
60 return np.prod(intervals, dtype=dtype)