blitz  Version 0.9
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
matrix.h
Go to the documentation of this file.
1 /***************************************************************************
2  * blitz/matrix.h Declaration of the Matrix<T_type, T_structure> class
3  *
4  * $Id: matrix.h,v 1.6 2003/12/11 03:44:22 julianc Exp $
5  *
6  * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * Suggestions: blitz-dev@oonumerics.org
19  * Bugs: blitz-bugs@oonumerics.org
20  *
21  * For more information, please see the Blitz++ Home Page:
22  * http://oonumerics.org/blitz/
23  *
24  ***************************************************************************/
25 
26 #ifndef BZ_MATRIX_H
27 #define BZ_MATRIX_H
28 
29 #ifndef BZ_BLITZ_H
30  #include <blitz/blitz.h>
31 #endif
32 
33 #ifndef BZ_MEMBLOCK_H
34  #include <blitz/memblock.h>
35 #endif
36 
37 #ifndef BZ_MSTRUCT_H
38  #include <blitz/mstruct.h>
39 #endif
40 
41 BZ_NAMESPACE(blitz)
42 
43 // Forward declarations
44 template<typename P_numtype, typename P_structure>
45 class _bz_MatrixRef;
46 
47 template<typename P_expr>
48 class _bz_MatExpr;
49 
50 // Declaration of class Matrix
51 template<typename P_numtype, typename P_structure BZ_TEMPLATE_DEFAULT(RowMajor)>
52 class Matrix : protected MemoryBlockReference<P_numtype> {
53 
54 private:
56  using T_base::data_;
57 
58 public:
59 
61  // Public Types
63 
64  typedef P_numtype T_numtype;
65  typedef P_structure T_structure;
67 
69  // Constructors //
71 
73  { }
74 
75  Matrix(int rows, int cols, T_structure structure = T_structure())
76  : structure_(structure)
77  {
78  structure_.resize(rows, cols);
79  MemoryBlockReference<T_numtype>::newBlock(structure_.numElements());
80  }
81 
82  // Matrix(int rows, int cols, T_numtype initValue,
83  // T_structure structure = T_structure(rows, cols));
84  // Matrix(int rows, int cols, Random);
85  // Matrix(int rows, int cols, matrix-expression);
86  // Matrix(int rows, int cols, T_numtype* data, int rowStride, int colStride);
87  // explicit Matrix(Vector<T_numtype>& matrix);
88  // explicit Matrix(unsigned length);
89 
90  // Create a vector view of an already allocated block of memory.
91  // Note that the memory will not be freed when this vector is
92  // destroyed.
93  // Matrix(unsigned length, T_numtype* data, int stride = 1);
94 
96  // Member functions
98 
99  //T_iterator begin() const;
100  //T_constIterator begin() const;
101  //T_vector copy() const;
102  //T_iterator end() const;
103  //T_constIterator end() const;
104 
105  unsigned cols() const
106  { return structure_.columns(); }
107 
108  unsigned columns() const
109  { return structure_.columns(); }
110 
111  void makeUnique() const;
112 
113  unsigned numElements() const
114  { return structure_.numElements(); }
115 
116  void reference(T_matrix&);
117 
118  void resize(unsigned rows, unsigned cols)
119  {
120  structure_.resize(rows, cols);
121  MemoryBlockReference<T_numtype>::newBlock(structure_.numElements());
122  }
123 
124 // void resizeAndPreserve(unsigned newLength);
125 
126  unsigned rows() const
127  { return structure_.rows(); }
128 
129  _bz_MatrixRef<T_numtype, T_structure> _bz_getRef() const
130  { return _bz_MatrixRef<T_numtype, T_structure>(*this); }
131 
133  // Subscripting operators
135 
136  T_numtype operator()(unsigned i, unsigned j) const
137  {
138  return structure_.get(data_, i, j);
139  }
140 
141  T_numtype& restrict operator()(unsigned i, unsigned j)
142  {
143  return structure_.get(data_, i, j);
144  }
145 
146  // T_matrix operator()(Range,Range);
147 
148  // T_matrixIndirect operator()(Vector<int>,Vector<int>);
149  // T_matrixIndirect operator()(integer-placeholder-expression, Range);
150  // T_matrix operator()(difference-equation-expression)
151 
153  // Assignment operators
155 
156  // Scalar operand
157  T_matrix& operator=(T_numtype);
158  T_matrix& operator+=(T_numtype);
159  T_matrix& operator-=(T_numtype);
160  T_matrix& operator*=(T_numtype);
161  T_matrix& operator/=(T_numtype);
162 
163  // Matrix operand
164 
165  template<typename P_numtype2, typename P_structure2>
166  T_matrix& operator=(const Matrix<P_numtype2, P_structure2> &);
167  template<typename P_numtype2, typename P_structure2>
168  T_matrix& operator+=(const Matrix<P_numtype2, P_structure2>&);
169  template<typename P_numtype2, typename P_structure2>
170  T_matrix& operator-=(const Matrix<P_numtype2, P_structure2> &);
171  template<typename P_numtype2, typename P_structure2>
172  T_matrix& operator*=(const Matrix<P_numtype2, P_structure2> &);
173  template<typename P_numtype2, typename P_structure2>
174  T_matrix& operator/=(const Matrix<P_numtype2, P_structure2> &);
175 
176  // Matrix expression operand
177  template<typename P_expr>
178  T_matrix& operator=(_bz_MatExpr<P_expr>);
179 
180  // Integer placeholder expression operand
181  // MatrixPick operand
182 
184  // Unary operators
186 
187  T_matrix& operator++();
188  void operator++(int);
189  T_matrix& operator--();
190  void operator--(int);
191 
192 private:
193  T_structure structure_;
194 };
195 
196 template<typename P_numtype, typename P_structure>
197 ostream& operator<<(ostream& os, const Matrix<P_numtype, P_structure>& matrix);
198 
199 // Global operators
200 // +,-,*,/ with all possible combinations of:
201 // - scalar
202 // - matrix
203 // - matrix pick
204 // - matrix expression
205 // Pointwise Math functions: sin, cos, etc.
206 // Global functions
207 
209 
210 #include <blitz/matrix.cc>
211 #include <blitz/matexpr.h>
212 
213 #endif // BZ_MATRIX_H