home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume21 / newmat02 / part00 next >
Text File  |  1991-08-01  |  11KB  |  247 lines

  1. Newsgroups: comp.sources.misc
  2. From: Robert Davies <robert@am.dsir.govt.nz>
  3. Subject:  v21i049:  newmat02 - A matrix package in C++, Part00/03
  4. Message-ID: <csm-v21i049=newmat02.202724@sparky.imd.sterling.com>
  5. Date: Fri, 2 Aug 1991 01:28:32 GMT
  6. Approved: kent@sparky.imd.sterling.com
  7. X-Md4-Signature: 33d0792923f597317ee2364260446a92
  8.  
  9. Submitted-by: Robert Davies <robert@am.dsir.govt.nz>
  10. Posting-number: Volume 21, Issue 49
  11. Archive-name: newmat02/part00
  12. Environment: C++
  13.  
  14. MATRIX PACKAGE                                          30 July, 1991
  15.  
  16. Introduction:
  17.  
  18.   This is a description of an experimental matrix package. The package is
  19.   an upgrade of one I distributed to a number of people in 1990.
  20.   
  21.   The package supports matrix types
  22.   
  23.   Matrix                       (rectangular matrix)
  24.   UpperTriangularMatrix
  25.   LowerTriangularMatrix
  26.   DiagonalMatrix
  27.   SymmetricMatrix
  28.   RowVector                    (derived from Matrix)
  29.   ColumnVector                 (derived from Matrix).
  30.   
  31.   Only one element type is supported.
  32.   
  33.   The package includes the operations *, +, -, inverse, transpose,
  34.   conversion between types, submatrix, determinant, Cholesky decomposition
  35.   and Householder triangularisation.
  36.   
  37.   This description begins with some general notes on matrix packages, then
  38.   a general description of my package and a list of features and problems.
  39.  
  40. What we want from a matrix package:
  41.  
  42.   A matrix package needs to provide
  43.   
  44.   1.   Evaluation of matrix expressions in a form familiar to
  45.        scientists and engineers. For example  A = B * (C + D.t());
  46.   2.   Access to the elements of a matrix;
  47.   3.   Access to submatrices;
  48.   4.   Common elementary matrix functions such as determinant and trace;
  49.   5.   A platform for developing advanced matrix functions such as eigen-
  50.        value analysis;
  51.   6.   Good efficiency and storage management.
  52.   7.   Graceful exit from errors.
  53.   
  54.   It may also provide
  55.   
  56.   8.   A variety of types of elements (eg real and complex);
  57.   9.   A variety of types of matrices (eg rectangular and symmetric);
  58.   
  59.   It may target small matrices (say 3 x 3), or medium sized matrices, or
  60.   very large matrices.
  61.   
  62.   I do not believe one can build a matrix package that will meet
  63.   everyone's requirements. In many cases if you put in one facility, you
  64.   impose overheads on everyone using the package. This both is storage
  65.   required for the program and in efficiency. Likewise a package that is
  66.   optimised towards handling large matrices is likely to become less
  67.   efficient for very small matrices where the administration time for the
  68.   matrix may become significant compared with the time to carry out the
  69.   operations.
  70.   
  71.   It is better to provide a variety of packages (hopefully compatible) so
  72.   that most users can find one that meets their requirements. My package
  73.   is intended to be one of these packages; but not all of them.
  74.   
  75.   I don't think it is obvious what is the best way of going about
  76.   structuring a matrix package. And I don't think you can figure this
  77.   out with "thought" experiments. Different people have to try out
  78.   different approaches. And someone else may have to figure out which is
  79.   best. Or, more likely, the ultimate packages will lift some ideas from
  80.   each of a variety of trial packages. So, I don't claim my package is an
  81.   "ultimate" package, but simply a trial of a number of ideas.
  82.   
  83.   But I do hope it will meet the immediate requirements of some people who
  84.   need to carry out matrix calculations.
  85.   
  86. The present package:
  87.  
  88.   The package described here is intended to meet the requirements of
  89.   someone carrying out statistical analysis and related calculations.
  90.   
  91.   Specifically it caters for rectangular matrices, upper and lower
  92.   triangular matrices, diagonal matrices, symmetric matrices, and row and
  93.   column vectors. Only one element type (either float or double) is
  94.   allowed. A later version may include band matrices and/or a limited
  95.   sparse matrix capability.
  96.   
  97.   It is intended for matrices in the range 4 x 4 to about 80 x 80. The
  98.   upper limit is imposed by the maximum number of elements that can be
  99.   contained in a single array (8000 doubles in some machines). 
  100.   
  101.   Operations provided include +, -, *, inverse, transpose, scaling by a
  102.   scalar, conversion between types, submatrix, determinant, trace, sum of
  103.   squares of elements, Householder triangularisation, Cholesky
  104.   decomposition. Singular value decomposition is to come but is not in the
  105.   present package.
  106.   
  107.   The package is designed for version 2.0 of C++. It works with Sun C++,
  108.   Turbo C++, Borland C++, Glockenspiel C++ (2.00a) on a PC. There are
  109.   problems with Zortech C++ (version 2.12) and JPI C++.
  110.   
  111. Features:
  112.  
  113.   Matrix expressions are evaluated in two stages. In the the first stage a
  114.   tree representation of the expression is formed. For example (A+B)*C is
  115.   represented by
  116.   
  117.                       *
  118.                      / \
  119.                     +   C
  120.                    / \
  121.                   A   B
  122.   
  123.   The expression is evaluated recursively during the second stage. The two
  124.   stage approach means that an expression can be scanned before evaluation
  125.   and the best method of evaluation found.
  126.   
  127.   The package recognises combinations of the form  A.i()*B  where i() is
  128.   the inverse operation and avoids evaluating the inverse explicitly.
  129.   
  130.   The package recognises and takes advantage of a matrix appearing on both
  131.   sides of an '=' as in  A=B-A; resulting in faster execution and less
  132.   temporary storage.  There is no need to provide an operator += since 
  133.   A=A+B  is automatically recognised as an "add into" operation.
  134.   
  135.   Matrices generated temporarily when an expression is evaluated are
  136.   destroyed or recycled as soon as their values are no longer required.
  137.   Each matrix has a tag which indicates if it is temporary so that its
  138.   memory is available for recycling or deleting.
  139.   
  140.   Further possibilities not yet included are to recognise A.t()*A and
  141.   A.t()+A as symmetric or to improve the efficiency of evaluation of
  142.   expressions like A+B+C, A*B*C, A*B.t()  [t() denotes transpose].
  143.   
  144.   The package attempts to solve the problem of the large number of
  145.   versions of the binary operations required when one has a variety of
  146.   types. With n types of matrices the binary operations will each require
  147.   n-squared separate algorithms. Some reduction in the number may be
  148.   possible by carrying out conversions. However the situation rapidly
  149.   becomes impossible with more than 4 or 5 types.
  150.   
  151.   In this package each matrix type includes routines for extracting
  152.   individual rows or columns. Only a single algorithm is then required for
  153.   each binary operation. The rows can be located very quickly since most
  154.   of the matrices are stored row by row. Columns must be copied and so the
  155.   access is somewhat slower. The alternative approach of using iterators
  156.   will be slower since the iterators will involve virtual functions.
  157.   
  158.   In fact, I provide two algorithms for operations like + . If one is
  159.   adding two matrices of the same type then there is no need to access the
  160.   individual rows or columns and a faster general algorithm is
  161.   appropriate.
  162.   
  163.   The original version of the package did not use this access by row or
  164.   column method and provided the multitude of algorithms for the
  165.   combination of different matrix types. The code file length turned out
  166.   to be just a little longer than the present one when providing the same
  167.   facilities with 5 distinct types of matrices. It would have been very
  168.   difficult to increase the number of matrix types in the original
  169.   version. Apparently 4 to 5 types is about the break even point for
  170.   switching to the approach adopted in the present package.
  171.  
  172. Problems and limitations:
  173.  
  174.   The package does not have graceful exit from errors. There seems little
  175.   point in developing this until exceptions become available in C++.
  176.   
  177.   The package does not have delayed copy. In most situations this is not a
  178.   significant disadvantage. It is a problem with functions returning a
  179.   matrix; see the detailed documentation.
  180.   
  181.   The package does not readily handle matrices declared constant. Every
  182.   operation must have the option of recycling the memory of matrices in
  183.   its arguments. So it is not possible to declare these arguments as
  184.   constant. It is too complicated to declare versions of each operation
  185.   for constant and non-constant arguments and there doesn't seem to be a
  186.   way of doing automatic conversions. In the present package one needs to
  187.   carry out an explicit conversion.
  188.   
  189.   The package is long with a large .h file. I do not recommended it if you
  190.   are short of storage and require only matrices and vectors and do not
  191.   require triangular or symmetric matrices. If you need to deal with
  192.   several types of matrices then I am pretty sure you need the row and
  193.   column operations used in this package. I am not so sure about the two
  194.   stage approach to evaluating matrix expressions.
  195.   
  196.   It is also not recommended when you are dealing with very small matrices
  197.   when it becomes inefficient or very large matrices when memory
  198.   management becomes a problem (I may introduce a HugeMatrix type or alter
  199.   the memory storage method in a later version).
  200.   
  201.   There is a problem with overloading a function to access a variety of
  202.   matrix types. This works fine if you access only matrices with the type
  203.   known to the compiler. But it does not work with matrix expressions
  204.   since the compiler cannot determine the type.
  205.   
  206.   Matrix types of expressions are determined at run-time so the compiler
  207.   will not detect errors of the type Matrix M; DiagonalMatrix D; .... D=M;
  208.   Such errors will be detected when the statement is executed.
  209.   
  210.   Symmetric matrices are not handled very efficiently (yet) since complete
  211.   rows are not stored explicitly.
  212.   
  213.   It will be non-trivial to include band matrices or sparse matrices in
  214.   this package.
  215.  
  216. How to get a copy of this package:
  217.  
  218.   I am putting copies on at least some of Compuserve (Borland library, zip
  219.   format), SIMTEL20 (zip format), comp.sources.misc on Internet (shar
  220.   format), and on one of the program libraries at Victoria University,
  221.   Wellington.
  222.   
  223.   Users must understand that this is a test version; there may still be
  224.   bugs and errors. Use at your own risk. Neither I nor DSIR take any
  225.   responsibility for any errors or omissions in this package or for any
  226.   misfortune that may befall you or others as a result of its use.
  227.   
  228.   Please report bugs to me at
  229.   
  230.       robert@am.dsir.govt.nz
  231.   
  232.   or
  233.   
  234.       Compuserve 72777,656
  235.   
  236.   The matrix inverse routine is adapted from "Numerical Recipies in C" by
  237.   Press, Flannery, Teukolsky, Vetterling, published by the Cambridge
  238.   University Press.
  239.   
  240.   Some other code is adapted from routines in "Handbook for Automatic
  241.   Computation, Vol II, Linear Algebra" by Wilkinson and Reinsch, published
  242.   by Springer Verlag. 
  243.   
  244.  
  245. Robert Davies  (robert@am.dsir.govt.nz)
  246. exit 0 # Just in case...
  247.