Discussion:
Configuring Maxima output as an improper fraction?
Luke Scharf
2009-01-29 19:21:13 UTC
Permalink
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?

Many thanks!
-Luke

P.S. Please CC me -- I'm not subscribed to the list.

P.P.S. I'm impressed with Maxima -- after using Mathematica and Matlab,
I'm thrilled to find a mature open-source computer algebra system! With
wxMaxima, I'll certainly be recommending it to many people!
van Nek
2009-01-30 16:27:36 UTC
Permalink
Hi Luke,

for better readability in an e-mail I use
(%i1) display2d:false$

I guess you are dealing with positive numbers. So the following works for positive numbers.

(%i2) x: 23/7$
(%i3) f(x):= [floor(x), mod(x,1)]$
(%i4) f(x);
(%o4) [3,2/7]

If this representation fits your needs you can format your results with such a function. The
computation backwards is done by an apply "+" or by accessing the list entries via
%[1]+%[2].

(%i5) apply("+",%);
(%o5) 23/7

If you really want to print the "+" here is a quick hack:

(%i6) :lisp(defun $unsimplified_plus (a b) `((mplus simp) ,b ,a))
$UNSIMPLIFIED_PLUS
(%i6) f(x):= unsimplified_plus(floor(x), mod(x,1))$
(%i7) f(x);
(%o7) 3+2/7

This unsimplified "+" can be reevaluated by two single quotes:

(%i8) ''%;
(%o8) 23/7


And there is another one:

(%i9) f(x):= sconcat(floor(x), "+", mod(x,1))$
(%i10) f(x);
(%o10) "3+2/7"

This is a string. (In the default - display2d:true - mode you won't see the quotes.)

(%i11) eval_string(%);
(%o11) 23/7


HTH
Volker van Nek
Post by Luke Scharf
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?
Many thanks!
-Luke
P.S. Please CC me -- I'm not subscribed to the list.
P.P.S. I'm impressed with Maxima -- after using Mathematica and Matlab,
I'm thrilled to find a mature open-source computer algebra system! With
wxMaxima, I'll certainly be recommending it to many people!
_______________________________________________
Maxima mailing list
http://www.math.utexas.edu/mailman/listinfo/maxima
Stavros Macrakis
2009-01-30 17:35:13 UTC
Permalink
Post by van Nek
(%i6) :lisp(defun $unsimplified_plus (a b) `((mplus simp) ,b ,a))
(%i6) f(x):= unsimplified_plus(floor(x), mod(x,1))$
Be aware that any further arithmetic with the results of
unsimplified_plus may not be fully simplified. For example,
f(-3/2)+f(3/2) => 1 - 2 + 1, not zero.
Though two single quotes work in this case, more generally if you want
an expression resimplified, you should use expand(expr,1,1). You may
find it convenient to define resimplify(expr):=expand(expr,1,1)$

-s

-s
Robert Dodier
2009-01-31 16:30:09 UTC
Permalink
Post by Luke Scharf
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)))))
Richard Fateman
2009-02-02 15:15:50 UTC
Permalink
Why not multiply your result by 1.0, and use decimal. It seems to me
that you probably don't want improper fractions that are smaller than
1/16 or 1/32. You can buy a ruler that has a decimal to fraction table
engraved on the back.
RJF
Luke Scharf
2009-02-02 15:47:40 UTC
Permalink
Post by Richard Fateman
Why not multiply your result by 1.0, and use decimal. It seems to me
that you probably don't want improper fractions that are smaller than
1/16 or 1/32. You can buy a ruler that has a decimal to fraction
table engraved on the back.
I thought about that, and about making a lookup table for the
decimals->fraction conversions that I haven't memorized yet, and using a
conventional calculator -- but I couldn't find my calculator, and I was
stubborn, and it was late, and it was a good opportunity to learn a new
application. Plus, the reason I wanted to use a symbolic math system
was because I wanted my calculations to match my application, not the
other way around. :-)

I guess I just have an engineer's attitude toward math involving Real
numbers -- no matter how much guff my girlfriend (who is pursuing a PHD
in a sciency field) gives me for it... :-)

-Luke
Luke Scharf
2009-02-03 16:40:40 UTC
Permalink
Post by Robert Dodier
Post by Luke Scharf
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.
Very cool -- many thanks!

-Luke

Loading...