📄 question.html
字号:
<HTML><HEAD><TITLE>Newmat09 - design questions</TITLE></HEAD><BODY><H2>Design questions</H2><A HREF="stor.html"> next</A> -<A HREF="stor.html"> skip</A> -<A HREF="design.html"> up</A> -<A HREF="index.html"> start</A><P>Even within the bounds set by the requirements of a matrixlibrarythere is a substantial opportunity for variation between whatdifferent matrix packages might provide. It is not possible tobuild a matrix package that will meet everyone's requirements. Inmany cases if you put in one facility, you impose overheads oneveryone using the package. This both in storage required for theprogram and in efficiency. Likewise a package that is optimisedtowards handling large matrices is likely to become lessefficient for very small matrices where the administration timefor the matrix may become significant compared with the time tocarry out the operations. It is better to provide a variety of packages(hopefully compatible) so that most users can find one that meetstheir requirements. This package is intended to be one of thesepackages; but not all of them.<P>Since my background is in statistical methods, this package isoriented towards the kinds things you need for statisticalanalyses.<P>Now looking at some specific questions.<P><H3>What size of matrices?</H3>A matrix library may target small matrices (say 3 x 3), ormedium sized matrices, or very large matrices.<P>A library targeting very small matrices will seek to minimise administration.A library for medium sized or very large matrices can spend moretime on administration in order to conserve space or optimise theevaluation of expressions. A library for very large matrices willneed to pay special attention to storage and numericalproperties. This library is designed for medium sized matrices. Thismeans it is worth introducing some optimisations, but I don'thave to worry about setting up some form of virtual memory.<P><H3>Which matrix types?</H3><P>As well as the usual rectangular matrices, matrices occuringrepeatedly in numerical calculations are upper and lowertriangular matrices, symmetric matrices and diagonal matrices.This is particularly the case in calculations involving leastsquares and eigenvalue calculations. So as a first stage these werethe types I decided to include.<P>It is also necessary to have types row vector and columnvector. In a <I>matrix</I> package, in contrast to an <I>array</I>package, it is necessary to have both these types since theybehave differently in matrix expressions. The vector types can bederived for the rectangular matrix type, so having them does not greatlyincrease the complexity of the package.<P>The problem with having several matrix types is the number ofversions of the binary operators one needs. If one has 5 distinctmatrix types then a simple library will need 25 versions of eachof the binary operators. In fact, we can evade this problem, but atthe cost of some complexity. <P><H3>What element types?</H3><P>Ideally we would allow element types double, float, complexand int, at least. It might be reasonably easy, using templatesor equivalent, to provide a library which could handle a varietyof element types. However, as soon as one starts implementing thebinary operators between matrices with different element types,again one gets an explosion in the number of operations one needsto consider. At the present time the compilers I deal with are notup to handling this problem with templates. (Of course, when I startedwriting <I>newmat</I> there were no templates). But even when thecompilers do meet the specifications of the draft standard, writinga matrix package that allows for a variety of element types usingthe template mechanism is going to be very difficult. I am inclinedto use templates in an <I>array</I> library but not in a <I>matrix</I>library.<P>Hence I decided to implement only one element type. But theuser can decide whether this is float or double. The packageassumes elements are of type Real. The user typedefs Real tofloat or double.<P>It might also be worth including symmetric and triangularmatrices with extra precision elements (double or long double) tobe used for storage only and with a minimum of operationsdefined. These would be used for accumulating the results of sumsof squares and product matrices or multistage QR triangularisations. <P><H3>Allow matrix expressions</H3><P>I want to be able to write matrix expressions the way I wouldon paper. So if I want to multiply two matrices and then add thetranspose of a third one I can write something like<TT>X = A * B + C.t();</TT>.I want this expression to be evaluated with close to thesame efficiency as a hand-coded version. This is not so much of aproblem with expressions including a multiply since the multiplywill dominate the time. However, it is not so easy to achievewith expressions with just <TT>+</TT> and <TT>-</TT>.<P>A second requirement is that temporary matrices generatedduring the evaluation of an expression are destroyed as quicklyas possible.<P>A desirable feature is that a certain amount of<I>intelligence</I> be displayed in the evaluation of an expression.For example, in the expression <TT>X = A.i() * B;</TT> where <TT>i()</TT> denotesinverse, it would be desirable if the inverse wasn't explicitlycalculated.<P><H3>Naming convention</H3>How are classes and public member functions to be named? As ageneral rule I have spelt identifiers out in full with individualwords being capitalised. For example<I>UpperTriangularMatrix</I>. If you don't like this you can#define or typedef shorter names. This convention means you can selectan abbreviation scheme that makes sense to you.<P>Exceptions to the general rule are the functions for transposeand inverse. To make matrix expressions more like thecorresponding mathematical formulae, I have used the singleletter abbreviations, <TT>t()</TT> and <TT>i()</TT>. <P><H3>Row and column index ranges</H3>In mathematical work matrix subscripts usually start at one.In C, array subscripts start at zero. In Fortran, they start atone. Possibilities for this package were to make them start at 0or 1 or be arbitrary.<P>Alternatively one could specify an <I>index set</I> forindexing the rows and columns of a matrix. One would be able toadd or multiply matrices only if the appropriate row and columnindex sets were identical.<P>In fact, I adopted the simpler convention of making the rowsand columns of a matrix be indexed by an integer starting at one,following the traditional convention. In an earlier version ofthe package I had them starting at zero, but even I was gettingmixed up when trying to use this earlier package. So I revertedto the more usual notation and started at 1. <P><H3>Element access - method and checking</H3>We want to be able to use the notation <TT>A(i,j)</TT> to specify the<TT>(i,j)</TT>-th element of a matrix. This is the way mathematiciansexpect to address the elements of matrices. I consider thenotation <TT>A[i][j]</TT> totally alien. However I include this as anoption to help people converting from C.<P>There are two ways of working out the address of <TT>A(i,j)</TT>. Oneis using a <I>dope</I> vector which contains the firstaddress of each row. Alternatively you can calculate the addressusing the formula appropriate for the structure of <TT>A</TT>. I use thissecond approach. It is probably slower, but saves worrying aboutan extra bit of storage.<P>The other question is whether to check for <TT>i</TT> and <TT>j</TT>being in range.I do carry out this check following years of experience with bothsystems that do and systems that don't do this check. I wouldhope that the routines I supply with this package will reduceyour need to access elements of matrices so speed of access isnot a high priority. <P><H3>Use iterators</H3>Iterators are an alternative way of providing fast access to theelements of an array or matrix when they are to be accessedsequentially. They need to be customised for each type of matrix. I havenot implemented iterators in this package, although some iterator likefunctions are used internally for some row and column functions.<P><A HREF="stor.html"> next</A> -<A HREF="stor.html"> skip</A> -<A HREF="design.html"> up</A> -<A HREF="index.html"> start</A><P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -