amino  1.0-beta2
Lightweight Robot Utility Library
mixin.py
1 # Copyright (c) 2019, Colorado School of Mines
2 # All rights reserved.
3 #
4 # Author(s): Neil T. Dantam <ndantam@mines.edu>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 #
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 # CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
23 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25 # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28 # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 # SUCH DAMAGE.
31 
32 # Object must provide .ssd(other), .nrm2(), .__setitem__()
33 """Helper mixins."""
34 
35 
36 class SSDEqMixin:
37  """Equality test mixin using sum-square-differences."""
38 
39  def __eq__(self, other):
40  return self.ssd(other) == 0
41 
42  def __ne__(self, other):
43  return self.ssd(other) != 0
44 
45  def isclose(self, other, rel_tol=1e-09, abs_tol=0.0):
46  """Returns true if object is close to other."""
47  a = self
48  b = other
49  na = a.nrm2()
50  nb = b.nrm2()
51  d = a.ssd(b)
52  return d <= max(rel_tol * max(na, nb), abs_tol)
53 
54 
55 class CopyEltsMixin:
56  """Copy Elements mixin."""
57 
58  def copy_from(self, src):
59  """Copy elements from src to self"""
60  n = len(self)
61  if n != len(src):
62  raise IndexError()
63  for i in range(0, n):
64  self[i] = src[i]
65  return self
66 
67  def copy_to(self, dst):
68  """Copy elements from self to dst"""
69  n = len(self)
70  if n != len(dst):
71  raise IndexError()
72  for i in range(0, n):
73  dst[i] = self[i]
74  return self
75 
76 
77 class DivCompatMixin:
78  """Mixin for compatibility division operator."""
79  def __div__(self, other):
80  return self.__truediv__(other)
81 
82  def __idiv__(self, other):
83  """Divide self by a scalar"""
84  return self.__itruediv__(other)
85 
86 
88  """Mixin for vector-like objects."""
89 
90  def __radd__(self, other):
91  return self + other
92 
93 
94 class MatMixin:
95  """Mixin for matrix-like objects."""
96  def _str_helper(self, name, m=None, n=None):
97  if m is None:
98  m = self.rows
99  if n is None:
100  n = self.cols
101  s = "%s([" % name
102  spaces = " "*len(s)
103  newrow = ",\n%s[" % spaces
104  for i in range(0, m):
105  if i == 0:
106  s += "["
107  else:
108  s += newrow
109  for j in range(0, n):
110  if j == 0:
111  s += "%f" % self[i, j]
112  else:
113  s += ", %f" % self[i, j]
114  s += "]"
115  s += "])"
116  return s
Copy Elements mixin.
Definition: mixin.py:56
def copy_from(self, src)
Copy elements from src to self.
Definition: mixin.py:59
def copy_to(self, dst)
Copy elements from self to dst.
Definition: mixin.py:68
Mixin for compatibility division operator.
Definition: mixin.py:78
def __idiv__(self, other)
Divide self by a scala.
Definition: mixin.py:83
Mixin for matrix-like objects.
Definition: mixin.py:95
Helper mixins.
Definition: mixin.py:37
def isclose(self, other, rel_tol=1e-09, abs_tol=0.0)
Returns true if object is close to other.
Definition: mixin.py:46
Mixin for vector-like objects.
Definition: mixin.py:88