2 mat - keyword to create a matrix value
5 mat [index-range-list] [ = {value_0. ...} ]
6 mat [] [= {value_0, ...}]
7 mat variable_1 ... [index-range-list] [ = {value_0, ...} ]
8 mat variable_1 ... [] [ = {value_0, ...} ]
10 mat [index-range-list_1[index-ranges-list_2] ... [ = { { ...} ...} ]
12 decl id_1 id_2 ... [index-range-list] ...
15 index-range-list range_1 [, range_2, ...] up to 4 ranges
16 range_1, ... integer, or integer_1 : integer_2
17 value, value_1, ... any
19 decl declarator = global, static or local
23 The expression mat [index-range-list] returns a matrix value.
24 This may be assigned to one or more lvalues A, B, ... by either
26 mat A B ... [index-range-list]
30 A = B = ... = mat[index-range-list]
32 If a variable is specified by an expression that is not a symbol with
33 possibly object element specifiers, the expression should be enclosed
34 in parentheses. For example, parentheses are required in
35 mat (A[2]) [3] and mat (*p) [3] but mat P.x [3] is acceptable.
37 When an index-range is specified as integer_1 : integer_2, where
38 integer_1 and integer_2 are expressions which evaluate to integers,
39 the index-range consists of all integers from the minimum of the
40 two integers to the maximum of the two integers. For example,
41 mat[2:5, 0:4] and mat[5:2, 4:0] return the same matrix value.
43 If an index-range is an expression which evaluates to an integer,
44 the range is as if specified by 0 : integer - 1. For example,
45 mat[4] and mat[0:3] return the same 4-element matrix; mat[-2] and
46 mat[-3:0] return the same 4-element matrix.
48 If the variable A has a matrix value, then for integer indices
49 i_1, i_2, ..., equal in number to the number of ranges specified at
50 its creation, and such that each index is in the corresponding range,
51 the matrix element associated with those index list is given as an
52 lvalue by the expressions A[i_1, i_2, ...].
54 The elements of the matrix are stored internally as a linear array
55 in which locations are arranged in order of increasing indices.
56 For example, in order of location, the six element of A = mat [2,3]
59 A[0,0], A[0,1], A[0,2], A[1,0], A[1,,1], A[1,2].
61 These elements may also be specified using the double-bracket operator
62 with a single integer index as in A[[0]], A[[1]], ..., A[[5]].
63 If p is assigned the value &A[0.0], the address of A[[i]] for 0 <= i < 6
64 is p + i as long as A exists and a new value is not assigned to A.
66 When a matrix is created, each element is initially assigned the
67 value zero. Other values may be assigned then or later using the
68 "= {...}" assignment operation. Thus
70 A = {value_0, value_1, ...}
72 assigns the values value_0, value_1, ... to the elements A[[0]],
73 A[[1]], ... Any blank "value" is passed over. For example,
77 will assign the value 1 to A[[0]], 2 to A[[2]] and leave all other
78 elements unchanged. Values may also be assigned to elements by
79 simple assignments, as in A[0,0] = 1, A[0,2] = 2;
81 If the index-range is left blank but an initializer list is specified
85 ; B = mat[] = {1, , 3, }
87 the matrix created is one-dimensional. If the list contains a
88 positive number n of values or blanks, the result is as if the
89 range were specified by [n], i.e. the range of indices is from
90 0 to n - 1. In the above examples, A is of size 2 with A[0] = 1
91 and A[1] = 2; B is of size 4 with B[0] = 1, B[1] = B[3] = 0,
92 B[2] = 3. The specification mat[] = { } creates the same as mat[1].
94 If the index-range is left blank and no initializer list is specified,
95 as in mat C[] or C = mat[], the matrix assigned to C has zero
96 dimension; this has one element C[].
98 To assign a value using "= { ...}" at the same time as creating C,
99 parentheses are required as in (mat[]) = {value} or (mat C[]) =
100 {value}. Later a value may be assigned to C[] by C[] = value or
103 The value assigned at any time to any element of a matrix can be of
104 any type - number, string, list, matrix, object of previously specified
105 type, etc. For some matrix operations there are of course conditions
106 that elements may have to satisfy: for example, addition of matrices
107 requires that addition of corresponding elements be possible.
108 If an element of a matrix is a structure for which indices or an
109 object element specifier is required, an element of that structure is
110 referred to by appropriate uses of [ ] or ., and so on if an element
111 of that element is required.
113 For example, one may have an expressions like:
115 ; A[1,2][3].alpha[2];
117 if A[1,2][3].alpha is a list with at least three elements, A[1,2][3] is
118 an object of a type like obj {alpha, beta}, A[1,2] is a matrix of
119 type mat[4] and A is a mat[2,3] matrix. When an element of a matrix
120 is a matrix and the total number of indices does not exceed 4, the
121 indices can be combined into one list, e.g. the A[1,2][3] in the
122 above example can be shortened to A[1,2,3]. (Unlike C, A[1,2] cannot
123 be expressed as A[1][2].)
125 The function ismat(V) returns 1 if V is a matrix, 0 otherwise.
127 isident(V) returns 1 if V is a square matrix with diagonal elements 1,
128 off-diagonal elements zero, or a zero- or one-dimensional matrix with
129 every element 1; otherwise zero is returned. Thus isident(V) = 1
130 indicates that for V * A and A * V where A is any matrix of
131 for which either product is defined and the elements of A are real
132 or complex numbers, that product will equal A.
134 If V is matrix-valued, test(V) returns 0 if every element of V tests
135 as zero; otherwise 1 is returned.
137 The dimension of a matrix A, i.e. the number of index-ranges in the
138 initial creation of the matrix, is returned by the function matdim(A).
139 For 1 <= i <= matdim(A), the minimum and maximum values for the i-th
140 index range are returned by matmin(A, i) and matmax(A,i), respectively.
141 The total number of elements in the matrix is returned by size(A).
142 The sum of the elements in the matrix is returned by matsum(A).
144 The default method of printing matrices is to give a line of information
145 about the matrix, and to list on separate lines up to 15 elements,
146 the indices and either the value (for numbers, strings, objects) or
147 some descriptive information for lists or matrices, etc.
148 Numbers are displayed in the current number-printing mode.
149 The maximum number of elements to be printed can be assigned
150 any nonnegative integer value m by config("maxprint", m).
152 Users may define another method of printing matrices by defining a
153 function mat_print(M); for example, for a not too big 2-dimensional
154 matrix A it is a common practice to use a loop like:
156 define mat_print(A) {
159 for (i = matmin(A,1); i <= matmax(A,1); i++) {
160 if (i != matmin(A,1))
162 for (j = matmin(A,2); j <= matmax(A,2); j++)
163 printf(" [%d,%d]: %e", i, j, A[i,j]);
164 if (i != matmax(A,1))
169 So that when one defines a 2D matrix such as:
171 ; mat X[2,3] = {1,2,3,4,5,6}
173 then printing X results in:
175 [0,0]: 1 [0,1]: 2 [0,2]: 3
176 [1,0]: 4 [1,1]: 5 [1,2]: 6
178 The default printing may be restored by
180 ; undefine mat_print;
182 The keyword "mat" followed by two or more index-range-lists returns a
183 matrix with indices specified by the first list, whose elements are
184 matrices as determined by the later index-range-lists. For
185 example mat[2][3] is a 2-element matrix, each of whose elements has
186 as its value a 3-element matrix. Values may be assigned to the
187 elements of the innermost matrices by nested = {...} operations as in
189 ; mat [2][3] = {{1,2,3},{4,5,6}}
191 An example of the use of mat with a declarator is
193 ; global mat A B [2,3], C [4]
195 This creates, if they do not already exist, three global variables with
196 names A, B, C, and assigns to A and B the value mat[2,3] and to C mat[4].
198 Some operations are defined for matrices.
201 Returns 1 if A and B are of the same "shape" and "corresponding"
202 elements are equal; otherwise 0 is returned. Being of the same
203 shape means they have the same dimension d, and for each i <= d,
205 matmax(A,i) - matmin(A,i) == matmax(B,i) - matmin(B,i),
207 One consequence of being the same shape is that the matrices will
208 have the same size. Elements "correspond" if they have the same
209 double-bracket indices; thus A == B implies that A[[i]] == B[[i]]
210 for 0 <= i < size(A) == size(B).
214 These are defined A and B have the same shape, the element
215 with double-bracket index j being evaluated by A[[j]] + B[[j]] and
216 A[[j]] - B[[j]], respectively. The index-ranges for the results
217 are those for the matrix A.
220 If A is two-dimensional, it is customary to speak of the indices
221 i, j in A[i,j] as referring to rows and columns; the number of
222 rows is matmax(A,1) - matmin(A,1) + 1; the number of columns if
223 matmax(A,2) - matmin(A,2) + 1. A matrix is said to be square
224 if it is two-dimensional and the number of rows is equal to the
228 Multiplication is defined provided certain conditions by the
229 dimensions and shapes of A and B are satisfied. If both have
230 dimension 2 and the column-index-list for A is the same as
231 the row-index-list for B, C = A * B is defined in the usual
232 way so that for i in the row-index-list of A and j in the
233 column-index-list for B,
235 C[i,j] = Sum A[i,k] * B[k,j]
237 the sum being over k in the column-index-list of A. The same
238 formula is used so long as the number of columns in A is the same
239 as the number of rows in B and k is taken to refer to the offset
240 from matmin(A,2) and matmin(B,1), respectively, for A and B.
241 If the multiplications and additions required cannot be performed,
242 an execution error may occur or the result for C may contain
243 one or more error-values as elements.
245 If A or B has dimension zero, the result for A * B is simply
246 that of multiplying the elements of the other matrix on the
247 left by A[] or on the right by B[].
249 If both A and B have dimension 1, A * B is defined if A and B
250 have the same size; the result has the same index-list as A
251 and each element is the product of corresponding elements of
252 A and B. If A and B have the same index-list, this multiplication
253 is consistent with multiplication of 2D matrices if A and B are
254 taken to represent 2D matrices for which the off-diagonal elements
255 are zero and the diagonal elements are those of A and B.
256 the real and complex numbers.
258 If A is of dimension 1 and B is of dimension 2, A * B is defined
259 if the number of rows in B is the same as the size of A. The
260 result has the same index-lists as B; each row of B is multiplied
261 on the left by the corresponding element of A.
263 If A is of dimension 2 and B is of dimension 1, A * B is defined
264 if number of columns in A is the same as the size of A. The
265 result has the same index-lists as A; each column of A is
266 multiplied on the right by the corresponding element of B.
268 The algebra of additions and multiplications involving both one-
269 and two-dimensional matrices is particularly simple when all the
270 elements are real or complex numbers and all the index-lists are
271 the same, as occurs, for example, if for some positive integer n,
272 all the matrices start as mat [n] or mat [n,n].
275 If A is a square, det(A) is evaluated by an algorithm that returns
276 the determinant of A if the elements of A are real or complex
277 numbers, and if such an A is non-singular, inverse(A) returns
278 the inverse of A indexed in the same way as A. For matrix A of
279 dimension 0 or 1, det(A) is defined as the product of the elements
280 of A in the order in which they occur in A, inverse(A) returns
281 a matrix indexed in the same way as A with each element inverted.
284 The following functions are defined to return matrices with the same
285 index-ranges as A and the specified operations performed on all
286 elements of A. Here num is an arbitrary complex number (nonzero
287 when it is a divisor), int an integer, rnd a rounding-type
288 specifier integer, real a real number.
306 If A and B are one-dimensional of the same size dp(A, B) returns
307 their dot-product, i.e. the sum of the products of corresponding
310 If A and B are one-dimension and of size 3, cp(A, B) returns their
313 randperm(A) returns a matrix indexed the same as A in which the elements
314 of A have been randomly permuted.
316 sort(A) returns a matrix indexed the same as A in which the elements
317 of A have been sorted.
319 If A is an lvalue whose current value is a matrix, matfill(A, v)
320 assigns the value v to every element of A, and if also, A is
321 square, matfill(A, v1, v2) assigns v1 to the off-diagonal elements,
322 v2 to the diagonal elements. To create and assign to A the unit
323 n * n matrix, one may use matfill(mat A[n,n], 0, 1).
325 For a square matrix A, mattrace(A) returns the trace of A, i.e. the
326 sum of the diagonal elements. For zero- or one-dimensional A,
327 mattrace(A) returns the sum of the elements of A.
329 For a two-dimensional matrix A, mattrans(A) returns the transpose
330 of A, i.e. if A is mat[m,n], it returns a mat[n,m] matrix with
331 [i,j] element equal to A[j,i]. For zero- or one-dimensional A,
332 mattrace(A) returns a matrix with the same value as A.
334 The functions search(A, value, start, end]) and
335 rsearch(A, value, start, end]) return the first or last index i
336 for which A[[i]] == value and start <= i < end, or if there is
337 no such index, the null value. For further information on default
338 values and the use of an "accept" function, see the help files for
341 reverse(A) returns a matrix with the same index-lists as A but the
342 elements in reversed order.
344 The copy and blkcpy functions may be used to copy data to a matrix from
345 a matrix or list, or from a matrix to a list. In copying from a
346 matrix to a matrix the matrices need not have the same dimension;
347 in effect they are treated as linear arrays.
351 ; mat A[5] = {1, 2+3i, "ab", mat[2] = {4,5}, obj point = {6,7}}
353 mat [5] (5 elements, 5 nonzero):
357 [3] = mat [2] (2 elements, 2 nonzero)
358 [4] = obj point {6, 7}
360 ; print A[0], A[1], A[2], A[3][0], A[4].x
363 ; define point_add(a,b) = obj point = {a.x + b.x, a.y + b.y}
364 point_add(a,b) defined
366 ; mat [B] = {8, , "cd", mat[2] = {9,10}, obj point = {11,12}}
369 mat [5] (5 elements, 5 nonzero):
373 [3] = mat [2] (2 elements, 2 nonzero)
374 [4] = obj point {17, 19}
376 ; mat C[2,2] = {1,2,3,4}
379 mat [2,2] (4 elements, 4 nonzero):
387 mat [2,2] (4 elements, 4 nonzero):
388 [0,0] = 14884.650390625
389 [0,1] = -6808.642578125
390 [1,0] = -10212.9638671875
391 [1,1] = 4671.6865234375
393 ; mat A[4] = {1,2,3,4}, A * reverse(A);
395 mat [4] (4 elements, 4 nonzero):
402 The theoretical upper bound for the absolute values of indices is
403 2^31 - 1, but the size of matrices that can be handled in practice will
404 be limited by the availability of memory and what is an acceptable
405 runtime. For example, although it may take only a fraction of a
406 second to invert a 10 * 10 matrix, it will probably take about 1000
407 times as long to invert a 100 * 100 matrix.
413 ismat, matdim, matmax, matmin, mattrans, mattrace, matsum, matfill,
414 det, inverse, isident, test, config, search, rsearch, reverse, copy,
415 blkcpy, dp, cp, randperm, sort
417 ## Copyright (C) 1999-2006 Landon Curt Noll
419 ## Calc is open software; you can redistribute it and/or modify it under
420 ## the terms of the version 2.1 of the GNU Lesser General Public License
421 ## as published by the Free Software Foundation.
423 ## Calc is distributed in the hope that it will be useful, but WITHOUT
424 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
425 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
426 ## Public License for more details.
428 ## A copy of version 2.1 of the GNU Lesser General Public License is
429 ## distributed with calc under the filename COPYING-LGPL. You should have
430 ## received a copy with calc; if not, write to Free Software Foundation, Inc.
431 ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
433 ## @(#) $Revision: 30.1 $
434 ## @(#) $Id: mat,v 30.1 2007/03/16 11:10:42 chongo Exp $
435 ## @(#) $Source: /usr/local/src/cmd/calc/help/RCS/mat,v $
437 ## Under source code control: 1991/07/21 04:37:22
438 ## File existed as early as: 1991
440 ## chongo <was here> /\oo/\ http://www.isthe.com/chongo/
441 ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/