mpusz / mp-units

The quantities and units library for C++
https://mpusz.github.io/mp-units/
MIT License
1.06k stars 85 forks source link

Poll: How to constrain operations in a class template? #29

Closed mpusz closed 4 years ago

mpusz commented 4 years ago

The more I think about the "number" concept (#28) the more doubts I have. I see 2 solutions here:

Which option do you prefer?

(please vote only once by clicking on the correct bar above)

i-ky commented 4 years ago

@mpusz, here you have pointed out that

quantity takes Rep as a template parameter and maybe it could be some vector representation there.

Option 1 will not allow it, because division is not defined (mathematically speaking) for vectors. This is the first reason, why I would prefer Option 2.

The second reason to prefer Option 2 is that it can potentially provide a way to have non-additive quantities and stuff like that. E.g. it would make sense to prohibit adding absolute temperatures to one another using a restrictive Rep for quantity.

mpusz commented 4 years ago

@i-ky Thanks for your feedback! Regarding "second reason" we will probably solve the temperature problem with a dedicated affine space support. There will be no need to restrict Rep type for it.

i-ky commented 4 years ago

I guess by "temperature problem" you mean conversions between unit that have different zeros. I had a different thing in mind.

Imagine we have two temperatures (could be air temperatures in different places or on different days). What would adding them up mean? Does the sum of two temperatures have physical meaning? I think no. However, it makes sense to multiply temperature by Bolzmann constant and number of particles to get heat energy, which can be added together. Or you can multiply temperature by duration of time during which it was constant, add these numbers (in K*s) together and divide by total time to get time-average temperature.

Your library provides conversions between different units. Maybe someone will be able to come up with a clever fool-proof type system for thermodynamics or other domains.

oschonrock commented 4 years ago

Even more "illogically", it seems that it's sometimes appropriate to "subtract" temperatures:

eg Spec Heat Capacity = m C dt

I think these "illogical" exceptions are probably not the concern of a unit library?

JohelEGP commented 4 years ago

The current approach seems like the right one. By constraining an operator on the representation's operator, units::quantity can work with any algebraic strucutre. Maybe it is the algorithms that should be constrained on algebraic structure concepts. I'm wondering how to fit dimensional analysis into those without having an explosion of concepts.

JohelEGP commented 4 years ago

Perhaps one can fit dimensional analysis into those like this:

export template <class T>
concept quantity_field = std::regular<T> && requires(const T& c, T& l) {
//  zero<T>, one<T>, +c, -c, l+=c, l-=c, c+c, c-c
    { c * c } -> std::regular;
    { c / c } -> std::regular;
    { c * c / c } -> std::common_with<T>;
};

export template <class T>
concept field = quantity_field<T> && requires(const T& c, T& l) {
    { l *= c } -> std::same_as<T&>;
    { l /= c } -> std::same_as<T&>;
    { c * c } -> std::common_with<T>;
    { c / c } -> std::common_with<T>;
};
mpusz commented 4 years ago

My first try was to reuse http://wg21.link/P01813 but those concepts were overconstrained for our needs. After that, I went with Scalar concepts, and even though this is a really bad name (please help me find a better one) it seems to work nicely here.

It does not mean the Scalar concept is perfect though so if you would like to refine it please, feel encouraged to do so.

JohelEGP commented 1 year ago

The current approach seems like the right one. By constraining an operator on the representation's operator, units::quantity can work with any algebraic strucutre.

Things have changed since. We have quantity and quantity_point, documented to model a vector space and point space, respectively (https://mpusz.github.io/mp-units/2.0/users_guide/framework_basics/the_affine_space/). So now we know that we require the number to model those (and not just any algebraic structure). We further support operations on a scalar quantity and modulo.

I have been working on my own number concepts for quite some time now. I have included them in some of the code fragments I share here. I once shared the specification at https://cpplang.slack.com/archives/CBGC8H3T7/p1640214020006000.

I recently improved naming based on https://github.com/hsutter/cppfront/issues/231#issuecomment-1486044417 E.g., negatable -> negative representing negative (IEV 102-01-14). And I concurrently improved the hierarchy based on the discussion at #468. E.g., I split relative_quantity, which required division, into vector_space (which doesn't require division) and f_vector_space (which does require division; search for "F-vector" at https://en.wikipedia.org/wiki/Vector_space).

In case you're interested in how I constrained quantities before, expand the "Cpp1 quantity" at https://github.com/hsutter/cppfront/discussions/658#discussioncomment-6899974. This is how I'm constraining the numbers now: https://godbolt.org/z/W4anGEsss.

There might be room for improving this hierarchy of number concepts:

Anyways, I think using these concepts is an improvement over the status-quo.

mpusz commented 1 year ago

This looks super interesting! 😃 I think it should turn into a PR in order to prototype something and see how it looks, feels, and works with the current code.