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

1import numpy as np 

2 

3 

4def volume(intervals, dtype=None): 

5 """ 

6 Calculates average geometric value of intervals lengths. 

7 

8 $$ V=\\prod_{i=1}^{n} \\Delta_{i}$$ 

9 

10 where \\( \\Delta_{i} \\) represents each interval and \\( n \\) 

11 is the total number of intervals. 

12 

13 Parameters 

14 ---------- 

15 intervals : array_like 

16 An array of intervals 

17 dtype : dtype, optional 

18 The dtype of the output. 

19 

20 Returns 

21 ------- 

22 : float 

23 The volume of the input array of intervals. 

24 

25 Examples 

26 -------- 

27 

28 Calculate the geometric mean of intervals of a sequence. 

29 

30 ``` py linenums="1" 

31 import foapy 

32 

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 ``` 

39 

40 Improve precision and avoid overflow by specifying a dtype. 

41 

42 ``` py linenums="1" 

43 import foapy 

44 import numpy as np 

45 

46 alphabet = np.arange(0, 200) 

47 source = np.random.choice(alphabet, 1000) 

48 intervals = foapy.intervals(source, foapy.binding.start, foapy.mode.normal) 

49 

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 ``` 

57 

58 """ # noqa: W605 

59 

60 return np.prod(intervals, dtype=dtype)