source: trunk/LinAlgImpl/Serial/IpExpansionMatrix.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.6 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: IpExpansionMatrix.cpp 2 2004-10-21 01:03:09Z andreasw $
6//
7// Authors:  Carl Laird, Andreas Waechter     IBM    2004-08-13
8
9#include "IpExpansionMatrix.hpp"
10#include "IpDenseVector.hpp"
11
12namespace Ipopt
13{
14
15  ExpansionMatrix::ExpansionMatrix(const ExpansionMatrixSpace* owner_space)
16      :
17      Matrix(owner_space),
18      owner_space_(owner_space)
19  {}
20
21  ExpansionMatrix::~ExpansionMatrix()
22  {}
23
24  void ExpansionMatrix::MultVectorImpl(Number alpha, const Vector &x,
25                                       Number beta, Vector &y) const
26  {
27    //  A few sanity checks
28    DBG_ASSERT(NCols()==x.Dim());
29    DBG_ASSERT(NRows()==y.Dim());
30
31    // Take care of the y part of the addition
32    if( beta!=0.0 ) {
33      y.Scal(beta);
34    }
35    else {
36      y.Set(0.0);  // In case y hasn't been initialized yet
37    }
38
39    // See if we can understand the data
40    const DenseVector* dense_x = dynamic_cast<const DenseVector*>(&x);
41    DBG_ASSERT(dense_x); /* ToDo: Implement others */
42    DenseVector* dense_y = dynamic_cast<DenseVector*>(&y);
43    DBG_ASSERT(dense_y); /* ToDo: Implement others */
44
45    const Index* exp_pos = ExpandedPosIndices();
46
47    if (dense_x && dense_y) {
48      const Number* xvals=dense_x->Values();
49      Number* yvals=dense_y->Values();
50      for(Index i=0; i<NCols(); i++) {
51        yvals[exp_pos[i]] += alpha * xvals[i];
52      }
53    }
54  }
55
56  void ExpansionMatrix::TransMultVectorImpl(Number alpha, const Vector &x,
57      Number beta, Vector &y) const
58  {
59    //  A few sanity checks
60    DBG_ASSERT(NCols()==y.Dim());
61    DBG_ASSERT(NRows()==x.Dim());
62
63    // Take care of the y part of the addition
64    if( beta!=0.0 ) {
65      y.Scal(beta);
66    }
67    else {
68      y.Set(0.0);  // In case y hasn't been initialized yet
69    }
70
71    // See if we can understand the data
72    const DenseVector* dense_x = dynamic_cast<const DenseVector*>(&x);
73    DBG_ASSERT(dense_x); /* ToDo: Implement others */
74    DenseVector* dense_y = dynamic_cast<DenseVector*>(&y);
75    DBG_ASSERT(dense_y); /* ToDo: Implement others */
76
77    const Index* exp_pos = ExpandedPosIndices();
78
79    if (dense_x && dense_y) {
80      const Number* xvals=dense_x->Values();
81      Number* yvals=dense_y->Values();
82      for(Index i=0; i<NCols(); i++) {
83        yvals[i] += alpha * xvals[exp_pos[i]];
84      }
85    }
86  }
87
88  void ExpansionMatrix::PrintImpl(FILE* fp, std::string name, Index indent, std::string prefix) const
89  {
90    fprintf(fp, "\n");
91    for (Index ind=0; ind<indent; ind++) {
92      fprintf(fp, " ");
93    }
94    fprintf(fp, "%sExpansionMatrix \"%s\" with %d nonzero elements:\n",
95            prefix.c_str(), name.c_str(), NCols());
96
97    const Index* exp_pos = ExpandedPosIndices();
98
99    for (Index i=0; i<NCols(); i++) {
100      for (Index ind=0; ind<indent; ind++) {
101        fprintf(fp, " ");
102      }
103      fprintf(fp, "%s%s[%5d,%5d]=%23.16e  (%d)\n", prefix.c_str(), name.c_str(), exp_pos[i]+1,
104              i+1, 1., i);
105    }
106  }
107
108  ExpansionMatrixSpace::ExpansionMatrixSpace(Index NLargeVec,
109      Index NSmallVec,
110      const Index *ExpPos,
111      const int offset /*= 0*/)
112      :
113      MatrixSpace(NLargeVec, NSmallVec),
114      expanded_pos_(NULL),
115      compressed_pos_(NULL)
116  {
117    expanded_pos_  = new Index[NCols()];
118    compressed_pos_ = new Index[NRows()];
119    for (Index j=0; j<NRows(); j++) {
120      compressed_pos_[j] = -1;
121    }
122    for(Index i=0; i<NCols(); i++) {
123      //ToDo decide for offset
124      DBG_ASSERT(ExpPos[i]-offset<NRows() && ExpPos[i]-offset>=0);
125      expanded_pos_[i]=ExpPos[i]-offset;
126      compressed_pos_[ExpPos[i]-offset] = i;
127    }
128  }
129
130} // namespace Ipopt
Note: See TracBrowser for help on using the repository browser.