-
Notifications
You must be signed in to change notification settings - Fork 0
/
accumulated_distance.py
executable file
·75 lines (59 loc) · 2.13 KB
/
accumulated_distance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# Software License Agreement (GNU GPLv3 License)
#
# Copyright (c) 2020, Roland Jung (roland.jung@aau.at) , AAU, KPK, NAV
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Requirements:
# numpy
########################################################################################################################
import numpy as np
def accumulated_distance(p_vec):
""""
sums up elements of
* [1xM] vector resulting in a [1xM-1] vector.
* [MxN] matrix resulting a [M-1xN] matrix
Example:
>> v = np.array([0, 1, 2, 3, 4, 5])
>> a = accumulated_distance(p_vec=v) # [1 2 3 4 5]
Input:
p_vec -- numpy.ndarray, [1xM] vector or [MxN] matrix
Output:
res -- numpy.ndarray, [1xM-1] vector or ,[M-1xN] matrix
"""
if isinstance(p_vec, np.ndarray): # and p_vec.ndim == 1:
motion_vec = np.diff(p_vec, n=1, axis=0)
return np.cumsum(np.abs(motion_vec), axis=0)
else:
raise ValueError('Type error: np.ndarray expected')
def total_distance(p_vec):
""""
sums up absolute traversed distance
* [1xM] vector
* [MxN] matrix
Example:
>> v = np.array([0, 1, 2, 3, 4, 5, -5])
>> a = total_distance(p_vec=v) # [15]
Input:
p_vec -- numpy.ndarray, [1xM] vector or [MxN] matrix
Output:
res -- scalar
"""
if isinstance(p_vec, np.ndarray):
motion_vec = np.diff(p_vec, n=1, axis=0)
dist_vec = np.sum(np.abs(motion_vec), axis=0)
return np.linalg.norm(dist_vec)
else:
raise ValueError('Type error: np.ndarray expected')