From cc35ea6fb4264b7deceb2a1839cd5cbd42b49530 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Thu, 12 Mar 2020 13:30:18 +0000 Subject: [PATCH] Add a copy argument to `MultiVector.__init__` The behavior of `MultiVector(layout, some_array)` has always been to copy `some_array`. This change makes that copy opt-out, via `MultiVector(layout, some_array, copy=False)`. This is used internally by all of the arithmetic operators to avoid unnecessary copies. --- clifford/_multivector.py | 22 +++++++++++++++++----- docs/changelog.rst | 2 ++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/clifford/_multivector.py b/clifford/_multivector.py index 506cb680..3da69d49 100644 --- a/clifford/_multivector.py +++ b/clifford/_multivector.py @@ -15,10 +15,22 @@ class MultiVector(object): Parameters ------------- layout: instance of :class:`clifford.Layout` - the layout of the algebra + The layout of the algebra value : sequence of length ``layout.gaDims`` - the coefficients of the base blades + The coefficients of the base blades + + dtype : numpy.dtype + The datatype to use for the multivector, if no + value was passed. + + .. versionadded:: 1.1.0 + + copy : bool + If True, the default, create a copy of `value` to store in this + multivector. + + .. versionadded:: 1.3.0 Notes ------ @@ -33,7 +45,7 @@ class MultiVector(object): * ``M[N]`` : blade projection """ - def __init__(self, layout, value=None, string=None, *, dtype: np.dtype = np.float64) -> None: + def __init__(self, layout, value=None, string=None, *, dtype: np.dtype = np.float64, copy: bool = True) -> None: """Constructor.""" self.layout = layout @@ -45,7 +57,7 @@ def __init__(self, layout, value=None, string=None, *, dtype: np.dtype = np.floa else: self.value = layout.parse_multivector(string).value else: - self.value = np.array(value) + self.value = np.array(value, copy=copy) if self.value.shape != (self.layout.gaDims,): raise ValueError( "value must be a sequence of length %s" % @@ -85,7 +97,7 @@ def _newMV(self, newValue=None, *, dtype: np.dtype = None) -> 'MultiVector': if newValue is None and dtype is None: raise TypeError("Must specify either a type or value") - return self.__class__(self.layout, newValue, dtype=dtype) + return self.__class__(self.layout, newValue, dtype=dtype, copy=False) # numeric special methods # binary diff --git a/docs/changelog.rst b/docs/changelog.rst index 7da11288..7c61b442 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -18,6 +18,8 @@ Changes in 1.3.x * Faster algebra construction. ``Cl(3)`` is now 100\ |times| faster, and ``Cl(6)`` is 20\ |times| faster. This is achieved by deferring product JIT compilation until the product is used for the first time. + * The :class:`MultiVector` constructor gained a ``copy`` argument, which can be + set to ``False`` to allow construction without copying. Bugs fixed ----------