Commit f568111d authored by dboe's avatar dboe
Browse files

added nd dot product

parent bf7dcd5c
Pipeline #91248 passed with stages
in 50 seconds
......@@ -46,7 +46,7 @@ packages = find:
install_requires =
six
numpy
sympy
sympy<=1.6.2 # diffgeom changes in 1.7 see unit branch for first implementation of compatibility
scipy
pathlib
rna>=0.6.3
......
......@@ -662,16 +662,6 @@ class Tensors(AbstractNdarray): # pylint: disable=too-many-public-methods
tensors = np.asarray(tensors, dtype=dtype, order=order)
obj = tensors.view(cls)
# check dimension(s)
for obj_dim in obj.shape[1:]:
if not obj_dim == obj.dim:
raise ValueError(
"Dimensions are inconstistent. "
"Manifold dimension is {obj.dim}. "
"Found dimensions {found} in {obj}.".format(
found=obj.shape[1:], **locals()
)
)
if dim_ is not None:
if dim_ != obj.dim:
raise ValueError(
......@@ -1612,6 +1602,38 @@ class Tensors(AbstractNdarray): # pylint: disable=too-many-public-methods
evalfs, evecs = np.linalg.eigh(cov)
return (evecs * evalfs.T).T
@property
def t(self):
"""
Same as self.T but for tensor dimension only. Keeping the order of stacked tensors.
Examples:
>>> import tfields
>>> a = tfields.Tensors([[[1,2,3,4],[5,6,7,8]]])
>>> assert a.t.equal([a[0].T])
"""
return self.transpose(0, *range(self.ndim)[-1:0:-1])
def dot(self, other):
"""
Computes the n-d dot product between self and other defined as in mathematica
(reference.wolfram.com/legacy/v5/Built-inFunctions/AdvancedDocumentation/LinearAlgebra/2.7.html)
by summing over the last dimension.
When self and other are both one-dimensional vectors, this is just the "usual" dot product;
when self and other are 2D matrices, this is matrix multiplication.
Examples:
>>> import tfields
>>> import numpy as np
>>> a = tfields.Tensors([[4, 0, 4]])
>>> b = tfields.Tensors([[10, 0, 0.5]])
>>> c = a.t.dot(b)
>>> assert c.equal([42])
>>> assert c.equal(np.dot(a[0], b[0]))
>>> assert c.rank == 0
"""
return Tensors(np.einsum("t...i,t...i->t...", self, other))
def plot(self, **kwargs):
"""
Forwarding to rna.plotting.plot_array
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment