Welcome to mirror list, hosted at ThFree Co, Russian Federation.

nl-fixedmatrix.h « include « rvtl « hhmm « synlm « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dbb9d9d9d49ab65ca21aba5cb34e909f6447eef7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// This file is part of ModelBlocks. Copyright 2009, ModelBlocks developers. //
//                                                                           //
//    ModelBlocks is free software: you can redistribute it and/or modify    //
//    it under the terms of the GNU General Public License as published by   //
//    the Free Software Foundation, either version 3 of the License, or      //
//    (at your option) any later version.                                    //
//                                                                           //
//    ModelBlocks is distributed in the hope that it will be useful,         //
//    but WITHOUT ANY WARRANTY; without even the implied warranty of         //
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          //
//    GNU General Public License for more details.                           //
//                                                                           //
//    You should have received a copy of the GNU General Public License      //
//    along with ModelBlocks.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                           //
//    ModelBlocks developers designate this particular file as subject to    //
//    the "Moses" exception as provided by ModelBlocks developers in         //
//    the LICENSE file that accompanies this code.                           //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////


//basically a SafeArray2D with operators defined
template<class T>
class Matrix : public SafeArray2D<Id<int>,Id<int>,T> {
  //public:
  //int xSize;
  //int ySize;
 public:
  // Constructor / destructor methods...
  //~Matrix( )                           { delete[] at; }
  Matrix ( )                         : SafeArray2D<Id<int>,Id<int>,T>( )     { }//{ xSize=0; ySize=0;  }
  Matrix (int x, int y)              : SafeArray2D<Id<int>,Id<int>,T>(x,y)   { }//{ xSize=x; ySize=y; }
  Matrix (int x, int y, const T& t)  : SafeArray2D<Id<int>,Id<int>,T>(x,y,t) { }//{ xSize=x; ySize=y; }
  Matrix (const Matrix& a)           : SafeArray2D<Id<int>,Id<int>,T>(a.xSize(),a.ySize()) { //xSize=a.xSize; ySize=a.ySize; 
                                                                               for(int i=0;i<xSize();i++) for(int j=0;j<ySize();j++) this->set(i,j)=a.get(i,j); }
  // Specification methods...
  //Matrix& operator= ( const Matrix<T>& sat )
  //  { xSize=sat.xSize; ySize=sat.ySize; //at=new T[xSize*ySize];
  //    for(int i=0;i<xSize;i++) for(int j=0;j<ySize;j++) set(i,j)=sat.at[i]; return *this; }
  void  init ( int x,int y )                   { (*this)=Matrix<T>(x,y,T()); }//xSize=x; ySize=y; }
  void  init ( int x,int y,const T& t )        { (*this)=Matrix<T>(x,y,t); }//xSize=x; ySize=y; }
  void  reset()                                { (*this)=Matrix<T>( ); }//xSize=0; ySize=0; }

  // Inherited methods
  //T&    set  ( const X1& x,const X2& y);
  //const T& get (const X1& x,const X2& y) const;

  int xSize( ) const { return this->getxSize(); }
  int ySize( ) const { return this->getySize(); }

  // Math...
  friend Matrix<T> operator* ( const Matrix<T>& a, const Matrix<T>& b ) {
    if (a.ySize()!=b.xSize()) {
      cerr<<"ERROR: matrix multiplication requires matching inner indices; "<<a.xSize()<<"x"<<a.ySize()<<" "<<b.xSize()<<"x"<<b.ySize()<<endl;
      #ifndef NDEBUG
      cerr<<" a= "<<a<<"\n\n b= "<<b<<endl;
      #endif
      return Matrix<T>();
    }
    Matrix mOut(a.xSize(),b.ySize(),T());
    for (int i=0; i<a.xSize(); i++ ){
      for (int k=0; k<a.ySize(); k++ ) {
	for (int j=0; j<b.ySize(); j++ ) {
	  mOut.set(i,j) += a.get(Id<int>(i),Id<int>(k))*b.get(Id<int>(k),Id<int>(j));
	}
      }
    }
    //cerr<<" a= "<<a<<"\n b= "<<b<<"\n c= "<<mOut<<endl<<endl;
    return mOut;
  }
  friend Matrix<T> operator& ( const Matrix<T>& a, const Matrix<T>& b ) {
    if (a.xSize()!=b.xSize() || a.ySize()!=b.ySize()) {
      cerr<<"ERROR: pt-by-pt multiplication requires matching indices; "<<a.xSize()<<"x"<<a.ySize()<<" "<<b.xSize()<<"x"<<b.ySize()<<endl;
      #ifndef NDEBUG
      cerr<<" a= "<<a<<"\n\n b= "<<b<<endl;
      #endif
      return Matrix<T>();
    }
    Matrix mOut(a.xSize(),a.ySize(),T());
    for (int i=0; i<a.xSize(); i++ ){
	for (int j=0; j<b.ySize(); j++ ) {
	  mOut.set(i,j) += a.get(Id<int>(i),Id<int>(j))*b.get(Id<int>(i),Id<int>(j));
	}
    }
    //cerr<<" a= "<<a<<"\n b= "<<b<<"\n c= "<<mOut<<endl<<endl;
    return mOut;
  }
  friend Matrix<T> operator+ ( const Matrix<T>& a, const Matrix<T>& b ) {
    if (a.xSize()!=b.xSize() || a.ySize()!=b.ySize()) {
      cerr<<"ERROR: matrix addition requires matching dimensions"<<endl;
      return Matrix<T>();
    }
    Matrix mOut(a.xSize(),b.ySize(),T());
    for (int i=0; i<a.xSize(); i++ ){
      for (int j=0; j<a.ySize(); j++ ) {
	mOut.set(i,j) = a.get(Id<int>(i),Id<int>(j))+b.get(Id<int>(i),Id<int>(j));
      }
    }
    return mOut;
  }
  friend Matrix<T> operator- ( const Matrix<T>& a, const Matrix<T>& b ) {
    if (a.xSize()!=b.xSize() || a.ySize()!=b.ySize()) {
      cerr<<"ERROR: matrix subtraction requires matching dimensions"<<endl;
      //cerr<<"aSize="<<a.xSize<<","<<a.ySize()<<"     bSize="<<b.xSize<<","<<b.ySize()<<endl;
      //cerr<<" a= "<<a<<"\n b= "<<b<<"\n c= "<<mOut<<endl<<endl;
      return Matrix<T>();
    }
    Matrix mOut(a.xSize(),b.ySize(),T());
    for (int i=0; i<a.xSize(); i++ ){
      for (int j=0; j<a.ySize(); j++ ) {
	mOut.set(i,j) = a.get(Id<int>(i),Id<int>(j))-b.get(Id<int>(i),Id<int>(j));
      }
    }
    return mOut;
  }
  friend Matrix<T> operator* ( const Matrix<T>& a, const T& t ) {
    Matrix mOut(a.xSize(),a.ySize());
    for (int i=0; i<a.xSize(); i++ ){
      for (int j=0; j<a.ySize(); j++ ) {
	mOut.set(i,j) = a.get(Id<int>(i),Id<int>(j))*t;
      }
    }
    return mOut;
  }
  friend Matrix<T> operator+ ( const Matrix<T>& a, const T& t ) {
    Matrix mOut(a.xSize(),a.ySize());
    for (int i=0; i<a.xSize(); i++ ){
      for (int j=0; j<a.ySize(); j++ ) {
	mOut.set(i,j) = a.get(Id<int>(i),Id<int>(j))+t;
      }
    }
    return mOut;
  }
  friend Matrix<T> operator- ( const Matrix<T>& a, const T& t ) {
    Matrix mOut(a.xSize(),a.ySize());
    for (int i=0; i<a.xSize(); i++ ){
      for (int j=0; j<a.ySize(); j++ ) {
	mOut.set(i,j) = a.get(Id<int>(i),Id<int>(j))-t;
      }
    }
    return mOut;
  }

  // Scalar inf-norm (max) of matrix / vector...
  T infnorm ( ) const {
    T tOut = T();
    for (int i=0; i<xSize(); i++ ){
      for (int j=0; j<ySize(); j++ ) {
	if ( this->get(Id<int>(i),Id<int>(j))>tOut ) tOut = this->get(Id<int>(i),Id<int>(j));
      }
    }
    return tOut;
  }

  /*
  // Argmax of matrix / vector...  //NOT WORKING
  pair<int,int> argmax ( ) const {
    T tOut = T();
    pair<int,int> ij();
    for (int i=0; i<xSize(); i++ ){
      for (int j=0; j<ySize(); j++ ) {
	if ( this->get(Id<int>(i),Id<int>(j))>tOut ) {
	  tOut = this->get(Id<int>(i),Id<int>(j));
	  ij = make_pair(i,j);
	}
      }
    }
    return ij; //pair<int,int>( ij.getIndex(), ij.getIndex() );
  }
  */
  // Diagonal matrix of vector...
  friend Matrix<T> diag ( const Matrix<T>& a ) {
    Matrix mOut(a.xSize(),a.xSize(),T()); // output is n x n
    for (int i=0;i<a.xSize();i++) {
      for (int j=0;j<a.ySize();j++) {
        assert(j==0); // must be vector, n x 1
        mOut.set(Id<int>(i),Id<int>(i)) += a.get(Id<int>(i),Id<int>(j));
      }
    }
    return mOut;
  }

  // Ordering method (treat as bit string)...
  bool operator< ( const Matrix<T>& mt ) const {
    if (xSize()<mt.xSize() || ySize()<mt.ySize()) return true;
    if (xSize()>mt.xSize() || ySize()>mt.ySize()) return false;
    for (int i=0; i<xSize(); i++ ) {
      for (int j=0; j<ySize(); j++ ) {
	if ( this->get(Id<int>(i),Id<int>(j)) < mt.get(Id<int>(i),Id<int>(j)) ) return true;
	else if ( this->get(Id<int>(i),Id<int>(j)) > mt.get(Id<int>(i),Id<int>(j)) ) return false;
      }
    }
    return false;
  }
  bool operator== ( const Matrix<T>& a ) const { 
    if (xSize()!=a.xSize() || ySize()!=a.ySize()) return false;
    for (int i=0;i<a.xSize();i++) 
      for (int j=0;j<a.ySize();j++)
	if (this->get(Id<int>(i),Id<int>(j))!=a.get(Id<int>(i),Id<int>(j))) return false;
    return true;
  }

  // Input/output methods...
  friend ostream& operator<< ( ostream& os, const Matrix<T>& a ) { 
    os<<"\n    ";
    for (int i=0;i<a.xSize();i++) {
      for (int j=0;j<a.ySize();j++) {
	os<<((j==0)?"":",")<<a.get(Id<int>(i),Id<int>(j));
      } 
      os<<(i==a.xSize()-1?"\n":"\n    ");
    }
    return os;  
  }
  friend String& operator<< ( String& str, const Matrix<T>& a ) { 
    str<<"\n    ";
    for (int i=0;i<a.xSize();i++) {
      for (int j=0;j<a.ySize();j++) {
	str<<((j==0)?"":",")<<a.get(Id<int>(i),Id<int>(j));
      } 
      str<<";";
    }
    return str;  
  }
  string getString( ) const;

};
template <class T>
string Matrix<T>::getString() const {
  string str;
  for (int i=0;i<xSize();i++) {
    for (int j=0;j<ySize();j++) {
      str += ((j==0)?"":",");
      str += this->get(Id<int>(i),Id<int>(j));
    } 
    str += ";";
  }
  return str;
}