Tehforsch / diman

Define rust compile time unit systems using const generics
53 stars 2 forks source link

Distinguish absolute and relative quantities #38

Open Tehforsch opened 11 months ago

Tehforsch commented 11 months ago

Extracted from #36

@jedbrown

uom distinguishes (absolute) ThermodynamicTemperature from TemperatureInterval, and one can't add two absolute temperatures. There are other places where distinguishing absolute and incremental quantities is important, especially when working with derivatives (I intend to experiment with diman and Enzyme once some minor bugs are fixed to better understand the safety/usability tradeoff).

@Tehforsch

This is something that I want to implement, but haven't gotten around to yet. This seems similar to differentiating covariant and contravariant vectors. I think that this should be possible in principle by adding another entry to dimension, such as

struct Dimension {
    temperature: (i32, Kind),
    ...
}

enum Kind {
    Relative,
    Absolute,
}

and then changing the definitions of some of the inbuilt functions (for example of the Add trait for quantities would have to be implemented for Relative + Relative = Relative, Relative + Absolute -> Absolute and Absolute + Relative = Absolute). I should definitely test this out in a small setup because I am never quite sure how good the compiler is at inferring that those are definitely different implementations that will never affect the same type twice.

Contravariance/Covariance and Derivatives should be representable in a similar way, if I am not mistaken.

As above, I'd like to make this an optional choice only, so that people can opt-out of the additional complexity and have a simpler unit system.

@jedbrown

Distinguishing covariance and contravariance would be amazing when working in non-orthogonal coordinate systems. I don't know how what of that belongs at this level versus tensor algebra library based on diman.

Tehforsch commented 11 months ago
struct Dimension {
    temperature: (i32, Kind),
    ...
}

enum Kind {
    Relative,
    Absolute,
}

Thinking more about it, I am not sure this really covers the use case, since it wouldn't really transfer when dimensions change during operation. As an example, what if I multiply a relative temperature with the boltzmann constant? I would expect the result to be a relative energy, but where would this information be stored? In SI, it would have to be stored in any combination of length, time and mass. How would that work? Also what if we simultaneously want to differentiate between distances and position, for example? We'd suddenly have an explosion of these kinds that doesn't really make sense.

I'll have to look more at uoms implementation and how exactly ThermodynamicTemperature and TemperatureInterval are treated under the different operations. I still think that this is mostly an issue of getting the definition right (and looking at uom should help with that) and not something that will stretch the current const generics to its limits (as opposed to #39, for example).

Distinguishing covariance and contravariance would be amazing when working in non-orthogonal coordinate systems. I don't know how what of that belongs at this level versus tensor algebra library based on diman.

I think it should at least be possible to track this in diman, but have no idea whether it is the best strategy.