On Sun, Oct 06, 2024 at 03:34:36PM +0200, Michael Soegtrop wrote:
> Dear FriCas Developers & Users,
>
> I wonder what the opinion of the FriCAS experts is on the question if the F#
> approach (units are parts of types) or the "traditional" approach (units are
> part of the value) approach looks more suitable for FriCAS.
>
> IMHO it is conceptually better to treat units as part of the type, so that
> one can check dimensionality of equations without inserting actual values.
>
> The question is if the symbolic mathematics engine of FriCAS could handle
> number types extended with a unit.
FriCAS types are parametrized, to you could have a unit as type
parameter. So one can make "units as part of the type" work.
However, one of fundamental FriCAS types is Ring. Once you have
units multiplication produces a new type. So your type no longer
is a Ring and you can no longer use significant part of FriCAS
library.
So "units as part of the value" look more viable, but even there
you can hit problems.
> In either case the unit system should support
>
> - rational exponents for dimensions
>
> - logarithms of units
>
> Otherwise perfectly valid equations would not type check or compute. In
> general it doesn't make a lot of sense to have fractional units in end
> results (square roots happen) but it is quite frequent that intermediate
> results have fractional units and then cancel out in the end.
>
> The code in
https://wiki.fricas.org/SandBoxPhysicalUnitSystem will only work
> in rather simple cases (it supports neither of the above). The extension to
> rational units should be easy, but the support for log units might be
> tricky.
I am not sure what you mean by "log units". AFAICS fundamental property
of units system is that units form a group. You can make a group
containing say 'log(kg)' as new element. But alone it looks
useless. And naive definition of operatation leads to contradiction:
for normal units say kg adding two things dimensioned as kg again
gives you kg. But one would expect that if a is dimensioned in kg
than log(a) + log(a) should be dimensioned the same as log(a^2),
which is quite different than normal units.
> If one puts units in types, one needs a fairly flexible unit system to avoid
> that e.g. results of an integration don't type check. If one puts units into
> values things are more relaxed because one can do some simplifications
> before one substitutes values with units into a term.
Natural way of doing units in FriCAS would simply signal unit error
at runtime. By natural way I mean checking that units match in
addition/subtraction and computing new unit in other operations.
And during intermediate calculations FriCAS may transform expressions
in various ways which are likely to lead to wrong units (for example,
when perfoming operations on homogeneous polynomials FriCAS may
use non-homogeneous polynomials in intermediate steps).
AFAICS "interesting" computations are dimensionless, so natural way
is "unit erasure": extract dimensionless coefficients, do computations
on them and then restore units.
> E.g. what I do
> frequently in Mathematica is to fully expand and then ReplaceAll (/.) with
>
> a_^e_*b_^f_ /; ! IntegerQ[e] && ! IntegerQ[f] && IntegerQ[Simplify[e + f]]
> -> (a/b)^e*b^(Simplify[e + f])
>
> Here the (a/b)^e part always ends up to have unit 1 because in final results
> fractional units don't make sense (and I did expand before). The b^(e+f)
> part can have arbitrary units because the exponent is known to be an
> integer. I use similar simplifications for logarithms and exponentials.
>
> Note that in a programming language like F# fractional units and log units
> are not required, because one would generally use reasonably "unit nice"
> simplified forms of terms. But a CAS has to be able to handle intermediate
> results and simplifications - unless one wants to delegate this to the user
> in a "units in values" concept as e.g. Mathematica and Maxima do.
>
> Best regards,
>
> Michael
>
> --
> You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
fricas-devel...@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/fricas-devel/4027bf63-80e2-4dc8-a665-c375c619d909%40michael-soegtrop.de.
--
Waldek Hebisch