source: trunk/Common/IpSumMatrix.cpp @ 2

Last change on this file since 2 was 2, checked in by andreasw, 15 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.2 KB
Line 
1// Copyright (C) 2004, International BusinDess Machines and others.
2// All Rights Reserved.
3// This code is published under the Common Public License.
4//
5// $Id: IpSumMatrix.cpp 2 2004-10-21 01:03:09Z andreasw $
6//
7// Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13
8
9#include "IpSumMatrix.hpp"
10
11namespace Ipopt
12{
13
14  SumMatrix::SumMatrix(const SumMatrixSpace* owner_space)
15      :
16      Matrix(owner_space),
17      owner_space_(owner_space),
18      factors_(owner_space->NTerms(), 1.0),
19      matrices_(owner_space->NTerms())
20  {}
21
22  SumMatrix::~SumMatrix()
23  {}
24
25  void SumMatrix::SetTerm(Index iterm, Number factor,
26                          const Matrix& matrix)
27  {
28    DBG_ASSERT(iterm<owner_space_->NTerms());
29    factors_[iterm] = factor;
30    matrices_[iterm] = &matrix;
31  }
32
33  void SumMatrix::GetTerm(Index iterm, Number& factor, SmartPtr<const Matrix>& matrix) const
34  {
35    DBG_ASSERT(iterm<owner_space_->NTerms());
36    factor = factors_[iterm];
37    matrix = matrices_[iterm];
38  }
39
40  Index SumMatrix::NTerms() const
41  {
42    return owner_space_->NTerms();
43  }
44
45  void SumMatrix::MultVectorImpl(Number alpha, const Vector &x,
46                                 Number beta, Vector &y) const
47  {
48    //  A few sanity checks
49    DBG_ASSERT(NCols()==x.Dim());
50    DBG_ASSERT(NRows()==y.Dim());
51
52    // Take care of the y part of the addition
53    if( beta!=0.0 ) {
54      y.Scal(beta);
55    }
56    else {
57      y.Set(0.0);  // In case y hasn't been initialized yet
58    }
59
60    for (Index iterm=0; iterm<NTerms(); iterm++) {
61      DBG_ASSERT(IsValid(matrices_[iterm]));
62      matrices_[iterm]->MultVector(alpha*factors_[iterm], x,
63                                   1.0, y);
64    }
65  }
66
67  void SumMatrix::TransMultVectorImpl(Number alpha, const Vector& x,
68                                      Number beta, Vector& y) const
69  {
70    //  A few sanity checks
71    DBG_ASSERT(NRows()==x.Dim());
72    DBG_ASSERT(NCols()==y.Dim());
73
74    // Take care of the y part of the addition
75    if( beta!=0.0 ) {
76      y.Scal(beta);
77    }
78    else {
79      y.Set(0.0);  // In case y hasn't been initialized yet
80    }
81
82    for (Index iterm=0; iterm<NTerms(); iterm++) {
83      DBG_ASSERT(IsValid(matrices_[iterm]));
84      matrices_[iterm]->TransMultVector(alpha*factors_[iterm], x,
85                                        1.0, y);
86    }
87  }
88
89
90
91  void SumMatrix::PrintImpl(FILE* fp, std::string name, Index indent, std::string prefix) const
92  {
93    fprintf(fp, "\n");
94    for (Index ind=0; ind<indent; ind++) {
95      fprintf(fp, " ");
96    }
97    fprintf(fp, "%sSumMatrix \"%s\" of dimension %d x %d with %d terms:\n",
98            prefix.c_str(), name.c_str(), NRows(), NCols(), NTerms());
99    for (Index iterm=0; iterm<NTerms(); iterm++) {
100      for (Index ind=0; ind<indent; ind++) {
101        fprintf(fp, " ");
102      }
103      fprintf(fp, "%sTerm %d with factor %23.16e and the following matrix:\n",
104              prefix.c_str(), iterm, factors_[iterm]);
105      char buffer[256];
106      sprintf(buffer, "Term: %d", iterm);
107      std::string name = buffer;
108      matrices_[iterm]->Print(fp,name,indent,prefix);
109    }
110  }
111
112  SumMatrix* SumMatrixSpace::MakeNewSumMatrix() const
113  {
114    return new SumMatrix(this);
115  }
116
117  Matrix* SumMatrixSpace::MakeNew() const
118  {
119    return MakeNewSumMatrix();
120  }
121} // namespace Ipopt
Note: See TracBrowser for help on using the repository browser.