Skip to content

Commit

Permalink
Add a copy argument to MultiVector.__init__
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eric-wieser committed Mar 12, 2020
1 parent 36b5963 commit cc35ea6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
22 changes: 17 additions & 5 deletions clifford/_multivector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
------
Expand All @@ -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
Expand All @@ -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" %
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand Down

0 comments on commit cc35ea6

Please sign in to comment.