home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e032 / 3.ddi / FILES / LINEARAL.PAK / MATRIXMA.M < prev    next >
Encoding:
Text File  |  1992-07-29  |  5.2 KB  |  167 lines

  1.  
  2. (*:Mathematica:: V1.2  *)
  3.  
  4. (*:Context: "LinearAlgebra`MatrixManipulation`"  *)
  5.  
  6. (*:Title: Basic Matrix Manipulation Functions  *)
  7.  
  8. (*:Author:   Kevin McIsaac  *)
  9.  
  10. (*:Summary:  Provides basic matrix manipulation routines for composing matrices 
  11. from block elements and taking subblocks from a matrix.  *)
  12.  
  13. (*:Keywords: matrix, block, submatrix  *)
  14.  
  15. (*:Requirements: none.  *)
  16.  
  17. (*:Sources:   *)
  18.  
  19. (*:History:   Created January 25, 1989 by K. McIsaac.
  20.                    Updated February 4, 1989 by John Novak.  *)
  21.  
  22. BeginPackage["LinearAlgebra`MatrixManipulation`"];
  23.  
  24. AppendColumns::usage=
  25. "AppendColumns[mat1, mat2...] returns a new matrix composed of the
  26. submatrices mat1, mat2, etc., by joining their columns. The submatrices must
  27. all have the same number of columns."
  28.  
  29. AppendRows::usage=
  30. "AppendRows[mat1, mat2...] returns a new matrix composed of the submatrices
  31. mat1, mat2, etc., by joining their rows. The submatrices must all have the same
  32. number of rows."
  33.  
  34. SquareMatrixQ::usage=
  35. "SquareMatrixQ[mat] tests whether mat is square." 
  36.  
  37. TakeRows::usage=
  38. "TakeRows[mat, part] takes the rows of matrix mat give by the partspec,
  39. part. This uses the same notation as Take."
  40.  
  41. TakeColumns::usage=
  42. "TakeColumns[mat, part] takes the columns of matrix mat give by the
  43. partspec, part. This uses the same notation as Take."
  44.  
  45. TakeMatrix::usage=
  46. "TakeMatrix[mat, start, end] returns the submatrix starting at start and
  47. ending at end, where the positions are positions in the matrix, e.g., {1,2}."
  48.  
  49. SubMatrix::usage=
  50. "SubMatrix[mat, start, dim] returns the submatrix of dimension dim starting
  51. at position start in mat."
  52.  
  53. BlockMatrix::usage=
  54. "BlockMatrix[block] returns a matrix composed of a matrix of matrices."
  55.  
  56. TakeMatrix::usage=
  57. "TakeMatrix[mat, start, end] returns the submatrix that starts at start and
  58. ends at end, where start and end are expressed as the pair defining a position
  59. in the array (e.g., {1,2})."
  60.  
  61. UpperDiagonalMatrix::usage=
  62. "UpperDiagonalMatrix[fn, size] creates an upper-diagonal matrix with the i-th,
  63. j-th element being f[i,j] for i>= j and zero otherwise."
  64.  
  65. LowerDiagonalMatrix::usage=
  66. "LowerDiagonalMatrix[fn, size] creates a tridiagonal matrix with the i-th, j-th
  67. element being f[i,j] for i<= j and zero otherwise."
  68.  
  69. TridiagonalMatrix::usage=
  70. "TridiagonalMatrix[fn, size] creates a tridiagonal matrix with the i-th, j-th
  71. element being f[i,j] for i= j and zero otherwise."
  72.  
  73. ZeroMatrix::usage=
  74. "ZeroMatrix[m] returns an m x m block matrix of zeros. ZeroMatrix[m, n]
  75. returns an m x n block matrix of zeros."
  76.  
  77. HilbertMatrix::usage =
  78. "HilbertMatrix[m,n] returns the m x n Hilbert Matrix, where the
  79. (i,j)-th element is defined as 1/(i + j + 1). HilbertMatrix[m] returns the m x
  80. m Hilbert matrix."  
  81.  
  82. HankelMatrix::usage =
  83. "HankelMatrix[column,row] returns the Hankel matrix defined by the
  84. given column and row; note that the last element of column must match the
  85. first element of row.  HankelMatrix[col] gives the Hankel matrix whose first
  86. column is col, and where every element under the anti-diagonal is filled with
  87. zeros. HankelMatrix[n] gives the n x n Hankel matrix where the leading column
  88. is the integers 1 - n."
  89.  
  90. Begin["`Private`"]
  91.  
  92. AppendColumns[l__?MatrixQ] := Join[l] /; SameColumnSize[{l}]
  93.  
  94. AppendRows[l__?MatrixQ] := Apply[Join, Transpose[{l}], {1}] /; 
  95.    SameRowSize[{l}]
  96.  
  97. BlockMatrix[block_] :=
  98.     AppendColumns @@ Apply[AppendRows, block, {1}] 
  99.  
  100. SquareMatrixQ[mat_?MatrixQ] := SameQ @@ Dimensions[mat]
  101.  
  102. SameColumnSize[l_List] := (SameQ @@ (Dimensions[#][[2]]& /@ l) )
  103.     SameRowSize[l_List] := (SameQ @@ (Dimensions[#][[1]]& /@ l) )
  104.  
  105.  
  106. TakeRows[mat_?MatrixQ, part_] := Take[mat, part]
  107.  
  108. TakeColumns[mat_?MatrixQ, part_] := Take[#, part]& /@ mat
  109.  
  110. TakeMatrix[mat_?MatrixQ, start:{startR_Integer, startC_Integer},
  111. end:{endR_Integer, endC_Integer}] :=
  112.     Take[#, {startC, endC}]& /@ Take[mat, {startR, endR}] /;
  113.     And @@ Thread[Dimensions[mat] >= start] && 
  114.     And @@ Thread[Dimensions[mat] >= end]
  115.  
  116. SubMatrix[mat_List, start:{_Integer, _Integer}, dim:{_Integer,_Integer}] :=
  117.     TakeMatrix[mat, start, start+dim-1] 
  118.  
  119. TridiagonalMatrix[fn_,size_Integer] :=
  120.     DiagonalMatrix[Map[fn[#,#]&,Range[1,size]]]
  121.  
  122. (* TridiagonalMatrix[fn_, size_Integer] :=
  123.     Array[If[#1===#2, fn[#1, #2], 0]&, {size, size}] /;
  124.     size >= 0
  125. *)
  126.  
  127. LowerDiagonalMatrix[fn_, size_Integer] :=
  128.     Array[If[#1>=#2, fn[#1, #2], 0]&, {size, size}] /;
  129.     size >= 0
  130.  
  131. UpperDiagonalMatrix[fn_, size_Integer] :=
  132.     Array[If[#1<=#2, fn[#1, #2], 0]&, {size, size}] /;
  133.     size >= 0
  134.  
  135. ZeroMatrix[m_Integer,n_Integer] := 
  136.     Table[0, {m}, {n}] /; m >= 0 && n>=0
  137.  
  138. ZeroMatrix[m_Integer] := ZeroMatrix[m, m] /; m >= 0 
  139.  
  140. HilbertMatrix[m_Integer,n_Integer] := 
  141.     Table[1/(i+j+1), {i, 0, m-1}, {j, 0, n-1}]
  142.  
  143. HilbertMatrix[m_Integer] := HilbertMatrix[m,m]
  144.  
  145. HankelMatrix[size_Integer] :=
  146.     HankelMatrix[Range[1,size]] /; size >= 0
  147.  
  148. HankelMatrix[col_List] :=
  149.     Module[{size = Length[col]},
  150.         HankelMatrix[Join[col,Table[0,{size - 1}]],
  151.             {size,size}]
  152.     ]
  153.  
  154. HankelMatrix[col_List,row_List] :=
  155.     HankelMatrix[Join[col,Drop[row,1]],
  156.         {Length[col],Length[row]}]/;
  157.     Last[col] == First[row]
  158.  
  159. HankelMatrix[vec_List,size:{rows_Integer,cols_Integer}] :=
  160.     Array[(vec[[#1 + #2 - 1]])&,size]/;
  161.         Length[vec] >= (rows + cols - 1)
  162.  
  163. End[]
  164.  
  165. EndPackage[]
  166.  
  167.