File: matrix.yo

package info (click to toggle)
c%2B%2B-annotations 13.02.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,576 kB
  • sloc: cpp: 25,297; makefile: 1,523; ansic: 165; sh: 126; perl: 90; fortran: 27
file content (59 lines) | stat: -rw-r--r-- 3,216 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
We'll start out by introducing a class template defining a matrix. Having
defined this class template we'll continue with defining several
specializations.

    Since matrices consist of well defined numbers of rows and columns (the
em(dimensions) of the matrix), that normally do not change when matrices are
used, we might consider specifying their values as template non-type
parameters. The tt(DataType = double) will be used in the majority of
cases. Therefore, tt(double) can be selected as the template's default type
argument. Since it's a sensible default, the tt(DataType) template type
parameter is used last in the template type parameter list.

Our template class tt(Matrix) begins its life as:
        verb(    template <size_t Rows, size_t Columns, typename DataType = double>
    class Matrix
    ...)

What do we want our class template to offer?
    itemization(
    it() It needs a place to store its matrix elements. This can be defined as
an array of `tt(Rows)' rows each containing `tt(Columns)' elements of type
tt(DataType). It can be an array, rather than a pointer, since the matrix'
dimensions are known em(a priori). Since a vector of tt(Columns) elements (a
em(row) of the matrix), as well as a vector of tt(Row) elements (a em(column)
of the matrix) is often used, the class could specify using-declarations to
represent them. The class interface's initial section thus contains:
        verbinclude(//HEAD examples/matrix.h)
    it() It should offer constructors: a default constructor and (e.g.,) a
constructor initializing the matrix from a stream. A copy or move constructor
is not required as the class does not use pointers. Likewise, no overloaded
assignment operator or destructor is required. Implementations:
        verbinclude(//CONSTRUCTORS examples/matrix.h)
    it() The class's tt(operator[]) member (and its tt(const) variant) only
handles the first index, returning a reference to a complete
tt(MatrixRow). How elements in a tt(MatrixRow) can be retrieved is shortly
covered. To keep the example simple, no array bound check has been
implemented:
        verbinclude(//OPERATORINDEX examples/matrix.h)
    it() Now we get to the interesting parts: computing marginals and the sum
of all elements in a tt(Matrix). We'll define the type tt(MatrixColumn) as the
type containing the row marginals of a matrix, and the type tt(MatrixRow) as
the type containing the column marginals of a matrix.

There is also the sum of all the elements of a matrix. This sum of all the
elements of a matrix is a number that itself can be thought of as a tt(1 x 1)
matrix.

Marginals can be considered as special forms of matrices. To represent these
marginals we can construct em(partial specializations) defining the class
templates tt(MatrixRow) and tt(MatrixColumn) objects; and we construct a
partial specialization handling tt(1 x 1) matrices.  These partial
specializations are used to compute marginals and the sum of all the elements
of a matrix.

Before concentrating on these partial specializations themselves we'll use
them here to implement the members computing the marginals and the sum of all
elements of a matrix:
        verbinclude(//MARGINALS examples/matrix.h)
    )