Subject: More intelligent solution for units problems
From: C Y
Date: Sat, 21 May 2005 11:09:42 -0700 (PDT)
OK, I think I have a (reasonably) elegant solution for the problems I
was seeing - specifically sin(kg/s) wiping out big time and in some
cases taking Maxima with it. It required the introduction of
pre_eval_functions along with post_eval_functions, but the results are
excellent. Essentially what I'm guessing was happening is nformat was
moving units to their own section at all stages of the calculation, and
at some point sin gets a structure it can't handle and wipes out. The
changes now in place allow nformat to work its unit magic only when
processunits is doing its thing, which means all mathematical work
takes place as normal and avoids the oddities of the formatting. The
problem was the variable used to accomplish this can't be reset until
AFTER the result is displayed, or it's all undone. Hence
pre_eval_functions, which resets things to the defaults supplied before
any new mathematical work begins, but after display and friends are
done messing with the previous result's formatting.
Here are some examples (also in the commit message) which show where
matters currently stand. groupadd doesn't handle sin(s) or -1/s
properly, but that ought to be fixible. Once that's ironed out, I'll
bump the version to 0.50 and fill in the rest of the blanks
(temperature, setunitprefix, rad and deg, filling out unit defs, other
fun stuff.) Once that's under control I'll ask for testing and then
propose replacing the current units file (which is a simple set of
definitions only) with this code.
Input:
load("./newunits.mac");
a1 : a;
a2 : a+b;
a3 : a+b+c;
a4 : a*s+c;
a5 : a+c*s;
a6 : a*kg/s+c;
a7 : a*kg/s+c*kg/s;
a8 : a*kg/s-c*kg/s+d*kg/s;
a9 : a*kg*m/s^2+c;
a10 : a*kg*m/s^2+c*kg*m/s^2;
a11 : a*kg*m/s^2+c*kg*m/s^2-d*kg*m/s^2;
a12 : a*kg/s+c*kg/s+d*kg*m/s^2;
a13 : a*kg/s+c*kg*m/s^2+d*kg*m/s^2;
a14 : a*kg/s+b*N+c*kg/s+d*N+e*kg/s;
a15 : 1/60*kg/s+a*kg/s+b*kg/s+c*kg/s^2+d*kg/s^2;
a16 : a/60*kg/s+a*kg/s+b*kg/s+c*kg/s^2+d*kg/s^2;
a17 : (a/60)*kg/s+a*b*kg/s;
a18 : a*kg*m/s^2+b*N*J+c*kg/s+d*N*J-e*kg*m/s^2;
a19 : sin(s);
a20 : -1/s;
a21 : sin(kg/s);
enablegroupbyadd();
a1 : a;
a2 : a+b;
a3 : a+b+c;
a4 : a*s+c;
a5 : a+c*s;
a6 : a*kg/s+c;
a7 : a*kg/s+c*kg/s;
a8 : a*kg/s-c*kg/s+d*kg/s;
a9 : a*kg*m/s^2+c;
a10 : a*kg*m/s^2+c*kg*m/s^2;
a11 : a*kg*m/s^2+c*kg*m/s^2-d*kg*m/s^2;
a12 : a*kg/s+c*kg/s+d*kg*m/s^2;
a13 : a*kg/s+c*kg*m/s^2+d*kg*m/s^2;
a14 : a*kg/s+b*N+c*kg/s+d*N+e*kg/s;
a15 : 1/60*kg/s+a*kg/s+b*kg/s+c*kg/s^2+d*kg/s^2;
a16 : a/60*kg/s+a*kg/s+b*kg/s+c*kg/s^2+d*kg/s^2;
a17 : (a/60)*kg/s+a*b*kg/s;
a18 : a*kg*m/s^2+b*N*J+c*kg/s+d*N*J-e*kg*m/s^2;
a19 : sin(s);
a20 : -1/s;
a21 : sin(kg/s);
and output:
(%o1) ./newunits.mac
(%i2)
(%o2) a
(%i3)
(%o3) b + a
(%i4)
(%o4) c + b + a
(%i5)
(%o5) (a) (s) + c
(%i6)
(%o6) (c) (s) + a
(%i7)
kg
(%o7) (a) (--) + c
s
(%i8)
kg kg
(%o8) (c) (--) + (a) (--)
s s
(%i9)
kg kg kg
(%o9) (d) (--) + (- c) (--) + (a) (--)
s s s
(%i10)
kg m
(%o10) (a) (----) + c
2
s
(%i11)
kg m kg m
(%o11) (c) (----) + (a) (----)
2 2
s s
(%i12)
kg m kg m kg m
(%o12) (- d) (----) + (c) (----) + (a) (----)
2 2 2
s s s
(%i13)
kg kg kg m
(%o13) (c) (--) + (a) (--) + (d) (----)
s s 2
s
(%i14)
kg kg m kg m
(%o14) (a) (--) + (d) (----) + (c) (----)
s 2 2
s s
(%i15)
kg kg kg kg m kg m
(%o15) (e) (--) + (c) (--) + (a) (--) + (d) (----) + (b) (----)
s s s 2 2
s s
(%i16)
kg kg 1 kg kg kg
(%o16) (b) (--) + (a) (--) + (--) (--) + (d) (--) + (c) (--)
s s 60 s 2 2
s s
(%i17)
kg 61 a kg kg kg
(%o17) (b) (--) + (----) (--) + (d) (--) + (c) (--)
s 60 s 2 2
s s
(%i18)
kg a kg
(%o18) (a b) (--) + (--) (--)
s 60 s
(%i19)
2 3 2
3
kg kg m kg m kg m kg m
(%o19) (c) (--) + (- e) (----) + (a) (----) + (d) (------) + (b)
(------)
s 2 2 4 4
s s s s
(%i20)
(%o20) sin(s)
(%i21)
1
(%o21) - -
s
(%i22)
kg
(%o22) sin(--)
s
(%i23)
(%o23) [processunits, groupadd]
(%i24)
(%o24) a
(%i25)
(%o25) b + a
(%i26)
(%o26) c + b + a
(%i27)
(%o27) (a) (s) + c
(%i28)
(%o28) (c) (s) + a
(%i29)
kg
(%o29) (a) (--) + c
s
(%i30)
kg
(%o30) (c + a) (--)
s
(%i31)
kg
(%o31) (d - c + a) (--)
s
(%i32)
kg m
(%o32) (a) (----) + c
2
s
(%i33)
kg m
(%o33) (c + a) (----)
2
s
(%i34)
kg m
(%o34) (- d + c + a) (----)
2
s
(%i35)
kg kg m
(%o35) (c + a) (--) + (d) (----)
s 2
s
(%i36)
kg kg m
(%o36) (a) (--) + (d + c) (----)
s 2
s
(%i37)
kg kg m
(%o37) (e + c + a) (--) + (d + b) (----)
s 2
s
(%i38)
1 kg kg
(%o38) (b + a + --) (--) + (d + c) (--)
60 s 2
s
(%i39)
61 a kg kg
(%o39) (b + ----) (--) + (d + c) (--)
60 s 2
s
(%i40)
a kg
(%o40) (a b + --) (--)
60 s
(%i41)
2 3
kg kg m kg m
(%o41) (c) (--) + (a - e) (----) + (d + b) (------)
s 2 4
s s
(%i42)
(%o42) s
(%i43)
1
(%o43) -
s
(%i44)
kg
(%o44) --
s
Discover Yahoo!
Have fun online with music videos, cool games, IM and more. Check it out!
http://discover.yahoo.com/online.html