1 // Copyright (C) 2004, 2006 International Business Machines and others.
2 // All Rights Reserved.
3 // This code is published under the Common Public License.
4 //
5 // $Id: IpSumSymMatrix.hpp 759 2006-07-07 03:07:08Z andreasw $
6 //
7 // Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13
8 
9 #ifndef __IPSUMSYMMATRIX_HPP__
10 #define __IPSUMSYMMATRIX_HPP__
11 
12 #include "IpUtils.hpp"
13 #include "IpSymMatrix.hpp"
14 
15 namespace SimTKIpopt
16 {
17 
18   /* forward declarations */
19   class SumSymMatrixSpace;
20 
21   /** Class for Matrices which are sum of symmetric matrices.
22    *  For each term in the we store the matrix and a factor.
23    */
24   class SumSymMatrix : public SymMatrix
25   {
26   public:
27 
28     /**@name Constructors / Destructors */
29     //@{
30 
31     /** Constructor, initializing with dimensions of the matrix and
32      *  the number of terms in the sum.
33      */
34     SumSymMatrix(const SumSymMatrixSpace* owner_space);
35 
36     /** Destructor */
37     ~SumSymMatrix();
38     //@}
39 
40     /** Method for setting term iterm for the sum.  Note that counting
41      *  of terms starts at 0. */
42     void SetTerm(Index iterm, Number factor, const SymMatrix& matrix);
43 
44     /** Method for getting term iterm for the sum.  Note that counting
45      *  of terms starts at 0. */
46     void GetTerm(Index iterm, Number& factor, SmartPtr<const SymMatrix>& matrix) const;
47 
48     /** Return the number of terms */
49     Index NTerms() const;
50 
51   protected:
52     /**@name Methods overloaded from matrix */
53     //@{
54     virtual void MultVectorImpl(Number alpha, const Vector& x,
55                                 Number beta, Vector& y) const override;
56 
57     /** Method for determining if all stored numbers are valid (i.e.,
58      *  no Inf or Nan). */
59     virtual bool HasValidNumbersImpl() const override;
60 
61     virtual void PrintImpl(const Journalist& jnlst,
62                            EJournalLevel level,
63                            EJournalCategory category,
64                            const std::string& name,
65                            Index indent,
66                            const std::string& prefix) const override;
67     //@}
68 
69   private:
70     /**@name Default Compiler Generated Methods
71      * (Hidden to avoid implicit creation/calling).
72      * These methods are not implemented and
73      * we do not want the compiler to implement
74      * them for us, so we declare them private
75      * and do not define them. This ensures that
76      * they will not be implicitly created/called. */
77     //@{
78     /** Default Constructor */
79     SumSymMatrix();
80 
81     /** Copy Constructor */
82     SumSymMatrix(const SumSymMatrix&);
83 
84     /** Overloaded Equals Operator */
85     void operator=(const SumSymMatrix&);
86     //@}
87 
88     /** std::vector storing the factors for each term. */
89     std::vector<Number> factors_;
90 
91     /** std::vector storing the matrices for each term. */
92     std::vector<SmartPtr<const SymMatrix> > matrices_;
93 
94     /** Copy of the owner_space as a SumSymMatrixSpace */
95     const SumSymMatrixSpace* owner_space_;
96   };
97 
98   /** Class for matrix space for SumSymMatrix */
99   class SumSymMatrixSpace : public SymMatrixSpace
100   {
101   public:
102     /** @name Constructors / Destructors */
103     //@{
104     /** Constructor, given the dimension of the matrix and the number
105      *  of terms in the sum. */
SumSymMatrixSpace(Index ndim,Index nterms)106     SumSymMatrixSpace(Index ndim, Index nterms)
107         :
108         SymMatrixSpace(ndim),
109         nterms_(nterms)
110     {}
111 
112     /** Destructor */
~SumSymMatrixSpace()113     ~SumSymMatrixSpace()
114     {}
115     //@}
116 
117     /** @name Accessor functions */
118     //@{
119     /** Number of terms in the sum. */
NTerms() const120     Index NTerms() const
121     {
122       return nterms_;
123     }
124     //@}
125 
126     /** Use this method to set the matrix spaces for the various terms.
127      *  You will not be able to create a matrix until all these spaces
128      *  are set. */
129     void SetTermSpace(Index term_idx, const SymMatrixSpace& space);
130 
131     /** Get the matix space for a particular term */
132     SmartPtr<const SymMatrixSpace> GetTermSpace(Index term_idx) const;
133 
134     /** Method for creating a new matrix of this specific type. */
135     SumSymMatrix* MakeNewSumSymMatrix() const;
136 
137     /** Overloaded MakeNew method for the SymMatrixSpace base class.
138      */
139     virtual SymMatrix* MakeNewSymMatrix() const override;
140 
141   private:
142     Index nterms_;
143 
144     std::vector< SmartPtr<const SymMatrixSpace> > term_spaces_;
145   };
146 
147 } // namespace Ipopt
148 #endif
149