Re: Simplifying Units in Ezunits Package
"Mateusz M." <[email protected]> Sat, 30 May 2026 14:50:10 +0200
| Newsgroups | gmane.comp.mathematics.maxima.general |
|---|---|
| Message-ID | <CAOJ_3NFuAmiyMxkgwKuT6-8GoPTe88JsHxLqFsKQCmHhFeHy_w@mail.gmail.com> |
Hello everyone,
Your message, Michael, is very interesting. Thank you for sharing it. I’m
also reading the posts in the thread “ezunits fundamental_units quite slow”
with great interest, and I’m curious to see the outcome.
I came across an example where a single "apply1(expr, rfoo)" wasn't enough.
The denominator of this equation requires more than one iteration to be
fully simplified:
(%i6) I_1: 0.1 `nA;
I_2: 0.5 `mA;
I_3: 0.4 `nA;
R_1: 2 `kOhm;
V_1: 0.2 `kOhm*nA;
expr: I_1/((I_2 + I_3)*R_1 + V_1);
(expr) 0.1 ` nA/((0.5 ` mA+0.4 ` nA)*(2 ` kOhm)+0.2 ` kOhm*nA)
(%i7) apply1(expr, rfoo);
(%o7) 0.1 ` nA/(0.2 ` kOhm*nA+0.0010000008 ` A*kOhm)
(%i8) apply1(%, rfoo) ``1/GOhm;
(%o8) 0.0999999000001 ` 1/GOhm
To simplify this process, I wrote a very short function that calls
"apply1(expr, rfoo)" in a loop until the next result matches the previous
one, assuming this is the simplest expression possible.
Maybe someone will find it useful:
simplify_units(expr) :=
block ([previous, current : expr],
previous = false,
while not (current = previous) do (
previous : current,
current : apply1(current, rfoo)),
current)$
This could surely be improved. Perhaps it would be worth adding the maximum
possible number of iterations.
I also measured the execution time of the methods of simplifying the
example expression from the beginning of the mail. All of them succeeded.
- Function "to_si()" provided by Serge: 5.35 s
- Function "simplify_units()" which calls Robert's original "apply1(expr,
rfoo)" function: 4 s
- Function "simplify_units()" calling modified "apply1(expr, rfoo)"
function, where "fundamental_units" is replaced with "units" as suggested
by Michael: 3.21 s
Yes, my old laptop is slow =)
Best regards,
Mateusz
_______________________________________________
Maxima-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/maxima-discuss