Peter F
Peter F

Reputation: 322

How to substitute expressions containing units when using the ezunits package?

Without specifying units, I can express area and volume and have Maxima show the relationship:

(%i1) areaNoUnits: area = width * length$

(%i2) volumeNoUnits: volume  = area * height$

(%i3) volumeNoUnits, areaNoUnits;
(%o3)                    volume = height length width

(%i4) subst(areaNoUnits, volumeNoUnits);
(%o4)                    volume = height length width

Now I want to specify units so I will use the ezunits package.

The ` (backtick) operator is the building block of ezunits:

An expression a ` b represents a dimensional quantity, with a indicating a nondimensional quantity and b indicating the dimensional units.

When I add units to the area and volume expressions, evaluation and substitution do not work:

(%i1) load ("ezunits")$

(%i2) areaWithUnits: area ` m^2 = (width ` m) * (length ` m);
                                 2                   2
(%o2)                    area ` m  = length width ` m


(%i3) volumeWithUnits: volume ` m^3 = (area ` m^2) * (height ` m);
                                  3                  3
(%o3)                   volume ` m  = area height ` m


(%i4) volumeWithUnits, areaWithUnits;
                                  3                  3
(%o4)                   volume ` m  = area height ` m


(%i5) subst(areaWithUnits, volumeWithUnits);
                                  3                  3
(%o5)                   volume ` m  = area height ` m

The expected output is:

volumeWithUnits, areaWithUnits;
                            3                          3
                  volume ` m  = height length width ` m

I do not see a function in the ezunits package to do evaluation or substitution. What is the right way to do this?

Upvotes: 1

Views: 75

Answers (1)

Robert Dodier
Robert Dodier

Reputation: 17576

I would phrase it like this:

(%i2) load (ezunits) $

(%i3) width: W ` m;
(%o3)                         W ` m
(%i4) length: L ` m;
(%o4)                         L ` m
(%i5) area: width * length;
                                   2
(%o5)                       L W ` m
(%i6) height: H ` m;
(%o6)                         H ` m
(%i7) volume: area * height;
                                    3
(%o7)                      H L W ` m

I wrote each part as conceptualname: symbolforquantity ` unit and then wrote just conceptualname in further calculations, instead of conceptualname ` unit.

The substitution you tried in %i5 didn't work because subst is a purely formal substitution -- if there isn't a literal subexpression which is the same as the substituted-for expression, it doesn't match; subst doesn't look for rearrangements or factorizations which could help make a match. There are ways to work around that, so it might be possible to make your original formulation work, but I think it's better overall to sidestep the problem and work with conceptualname and symbolforquantity ` unit.

To say a little about what more one could do with expressions like %o7 above. There are at least two ways to replace symbols H, L, and W with specific values. One is to call subst:

(%i2) load (ezunits) $ 
(%i3) volume: H*L*W ` m^3;
                                    3
(%o3)                      H L W ` m
(%i4) subst ([L = 20, W = %pi], volume);
                                      3
(%o4)                     20 %pi H ` m

Another is to make use of ev.

(%i5) ev (volume, L = 20, W = %pi);
                                      3
(%o5)                     20 %pi H ` m

Note that at the input prompt, something, someflags, somevalues is equivalent to ev(something, someflags, somevalues).

(%i6) volume, L = 20, W = %pi;
                                      3
(%o6)                     20 %pi H ` m

This is just a convenience. Within a function, one has to say ev(...); the shorter syntax isn't understood there.

ev is often convenient, but it's generally simpler to predict what the result is going to be with subst instead.

Upvotes: 2

Related Questions