Subject: Please help me come up with some good examples
From: Barton Willis
Date: Fri, 27 Jun 2003 10:29:57 -0500
A user can change the functions that add and multiply matrix elements.
I once tried unsuccessfully to figure out how to do this with one of
the other CAS. At first thought, re-defining these functions might
not seem useful, but it is. Here is one application.
Let m be the matrix of a weighted graph. Think of m as giving the
hiway distances between cities. If no road directly connects two
cities, let the matrix element be infinity. The diagonal elements of
m all vanish. We can compute the shortest paths between cities by
re-defining the functions that add and multiply matrix elements.
[barton@localhost orthopoly-0.93]$ maxima
Maxima 5.9.0rc3 http://maxima.sourceforge.net
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
This is a development version of Maxima. The function bug_report()
provides bug reporting information.
(C1) matrix_element_add : lambda([[x]], apply(min,x));
(D1) LAMBDA([[x]], APPLY(MIN, x))
(C2) matrix_element_mult : lambda([x,y], if (x = inf) or (y = inf) then inf
else x+y);
(D2) LAMBDA([x, y], IF x = INF OR y = INF THEN INF ELSE x + y)
(C3) m : matrix([0,3,5],[inf,0,8],[7,inf,0]);
[ 0 3 5 ]
[ ]
(D3) [ INF 0 8 ]
[ ]
[ 7 INF 0 ]
The shortest paths via two roads is given by
(C4) m.m;
[ 0 3 5 ]
[ ]
(D4) [ 15 0 8 ]
[ ]
[ 7 10 0 ]
The shortest paths via three roads is given by
(C5) m.m.m;
[ 0 3 5 ]
[ ]
(D5) [ 15 0 8 ]
[ ]
[ 7 10 0 ]
(C6) quit();
We could find the longest path by using max instead of min. Cool.