Re: Simplifying Units in Ezunits Package

Robert Dodier <[email protected]> Thu, 7 May 2026 21:34:37 -0700
Newsgroups gmane.comp.mathematics.maxima.general
Message-ID <CAAsY_sQZh8Nxu_w-nmAYMUAJMMNFOxz1xQO-6jmyw8t8A+g=tg@mail.gmail.com>
Mateusz, I'm glad to hear that Maxima + ezunits are working for you.
For what it's worth, I wrote ezunits hoping that it would be useful
for engineering calculations.

On Thu, May 7, 2026 at 4:07 AM Mateusz M. <[email protected]> wrote:

> (%o7)   (1/10 ` 1/kOhm)/(100 ` pF+10 ` nF) `` 1/s

As you have seen, ezunits doesn't convert different units with the
same dimensions to a common unit when adding dimensional values.
(Adding values with the same units is carried out automatically.)

One approach is to convert different units being added to fundamental
units so they can all be added by the existing rules. Here is a proof
of concept using defrule. This defines a function which can be called
to carry out the operation on any applicable subexpressions via the
Maxima function apply1. (Note that's apply1, not apply.)

defrule (rfoo, aa + uu%, add_with_units_try_harder (aa, uu%));
add_with_units_try_harder (x%, u%) :=
    if op(u%) = "+"
        then (if length (unique (map (second, u%))) = 1 then
add_with_units (x%, u%) else add_with_units (x%,u% ``
fundamental_units (first (u%))))
        else x% + u%;

For the expression above, I get:

expr1: (1/10 ` 1/kOhm)/(100 ` pF+10 ` nF);
expr2: apply1 (expr1, rfoo);
 => 1000000000/101 ` (kg*m^2)/(A^2*kOhm*s^4)
fundamental_units (expr2);
 => 1/s
expr2 `` 1/s;
 => 1000000/101 ` 1/s

I made it a rule that has to be applied explicitly (not automatically)
since it has the potential to make expressions more messy and I'm
thinking the user should choose to do so.

This operation is probably important enough to make it a part of
ezunits. Let me think about that.

> (%i15)  H(s, C, R_1, R_2) := (1/(s * C)) / (R_1 + R_2 + 1/(s * C));

This is a little confusing to me, I'm not sure what is s here. Is it
supposed to indicate 1 second? If so there is a problem adding
resistance to 1/(time * capacitance), right? Did you mean to say s/C
instead of 1/(s*C) ? or something else?

Anyway I find that if I say

expr3: H(1`Hz, 18`nF, 1`kOhm, 1`kOhm);
 => (1/18 ` 1/(Hz*nF))/(2 ` kOhm+1/18 ` 1/(Hz*nF))

and then I can use rfoo again,

expr4: apply1 (expr3, rfoo);
 => 1/1000036000 ` (A^2*s^3)/(Hz*kg*m^2*nF)

and then

fundamental_units (expr4);
 => 1
expr4 `` 1;
 => 250000/250009

I don't know if that's really valid, since it depends on calling H
with s = 1 ` Hz.


> Is there perhaps a simple and universal way to simplify units like in MathCad or other engineering tools?

Well, simplifying units is a bit tricky since what's "simpler" depends
on the context to some extent. ezunits doesn't automatically convert
units, as you have seen, instead letting the units be whatever the
user wrote.

As you know, this has some drawbacks. How does MathCad handle problems
such as the ones you posed? I will think about ways to try to get
similar behavior. A solution will probably involve some heuristics
about how to find simpler units.

Hope this helps,

Robert


_______________________________________________
Maxima-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/maxima-discuss