Skip to content

Commit

Permalink
bump version and lint lblack
Browse files Browse the repository at this point in the history
  • Loading branch information
adtzlr committed Dec 7, 2022
1 parent 683dc75 commit e4778b6
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "tensortrax"
version = "0.0.16"
version = "0.1.0"
description = "Math on (Hyper-Dual) Tensors with Trailing Axes"
readme = "README.md"
requires-python = ">=3.7"
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = tensortrax
version = 0.0.16
version = 0.1.0
author = Andreas Dutzler
author_email = a.dutzler@gmail.com
description = Math on (Hyper-Dual) Tensors with Trailing Axes
Expand Down
16 changes: 8 additions & 8 deletions tensortrax/_evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ def kernel(a, x, δx, Δx, args, kwargs):
func = fun(t, *args, **kwargs)
fx[:] = f(func)
dfdx[a] = δ(func)

if not parallel:
for a in indices:
kernel(a, x, δx, Δx, args, kwargs)

else:
threads = [
Thread(target=kernel, args=(a, x, δx, Δx, args, kwargs))
for a in indices
]

for th in threads:
th.start()

for th in threads:
th.join()

Expand Down Expand Up @@ -81,20 +81,20 @@ def kernel(a, b, x, δx, Δx, args, kwargs):
fx[:] = f(func)
dfdx[a] = δ(func)
d2fdx2[a, b] = d2fdx2[b, a] = Δδ(func)

if not parallel:
for a, b in indices:
kernel(a, b, x, δx, Δx, args, kwargs)

else:
threads = [
Thread(target=kernel, args=(a, b, x, δx, Δx, args, kwargs))
for a, b in indices
]

for th in threads:
th.start()

for th in threads:
th.join()

Expand Down
14 changes: 7 additions & 7 deletions tensortrax/_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __sub__(self, B):
Δx = Δ(A)
Δδx = Δδ(A)
return Tensor(x=x, δx=δx, Δx=Δx, Δδx=Δδx, ntrax=A.ntrax)

def __rsub__(self, B):
return -self.__sub__(B)

Expand Down Expand Up @@ -147,7 +147,7 @@ def T(self):

def __matmul__(self, B):
return matmul(self, B)

def __rmatmul__(self, B):
return matmul(B, self)

Expand All @@ -160,16 +160,16 @@ def einsum2(subscripts, *operands):
"Einsum with two operands."
A, B = operands
_einsum = lambda *operands: np.einsum(subscripts, *operands)

if isinstance(A, Tensor) and isinstance(B, Tensor):
x = _einsum(f(A), f(B))
δx = _einsum(δ(A), f(B)) + _einsum(f(A), δ(B))
Δx = _einsum(Δ(A), f(B)) + _einsum(f(A), Δ(B))
Δδx = (
_einsum(Δδ(A), f(B))
+ _einsum(f(A), Δδ(B))
+ _einsum(δ(A), Δ(B))
+ _einsum(Δ(A), δ(B))
_einsum(Δδ(A), f(B))
+ _einsum(f(A), Δδ(B))
+ _einsum(δ(A), Δ(B))
+ _einsum(Δ(A), δ(B))
)
ntrax = A.ntrax
elif isinstance(A, Tensor) and not isinstance(B, Tensor):
Expand Down
2 changes: 1 addition & 1 deletion tensortrax/math/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
einsum,
matmul,
)
from . import _math_array as array
from . import _math_array as array
2 changes: 1 addition & 1 deletion tensortrax/math/_linalg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"""

from ._linalg_array import det as _det, inv as _inv
from ._linalg_tensor import det, eigvalsh
from ._linalg_tensor import det, eigvalsh
2 changes: 1 addition & 1 deletion tensortrax/math/_math_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def cross(a, b):
"Cross product of two vectors a and b."
return np.einsum(
"...i->i...", np.cross(np.einsum("i...->...i", a), np.einsum("i...->...i", b))
)
)
1 change: 0 additions & 1 deletion tensortrax/math/_math_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,3 @@ def tanh(A):
)
else:
return np.tanh(A)

4 changes: 2 additions & 2 deletions tests/test_hessian.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def test_function_gradient_hessian():
ww = tr.function(fun, ntrax=2, parallel=parallel)(F)
dwdf, w = tr.gradient(fun, ntrax=2, parallel=parallel)(F)
d2WdF2, dWdF, W = tr.hessian(fun, ntrax=2, parallel=parallel)(F)

assert W.shape == (1, 1)
assert dWdF.shape == (3, 3, 1, 1)
assert d2WdF2.shape == (3, 3, 3, 3, 1, 1)

assert np.allclose(w, ww)
assert np.allclose(w, W)
assert np.allclose(dwdf, dWdF)
Expand Down
10 changes: 6 additions & 4 deletions tests/test_hessian_vector_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ def ogden(F, mu=1, alpha=2):
def test_hvp():

F = δF = ΔF = (np.eye(3).ravel() + np.arange(9) / 10).reshape(3, 3, 1, 1)

for parallel in [False, True]:

for fun in [neo_hooke, ogden]:
δfun = tr.gradient_vector_product(fun, ntrax=2, parallel=parallel)(F, δF)
Δδfun = tr.hessian_vector_product(fun, ntrax=2, parallel=parallel)(F, δF, ΔF)

Δδfun = tr.hessian_vector_product(fun, ntrax=2, parallel=parallel)(
F, δF, ΔF
)

assert δfun.shape == (1, 1)
assert Δδfun.shape == (1, 1)

assert not np.any(np.isnan(δfun))
assert not np.any(np.isnan(Δδfun))

Expand Down
36 changes: 19 additions & 17 deletions tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,54 @@


def test_math():

F = np.eye(3) + np.arange(9).reshape(3, 3) / 10
T = tr.Tensor(F)

assert isinstance(T @ F, tr.Tensor)
assert isinstance(F @ T, tr.Tensor)
assert isinstance(T @ T, tr.Tensor)

assert isinstance(T * F, tr.Tensor)
assert isinstance(F * T, tr.Tensor)
assert isinstance(T * T, tr.Tensor)

assert isinstance(T / F, tr.Tensor)
with pytest.raises(TypeError): F / T
with pytest.raises(NotImplementedError): T / T

with pytest.raises(TypeError):
F / T
with pytest.raises(NotImplementedError):
T / T

assert isinstance(T + F, tr.Tensor)
assert isinstance(F + T, tr.Tensor)
assert isinstance(T + T, tr.Tensor)

assert isinstance(T - F, tr.Tensor)
assert isinstance(F - T, tr.Tensor)
assert isinstance(T - T, tr.Tensor)

assert np.allclose((-T).x, -F)

F = np.eye(3) + np.arange(9).reshape(3, 3) / 10
T = tr.Tensor(F)

assert np.allclose(tm.linalg.det(F), tm.linalg.det(T).x)

tm.linalg._det(F[:2, :2])
tm.linalg._det(F[:1, :1])
tm.linalg._inv(F[:2, :2])

for fun in [tm.sin, tm.cos, tm.tan, tm.tanh, tm.sqrt]:
assert np.allclose(fun(F), fun(T).x)

for fun in [tm.linalg.det]:
assert np.allclose(fun(F), fun(T).x)

assert tm.linalg.eigvalsh(T).shape == (3,)

assert tm.array.cross(F, F).shape == F.shape
assert tm.array.eye(F).shape == F.shape

with pytest.raises(NotImplementedError):
tm.einsum("ij...,kl...,mn...->ijklmn...", T, T, T)

Expand Down

0 comments on commit e4778b6

Please sign in to comment.