Skip to content

Generalized indexing, correct formatting for arrays, new constants. - #100

Closed
RolfStierle wants to merge 1 commit into
itt-ustutt:masterfrom
RolfStierle:master
Closed

RolfStierle wants to merge 1 commit into
itt-ustutt:masterfrom
RolfStierle:master

Conversation

@RolfStierle

@RolfStierle RolfStierle commented Mar 6, 2026 •

Copy link
Copy Markdown

Corrected error where arrays with units show a unit but only the underlying stored data in basic SI units is shown (confusing when using Jupyter notebooks, underlying data is correct, just wrongly shown).

Indexing generalized such that sliding operations array[:, 1:3] are now possible for multiple-dimensional arrays.

Added electric and Coulomb constants (#47, @g-bauer) and added derived units ATM and POISE (#46, @g-bauer).

@prehner prehner left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I like the generalized indexing, that will make a lot of scripts much easier. Not sure what problem you are addressing with the formating, though?

Comment thread si-units/src/lib.rs
Comment on lines +283 to +293
if let Ok(v) = value.extract::<SINumber>() {
if self.unit == v.unit {
self.value.call_method1(py, "__setitem__", (idx, v.value))?;
return Ok(());
}
return Err(QuantityError::InconsistentUnits {
unit1: self.unit,
unit2: v.unit,
}
.into());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if let Ok(v) = value.extract::<SINumber>() {
if self.unit == v.unit {
self.value.call_method1(py, "__setitem__", (idx, v.value))?;
return Ok(());
}
return Err(QuantityError::InconsistentUnits {
unit1: self.unit,
unit2: v.unit,
}
.into());
}

I don't think that does anything.

Comment thread si-units/src/lib.rs
&self,
py: Python,
idx: &Bound<'_, PyAny>,
value: &Bound<'_, PyAny>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
value: &Bound<'_, PyAny>,
value: &PySIObject,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only need SINumber if we want to make sure that we can only pass a scalar (e.g., in linspace). With the more general indexing, we can simply pass any SI object.

Comment thread CHANGELOG.md

## [Unreleased]
### Added
- Electric and Coulomb constants added. [#47](https://github.com/itt-ustutt/quantity/issues/47)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

link to the PR, not the issue

Comment thread si-units/CHANGELOG.md

## [Unreleased]
### Fixed
- Correctly formats output of arrays with a unit.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also link the PR here

Comment thread si-units/CHANGELOG.md
- Correctly formats output of arrays with a unit.

### Added
- Getter and Setter generalized to common NumPy indexing, such that general slicing operations are possible in multiple dimensions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Getter and Setter generalized to common NumPy indexing, such that general slicing operations are possible in multiple dimensions.
- Indexing generalized, such that, e.g., general multidimensional slicing operations are possible for quantities containing NumPy arrays.

Comment thread src/fmt.rs
pub(crate) trait PrintUnit {
const UNIT: &'static str;

fn scale() -> f64;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this used anywhere?

Comment thread si-units/src/lib.rs
.call_method0(py, "__repr__")?
.extract::<String>(py)?;
Ok(format!("{} {}", value, self.unit))
let (multiplier, symbol) = SINumber::new(1.0, self.unit).into_scaled_parts();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this for the annoying thing with grams and kilograms? I'm not sure I follow here.

@RolfStierle

Copy link
Copy Markdown
Author

Thanks! I like the generalized indexing, that will make a lot of scripts much easier. Not sure what problem you are addressing with the formating, though?

The formatting of arrays with attached units not in the units the arrays are saved in produce the following inconsistencies:
grafik

Calculations are correct, underlying data is correct. Just the output might be confusing.

@prehner

prehner commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

The changes are implemented in individual PRs, so I am closing this one.

@prehner prehner closed this Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add electric constant and Coulomb constant to constants Add atmosphere and poise as derived units

2 participants