-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
__all__ = ["abs", "allclose", "exp", "tensordot"] | ||
|
||
|
||
import importlib | ||
|
||
from pymanopt.numerics.core import abs, allclose, exp, tensordot | ||
|
||
|
||
def _register_backends(): | ||
for backend in ["numpy", "jax", "pytorch", "tensorflow"]: | ||
try: | ||
importlib.import_module(f"pymanopt.numerics._backends.{backend}") | ||
except ImportError: | ||
pass | ||
|
||
|
||
_register_backends() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import numpy as np | ||
|
||
import pymanopt.numerics.core as nx | ||
|
||
|
||
@nx.abs.register(np.ndarray) | ||
def _(array): | ||
return np.abs(array) | ||
|
||
|
||
@nx.allclose.register(np.ndarray) | ||
def _(array_a, array_b): | ||
return np.allclose(array_a, array_b) | ||
|
||
|
||
@nx.exp.register(np.ndarray) | ||
def _(array): | ||
return np.abs(array) | ||
|
||
|
||
@nx.tensordot.register(np.ndarray) | ||
def _(array_a, array_b, axes: int): | ||
return np.tensordot(array_a, array_b, axes=axes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import functools | ||
|
||
|
||
def _not_implemented(function): | ||
@functools.wraps(function) | ||
def inner(*arguments): | ||
raise TypeError( | ||
f"Function '{function.__name__}' not implemented for arguments of " | ||
f"type '{type(arguments[0])}'" | ||
) | ||
|
||
return inner | ||
|
||
|
||
@functools.singledispatch | ||
@_not_implemented | ||
def abs(_): | ||
pass | ||
|
||
|
||
@functools.singledispatch | ||
@_not_implemented | ||
def allclose(*_): | ||
pass | ||
|
||
|
||
@functools.singledispatch | ||
@_not_implemented | ||
def exp(_): | ||
pass | ||
|
||
|
||
@functools.singledispatch | ||
@_not_implemented | ||
def tensordot(*_): | ||
pass |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import pymanopt.numerics as nx | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"argument, expected_output", [(np.array([-4, 2]), np.array([4, 2]))] | ||
) | ||
def test_abs(argument, expected_output): | ||
output = nx.abs(argument) | ||
assert nx.allclose(output, expected_output) |