1 | |
---|
2 | /**************************************************************************\ |
---|
3 | |
---|
4 | MODULE: matrix |
---|
5 | |
---|
6 | SUMMARY: |
---|
7 | |
---|
8 | Macros are deined providing template-like classes for dynamic-sized, |
---|
9 | recatngular matrices. |
---|
10 | |
---|
11 | The macro NTL_matrix_decl(T,vec_T,vec_vec_T,mat_T) declares a new class |
---|
12 | mat_T representing matrices over T, where vec_T and vec_vec_T are |
---|
13 | classes representing "NTL vectors" over T and vec_T, respectively. |
---|
14 | |
---|
15 | The implementation of mat_T can be instantiated with |
---|
16 | NTL_matrix_impl(T,vec_T,vec_vec_T,mat_T). |
---|
17 | |
---|
18 | If T supports I/O and/or equluality testing, then mat_T |
---|
19 | can also be made to support these. |
---|
20 | |
---|
21 | For example, the declaration |
---|
22 | |
---|
23 | mat_T M; |
---|
24 | |
---|
25 | creates a 0 x 0 matrix. We can make it have 10 rows and 20 columns like this: |
---|
26 | |
---|
27 | M.SetDims(10, 20); |
---|
28 | |
---|
29 | A row can be accessed as M[i], indexing from 0, or as M(i), indexing from 1. |
---|
30 | A matrix entry can be accessed as M[i][j], indexing from 0, or as |
---|
31 | M(i, j), indexing from 1. |
---|
32 | |
---|
33 | A matrix is represented as a vec_vec_T: a vector of rows, where |
---|
34 | each row is a vec_T. Any attempt to resize one of the rows so |
---|
35 | as to create a non-rectangular matrix will result in a run-time |
---|
36 | error. |
---|
37 | |
---|
38 | The dimensions of an existing matrix may be changed. If the number of |
---|
39 | columns does not change, then the matrix is just "resized" like a vector, |
---|
40 | and no information is lost. Otherwise, if the number of columns changes, |
---|
41 | the matrix is completely destroyed, and a new matrix is created |
---|
42 | |
---|
43 | |
---|
44 | \**************************************************************************/ |
---|
45 | |
---|
46 | class mat_T { |
---|
47 | mat_T(); // initially 0 x 0 |
---|
48 | |
---|
49 | mat_T(const mat_T& a); |
---|
50 | mat_T& operator=(const mat_T& a); |
---|
51 | ~mat_T(); |
---|
52 | |
---|
53 | mat_T(INIT_SIZE_TYPE, long n, long m); |
---|
54 | // mat_T(INIT_SIZE, n, m) initializes an n x m matrix, invoking |
---|
55 | // the default constructor for T to initialize entries. |
---|
56 | |
---|
57 | void SetDims(long n, long m); |
---|
58 | // M.SetDims(n, m) makes M have dimension n x m. If the number of |
---|
59 | // columns (m) changes, previous storage is freed, and space for M |
---|
60 | // is reallocated and initialized; otherwise, more rows are |
---|
61 | // allocated as necessary (when number of rows increases), |
---|
62 | // excess rows are retained (when number of rows decreases), |
---|
63 | // and--importantly--the contents do not change. |
---|
64 | |
---|
65 | void kill(); free storage and make 0 x 0 |
---|
66 | |
---|
67 | long NumRows() const; |
---|
68 | // M.NumRows() returns the number of rows of M |
---|
69 | |
---|
70 | long NumCols() const; |
---|
71 | // M.NumCols() returns the number of columns of M |
---|
72 | |
---|
73 | vec_T& operator[](long i); |
---|
74 | const vec_T& operator[](long i) const; |
---|
75 | // access row i, initial index 0. Any attempt to change the length |
---|
76 | // of this row will raise an error. |
---|
77 | |
---|
78 | vec_T& operator()(long i); |
---|
79 | const vec_T& operator()(long i) const; |
---|
80 | // access row i, initial index 1. Any attempt to change the length |
---|
81 | // of this row will raise an error. |
---|
82 | |
---|
83 | T& operator()(long i, long j); |
---|
84 | const T& operator()(long i, long j) const; |
---|
85 | // access element (i, j), both indices starting at 1 |
---|
86 | |
---|
87 | long position(const vec_T& a) const; |
---|
88 | // returns index of a in matrix, or -1 if not present; |
---|
89 | // equivalent to rep(*this).position(a). |
---|
90 | |
---|
91 | |
---|
92 | long position1(const vec_T& a) const; |
---|
93 | // returns index of a in matrix, or -1 if not present; |
---|
94 | // equivalent to rep(*this).position1(a). |
---|
95 | |
---|
96 | |
---|
97 | }; |
---|
98 | |
---|
99 | const vec_vec_T& rep(const mat_T& a); |
---|
100 | // read-only access to underlying representation |
---|
101 | |
---|
102 | void swap(mat_T& X, mat_T& Y); |
---|
103 | // swaps X and Y (by swapping pointers) |
---|
104 | |
---|
105 | void MakeMatrix(mat_T& x, const vec_vec_T& a); |
---|
106 | // copies a to x, checking that it is "rectangular" |
---|
107 | |
---|
108 | /**************************************************************************\ |
---|
109 | |
---|
110 | Input/Output |
---|
111 | |
---|
112 | The I/O operators can be declared with |
---|
113 | NTL_io_matrix_decl(T,vec_T,vec_vec_T,mat_T), and |
---|
114 | implemented using NTL_io_matrix_impl(T,vec_T,vec_vec_T,mat_T). |
---|
115 | I/O is implemented using the underlying I/O operators for vec_vec_T. |
---|
116 | |
---|
117 | \**************************************************************************/ |
---|
118 | |
---|
119 | |
---|
120 | istream& operator>>(istream&, mat_T&); |
---|
121 | ostream& operator<<(ostream&, const mat_T&); |
---|
122 | |
---|
123 | /**************************************************************************\ |
---|
124 | |
---|
125 | Equality Testing |
---|
126 | |
---|
127 | The equality testing operators == and != can be declared |
---|
128 | NTL_eq_matrix_decl(T,vec_T,vec_vec_T,mat_T), and |
---|
129 | implemented using NTL_eq_matrix_impl(T,vec_T,vec_vec_T,mat_T). |
---|
130 | Equality testing is implemented using the underlying |
---|
131 | equality operators for vec_vec_T. |
---|
132 | |
---|
133 | |
---|
134 | \**************************************************************************/ |
---|
135 | |
---|
136 | |
---|
137 | long operator==(const mat_T& a, const mat_T& b); |
---|
138 | long operator!=(const mat_T& a, const mat_T& b); |
---|
139 | |
---|