Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default units for each dimension? #353

Closed
tadamcz opened this issue Feb 1, 2023 · 5 comments
Closed

Set default units for each dimension? #353

tadamcz opened this issue Feb 1, 2023 · 5 comments
Labels

Comments

@tadamcz
Copy link

tadamcz commented Feb 1, 2023

First of all, this is a very nice library!

I wonder if it would be easy to add the ability to set a default unit for a dimension (on a per-session basis or globally). I didn't find another issue about this.

Suppose I want to do a bunch of calculations with lumens per area, and I want all the results to be in lumens per cm^2 for easy comparison.

By default I get this behaviour:

>>> 3050 lm / (1m*12mm)
  3050 lm / (1 m × 12 mm)
   = 254167 lm/m²

>>> 1521 lm / (60mm*60mm)
  1521 lm / (60 mm × 60 mm)
   = 0.4225 lm/mm²

I have to manually do:

>>> lmcm2=lm/cm^2
  lmcm2 = 1 lm/cm²

>>> 3050lm/(1m*12mm) -> lmcm2
  3050 lm / (1 m × 12 mm) ➞ lmcm2
   = 25.4167 lm/cm²

>>> 1521 lm / (60mm*60mm) -> lmcm2
  1521 lm / (60 mm × 60 mm) ➞ lmcm2
   = 42.25 lm/cm²

and keep typing -> lmcm2 every time I want to calculate something.

It would be great to be able to define default units for a dimension, something like:

default lm/cm^2

Internally each dimension could be represented by its SI base unit form. Then you would have a default mapping from dimensions to units. So e.g. the dimension of "illluminance" (of which lm/cm^2 and lm/m^2 are units) would be represented by candela*sr/m^2. The mapping would map candela*sr/m^2 to the desired lm/cm^2 unit.

@triallax
Copy link
Collaborator

This sounds like a nice idea, and to have the default units be persistent, you could just place them in your .insectrc. @sharkdp what do you think?

@sharkdp
Copy link
Owner

sharkdp commented Feb 11, 2023

Thank you for your request. One (minor?) difficulty arises because the mapping between phys. dimensions and 'default units' is not a bijection. For example: energy is typically measured in units of joule and torque is typically measured in units of newton meters (mathematically the same as joule). If we have an expression in units of kg m² / s², there is no way of knowing whether it's supposed to be an energy or a torque. And whether we should prefer J or N·m.

@tadamcz
Copy link
Author

tadamcz commented Feb 11, 2023

Good point. In that case it doesn't seem too bad to just use one of them (as set by the user).

Just to be clear, for any dimensions that don't have a default unit set explicitly by the user, the current behaviour would be retained.

@triallax
Copy link
Collaborator

@sharkdp good point, that's something we should account for. A user should be able to make a conversion explicitly, which would override the default unit previously set. We should also add a facility to "unset" default units. Any syntax suggestions?

@sharkdp
Copy link
Owner

sharkdp commented Sep 26, 2023

Follow up ticket in Numbat with idea on how to implement this: sharkdp/numbat#116

See also: sharkdp/numbat#91 / sharkdp/numbat#167 which could potentially resolve this Energy/Torque ambiguity. But it comes at a cost...

@sharkdp sharkdp closed this as completed Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants