1struct::matrix(n)             Tcl Data Structures            struct::matrix(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       struct::matrix - Create and manipulate matrix objects
9

SYNOPSIS

11       package require Tcl  8.2
12
13       package require struct::matrix  ?2.0.1?
14
15       ::struct::matrix ?matrixName? ?=|:=|as|deserialize source?
16
17       matrixName option ?arg arg ...?
18
19       matrixName = sourcematrix
20
21       matrixName --> destmatrix
22
23       matrixName add column ?values?
24
25       matrixName add row ?values?
26
27       matrixName add columns n
28
29       matrixName add rows n
30
31       matrixName cells
32
33       matrixName cellsize column row
34
35       matrixName columns
36
37       matrixName columnwidth column
38
39       matrixName delete column column
40
41       matrixName delete columns n
42
43       matrixName delete row row
44
45       matrixName delete rows n
46
47       matrixName deserialize serialization
48
49       matrixName destroy
50
51       matrixName format 2string ?report?
52
53       matrixName format 2chan ??report? channel?
54
55       matrixName get cell column row
56
57       matrixName get column column
58
59       matrixName get rect column_tl row_tl column_br row_br
60
61       matrixName get row row
62
63       matrixName insert column column ?values?
64
65       matrixName insert row row ?values?
66
67       matrixName link ?-transpose? arrayvar
68
69       matrixName links
70
71       matrixName rowheight row
72
73       matrixName rows
74
75       matrixName search ?-nocase? ?-exact|-glob|-regexp? all pattern
76
77       matrixName  search  ?-nocase? ?-exact|-glob|-regexp? column column pat‐
78       tern
79
80       matrixName search ?-nocase? ?-exact|-glob|-regexp? row row pattern
81
82       matrixName  search  ?-nocase?  ?-exact|-glob|-regexp?  rect   column_tl
83       row_tl column_br row_br pattern
84
85       matrixName serialize ?column_tl row_tl column_br row_br?
86
87       matrixName set cell column row value
88
89       matrixName set column column values
90
91       matrixName set rect column row values
92
93       matrixName set row row values
94
95       matrixName sort columns ?-increasing|-decreasing? row
96
97       matrixName sort rows ?-increasing|-decreasing? column
98
99       matrixName swap columns column_a column_b
100
101       matrixName swap rows row_a row_b
102
103       matrixName transpose
104
105       matrixName unlink arrayvar
106
107_________________________________________________________________
108

DESCRIPTION

110       A  matrix  is a rectangular collection of cells, i.e. organized in rows
111       and columns. Each cell contains exactly one value  of  arbitrary  form.
112       The cells in the matrix are addressed by pairs of integer numbers, with
113       the first (left) number in the pair specifying the column and the  sec‐
114       ond (right) number specifying the row the cell is in. These indices are
115       counted from 0 upward. The special non-numeric index end refers to  the
116       last  row or column in the matrix, depending on the context. Indices of
117       the form end-number are counted from the end of the row or column, like
118       they  are  for  standard Tcl lists. Trying to access non-existing cells
119       causes an error.
120
121       The matrices here are created empty, i.e. they have  neither  rows  nor
122       columns.  The  user  then  has to add rows and columns as needed by his
123       application. A specialty of this structure is the ability to export  an
124       array-view onto its contents. Such can be used by tkTable, for example,
125       to link the matrix into the display.
126
127       The main command of the package is:
128
129       ::struct::matrix ?matrixName? ?=|:=|as|deserialize source?
130              The command creates a  new  matrix  object  with  an  associated
131              global  Tcl  command whose name is matrixName.  This command may
132              be used to invoke various operations on the matrix.  It has  the
133              following general form:
134
135              matrixName option ?arg arg ...?
136                     Option  and  the args determine the exact behavior of the
137                     command.
138
139       If matrixName is not specified a unique name will be generated  by  the
140       package  itself.  If  a source is specified the new matrix will be ini‐
141       tialized to it. For the operators =, :=, and as the argument source  is
142       interpreted  as  the  name of another matrix object, and the assignment
143       operator = will be executed. For deserialize the source is a serialized
144       matrix object and deserialize will be executed.
145
146       In other words
147
148
149           ::struct::matrix mymatrix = b
150
151
152       is equivalent to
153
154
155           ::struct::matrix mymatrix
156           mymatrix = b
157
158
159       and
160
161
162           ::struct::matrix mymatrix deserialize $b
163
164
165       is equivalent to
166
167
168           ::struct::matrix mymatrix
169           mymatrix deserialize $b
170
171
172       The following commands are possible for matrix objects:
173
174       matrixName = sourcematrix
175              This  is  the  assignment operator for matrix objects. It copies
176              the matrix contained in the matrix object sourcematrix over  the
177              matrix  data  in  matrixName. The old contents of matrixName are
178              deleted by this operation.
179
180              This operation is in effect equivalent to
181
182
183                  matrixName deserialize [sourcematrix serialize]
184
185
186       matrixName --> destmatrix
187              This is the reverse assignment operator for matrix  objects.  It
188              copies the matrix contained in the matrix object matrixName over
189              the matrix data in the object destmatrix.  The old  contents  of
190              destmatrix are deleted by this operation.
191
192              This operation is in effect equivalent to
193
194
195                  destmatrix deserialize [matrixName serialize]
196
197
198       matrixName add column ?values?
199              Extends  the  matrix by one column and then acts like set column
200              (see below) on this new column if there  were  values  supplied.
201              Without  values  the  new cells will be set to the empty string.
202              The new column is appended immediately behind the last  existing
203              column.
204
205       matrixName add row ?values?
206              Extends  the  matrix  by one row and then acts like set row (see
207              below) on this new row if there were  values  supplied.  Without
208              values  the  new  cells will be set to the empty string. The new
209              row is appended immediately behind the last existing row.
210
211       matrixName add columns n
212              Extends the matrix by n columns. The new cells will  be  set  to
213              the  empty  string.  The  new  columns  are appended immediately
214              behind the last existing column.  A  value  of  n  equal  to  or
215              smaller than 0 is not allowed.
216
217       matrixName add rows n
218              Extends  the  matrix by n rows. The new cells will be set to the
219              empty string. The new rows are appended immediately  behind  the
220              last  existing  row.  A value of n equal to or smaller than 0 is
221              not allowed.
222
223       matrixName cells
224              Returns the number of cells currently  managed  by  the  matrix.
225              This is the product of rows and columns.
226
227       matrixName cellsize column row
228              Returns  the  length  of  the string representation of the value
229              currently contained in the addressed cell.
230
231       matrixName columns
232              Returns the number of columns currently managed by the matrix.
233
234       matrixName columnwidth column
235              Returns the length of the longest string representation  of  all
236              the  values  currently  contained  in the cells of the addressed
237              column if these are all spanning only one line. For cell  values
238              spanning  multiple  lines  the length of their longest line goes
239              into the computation.
240
241       matrixName delete column column
242              Deletes the specified column from the matrix and shifts all col‐
243              umns with higher indices one index down.
244
245       matrixName delete columns n
246              Deletes  n  columns from the right of the matrix. The value of n
247              has to satisfy the constraint "0 < n < [matrixName columns]"
248
249       matrixName delete row row
250              Deletes the specified row from the matrix  and  shifts  all  row
251              with higher indices one index down.
252
253       matrixName delete rows n
254              Deletes n rows from the bottom of the matrix. The value of n has
255              to satisfy the constraint "0 < n < [matrixName rows]"
256
257       matrixName deserialize serialization
258              This is the complement to serialize. It replaces matrix data  in
259              matrixName with the matrix described by the serialization value.
260              The old contents of matrixName are deleted by this operation.
261
262       matrixName destroy
263              Destroys the matrix, including its storage space and  associated
264              command.
265
266       matrixName format 2string ?report?
267              Formats the matrix using the specified report object and returns
268              the string containing the result of this operation.  The  report
269              has to support the printmatrix method. If no report is specified
270              the system will use an internal report definition to format  the
271              matrix.
272
273       matrixName format 2chan ??report? channel?
274              Formats  the matrix using the specified report object and writes
275              the string containing the result  of  this  operation  into  the
276              channel.  The  report  has  to  support  the printmatrix2channel
277              method.  If no report is specified the system will use an inter‐
278              nal  report  definition  to  format the matrix. If no channel is
279              specified the system will use stdout.
280
281       matrixName get cell column row
282              Returns the value currently contained in the cell identified  by
283              row and column index.
284
285       matrixName get column column
286              Returns  a list containing the values from all cells in the col‐
287              umn identified by the index. The contents of the cell in  row  0
288              are stored as the first element of this list.
289
290       matrixName get rect column_tl row_tl column_br row_br
291              Returns a list of lists of cell values. The values stored in the
292              result come from the sub-matrix whose top-left and  bottom-right
293              cells  are  specified by column_tl, row_tl and column_br, row_br
294              resp. Note that the following equations have to be  true:  "col‐
295              umn_tl <= column_br" and "row_tl <= row_br". The result is orga‐
296              nized as follows: The outer list is the list of rows,  its  ele‐
297              ments  are  lists  representing  a  single row. The row with the
298              smallest index is the first element of the outer list. The  ele‐
299              ments  of  the row lists represent the selected cell values. The
300              cell with the smallest index is the first element  in  each  row
301              list.
302
303       matrixName get row row
304              Returns  a  list containing the values from all cells in the row
305              identified by the index. The contents of the cell  in  column  0
306              are stored as the first element of this list.
307
308       matrixName insert column column ?values?
309              Extends  the  matrix by one column and then acts like set column
310              (see below) on this new column if there  were  values  supplied.
311              Without  values  the  new cells will be set to the empty string.
312              The new column is inserted just before the column  specified  by
313              the  given index. This means, if column is less than or equal to
314              zero, then the new column is inserted at the  beginning  of  the
315              matrix, before the first column. If column has the value end, or
316              if it is greater than or equal to the number of columns  in  the
317              matrix,  then  the  new column is appended to the matrix, behind
318              the last column. The old column at the chosen index and all col‐
319              umns with higher indices are shifted one index upward.
320
321       matrixName insert row row ?values?
322              Extends  the  matrix  by one row and then acts like set row (see
323              below) on this new row if there were  values  supplied.  Without
324              values  the  new  cells will be set to the empty string. The new
325              row is inserted just before  the  row  specified  by  the  given
326              index.  This  means,  if row is less than or equal to zero, then
327              the new row is inserted at the beginning of the  matrix,  before
328              the  first  row.  If  row has the value end, or if it is greater
329              than or equal to the number of rows in the matrix, then the  new
330              row  is appended to the matrix, behind the last row. The old row
331              at that index and all rows with higher indices are  shifted  one
332              index upward.
333
334       matrixName link ?-transpose? arrayvar
335              Links  the  matrix  to  the specified array variable. This means
336              that the contents of all cells in the matrix is  stored  in  the
337              array  too, with all changes to the matrix propagated there too.
338              The contents of the cell (column,row) is  stored  in  the  array
339              using  the key column,row. If the option -transpose is specified
340              the key row,column will be used instead. It is possible to  link
341              the  matrix  to more than one array. Note that the link is bidi‐
342              rectional, i.e. changes to the array are mirrored in the  matrix
343              too.
344
345       matrixName links
346              Returns  a  list containing the names of all array variables the
347              matrix was linked to through a call to method link.
348
349       matrixName rowheight row
350              Returns the height of the specified row in lines.  This  is  the
351              highest  number of lines spanned by a cell over all cells in the
352              row.
353
354       matrixName rows
355              Returns the number of rows currently managed by the matrix.
356
357       matrixName search ?-nocase? ?-exact|-glob|-regexp? all pattern
358              Searches the whole matrix for cells  matching  the  pattern  and
359              returns a list with all matches. Each item in the aforementioned
360              list is a list itself and contains the column and row  index  of
361              the  matching  cell,  in  this order. The results are ordered by
362              column first and row second, both times in ascending order. This
363              means  that  matches  to the left and the top of the matrix come
364              before matches to the right and down.
365
366              The type of the pattern (string, glob,  regular  expression)  is
367              determined  by the option after the search keyword. If no option
368              is given it defaults to -exact.
369
370              If the option -nocase is specified  the  search  will  be  case-
371              insensitive.
372
373       matrixName  search  ?-nocase? ?-exact|-glob|-regexp? column column pat‐
374       tern
375              Like search all, but the search is restricted to  the  specified
376              column.
377
378       matrixName search ?-nocase? ?-exact|-glob|-regexp? row row pattern
379              Like  search  all, but the search is restricted to the specified
380              row.
381
382       matrixName  search  ?-nocase?  ?-exact|-glob|-regexp?  rect   column_tl
383       row_tl column_br row_br pattern
384              Like  search  all, but the search is restricted to the specified
385              rectangular area of the matrix.
386
387       matrixName serialize ?column_tl row_tl column_br row_br?
388              This method serializes the sub-matrix spanned up by the  rectan‐
389              gle  specification.  In  other words it returns a tcl value com‐
390              pletely describing that matrix. If no rectangle is specified the
391              whole  matrix will be serialized.  This allows, for example, the
392              transfer of matrix objects (or  parts  thereof)  over  arbitrary
393              channels,  persistence,  etc.  This method is also the basis for
394              both the copy constructor and the assignment operator.
395
396              The result of this method has to be semantically identical  over
397              all  implementations  of the matrix interface. This is what will
398              enable us to copy matrix data between different  implementations
399              of the same interface.
400
401              The result is a list containing exactly three items.
402
403              The  first  two  elements of the list specify the number of rows
404              and columns of the matrix, in that  order.  The  values  integer
405              numbers greater than or equal to zero.
406
407              The  last  element of the list contains the values of the matrix
408              cells we have serialized, in the form of  a  value  like  it  is
409              returned  by  the get rect. However empty cells to the right and
410              bottom of the matrix can be left out of that value as  the  size
411              information  in  the  serialization allows the receiver the cre‐
412              ation of a matrix with the proper size despite the missing  val‐
413              ues.
414
415                  # A possible serialization for the matrix structure
416                  #
417                  # | a b d g |
418                  # | c e     |
419                  # | f       |
420                  #
421                  # is
422                  #
423                  # 3 4 {{a b d g} {c e} {f}}
424
425
426
427       matrixName set cell column row value
428              Sets the value in the cell identified by row and column index to
429              the data in the third argument.
430
431       matrixName set column column values
432              Sets the values in the cells identified by the column  index  to
433              the  elements  of  the list provided as the third argument. Each
434              element of the list is assigned to one cell, with the first ele‐
435              ment  going into the cell in row 0 and then upward. If there are
436              less values in the list than there are rows the  remaining  rows
437              are  set  to  the  empty string. If there are more values in the
438              list than there are rows the superfluous elements  are  ignored.
439              The matrix is not extended by this operation.
440
441       matrixName set rect column row values
442              Takes  a  list  of lists of cell values and writes them into the
443              submatrix whose top-left cell is specified by the  two  indices.
444              If  the  sublists  of  the outerlist are not of equal length the
445              shorter sublists will be filled with empty strings to the length
446              of  the  longest sublist. If the submatrix specified by the top-
447              left cell and the number of  rows  and  columns  in  the  values
448              extends  beyond  the  matrix we are modifying the over-extending
449              parts of the values are ignored, i.e. essentially cut off.  This
450              subcommand  expects  its  input  in  the  format  as returned by
451              getrect.
452
453       matrixName set row row values
454              Sets the values in the cells identified by the row index to  the
455              elements  of  the list provided as the third argument. Each ele‐
456              ment of the list is assigned to one cell, with the first element
457              going  into  the  cell in column 0 and then upward. If there are
458              less values in the list than there  are  columns  the  remaining
459              columns are set to the empty string. If there are more values in
460              the list than there are columns  the  superfluous  elements  are
461              ignored. The matrix is not extended by this operation.
462
463       matrixName sort columns ?-increasing|-decreasing? row
464              Sorts  the columns in the matrix using the data in the specified
465              row as the key to sort by. The options -increasing and -decreas‐
466              ing  have the same meaning as for lsort.  If no option is speci‐
467              fied -increasing is assumed.
468
469       matrixName sort rows ?-increasing|-decreasing? column
470              Sorts the rows in the matrix using the  data  in  the  specified
471              column  as  the  key  to  sort  by.  The options -increasing and
472              -decreasing have the same meaning as for lsort.  If no option is
473              specified -increasing is assumed.
474
475       matrixName swap columns column_a column_b
476              Swaps the contents of the two specified columns.
477
478       matrixName swap rows row_a row_b
479              Swaps the contents of the two specified rows.
480
481       matrixName transpose
482              Transposes  the contents of the matrix, i.e. swaps rows for col‐
483              umns and vice versa.
484
485       matrixName unlink arrayvar
486              Removes the link between the matrix and the specified arrayvari‐
487              able, if there is one.
488

EXAMPLES

490       The  examples below assume a 5x5 matrix M with the first row containing
491       the values 1 to 5, with 1 in the top-left cell. Each other row contains
492       the contents of the row above it, rotated by one cell to the right.
493
494        % M getrect 0 0 4 4
495        {{1 2 3 4 5} {5 1 2 3 4} {4 5 1 2 3} {3 4 5 1 2} {2 3 4 5 1}}
496
497
498        % M setrect 1 1 {{0 0 0} {0 0 0} {0 0 0}}
499        % M getrect 0 0 4 4
500        {{1 2 3 4 5} {5 0 0 0 4} {4 0 0 0 3} {3 0 0 0 2} {2 3 4 5 1}}
501
502
503       Assuming  that the style definitions in the example section of the man‐
504       page for the package report are loaded  into  the  interpreter  now  an
505       example  which formats a matrix into a tabular report. The code filling
506       the matrix with data is not shown.  contains useful data.
507
508           % ::struct::matrix m
509           % # ... fill m with data, assume 5 columns
510           % ::report::report r 5 style captionedtable 1
511           % m format 2string r
512           +---+-------------------+-------+-------+--------+
513           |000|VERSIONS:          |2:8.4a3|1:8.4a3|1:8.4a3%|
514           +---+-------------------+-------+-------+--------+
515           |001|CATCH return ok    |7      |13     |53.85   |
516           |002|CATCH return error |68     |91     |74.73   |
517           |003|CATCH no catch used|7      |14     |50.00   |
518           |004|IF if true numeric |12     |33     |36.36   |
519           |005|IF elseif          |15     |47     |31.91   |
520           |   |true numeric       |       |       |        |
521           +---+-------------------+-------+-------+--------+
522           %
523           % # alternate way of doing the above
524           % r printmatrix m
525
526

BUGS, IDEAS, FEEDBACK

528       This document, and the package it describes, will  undoubtedly  contain
529       bugs  and other problems.  Please report such in the category struct ::
530       matrix    of     the     Tcllib     SF     Trackers     [http://source
531       forge.net/tracker/?group_id=12883].   Please  also report any ideas for
532       enhancements you may have for either package and/or documentation.
533

KEYWORDS

535       matrix
536
538       Copyright (c) 2002 Andreas Kupries <andreas_kupries@users.sourceforge.net>
539
540
541
542
543struct                               2.0.1                   struct::matrix(n)
Impressum