On Wed, 2010-04-07 at 18:46 +0100, Charles Ward wrote:
> The contribution by ?iga Lenar?i? seemed to be a fruitful way forward
He was invited to add his contribution to the additional packages, but
he wanted his code to right into core Maxima and perhaps even replace
the current makelist. That was not a good idea.
I think it is much better to start by extending what we already have
(makelist) without breaking anything.
The code I send in attachment is a sample of a safe replacement for
makelist, which adds two extensions:
(%i2) makelist2(i^2,i,5);
(%o2) [1, 4, 9, 16, 25]
(%i3) makelist2(i^2,i,2,10,3);
(%o3) [4, 25, 64]
If you want to try it out, copy it into your ~/.maxima directory and
load it with:
(%i1) load("makelist2");
> The command in Mathematica
> TableForm[Table[(i + j)^j, {i, 3}, {j, 3}], TableHeadings -> {{"A",
> "B", "C"}, {"X", "Y", "Z"}}]
With makelist2 you not get quite the same, but something close:
First, create the matrix:
(%i4) M: apply ( matrix, makelist2 (makelist2 ((i+j)^j, j, 3), i, 3));
[ 2 9 64 ]
[ ]
(%o4) [ 3 16 125 ]
[ ]
[ 4 25 216 ]
and then put on the headers (which are actually footers in this case):
(%i5) addcol ( addrow ( M, ["X","Y","Z"]), ["A","B","C",""]);
[ 2 9 64 A ]
[ ]
[ 3 16 125 B ]
(%o5) [ ]
[ 4 25 216 C ]
[ ]
[ X Y Z ]
In my opinion, it is clearer to work in steps, rather than having to
remember a mega-command that does everything at once. The next step
would be to extend "addcol" and "addrow" to be able to preprend columns
or row in addition to the current appending behavior.
Regards,
Jaime
-------------- next part --------------
(defmspec $makelist2 (x)
(setq x (cdr x))
(prog (n form arg a b c lv d)
(setq n (length x))
(if (or (< n 3) (> n 5))
(merror (intl:gettext "makelist: must have 3, 4 or 5 arguments.")))
(setq form (first x)
arg (second x)
a (meval (third x))
lv (cond ((= n 3)
(cond (($listp a)
(mapcar #'(lambda (u) (list '(mquote) u)) (cdr a)))
((integerp a)
(interval 1 a))
(t
(merror (intl:gettext "makelist: third argument must be an integer or evaluate to a list; found: ~M") a))))
(t
(setq b (meval (fourth x)))
(when (or (not (integerp (setq d (sub* b a)))) (< d -1))
(merror (intl:gettext "makelist: fourth argument minus third must be a nonnegative integer; found: ~M") d))
(if (= n 5)
(if
(or (not (integerp (setq c (meval (fifth x)))))
(< c 1))
(merror (intl:gettext "makelist: fifth argument must be a positive integer; found: ~M") c)
(interval2 a b c))
(interval a b)))))
(return
(do ((lv lv (cdr lv))
(ans))
((null lv) (cons '(mlist simp) (nreverse ans)))
(push (meval `(($ev)
,@(list (list '(mquote) form)
(list '(mequal simp) arg (car lv)))))
ans)))))
(defun interval2 (i j s)
(do ((nn i (add2 s nn))
(m 0 (add2 s m))
(k (sub* j i))
(ans))
((> m k) (nreverse ans))
(push nn ans)))