Skip to content

Commit

Permalink
Merge pull request #24 from Bchass/logspace
Browse files Browse the repository at this point in the history
Logspace
  • Loading branch information
Bchass authored Aug 14, 2024
2 parents 8438527 + dbcb674 commit 55d4768
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
28 changes: 28 additions & 0 deletions tinynumpy/tests/test_tinynumpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,34 @@ def test_linspace():
# data types
result = tnp.linspace(0, 1, dtype='float64')
assert result.dtype == 'float64'


def test_logspace():
"""test the logspace function for tinynumpy"""
# default
result = tnp.logspace(0, 3, 10)
expected_result = tnp.array([1.0, 2.154434690031884, 4.641588833612778, 10.0, 21.544346900318832, 46.4158883361278, 100.0, 215.44346900318845, 464.15888336127773, 1000.0])
assert all(result[i] == expected_result[i] for i in range(len(result)))

# num
result = tnp.logspace(2.0, 3.0, num=4)
expected_result = tnp.array([100.0, 215.44346900318845, 464.15888336127773, 1000.0])
assert all(result[i] == expected_result[i] for i in range(len(result)))

# endpoint
result = tnp.logspace(2.0, 3.0, num=4, endpoint=False)
expected_result = tnp.array([100.0, 177.82794100389228, 316.22776601683796, 562.341325190349, 1000.0])
assert all(result[i] == expected_result[i] for i in range(len(result)))

# base
result = tnp.logspace(2.0, 3.0, num=4, base=2.0)
expected_result = tnp.array([4.0, 5.039684199579493, 6.3496042078727974, 8.0])
assert all(result[i] == expected_result[i] for i in range(len(result)))

# axis
result = tnp.logspace(2.0, 3.0, num=4, base=[2.0, 3.0], axis=-1)
expected_result = tnp.array([9.0, 12.980246132766677, 18.720754407467133, 27.0])
assert all(result[i] == expected_result[i] for i in range(len(result)))

def test_astype():
"""test the astype function for tinynumpy"""
Expand Down
24 changes: 19 additions & 5 deletions tinynumpy/tinynumpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
certain features may not be supported.
"""

# todo: keep track of readonly better
# todo: mathematical operators
# todo: more methods?
# todo: logspace, meshgrid

from __future__ import division
from __future__ import absolute_import

Expand Down Expand Up @@ -368,6 +363,25 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None):
else:
return a

def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=None):

start, stop = float(start), float(stop)
ra = stop - start

if endpoint:
num -= 1

a = empty((num + 1,), dtype)

if isinstance(base, list):
for i, b in enumerate(base):
a = empty((num + 1,), dtype)
a[:] = [b ** (start + i * ra / (num)) for i in range(num + 1)]
else:
a[:] = [base ** (start + i * ra / (num)) for i in range(num + 1)]

return a

def add(ndarray_vec1, ndarray_vec2):
c = []
for a, b in zip(ndarray_vec1, ndarray_vec2):
Expand Down

0 comments on commit 55d4768

Please sign in to comment.