Skip to content

Commit 7d07eef

Browse files
committed
Add shape query method to Quantity.
1 parent e7512be commit 7d07eef

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ This software is licensed under the European Public License (EUPL) version 1.2 o
172172
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
173173
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
174174

175+
### [0.2.0] - 2024-08-04
176+
#### Added
177+
- Add `shape` method for `Quantity`, which allows to query the (array-) shape
178+
of the underlying data.
179+
175180
### [0.1.0] - 2024-05-05
176181
#### Added
177182
- Add `zeros_like` generator function for `Quantity` (Cython only)

cyantities/quantity.pyx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,15 @@ cdef class Quantity:
552552
return False
553553

554554

555+
def shape(self) -> int | tuple[int,...]:
556+
"""
557+
Return this quantity's shape.
558+
"""
559+
if self._is_scalar:
560+
return 1
561+
return self._val_object.shape
562+
563+
555564
def unit(self) -> Unit:
556565
return generate_from_cpp(self._unit)
557566

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
project('cyantities', 'cpp', 'cython',
2-
version : '0.1.0', default_options : ['optimization=3'])
2+
version : '0.2.0', default_options : ['optimization=3'])
33

44

55
#

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "Cyantities"
7-
version = "0.1.0"
7+
version = "0.2.0"
88
authors = [
99
{name = "Malte J. Ziebarth", email = "mjz.science@fmvkb.de"},
1010
]

testing/test_quantities.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def test_quantity():
3030
q1 = Quantity(np.array([1.0, 2.0, 3.0]), 'm')
3131
q3 = Quantity(2.0, "kg")
3232

33+
# Shapes:
34+
assert q0.shape() == q3.shape() == 1
35+
assert q1.shape() == (3,)
36+
3337
# Multiplication:
3438
q2 = q0 * q1
3539
assert q2.unit() == Unit("m^2")
@@ -38,6 +42,7 @@ def test_quantity():
3842

3943
# Division:
4044
q4 = q0 / q1
45+
assert q4.shape() == (3,)
4146
assert q4.unit() == Unit("1")
4247
assert (q4 * Unit("m")).unit() == Unit("m")
4348
assert np.all(q4 == Quantity(1.0 / np.array([1.0, 2.0, 3.0]), "1"))

0 commit comments

Comments
 (0)