Skip to content

Commit

Permalink
Merge branch 'dev-0.4.10' into dev-0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
francof2a committed Feb 10, 2024
2 parents 0940a3c + a8e4b01 commit 7198ec7
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 19 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package
name: Python App

on:
push:
branches: [ master ]
branches:
- '*'
pull_request:
branches: [ master ]
branches:
- '*'

jobs:
build:
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/python-os.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package
name: Python OS

on:
push:
branches: [ master ]
branches:
- '*'
pull_request:
branches: [ master ]
branches:
- '*'

jobs:
build:
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ name: Python package

on:
push:
branches: [ master ]
branches:
- '*'
pull_request:
branches: [ master ]
branches:
- '*'

jobs:
build:
Expand Down
20 changes: 12 additions & 8 deletions fxpmath/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,11 @@ def _mul_raw(x, y, n_frac):
if not isinstance(y, Fxp):
y = Fxp(y)

is_complex = x.vdtype == complex or y.vdtype == complex

signed = x.signed or y.signed
n_frac = x.n_frac + y.n_frac
n_word = x.n_word + y.n_word
n_word = x.n_word + y.n_word + int(is_complex)
n_int = n_word - int(signed) - n_frac
optimal_size = (signed, n_word, n_int, n_frac)

Expand Down Expand Up @@ -405,12 +407,13 @@ def _floordiv_raw_complex(x, y, n_frac):
if not isinstance(y, Fxp):
y = Fxp(y)

if x.vdtype == complex or y.vdtype == complex:
is_complex = x.vdtype == complex or y.vdtype == complex
if is_complex:
_floordiv_repr = _floordiv_repr_complex
_floordiv_raw = _floordiv_raw_complex

signed = x.signed or y.signed
n_int = x.n_int + y.n_frac + signed
n_int = x.n_int + y.n_frac + int(signed) + int(is_complex)
n_frac = 0
n_word = int(signed) + n_int + n_frac
optimal_size = (signed, n_word, n_int, n_frac)
Expand Down Expand Up @@ -444,12 +447,13 @@ def _truediv_raw_complex(x, y, n_frac):
if not isinstance(y, Fxp):
y = Fxp(y)

if x.vdtype == complex or y.vdtype == complex:
is_complex = x.vdtype == complex or y.vdtype == complex
if is_complex:
_truediv_raw = _truediv_raw_complex

signed = x.signed or y.signed
n_int = x.n_int + y.n_frac + signed
n_frac = x.n_frac + y.n_int
n_int = x.n_int + y.n_frac + int(signed) + int(is_complex)
n_frac = x.n_frac + y.n_int + int(is_complex)
n_word = int(signed) + n_int + n_frac
optimal_size = (signed, n_word, n_int, n_frac)

Expand Down Expand Up @@ -568,7 +572,7 @@ def cumsum(x, axis=None, out=None, out_like=None, sizing='optimal', method='raw'
"""
def _cumsum_raw(x, n_frac, **kwargs):
precision_cast = (lambda m: np.array(m, dtype=object)) if n_frac >= _n_word_max else (lambda m: m)
return np.cumsum(x.val, **kwargs) * precision_cast(2**(n_frac - x.n_frac))
return np.cumsum(precision_cast(x.val), **kwargs) * precision_cast(2**(n_frac - x.n_frac))

if not isinstance(x, Fxp):
x = Fxp(x)
Expand All @@ -591,7 +595,7 @@ def _cumprod_raw(x, n_frac, **kwargs):
precision_cast = (lambda m: np.array(m, dtype=object)) if n_frac >= _n_word_max else (lambda m: m)
pow_vals = n_frac - np.cumsum(np.ones_like(np.array(x)), axis=axis).astype(int) * x.n_frac
conv_factors = utils.int_array([2**pow_val for pow_val in precision_cast(pow_vals)])
return np.cumprod(x.val, **kwargs) * conv_factors
return np.cumprod(precision_cast(x.val), **kwargs) * precision_cast(conv_factors)

if not isinstance(x, Fxp):
x = Fxp(x)
Expand Down
10 changes: 7 additions & 3 deletions fxpmath/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,9 @@ def reshape(self, shape, order='C'):
"""

self.val = self.val.reshape(shape=shape, order=order)
return self
x = self.copy()
x.val = x.val.reshape(shape, order=order)
return x

def flatten(self, order='C'):
"""
Expand Down Expand Up @@ -849,7 +850,10 @@ def set_val(self, val, raw=False, vdtype=None, index=None):
val = val.astype(object)
else:
val = val.astype(original_vdtype)
val_dtype = np.int64 if self.signed else np.uint64
if _n_word_max_ <= 32:
val_dtype = np.int32 if self.signed else np.uint32
else:
val_dtype = np.int64 if self.signed else np.uint64

# rounding and overflowing
new_val = self._round(val * conv_factor , method=self.config.rounding)
Expand Down
36 changes: 36 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ def test_issue_76_v0_4_8():
y = np.cumsum(w)
assert np.all(y() == np.array([1, 2, 3, 4]))

# Increase word size to 96 bits
w = Fxp([1, 1, 1, 1], dtype='fxp-s96/0')
y = np.cumsum(w)
assert np.all(y() == np.array([1, 2, 3, 4]))

def test_issue_77_v0_4_8():
# Precision error when numpy.reshape

Expand Down Expand Up @@ -466,3 +471,34 @@ def test_issue_85_v0_4_8():

x = Fxp(0.0, dtype=dt, overflow='wrap') # EXCEPTION
assert x() == 0.0

def test_issue_90_v0_4_9():
# Built-in Fxp.reshape() method passes the wrong number of arguments to underlying numpy reshape() function
x = Fxp([1,2,3,4], False, 8, 0)
y = x.reshape((2,2))

assert np.all(x.val.shape == np.array((4,)))
assert np.all(y.val.shape == np.array((2,2)))

z = np.reshape(x, (2,2))

assert np.all(x.val.shape == np.array((4,)))
assert np.all(z.val.shape == np.array((2,2)))

zz = fxp.reshape(x, (2,2))

assert np.all(x.val.shape == np.array((4,)))
assert np.all(zz.val.shape == np.array((2,2)))


def test_issue_91_v0_4_10():
# Inaccuracy for certain complex multiplications

x = Fxp(-1-1j)
xa = np.array(-1-1j)

y = x * x
ya = xa * xa

assert np.all(y() == ya)

0 comments on commit 7198ec7

Please sign in to comment.