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

Cython implementation multiply and from_attributes #117

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 4 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@ jobs:
command: |
python3 -m venv venv
source venv/bin/activate
pip install -U pip
pip install -r requirements-dev.txt
pip install .[dev]
pip install -e .[dev]
- save_cache:
key: py310-{{ checksum "requirements-dev.txt"}}
paths:
- venv
- run:
name: "Run pylint"
name: "Run pytest"
command: |
source venv/bin/activate
pylint --rcfile=.pylintrc -E numpoly
pytest --doctest-modules numpoly/ test/
- run:
name: "Run pydocstyle"
command: |
Expand Down Expand Up @@ -72,7 +71,7 @@ jobs:
command: |
source venv/bin/activate
pip install twine build
python -m build
python -m build -s
twine upload -u __token__ -p $PYPI_PASSWORD --non-interactive dist/*

workflows:
Expand Down
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include README.rst
include LICENSE.txt
global-exclude *.pyc
recursive-include numpoly/cfunctions *.pyx
recursive-include numpoly/cfunctions *.pxd
4 changes: 0 additions & 4 deletions docs/reference/constructor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,5 @@ Save/Load
.. autosummary::
:toctree: ../api

load
loadtxt
save
savetxt
savez
savez_compressed
1 change: 1 addition & 0 deletions numpoly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from .sympy_ import to_sympy

from .array_function import *
from .cfunctions import *
from .poly_function import *
from .utils import (
bindex,
Expand Down
2 changes: 1 addition & 1 deletion numpoly/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def align_polynomials(*polys: PolyLike) -> Tuple[ndpoly, ...]:
>>> q0
polynomial(q0)
>>> q0.coefficients
[1]
[np.int64(1)]
>>> q0.indeterminants
polynomial([q0])
>>> q0, _ = numpoly.align_polynomials(q0, q0q1)
Expand Down
4 changes: 0 additions & 4 deletions numpoly/array_function/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
from .hsplit import hsplit
from .less import less
from .less_equal import less_equal
from .load import load
from .inner import inner
from .isclose import isclose
from .isfinite import isfinite
Expand Down Expand Up @@ -74,10 +73,7 @@
from .result_type import result_type
from .rint import rint
from .roots import roots
from .save import save
from .savetxt import savetxt
from .savez import savez
from .savez_compressed import savez_compressed
from .split import split
from .square import square
from .stack import stack
Expand Down
6 changes: 3 additions & 3 deletions numpoly/array_function/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ def all(
Example:
>>> q0 = numpoly.variable()
>>> numpoly.all(q0)
True
np.True_
>>> numpoly.all(0*q0)
False
np.False_
>>> numpoly.all([1, q0, 0])
False
np.False_
>>> numpoly.all([[True*q0, False], [True, True]], axis=0)
array([ True, False])

Expand Down
6 changes: 3 additions & 3 deletions numpoly/array_function/any.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def any(
Example:
>>> q0 = numpoly.variable()
>>> numpoly.any(q0)
True
np.True_
>>> numpoly.any(0*q0)
False
np.False_
>>> numpoly.any([1, q0, 0])
True
np.True_
>>> numpoly.any([[True*q0, False], [True, True]], axis=0)
array([ True, True])

Expand Down
6 changes: 3 additions & 3 deletions numpoly/array_function/argmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def argmax(
Example:
>>> q0, q1 = numpoly.variable(2)
>>> numpoly.argmax([13, 7])
0
np.int64(0)
>>> numpoly.argmax([1, q0, q0**2, q1])
2
np.int64(2)
>>> numpoly.argmax([1, q0, q1])
2
np.int64(2)
>>> numpoly.argmax([[3*q0**2, q0**2],
... [2*q0**2, 4*q0**2]], axis=0)
array([0, 1])
Expand Down
6 changes: 3 additions & 3 deletions numpoly/array_function/argmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def argmin(
Example:
>>> q0, q1 = numpoly.variable(2)
>>> numpoly.argmin([13, 7])
1
np.int64(1)
>>> numpoly.argmin([1, q0, q0**2, q1])
0
np.int64(0)
>>> numpoly.argmin([q0*q1, q0, q1])
1
np.int64(1)
>>> numpoly.argmin([[3*q0**2, q0**2], [2*q0**2, 4*q0**2]], axis=0)
array([1, 0])

Expand Down
12 changes: 6 additions & 6 deletions numpoly/array_function/isfinite.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ def isfinite(

Example:
>>> numpoly.isfinite(1)
True
np.True_
>>> numpoly.isfinite(0)
True
np.True_
>>> numpoly.isfinite(numpy.nan*numpoly.variable())
False
np.False_
>>> numpoly.isfinite(numpy.inf)
False
>>> numpoly.isfinite(numpy.NINF)
False
np.False_
>>> numpoly.isfinite(-numpy.inf)
np.False_
>>> numpoly.isfinite([numpy.log(-1.), 1., numpy.log(0)])
array([False, True, False])

Expand Down
108 changes: 0 additions & 108 deletions numpoly/array_function/load.py

This file was deleted.

2 changes: 1 addition & 1 deletion numpoly/array_function/logical_and.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def logical_and(
Example:
>>> q0, q1 = numpoly.variable(2)
>>> numpoly.logical_and(q0, 0)
False
np.False_
>>> numpoly.logical_and([q0, False], [q0, q1])
array([ True, False])
>>> const = numpy.arange(5)
Expand Down
2 changes: 1 addition & 1 deletion numpoly/array_function/logical_or.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def logical_or(

Example:
>>> numpoly.logical_or(True, False)
True
np.True_
>>> numpoly.logical_or([True, False], [False, False])
array([ True, False])
>>> x = numpy.arange(5)
Expand Down
42 changes: 28 additions & 14 deletions numpoly/array_function/multiply.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Multiply arguments element-wise."""

from __future__ import annotations
from typing import Any, Optional

Expand Down Expand Up @@ -56,6 +57,7 @@ def multiply(

"""
x1, x2 = numpoly.align_indeterminants(x1, x2)

dtype = numpy.result_type(x1, x2)
shape = numpy.broadcast_shapes(x1.shape, x2.shape)

Expand All @@ -76,21 +78,33 @@ def multiply(
else out
)

seen = set()
for expon1, coeff1 in zip(x1.exponents, x1.coefficients):
for expon2, coeff2 in zip(x2.exponents, x2.coefficients):
key = (expon1 + expon2 + x1.KEY_OFFSET).ravel()
key = key.view(f"U{len(expon1)}").item()
if key in seen:
out_.values[key] += numpy.multiply(
coeff1, coeff2, where=where, **kwargs
)
else:
numpy.multiply(
coeff1, coeff2, out=out_.values[key], where=where, **kwargs
)
seen.add(key)
# seen = set()
# for expon1, coeff1 in zip(x1.exponents, x1.coefficients):
# for expon2, coeff2 in zip(x2.exponents, x2.coefficients):
# key = (expon1 + expon2 + x1.KEY_OFFSET).ravel()
# key = key.view(f"U{len(expon1)}").item()
# if key in seen:
# out_.values[key] += numpy.multiply(
# coeff1, coeff2, where=where, **kwargs
# )
# else:
# numpy.multiply(
# coeff1, coeff2, out=out_.values[key], where=where, **kwargs
# )
# seen.add(key)
#
# if out is None:
# out_ = numpoly.clean_attributes(out_)

numpoly.cmultiply(
x1.exponents,
x2.exponents,
x1.coefficients,
x2.coefficients,
x1.KEY_OFFSET,
out_.values.ravel(),
)
if out is None:
out_ = numpoly.clean_attributes(out_)

return out_
8 changes: 5 additions & 3 deletions numpoly/array_function/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@
@implements(numpy.reshape)
def reshape(
a: PolyLike,
newshape: Union[int, Sequence[int]],
/,
shape: Union[int, Sequence[int]] = None,
order: Order = "C",
newshape: Union[int, Sequence[int]] = None,
) -> ndpoly:
"""
Give a new shape to an array without changing its data.

Args:
a:
Array to be reshaped.
newshape:
shape:
The new shape should be compatible with the original shape. If an
integer, then the result will be a 1-D array of that length. One
shape dimension can be -1. In this case, the value is inferred from
Expand Down Expand Up @@ -63,5 +65,5 @@ def reshape(

"""
poly = numpoly.aspolynomial(a)
array = numpy.reshape(poly.values, newshape=newshape, order=order)
array = numpy.reshape(poly.values, shape=shape, newshape=newshape, order=order)
return numpoly.aspolynomial(array, names=poly.indeterminants)
Loading