Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vdot implementation #842

Merged
merged 16 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- [#846](https://github.com/helmholtz-analytics/heat/pull/846) Fixed an issue in `min`, `max` where DNDarrays with empty processes can't be computed.

## Feature Additions
### Linear Algebra
- [#842](https://github.com/helmholtz-analytics/heat/pull/842) New feature: `vdot`

### DNDarray
- [#856](https://github.com/helmholtz-analytics/heat/pull/856) New `DNDarray` method `__torch_proxy__`
Expand Down
37 changes: 37 additions & 0 deletions heat/core/linalg/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"transpose",
"tril",
"triu",
"vdot",
"vecdot",
"vector_norm",
]
Expand Down Expand Up @@ -1917,6 +1918,42 @@ def triu(m: DNDarray, k: int = 0) -> DNDarray:
DNDarray.triu.__doc__ = triu.__doc__


def vdot(x1: DNDarray, x2: DNDarray) -> DNDarray:
"""
Computes the dot product of two vectors. Higher-dimensional arrays will be flattened.

Parameters
----------
x1 : DNDarray
first input array. If it's complex, it's complex conjugate will be used.
x2 : DNDarray
second input array.
coquelin77 marked this conversation as resolved.
Show resolved Hide resolved

Raises
------
ValueError
If the number of elements is inconsistent.

See Also
--------
dot
Return the dot product without using the complex conjugate.

Examples
--------
>>> a = ht.array([1+1j, 2+2j])
>>> b = ht.array([1+2j, 3+4j])
>>> ht.vdot(a,b)
DNDarray([(17+3j)], dtype=ht.complex64, device=cpu:0, split=None)
>>> ht.vdot(b,a)
DNDarray([(17-3j)], dtype=ht.complex64, device=cpu:0, split=None)
"""
x1 = manipulations.flatten(x1)
x2 = manipulations.flatten(x2)

return arithmetics.sum(arithmetics.multiply(complex_math.conjugate(x1), x2))


def vecdot(
x1: DNDarray, x2: DNDarray, axis: Optional[int] = None, keepdim: Optional[bool] = None
) -> DNDarray:
Expand Down
15 changes: 15 additions & 0 deletions heat/core/linalg/tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,21 @@ def test_triu(self):
if result.comm.rank == result.shape[0] - 1:
self.assertTrue(result.larray[0, -1] == 1)

def test_vdot(self):
a = ht.array([[1 + 1j, 2 + 2j], [3 + 3j, 4 + 4j]], split=0)
b = ht.array([[1 + 2j, 3 + 4j], [5 + 6j, 7 + 8j]], split=0)

vdot = ht.vdot(a, b)
self.assertEqual(vdot.dtype, a.dtype)
self.assertEqual(vdot.split, None)
self.assertTrue(ht.equal(vdot, ht.array([110 + 10j])))

vdot = ht.vdot(b, a)
self.assertTrue(ht.equal(vdot, ht.array([110 - 10j])))

with self.assertRaises(ValueError):
ht.vdot(ht.array([1, 2, 3]), ht.array([[1, 2], [3, 4]]))

def test_vecdot(self):
a = ht.array([1, 1, 1])
b = ht.array([1, 2, 3])
Expand Down
2 changes: 2 additions & 0 deletions heat/core/manipulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,8 @@ def flatten(a: DNDarray) -> DNDarray:
>>> ht.flatten(a)
DNDarray([1, 2, 3, 4, 5, 6, 7, 8], dtype=ht.int64, device=cpu:0, split=None)
"""
sanitation.sanitize_in(a)

if a.split is None:
return factories.array(
torch.flatten(a.larray), dtype=a.dtype, is_split=None, device=a.device, comm=a.comm
Expand Down