Skip to content

Commit

Permalink
Add shape query method to Quantity.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjziebarth committed Aug 4, 2024
1 parent e7512be commit 7d07eef
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ This software is licensed under the European Public License (EUPL) version 1.2 o
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### [0.2.0] - 2024-08-04
#### Added
- Add `shape` method for `Quantity`, which allows to query the (array-) shape
of the underlying data.

### [0.1.0] - 2024-05-05
#### Added
- Add `zeros_like` generator function for `Quantity` (Cython only)
Expand Down
9 changes: 9 additions & 0 deletions cyantities/quantity.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,15 @@ cdef class Quantity:
return False


def shape(self) -> int | tuple[int,...]:
"""
Return this quantity's shape.
"""
if self._is_scalar:
return 1
return self._val_object.shape


def unit(self) -> Unit:
return generate_from_cpp(self._unit)

Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project('cyantities', 'cpp', 'cython',
version : '0.1.0', default_options : ['optimization=3'])
version : '0.2.0', default_options : ['optimization=3'])


#
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "Cyantities"
version = "0.1.0"
version = "0.2.0"
authors = [
{name = "Malte J. Ziebarth", email = "mjz.science@fmvkb.de"},
]
Expand Down
5 changes: 5 additions & 0 deletions testing/test_quantities.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def test_quantity():
q1 = Quantity(np.array([1.0, 2.0, 3.0]), 'm')
q3 = Quantity(2.0, "kg")

# Shapes:
assert q0.shape() == q3.shape() == 1
assert q1.shape() == (3,)

# Multiplication:
q2 = q0 * q1
assert q2.unit() == Unit("m^2")
Expand All @@ -38,6 +42,7 @@ def test_quantity():

# Division:
q4 = q0 / q1
assert q4.shape() == (3,)
assert q4.unit() == Unit("1")
assert (q4 * Unit("m")).unit() == Unit("m")
assert np.all(q4 == Quantity(1.0 / np.array([1.0, 2.0, 3.0]), "1"))
Expand Down

0 comments on commit 7d07eef

Please sign in to comment.