Provided by: tcllib_1.14-dfsg-1_all

**NAME**

struct::matrix1 - Create and manipulate matrix objects (version 1)

**SYNOPSIS**

package requireTcl8.2package requirestruct::matrix?1.2.1?matrixNameoption?argarg...?matrixNameaddcolumn?values?matrixNameaddrow?values?matrixNameaddcolumnsnmatrixNameaddrowsnmatrixNamecellsmatrixNamecellsizecolumnrowmatrixNamecolumnsmatrixNamecolumnwidthcolumnmatrixNamedeletecolumncolumnmatrixNamedeleterowrowmatrixNamedestroymatrixNameformat2string?report?matrixNameformat2chan??report?channel?matrixNamegetcellcolumnrowmatrixNamegetcolumncolumnmatrixNamegetrectcolumn_tlrow_tlcolumn_brrow_brmatrixNamegetrowrowmatrixNameinsertcolumncolumn?values?matrixNameinsertrowrow?values?matrixNamelink?-transpose?arrayvarmatrixNamelinksmatrixNamerowheightrowmatrixNamerowsmatrixNamesearch?-nocase? ?-exact|-glob|-regexp?allpatternmatrixNamesearch?-nocase? ?-exact|-glob|-regexp?columncolumnpatternmatrixNamesearch?-nocase? ?-exact|-glob|-regexp?rowrowpatternmatrixNamesearch?-nocase? ?-exact|-glob|-regexp?rectcolumn_tlrow_tlcolumn_brrow_brpatternmatrixNamesetcellcolumnrowvaluematrixNamesetcolumncolumnvaluesmatrixNamesetrectcolumnrowvaluesmatrixNamesetrowrowvaluesmatrixNamesortcolumns?-increasing|-decreasing?rowmatrixNamesortrows?-increasing|-decreasing?columnmatrixNameswapcolumnscolumn_acolumn_bmatrixNameswaprowsrow_arow_bmatrixNameunlinkarrayvar_________________________________________________________________

**DESCRIPTION**

The::struct::matrixcommand creates a new matrix object with an associated global Tcl command whose name ismatrixName. This command may be used to invoke various operations on the matrix. It has the following general form:matrixNameoption?argarg...?Optionand theargs determine the exact behavior of the command. A matrix is a rectangular collection of cells, i.e. organized in rows and columns. Each cell contains exactly one value of arbitrary form. The cells in the matrix are addressed by pairs of integer numbers, with the first (left) number in the pair specifying the column and the second (right) number specifying the row the cell is in. These indices are counted from 0 upward. The special non-numeric indexendrefers to the last row or column in the matrix, depending on the context. Indices of the formend-numberare counted from the end of the row or column, like they are for standard Tcl lists. Trying to access non- existing cells causes an error. The matrices here are created empty, i.e. they have neither rows nor columns. The user then has to add rows and columns as needed by his application. A specialty of this structure is the ability to export an array-view onto its contents. Such can be used by tkTable, for example, to link the matrix into the display. The following commands are possible for matrix objects:matrixNameaddcolumn?values? Extends the matrix by one column and then acts likesetcolumn(see below) on this new column if there werevaluessupplied. Withoutvaluesthe new cells will be set to the empty string. The new column is appended immediately behind the last existing column.matrixNameaddrow?values? Extends the matrix by one row and then acts likesetrow(see below) on this new row if there werevaluessupplied. Withoutvaluesthe new cells will be set to the empty string. The new row is appended immediately behind the last existing row.matrixNameaddcolumnsnExtends the matrix byncolumns. The new cells will be set to the empty string. The new columns are appended immediately behind the last existing column. A value ofnequal to or smaller than 0 is not allowed.matrixNameaddrowsnExtends the matrix bynrows. The new cells will be set to the empty string. The new rows are appended immediately behind the last existing row. A value ofnequal to or smaller than 0 is not allowed.matrixNamecellsReturns the number of cells currently managed by the matrix. This is the product ofrowsandcolumns.matrixNamecellsizecolumnrowReturns the length of the string representation of the value currently contained in the addressed cell.matrixNamecolumnsReturns the number of columns currently managed by the matrix.matrixNamecolumnwidthcolumnReturns the length of the longest string representation of all the values currently contained in the cells of the addressed column if these are all spanning only one line. For cell values spanning multiple lines the length of their longest line goes into the computation.matrixNamedeletecolumncolumnDeletes the specified column from the matrix and shifts all columns with higher indices one index down.matrixNamedeleterowrowDeletes the specified row from the matrix and shifts all row with higher indices one index down.matrixNamedestroyDestroys the matrix, including its storage space and associated command.matrixNameformat2string?report? Formats the matrix using the specified report object and returns the string containing the result of this operation. The report has to support theprintmatrixmethod. If noreportis specified the system will use an internal report definition to format the matrix.matrixNameformat2chan??report?channel? Formats the matrix using the specified report object and writes the string containing the result of this operation into the channel. The report has to support theprintmatrix2channelmethod. If noreportis specified the system will use an internal report definition to format the matrix. If nochannelis specified the system will usestdout.matrixNamegetcellcolumnrowReturns the value currently contained in the cell identified by row and column index.matrixNamegetcolumncolumnReturns a list containing the values from all cells in the column identified by the index. The contents of the cell in row 0 are stored as the first element of this list.matrixNamegetrectcolumn_tlrow_tlcolumn_brrow_brReturns a list of lists of cell values. The values stored in the result come from the sub-matrix whose top-left and bottom-right cells are specified bycolumn_tl,row_tlandcolumn_br,row_brresp. Note that the following equations have to be true: "column_tl<=column_br" and "row_tl<=row_br". The result is organized as follows: The outer list is the list of rows, its elements are lists representing a single row. The row with the smallest index is the first element of the outer list. The elements of the row lists represent the selected cell values. The cell with the smallest index is the first element in each row list.matrixNamegetrowrowReturns a list containing the values from all cells in the row identified by the index. The contents of the cell in column 0 are stored as the first element of this list.matrixNameinsertcolumncolumn?values? Extends the matrix by one column and then acts likesetcolumn(see below) on this new column if there werevaluessupplied. Withoutvaluesthe new cells will be set to the empty string. The new column is inserted just before the column specified by the given index. This means, ifcolumnis less than or equal to zero, then the new column is inserted at the beginning of the matrix, before the first column. Ifcolumnhas the valueend, or if it is greater than or equal to the number of columns in the matrix, then the new column is appended to the matrix, behind the last column. The old column at the chosen index and all columns with higher indices are shifted one index upward.matrixNameinsertrowrow?values? Extends the matrix by one row and then acts likesetrow(see below) on this new row if there werevaluessupplied. Withoutvaluesthe new cells will be set to the empty string. The new row is inserted just before the row specified by the given index. This means, ifrowis less than or equal to zero, then the new row is inserted at the beginning of the matrix, before the first row. Ifrowhas the valueend, or if it is greater than or equal to the number of rows in the matrix, then the new row is appended to the matrix, behind the last row. The old row at that index and all rows with higher indices are shifted one index upward.matrixNamelink?-transpose?arrayvarLinks the matrix to the specified array variable. This means that the contents of all cells in the matrix is stored in the array too, with all changes to the matrix propagated there too. The contents of the cell(column,row)is stored in the array using the keycolumn,row. If the option-transposeis specified the keyrow,columnwill be used instead. It is possible to link the matrix to more than one array. Note that the link is bidirectional, i.e. changes to the array are mirrored in the matrix too.matrixNamelinksReturns a list containing the names of all array variables the matrix was linked to through a call to methodlink.matrixNamerowheightrowReturns the height of the specified row in lines. This is the highest number of lines spanned by a cell over all cells in the row.matrixNamerowsReturns the number of rows currently managed by the matrix.matrixNamesearch?-nocase? ?-exact|-glob|-regexp?allpatternSearches the whole matrix for cells matching thepatternand returns a list with all matches. Each item in the aforementioned list is a list itself and contains the column and row index of the matching cell, in this order. The results are ordered by column first and row second, both times in ascending order. This means that matches to the left and the top of the matrix come before matches to the right and down. The type of the pattern (string, glob, regular expression) is determined by the option after thesearchkeyword. If no option is given it defaults to-exact. If the option-nocaseis specified the search will be case-insensitive.matrixNamesearch?-nocase? ?-exact|-glob|-regexp?columncolumnpatternLikesearchall, but the search is restricted to the specified column.matrixNamesearch?-nocase? ?-exact|-glob|-regexp?rowrowpatternLikesearchall, but the search is restricted to the specified row.matrixNamesearch?-nocase? ?-exact|-glob|-regexp?rectcolumn_tlrow_tlcolumn_brrow_brpatternLikesearchall, but the search is restricted to the specified rectangular area of the matrix.matrixNamesetcellcolumnrowvalueSets the value in the cell identified by row and column index to the data in the third argument.matrixNamesetcolumncolumnvaluesSets the values in the cells identified by the column index to the elements of the list provided as the third argument. Each element of the list is assigned to one cell, with the first element going into the cell in row 0 and then upward. If there are less values in the list than there are rows the remaining rows are set to the empty string. If there are more values in the list than there are rows the superfluous elements are ignored. The matrix is not extended by this operation.matrixNamesetrectcolumnrowvaluesTakes a list of lists of cell values and writes them into the submatrix whose top- left cell is specified by the two indices. If the sublists of the outerlist are not of equal length the shorter sublists will be filled with empty strings to the length of the longest sublist. If the submatrix specified by the top-left cell and the number of rows and columns in thevaluesextends beyond the matrix we are modifying the over-extending parts of the values are ignored, i.e. essentially cut off. This subcommand expects its input in the format as returned bygetrect.matrixNamesetrowrowvaluesSets the values in the cells identified by the row index to the elements of the list provided as the third argument. Each element of the list is assigned to one cell, with the first element going into the cell in column 0 and then upward. If there are less values in the list than there are columns the remaining columns are set to the empty string. If there are more values in the list than there are columns the superfluous elements are ignored. The matrix is not extended by this operation.matrixNamesortcolumns?-increasing|-decreasing?rowSorts the columns in the matrix using the data in the specifiedrowas the key to sort by. The options-increasingand-decreasinghave the same meaning as forlsort. If no option is specified-increasingis assumed.matrixNamesortrows?-increasing|-decreasing?columnSorts the rows in the matrix using the data in the specifiedcolumnas the key to sort by. The options-increasingand-decreasinghave the same meaning as forlsort. If no option is specified-increasingis assumed.matrixNameswapcolumnscolumn_acolumn_bSwaps the contents of the two specified columns.matrixNameswaprowsrow_arow_bSwaps the contents of the two specified rows.matrixNameunlinkarrayvarRemoves the link between the matrix and the specified arrayvariable, if there is one.

**EXAMPLES**

The examples below assume a 5x5 matrix M with the first row containing the values 1 to 5, with 1 in the top-left cell. Each other row contains the contents of the row above it, rotated by one cell to the right. % M getrect 0 0 4 4 {{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}} % M setrect 1 1 {{0 0 0} {0 0 0} {0 0 0}} % M getrect 0 0 4 4 {{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}} Assuming that the style definitions in the example section of the manpage for the packagereportare loaded into the interpreter now an example which formats a matrix into a tabular report. The code filling the matrix with data is not shown. contains useful data. % ::struct::matrix m % # ... fill m with data, assume 5 columns % ::report::report r 5 style captionedtable 1 % m format 2string r +---+-------------------+-------+-------+--------+ |000|VERSIONS: |2:8.4a3|1:8.4a3|1:8.4a3%| +---+-------------------+-------+-------+--------+ |001|CATCH return ok |7 |13 |53.85 | |002|CATCH return error |68 |91 |74.73 | |003|CATCH no catch used|7 |14 |50.00 | |004|IF if true numeric |12 |33 |36.36 | |005|IF elseif |15 |47 |31.91 | | |true numeric | | | | +---+-------------------+-------+-------+--------+ % % # alternate way of doing the above % r printmatrix m

**BUGS,** **IDEAS,** **FEEDBACK**

This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the categorystruct::matrixof theTcllibSFTrackers[http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation.

**KEYWORDS**

matrix

**CATEGORY**

Data structures

**COPYRIGHT**

Copyright (c) 2002 Andreas Kupries <andreas_kupries@users.sourceforge.net>