Configuring Maxima output as an improper fraction?



On 1/29/09, Luke Scharf <luke.scharf at clusterbee.net> wrote:

> Is there a way to configure Maxima to output the result as an improper
>  fraction?
>
>  For instance, if I enter 3/4+3/4, then it outputs 3/2.  Since I'm doing
>  arithmetic for a woodworking project, it would be easier if it would
>  output as 1+1/2.  Or 1+4/8 would be super-useful, too.  Is there a way
>  to make Maxima to do that?

Luke, I've attached some code which changes the way literal rational
numbers are displayed. It doesn't change the way expressions are
constructed, only the pretty-printing display. It doesn't change the
1-dimensional display, that could be done with the same approach.

(On re-reading your message, I see you've mentioned wxMaxima.
The following code doesn't change the wxMaxima display, only the
display of the command line Maxima. There is a way to get a similar
result in wxMaxima, by clobbering the default display function,
but I'm not familiar with the details.)

Since you are working with physical units, perhaps the ezunits package
is useful to you. Ezunits has been evolving over the last few releases,
probably you would want to use the most recent (Maxima 5.17.1).

(%i1) load ("stuff.lisp");
(%i2) load (ezunits);
(%i3) 7/8 ` inch;
                                   7
(%o3)                              - ` inch
                                   8
(%i4) -3/8 ` inch;
                                    3
(%o4)                            (- -) ` inch
                                    8
(%i5) 3 ` feet - (5 + 5/8) ` inch;
                                           5
(%o5)                      3 ` feet + (- 5 -) ` inch
                                           8
(%i6) % `` feet;
Computing conversions to base units; may take a moment.
                                    17
(%o6)                             2 -- ` feet
                                    32
(%i7) % `` inch;
                                     3
(%o7)                             30 - ` inch
                                     8

It occurs to me that it might be nice to display lengths as, say,
X feet + Y inches after carrying out arithmetic. At present ezunits
can't do that, I'll think about how it could be done.

I haven't tested the code below very extensively, so I wouldn't be
surprised if you discover some bugs. Hope it's useful anyway.

best

Robert Dodier

PS.
;; display literal rational numbers as improper fractions
;; copyright 2009 by Robert Dodier
;; I release this work under terms of the GNU General Public License
;; save this stuff in a file and then enter load("stuff.lisp");

(setf (symbol-function 'dim-rat-original) #'dim-rat)

(defun dim-rat (form result)
  (if (or (great form 1) (great -1 form))
    (dim-rat-improper form result)
    (dim-rat-original form result)))

(setf (get 'foo 'rbp) 10)
(setf (get 'foo 'lbp) 10)
(setf (get 'foo 'dissym) '(#\ ))

(defun dim-rat-improper (form result)
  (let ((denom (third form)))
    (if (great 0 form)
      (multiple-value-bind (whole remainder)
        (mfuncall '$ceiling form)
        (dimension-infix `((foo) ,whole ((rat) ,(- remainder) ,denom))
result))
      (multiple-value-bind (whole remainder)
        (mfuncall '$floor form)
        (dimension-infix `((foo) ,whole ((rat) ,remainder ,denom))
result)))))