IpMatrix.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // Copyright (C) 2004, 2008 International Business Machines and others.
  2. // All Rights Reserved.
  3. // This code is published under the Eclipse Public License.
  4. //
  5. // $Id: IpMatrix.hpp 2276 2013-05-05 12:33:44Z stefan $
  6. //
  7. // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
  8. #ifndef __IPMATRIX_HPP__
  9. #define __IPMATRIX_HPP__
  10. #include "IpVector.hpp"
  11. namespace Ipopt
  12. {
  13. /* forward declarations */
  14. class MatrixSpace;
  15. /** Matrix Base Class. This is the base class for all derived matrix
  16. * types. All Matrices, such as Jacobian and Hessian matrices, as
  17. * well as possibly the iteration matrices needed for the step
  18. * computation, are of this type.
  19. *
  20. * Deriving from Matrix: Overload the protected XXX_Impl method.
  21. */
  22. class Matrix : public TaggedObject
  23. {
  24. public:
  25. /** @name Constructor/Destructor */
  26. //@{
  27. /** Constructor. It has to be given a pointer to the
  28. * corresponding MatrixSpace.
  29. */
  30. Matrix(const MatrixSpace* owner_space)
  31. :
  32. TaggedObject(),
  33. owner_space_(owner_space)
  34. {}
  35. /** Destructor */
  36. virtual ~Matrix()
  37. {}
  38. //@}
  39. /**@name Operations of the Matrix on a Vector */
  40. //@{
  41. /** Matrix-vector multiply. Computes y = alpha * Matrix * x +
  42. * beta * y. Do not overload. Overload MultVectorImpl instead.
  43. */
  44. void MultVector(Number alpha, const Vector& x, Number beta,
  45. Vector& y) const
  46. {
  47. MultVectorImpl(alpha, x, beta, y);
  48. }
  49. /** Matrix(transpose) vector multiply. Computes y = alpha *
  50. * Matrix^T * x + beta * y. Do not overload. Overload
  51. * TransMultVectorImpl instead.
  52. */
  53. void TransMultVector(Number alpha, const Vector& x, Number beta,
  54. Vector& y) const
  55. {
  56. TransMultVectorImpl(alpha, x, beta, y);
  57. }
  58. //@}
  59. /** @name Methods for specialized operations. A prototype
  60. * implementation is provided, but for efficient implementation
  61. * those should be specially implemented.
  62. */
  63. //@{
  64. /** X = X + alpha*(Matrix S^{-1} Z). Should be implemented
  65. * efficiently for the ExansionMatrix
  66. */
  67. void AddMSinvZ(Number alpha, const Vector& S, const Vector& Z,
  68. Vector& X) const;
  69. /** X = S^{-1} (r + alpha*Z*M^Td). Should be implemented
  70. * efficiently for the ExansionMatrix
  71. */
  72. void SinvBlrmZMTdBr(Number alpha, const Vector& S,
  73. const Vector& R, const Vector& Z,
  74. const Vector& D, Vector& X) const;
  75. //@}
  76. /** Method for determining if all stored numbers are valid (i.e.,
  77. * no Inf or Nan). */
  78. bool HasValidNumbers() const;
  79. /** @name Information about the size of the matrix */
  80. //@{
  81. /** Number of rows */
  82. inline
  83. Index NRows() const;
  84. /** Number of columns */
  85. inline
  86. Index NCols() const;
  87. //@}
  88. /** @name Norms of the individual rows and columns */
  89. //@{
  90. /** Compute the max-norm of the rows in the matrix. The result is
  91. * stored in rows_norms. The vector is assumed to be initialized
  92. * of init is false. */
  93. void ComputeRowAMax(Vector& rows_norms, bool init=true) const
  94. {
  95. DBG_ASSERT(NRows() == rows_norms.Dim());
  96. if (init) rows_norms.Set(0.);
  97. ComputeRowAMaxImpl(rows_norms, init);
  98. }
  99. /** Compute the max-norm of the columns in the matrix. The result
  100. * is stored in cols_norms The vector is assumed to be initialized
  101. * of init is false. */
  102. void ComputeColAMax(Vector& cols_norms, bool init=true) const
  103. {
  104. DBG_ASSERT(NCols() == cols_norms.Dim());
  105. if (init) cols_norms.Set(0.);
  106. ComputeColAMaxImpl(cols_norms, init);
  107. }
  108. //@}
  109. /** Print detailed information about the matrix. Do not overload.
  110. * Overload PrintImpl instead.
  111. */
  112. //@{
  113. virtual void Print(SmartPtr<const Journalist> jnlst,
  114. EJournalLevel level,
  115. EJournalCategory category,
  116. const std::string& name,
  117. Index indent=0,
  118. const std::string& prefix="") const;
  119. virtual void Print(const Journalist& jnlst,
  120. EJournalLevel level,
  121. EJournalCategory category,
  122. const std::string& name,
  123. Index indent=0,
  124. const std::string& prefix="") const;
  125. //@}
  126. /** Return the owner MatrixSpace*/
  127. inline
  128. SmartPtr<const MatrixSpace> OwnerSpace() const;
  129. protected:
  130. /** @name implementation methods (derived classes MUST
  131. * overload these pure virtual protected methods.
  132. */
  133. //@{
  134. /** Matrix-vector multiply. Computes y = alpha * Matrix * x +
  135. * beta * y
  136. */
  137. virtual void MultVectorImpl(Number alpha, const Vector& x, Number beta, Vector& y) const =0;
  138. /** Matrix(transpose) vector multiply.
  139. * Computes y = alpha * Matrix^T * x + beta * y
  140. */
  141. virtual void TransMultVectorImpl(Number alpha, const Vector& x, Number beta, Vector& y) const =0;
  142. /** X = X + alpha*(Matrix S^{-1} Z). Prototype for this
  143. * specialize method is provided, but for efficient
  144. * implementation it should be overloaded for the expansion matrix.
  145. */
  146. virtual void AddMSinvZImpl(Number alpha, const Vector& S, const Vector& Z,
  147. Vector& X) const;
  148. /** X = S^{-1} (r + alpha*Z*M^Td). Should be implemented
  149. * efficiently for the ExpansionMatrix.
  150. */
  151. virtual void SinvBlrmZMTdBrImpl(Number alpha, const Vector& S,
  152. const Vector& R, const Vector& Z,
  153. const Vector& D, Vector& X) const;
  154. /** Method for determining if all stored numbers are valid (i.e.,
  155. * no Inf or Nan). A default implementation always returning true
  156. * is provided, but if possible it should be implemented. */
  157. virtual bool HasValidNumbersImpl() const
  158. {
  159. return true;
  160. }
  161. /** Compute the max-norm of the rows in the matrix. The result is
  162. * stored in rows_norms. The vector is assumed to be
  163. * initialized. */
  164. virtual void ComputeRowAMaxImpl(Vector& rows_norms, bool init) const = 0;
  165. /** Compute the max-norm of the columns in the matrix. The result
  166. * is stored in cols_norms. The vector is assumed to be
  167. * initialized. */
  168. virtual void ComputeColAMaxImpl(Vector& cols_norms, bool init) const = 0;
  169. /** Print detailed information about the matrix. */
  170. virtual void PrintImpl(const Journalist& jnlst,
  171. EJournalLevel level,
  172. EJournalCategory category,
  173. const std::string& name,
  174. Index indent,
  175. const std::string& prefix) const =0;
  176. //@}
  177. private:
  178. /**@name Default Compiler Generated Methods
  179. * (Hidden to avoid implicit creation/calling).
  180. * These methods are not implemented and
  181. * we do not want the compiler to implement
  182. * them for us, so we declare them private
  183. * and do not define them. This ensures that
  184. * they will not be implicitly created/called. */
  185. //@{
  186. /** default constructor */
  187. Matrix();
  188. /** Copy constructor */
  189. Matrix(const Matrix&);
  190. /** Overloaded Equals Operator */
  191. Matrix& operator=(const Matrix&);
  192. //@}
  193. const SmartPtr<const MatrixSpace> owner_space_;
  194. /**@name CachedResults data members */
  195. //@{
  196. mutable TaggedObject::Tag valid_cache_tag_;
  197. mutable bool cached_valid_;
  198. //@}
  199. };
  200. /** MatrixSpace base class, corresponding to the Matrix base class.
  201. * For each Matrix implementation, a corresponding MatrixSpace has
  202. * to be implemented. A MatrixSpace is able to create new Matrices
  203. * of a specific type. The MatrixSpace should also store
  204. * information that is common to all Matrices of that type. For
  205. * example, the dimensions of a Matrix is stored in the MatrixSpace
  206. * base class.
  207. */
  208. class MatrixSpace : public ReferencedObject
  209. {
  210. public:
  211. /** @name Constructors/Destructors */
  212. //@{
  213. /** Constructor, given the number rows and columns of all matrices
  214. * generated by this MatrixSpace.
  215. */
  216. MatrixSpace(Index nRows, Index nCols)
  217. :
  218. nRows_(nRows),
  219. nCols_(nCols)
  220. {}
  221. /** Destructor */
  222. virtual ~MatrixSpace()
  223. {}
  224. //@}
  225. /** Pure virtual method for creating a new Matrix of the
  226. * corresponding type.
  227. */
  228. virtual Matrix* MakeNew() const=0;
  229. /** Accessor function for the number of rows. */
  230. Index NRows() const
  231. {
  232. return nRows_;
  233. }
  234. /** Accessor function for the number of columns. */
  235. Index NCols() const
  236. {
  237. return nCols_;
  238. }
  239. /** Method to test if a given matrix belongs to a particular
  240. * matrix space.
  241. */
  242. bool IsMatrixFromSpace(const Matrix& matrix) const
  243. {
  244. return (matrix.OwnerSpace() == this);
  245. }
  246. private:
  247. /**@name Default Compiler Generated Methods
  248. * (Hidden to avoid implicit creation/calling).
  249. * These methods are not implemented and
  250. * we do not want the compiler to implement
  251. * them for us, so we declare them private
  252. * and do not define them. This ensures that
  253. * they will not be implicitly created/called. */
  254. //@{
  255. /** default constructor */
  256. MatrixSpace();
  257. /** Copy constructor */
  258. MatrixSpace(const MatrixSpace&);
  259. /** Overloaded Equals Operator */
  260. MatrixSpace& operator=(const MatrixSpace&);
  261. //@}
  262. /** Number of rows for all matrices of this type. */
  263. const Index nRows_;
  264. /** Number of columns for all matrices of this type. */
  265. const Index nCols_;
  266. };
  267. /* Inline Methods */
  268. inline
  269. Index Matrix::NRows() const
  270. {
  271. return owner_space_->NRows();
  272. }
  273. inline
  274. Index Matrix::NCols() const
  275. {
  276. return owner_space_->NCols();
  277. }
  278. inline
  279. SmartPtr<const MatrixSpace> Matrix::OwnerSpace() const
  280. {
  281. return owner_space_;
  282. }
  283. } // namespace Ipopt
  284. // Macro definitions for debugging matrices
  285. #if COIN_IPOPT_VERBOSITY == 0
  286. # define DBG_PRINT_MATRIX(__verbose_level, __mat_name, __mat)
  287. #else
  288. # define DBG_PRINT_MATRIX(__verbose_level, __mat_name, __mat) \
  289. if (dbg_jrnl.Verbosity() >= (__verbose_level)) { \
  290. if (dbg_jrnl.Jnlst()!=NULL) { \
  291. (__mat).Print(dbg_jrnl.Jnlst(), \
  292. J_ERROR, J_DBG, \
  293. __mat_name, \
  294. dbg_jrnl.IndentationLevel()*2, \
  295. "# "); \
  296. } \
  297. }
  298. #endif // #if COIN_IPOPT_VERBOSITY == 0
  299. #endif