4 - Forth Interest Group
4 - Forth Interest Group
4 - Forth Interest Group
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
\ Locate i in row.<br />
\ See WHERE-IN-VECTOR for results on parameter stack.<br />
: WHERE IN-ROW ( i -- n flag )<br />
NEXT-IS<br />
RIGHT \ Set NEXT function<br />
IDX IS ?OL \ Index to modify is COL<br />
W H E ~<br />
- IN-VECTOR \ Find him<br />
\ Locate i in column.<br />
\ See WHERE-IN-VECTOR for results on parameter stack.<br />
: WHERE IN COL ( i -- n flag )<br />
NEXT-7s<br />
%WN \ Set NEXT function<br />
IDX IS ROW \ Index to modify is ROW<br />
WHERE - YN - VECTOR \ Find him<br />
\ ........................................<br />
\ ** INSERTING/REMOVING MATRIX ELEMENTS **<br />
\ ........................................<br />
\ Wire a sparse matrix element into a row or column vector.<br />
\ i is the sparse matrix element, n is the predecessor in the<br />
\ vector. n = NIL if i is to be wired in at the head of the list<br />
: WIRE-IN ( n i -- )<br />
OVER NIL = \ Wiring in at head?<br />
IFSWAP DROP \ Discard NIL<br />
R/C BASE @ W@ \ Get old head<br />
OVER &SMATRIX .NEXT w ! \ Old head is our next<br />
R/C BASE @ W! \ We are new head<br />
ELSE OVER &SMATRIX.NEXT W@ \ Fix our next<br />
OVER &SMATRIX.NEXT W!<br />
SWAP &SMATRIX.NEXT W! \ Fix predecessor next<br />
ENDIF<br />
\ Unwire a sparse matrix element from a row or column vector.<br />
\ Stack acts same as WIRE-IN.<br />
: UNWIRE ( n i -- )<br />
OVER NIL = \ Are we head?<br />
IFSWAP DROP \ Discard NIL<br />
&SMATRIX.NEXT W@ \ Our next is new head<br />
R/C BASE @ W!<br />
ELSE &SMATRIX .NEXT W@ \ Our next ...<br />
SWAP &SMATRIX.NEXT W! \ ... is predecessor's next<br />
ENDIF<br />
\ Given i -- index to a sparse matrix element -- link this<br />
\ element into the sparse matrix at row,col.<br />
: INTO - SMATRIX ( row col i -- )<br />
>R \ Save i<br />
\ Set the row & column members<br />
OVER R@ &SMATRIX.ROW C! \ Set row<br />
DUP R@ &SMATRIX. COL C ! \ Set column<br />
\ Wire element into row<br />
SWAP SET ROW BASE \ Set the row base address<br />
R@ WHERETIN - %W \ Locate where to wire in<br />
O= \ Flag=O means slot is free<br />
IF R@ WIRE-IN \ Wire him in<br />
ELSE ." Error attaching to row" CR<br />
ABORT<br />
ENDIF<br />
\ Wire new element into column<br />
SET - COL - BASE \ Set the column base addres<br />
nents of an element would<br />
allow for truly gargantuan<br />
sparse matrices. I suppose if<br />
you needed sparse multi-<br />
dimensional arrays, you could<br />
add the necessary anchoring<br />
arrays, extend the element<br />
structure, and add the neces-<br />
sary words for access to<br />
element components. Off the<br />
top of my head, I can't imagine<br />
an application for such<br />
structures.. . but I'll bet there's<br />
one out there somewhere.<br />
Rick Grehan is the Technical Director of<br />
BYTE Labs. He wrole the code for this<br />
article with CreativeSolutions' Mac<strong>Forth</strong>.<br />
<strong>Forth</strong> Dimensions 39 November 1993 December