Skip to content

Commit 7ed5cc0

Browse files
committed
version 0.4.3
* Fxp adds `config` as optional init parameter (kwarg). * Functions (operators) over one and two variables copy first operator configuration by default. * Changes above fixed wrap overflowing bug after operation (issue #42 solved). * Wrap function of utils fix bug when data has more than n_word_max (64 bits in the most of cases) (issue #41 soved). * Config new methods: copy, deepcopy. * Fix warning about internal complex check (issue #39). * Add link implementation between `functions.truediv` with `numpy.divide`. * Add `axes` parameter to `transpose` method.
1 parent 59fc284 commit 7ed5cc0

File tree

6 files changed

+67
-10
lines changed

6 files changed

+67
-10
lines changed

changelog.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
version 0.4.3
2+
--------------------------------------------------
3+
* Fxp adds `config` as optional init parameter (kwarg).
4+
* Functions (operators) over one and two variables copy first operator configuration by default.
5+
* Changes above fixed wrap overflowing bug after operation (issue #42 solved).
6+
* Wrap function of utils fix bug when data has more than n_word_max (64 bits in the most of cases) (issue #41 soved).
7+
* Config new methods: copy, deepcopy.
8+
* Fix warning about internal complex check (issue #39).
9+
* Add link implementation between `functions.truediv` with `numpy.divide`.
10+
* Add `axes` parameter to `transpose` method.
11+
112
version 0.4.2
213
--------------------------------------------------
314
* Add template to Config object.

fxpmath/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '0.4.2'
1+
__version__ = '0.4.3'
22

33
import sys
44
import os

fxpmath/functions.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def _function_over_one_var(repr_func, raw_func, x, out=None, out_like=None, sizi
9999
if not out.signed and signed:
100100
raise ValueError('Signed addition can not be stored in unsigned `out` object!')
101101
n_frac = out.n_frac
102+
config = None
103+
102104
elif out_like is not None:
103105
if not isinstance(out_like, Fxp):
104106
raise TypeError('`out_like` must be a Fxp object!')
@@ -107,6 +109,10 @@ def _function_over_one_var(repr_func, raw_func, x, out=None, out_like=None, sizi
107109
signed = None
108110
n_frac = None
109111
n_int = None
112+
config = None
113+
114+
else:
115+
config = x.config
110116

111117
if method == 'repr' or x.scaled or n_frac is None:
112118
raw = False
@@ -141,6 +147,8 @@ def _function_over_two_vars(repr_func, raw_func, x, y, out=None, out_like=None,
141147
if not out.signed and signed:
142148
raise ValueError('Signed addition can not be stored in unsigned `out` object!')
143149
n_frac = out.n_frac
150+
config = None
151+
144152
elif out_like is not None:
145153
if not isinstance(out_like, Fxp):
146154
raise TypeError('`out_like` must be a Fxp object!')
@@ -149,6 +157,10 @@ def _function_over_two_vars(repr_func, raw_func, x, y, out=None, out_like=None,
149157
signed = None
150158
n_frac = None
151159
n_int = None
160+
config = None
161+
162+
else:
163+
config = x.config
152164

153165
if method == 'repr' or x.scaled or n_frac is None:
154166
raw = False
@@ -163,7 +175,7 @@ def _function_over_two_vars(repr_func, raw_func, x, y, out=None, out_like=None,
163175
if out is not None:
164176
z = out.set_val(val, raw=raw)
165177
else:
166-
z = Fxp(val, signed=signed, n_int=n_int, n_frac=n_frac, like=out_like, raw=raw)
178+
z = Fxp(val, signed=signed, n_int=n_int, n_frac=n_frac, like=out_like, raw=raw, config=config)
167179

168180
return z
169181

@@ -374,7 +386,7 @@ def _floordiv_raw(x, y, n_frac):
374386

375387
return _function_over_two_vars(repr_func=_floordiv_repr, raw_func=_floordiv_raw, x=x, y=y, out=out, out_like=out_like, sizing=sizing, method=method, optimal_size=optimal_size, **kwargs)
376388

377-
@implements(np.true_divide)
389+
@implements(np.true_divide, np.divide)
378390
def truediv(x, y, out=None, out_like=None, sizing='optimal', method='raw', **kwargs):
379391
"""
380392
"""
@@ -572,13 +584,14 @@ def _conjugate_raw(x, n_frac, **kwargs):
572584
return _function_over_one_var(repr_func=np.conjugate, raw_func=_conjugate_raw, x=x, out=out, out_like=out_like, sizing=sizing, method=method, **kwargs)
573585

574586
@implements(np.transpose)
575-
def transpose(x, out=None, out_like=None, sizing='optimal', method='raw', **kwargs):
587+
def transpose(x, axes=None, out=None, out_like=None, sizing='optimal', method='raw', **kwargs):
576588
"""
577589
"""
578590
def _transpose_raw(x, n_frac, **kwargs):
579591
precision_cast = (lambda m: np.array(m, dtype=object)) if n_frac >= _n_word_max else (lambda m: m)
580592
return (x.val.T) * precision_cast(2**(n_frac - x.n_frac))
581593

594+
kwargs['axes'] = axes
582595
return _function_over_one_var(repr_func=np.transpose, raw_func=_transpose_raw, x=x, out=out, out_like=out_like, sizing=sizing, method=method, **kwargs)
583596

584597
@implements(np.clip)

fxpmath/objects.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ def __init__(self, val=None, signed=None, n_word=None, n_frac=None, n_int=None,
149149
'inaccuracy': False,
150150
'extended_prec': False}
151151

152+
# update config as argument
153+
_config = kwargs.pop('config', None)
154+
if _config is not None:
155+
if isinstance(_config, Config):
156+
self.config = _config.deepcopy()
157+
else:
158+
raise TypeError('config parameter must be Config class type!')
159+
152160
# update config from kwargs
153161
self.config.update(**kwargs)
154162

@@ -594,7 +602,7 @@ def set_val(self, val, raw=False, vdtype=None, index=None):
594602
conv_factor = self._get_conv_factor(raw)
595603

596604
# round, saturate and store
597-
if original_vdtype != complex and not np.issubdtype(original_vdtype, complex):
605+
if original_vdtype != complex and not np.issubdtype(original_vdtype, np.complexfloating):
598606
# val_dtype determination
599607
_n_word_max_ = min(_n_word_max, 64)
600608
if np.max(val) >= 2**_n_word_max_ or np.min(val) < -2**_n_word_max_ or self.n_word >= _n_word_max_:
@@ -1522,15 +1530,15 @@ def T(self):
15221530
x.val = x.val.T
15231531
return x
15241532

1525-
def transpose(self, **kwargs):
1533+
def transpose(self, axes=None, **kwargs):
15261534
from .functions import transpose
15271535

15281536
out = kwargs.pop('out', self.config.op_out)
15291537
out_like = kwargs.pop('out_like', self.config.op_out_like)
15301538
sizing = kwargs.pop('sizing', self.config.op_sizing)
15311539
method = kwargs.pop('method', self.config.op_method)
15321540

1533-
return transpose(self, out=out, out_like=out_like, sizing=sizing, method=method, **kwargs)
1541+
return transpose(self, axes=axes, out=out, out_like=out_like, sizing=sizing, method=method, **kwargs)
15341542

15351543
def item(self, *args):
15361544
if len(args) > 1:
@@ -1894,7 +1902,12 @@ def update(self, **kwargs):
18941902
if hasattr(self, k):
18951903
setattr(self, k, v)
18961904

1905+
# copy
1906+
def copy(self):
1907+
return copy.copy(self)
18971908

1909+
def deepcopy(self):
1910+
return copy.deepcopy(self)
18981911
# endregion
18991912

19001913
# ----------------------------------------------------------------------------------------

fxpmath/utils.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#%%
3636
import numpy as np
37+
from . import _n_word_max
3738

3839
#%%
3940
def array_support(func):
@@ -312,13 +313,20 @@ def int_clip(x, val_min, val_max):
312313
x_clipped = np.array(max(val_min, min(val_max, int(x))))
313314
return x_clipped
314315

315-
def wrap(x, signed, n_word):
316+
def wrap(x, signed, n_word):
317+
if n_word >= _n_word_max:
318+
dtype = object
319+
else:
320+
dtype = int
321+
316322
m = (1 << n_word)
317323
if signed:
318-
x = np.array(x).astype(int) & (m - 1)
324+
x = np.array(x).astype(dtype) & (m - 1)
325+
x = np.asarray(x).astype(dtype)
319326
x = np.where(x < (1 << (n_word-1)), x, x | (-m))
320327
else:
321-
x = np.array(x).astype(int) & (m - 1)
328+
x = np.array(x).astype(dtype) & (m - 1)
329+
x = np.asarray(x).astype(dtype)
322330
return x
323331

324332
def get_sizes_from_dtype(dtype):

tests/test_issues.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,15 @@ def test_issue_31_v0_4_0():
146146
q3 = t(0.125*2**3)
147147
assert q3.val.dtype == object
148148
assert q3() == 1.0
149+
150+
def test_issue_41_v0_4_2():
151+
x = Fxp(2, False, 63, 0, overflow='wrap')
152+
y = Fxp(2, False, 64, 0, overflow='wrap')
153+
154+
assert x() == 2
155+
assert y() == 2
156+
157+
def test_issue_42_v0_4_2():
158+
b = Fxp(2, True, 4, 0, overflow='wrap')
159+
assert (b + 8)() == -6.0
160+
assert (b - 8)() == -6.0

0 commit comments

Comments
 (0)