Skip to content

Commit

Permalink
Successfull setup of solvers
Browse files Browse the repository at this point in the history
  • Loading branch information
charnley committed Feb 3, 2024
1 parent 53c1e65 commit d7d92e0
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 31 deletions.
23 changes: 17 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
python=python
python=./env/bin/python
mamba=mamba
pkg=qmllib
pip=./env/bin/pip

all: env
all: env setup

env:
${mamba} env create -f ./environment_dev.yaml -p ./env --quiet
${pip} install -e .

setup:
pre-commit install
setup: ./.git/hooks/pre-commit

./.git/hooks/pre-commit:
${python} -m pre_commit install

format:
pre-commit run --all-files
${python} pre_commit run --all-files

test:
${python} -m pytest -rs tests
${python} -m pytest -rs ./tests/test_solvers.py

types:
# ${python} -m monkeytype run $(which pytest) ./tests/test_solvers.py
${python} -m monkeytype list-modules | grep ${pkg} | parallel -j1 "${python} -m monkeytype apply {}"

cov:
${python} -m pytest -vrs --cov=${pkg} --cov-report html tests

clean:
rm -rf ./env/
find ./src/ | grep -E "\(/__pycache__$$|\.pyc$$|\.pyo$$\)" | xargs rm -rf
rm ./.git/hooks/pre-commit
4 changes: 1 addition & 3 deletions environment_dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ channels:
- conda-forge
- defaults
dependencies:
- python==3.11
- jupytext
- monkeytype
- numpy
- openbabel
- pandas
- pip
- pre-commit
- pytest
- scikit-learn
- scipy
- pip:
- pybel
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ authors = []
requires-python = ">=3.10"

[tool.setuptools.dynamic]
version = {attr = "qmllib.__version__"}
version = {attr = "qmllib.version.__version__"}

[options.packages.find]
where="src"
2 changes: 1 addition & 1 deletion src/qmllib/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from qmllib.__version__ import VERSION as __version__
from qmllib.version import __version__
1 change: 0 additions & 1 deletion src/qmllib/__version__.py

This file was deleted.

1 change: 0 additions & 1 deletion src/qmllib/math/__init__.py

This file was deleted.

2 changes: 2 additions & 0 deletions src/qmllib/solvers/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
f2py -L/usr/lib/ -lblas -llapack -c ./fsolvers.f90 -m fsolvers
25 changes: 13 additions & 12 deletions src/qmllib/math/math.py → src/qmllib/solvers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
from numpy import ndarray

from .fsolvers import (
fbkf_invert,
Expand All @@ -12,7 +13,7 @@
)


def cho_invert(A):
def cho_invert(A: ndarray) -> ndarray:
"""Returns the inverse of a positive definite matrix, using a Cholesky decomposition
via calls to LAPACK dpotrf and dpotri in the F2PY module.
Expand All @@ -26,20 +27,20 @@ def cho_invert(A):
if len(A.shape) != 2 or A.shape[0] != A.shape[1]:
raise ValueError("expected square matrix")

I = np.asfortranarray(A)
matrix = np.asfortranarray(A)

fcho_invert(I)
fcho_invert(matrix)

# Matrix to store the inverse
i_lower = np.tril_indices_from(A)

# Copy lower triangle to upper
I.T[i_lower] = I[i_lower]
matrix.T[i_lower] = matrix[i_lower]

return I
return matrix


def cho_solve(A, y, l2reg=0.0, destructive=False):
def cho_solve(A: ndarray, y: ndarray, l2reg: float = 0.0, destructive: bool = False) -> ndarray:
"""Solves the equation
:math:`A x = y`
Expand Down Expand Up @@ -90,7 +91,7 @@ def cho_solve(A, y, l2reg=0.0, destructive=False):
return x


def bkf_invert(A):
def bkf_invert(A: ndarray) -> ndarray:
"""Returns the inverse of a positive definite matrix, using a Bausch-Kauffman decomposition
via calls to LAPACK dpotrf and dpotri in the F2PY module.
Expand All @@ -104,20 +105,20 @@ def bkf_invert(A):
if len(A.shape) != 2 or A.shape[0] != A.shape[1]:
raise ValueError("expected square matrix")

I = np.asfortranarray(A)
matrix = np.asfortranarray(A)

fbkf_invert(I)
fbkf_invert(matrix)

# Matrix to store the inverse
i_lower = np.tril_indices_from(A)

# Copy lower triangle to upper
I.T[i_lower] = I[i_lower]
matrix.T[i_lower] = matrix[i_lower]

return I
return matrix


def bkf_solve(A, y):
def bkf_solve(A: ndarray, y: ndarray) -> ndarray:
"""Solves the equation
:math:`A x = y`
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/qmllib/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "1.0.0"
11 changes: 5 additions & 6 deletions tests/test_math.py → tests/test_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import numpy as np

import qmllib
import qmllib.math
from qmllib.solvers import bkf_invert, bkf_solve, cho_invert, cho_solve


def test_cho_solve():
Expand All @@ -16,7 +15,7 @@ def test_cho_solve():

A = deepcopy(A_ref)
y = deepcopy(y_ref)
x_qml = qmllib.math.cho_solve(A, y)
x_qml = cho_solve(A, y)

# Check arrays are unchanged
assert np.allclose(y, y_ref)
Expand All @@ -36,7 +35,7 @@ def test_cho_invert():
A_ref = np.loadtxt(test_dir + "/data/K_local_gaussian.txt")

A = deepcopy(A_ref)
Ai_qml = qmllib.math.cho_invert(A)
Ai_qml = cho_invert(A)

# Check A is unchanged
assert np.allclose(A, A_ref)
Expand All @@ -55,7 +54,7 @@ def test_bkf_invert():
A_ref = np.loadtxt(test_dir + "/data/K_local_gaussian.txt")

A = deepcopy(A_ref)
Ai_qml = qmllib.math.bkf_invert(A)
Ai_qml = bkf_invert(A)

# Check A is unchanged
assert np.allclose(A, A_ref)
Expand All @@ -76,7 +75,7 @@ def test_bkf_solve():

A = deepcopy(A_ref)
y = deepcopy(y_ref)
x_qml = qmllib.math.bkf_solve(A, y)
x_qml = bkf_solve(A, y)

# Check arrays are unchanged
assert np.allclose(y, y_ref)
Expand Down

0 comments on commit d7d92e0

Please sign in to comment.