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

nl-matrix.h « include « rvtl « hhmm « synlm « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c8c18f8e152d6925222d44615f38a11616f6bb26 (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
///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// 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.                           //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

template<class I,class J,class P>
class SparseMatrix : public SimpleHash<I,SimpleHash<J,P> > {
 public:

  typedef SimpleHash<I,SimpleHash<J,P> > Parent;

  //// Matrix / vector operator methods...
  friend SparseMatrix<I,J,P> operator* ( const SparseMatrix<I,J,P>& a, const SparseMatrix<I,J,P>& b ) {
    SparseMatrix mOut;
    for ( typename Parent::const_iterator iit=a.begin(); iit!=a.end(); iit++ ) {
      I i = iit->first;
      for ( typename SimpleHash<J,P>::const_iterator kit=a.get(i).begin(); kit!=a.get(i).end(); kit++ ) {
	I k = kit->first;
        for ( typename SimpleHash<J,P>::const_iterator jit=b.get(k).begin(); jit!=b.get(k).end(); jit++ ) {
	  I j = jit->first;
          if ( a.get(i).get(k)!=0 && b.get(k).get(j)!=0 )
            mOut.set(i).set(j) += a.get(i).get(k) * b.get(k).get(j);
        }
      }
    }
    return mOut;
  }
  friend SparseMatrix<I,J,P> operator+ ( const SparseMatrix<I,J,P>& a, const SparseMatrix<I,J,P>& b ) {
    SparseMatrix mOut;
    for ( typename Parent::const_iterator iit=a.begin(); iit!=a.end(); iit++ ) {
      I i = iit->first;
      for ( typename SimpleHash<J,P>::const_iterator jit=a.get(i).begin(); jit!=a.get(i).end(); jit++ ) {
	I j = jit->first;
        mOut.set(i).set(j) = a.get(i).get(j);
      }
    }
    for ( typename Parent::const_iterator iit=b.begin(); iit!=b.end(); iit++ ) {
      I i = iit->first;
      for ( typename SimpleHash<J,P>::const_iterator jit=b.get(i).begin(); jit!=b.get(i).end(); jit++ ) {
	I j = jit->first;
        mOut.set(i).set(j) += b.get(i).get(j);
      }
    }
    return mOut;
  }
  friend SparseMatrix<I,J,P> operator- ( const SparseMatrix<I,J,P>& a, const SparseMatrix<I,J,P>& b ) {
    SparseMatrix mOut;
    for ( typename Parent::const_iterator iit=a.begin(); iit!=a.end(); iit++ ) {
      I i = iit->first;
      for ( typename SimpleHash<J,P>::const_iterator jit=a.get(i).begin(); jit!=a.get(i).end(); jit++ ) {
	I j = jit->first;
        mOut.set(i).set(j) = a.get(i).get(j);
      }
    }
    for ( typename Parent::const_iterator iit=b.begin(); iit!=b.end(); iit++ ) {
      I i = iit->first;
      for ( typename SimpleHash<J,P>::const_iterator jit=b.get(i).begin(); jit!=b.get(i).end(); jit++ ) {
	I j = jit->first;
        mOut.set(i).set(j) -= b.get(i).get(j);
      }
    }
    return mOut;
  }
  // Matrix + scalar operators...
  friend SparseMatrix<I,J,P> operator+ ( const SparseMatrix<I,J,P>& a, const P& p ) {
    SparseMatrix mOut;
    for ( typename Parent::const_iterator iit=a.begin(); iit!=a.end(); iit++ ) {
      I i = iit->first;
      for ( typename SimpleHash<J,P>::const_iterator jit=a.get(i).begin(); jit!=a.get(i).end(); jit++ ) {
	I j = jit->first;
        mOut.set(i).set(j) = a.get(i).get(j) + p;
      }
    }
    return mOut;
  }
  friend SparseMatrix<I,J,P> operator- ( const SparseMatrix<I,J,P>& a, const P& p ) {
    SparseMatrix mOut;
    for ( typename Parent::const_iterator iit=a.begin(); iit!=a.end(); iit++ ) {
      I i = iit->first;
      for ( typename SimpleHash<J,P>::const_iterator jit=a.get(i).begin(); jit!=a.get(i).end(); jit++ ) {
	I j = jit->first;
        mOut.set(i).set(j) = a.get(i).get(j) - p;
      }
    }
    return mOut;
  }
  // Diagonal matrix of vector...
  friend SparseMatrix<I,J,P> diag ( const SparseMatrix<I,J,P>& a ) {
    SparseMatrix mOut;
    for ( typename Parent::const_iterator iit=a.begin(); iit!=a.end(); iit++ ) {
      I i = iit->first;
      for ( typename SimpleHash<J,P>::const_iterator jit=a.get(i).begin(); jit!=a.get(i).end(); jit++ ) {
        I j = jit->first;
        assert(j==0); // must be vector
        mOut.set(i).set(i) += a.get(i).get(j);
      }
    }
    return mOut;
  }
  // Scalar inf-norm (max) of matrix / vector...
  P infnorm ( ) const {
    P pOut = 0;  // sparse matrix assumes some values are zero, so this is default infnorm.
    for ( typename Parent::const_iterator iit=Parent::begin(); iit!=Parent::end(); iit++ ) {
      I i = iit->first;
      for ( typename SimpleHash<J,P>::const_iterator jit=Parent::get(i).begin(); jit!=Parent::get(i).end(); jit++ ) {
	I j = jit->first;
        if ( Parent::get(i).get(j) > pOut ) pOut = Parent::get(i).get(j);
      }
    }
    return pOut;
  }

  // Scalar one-norm (sum) of matrix / vector...
  P onenorm ( ) const {
    P sum=0;
    for ( typename Parent::const_iterator iit=Parent::begin(); iit!=Parent::end(); iit++ ) {
      I i = iit->first;
      for ( typename SimpleHash<J,P>::const_iterator jit=Parent::get(i).begin(); jit!=Parent::get(i).end(); jit++ ) {
	I j = jit->first;
	sum += Parent::get(i).get(j);
      }
    }
    return sum;
  }

  //// Input / output methods...
  friend pair<StringInput,SparseMatrix<I,J,P>*> operator>> ( StringInput si, SparseMatrix<I,J,P>& m ) {
    return pair<StringInput,SparseMatrix<I,J,P>*>(si,&m);
  }
  friend StringInput operator>> ( pair<StringInput,SparseMatrix<I,J,P>*> si_m, const char* psD ) {
    if (StringInput(NULL)==si_m.first) return si_m.first;
    StringInput si; I i,j; P p;
    si=si_m.first>>i>>" : ">>j>>" = ">>p>>psD;
    if ( si!=NULL ) si_m.second->set(i).set(j) = p;
    return si;
  }
  friend ostream& operator<< ( ostream& os, const SparseMatrix<I,J,P>& m ) {
    int ctr=0;
    for ( typename Parent::const_iterator iit=m.begin(); iit!=m.end(); iit++ ) {
      I i = iit->first;
      for ( typename SimpleHash<J,P>::const_iterator jit=m.get(i).begin(); jit!=m.get(i).end(); jit++ ) {
	I j = jit->first;
        os<<((0==ctr++)?"":",")<<i<<":"<<j<<"="<<m.get(i).get(j);
      }
    }
    return os;
  }
  friend String&  operator<< ( String& str, const SparseMatrix<I,J,P>& m ) {
    int ctr=0;
    for ( typename Parent::const_iterator iit=m.begin(); iit!=m.end(); iit++ ) {
      I i = iit->first;
      for ( typename SimpleHash<J,P>::const_iterator jit=m.get(i).begin(); jit!=m.get(i).end(); jit++ ) {
	I j = jit->first;
        str<<((0==ctr++)?"":",")<<i<<j;
      }
    }
    return str;
  }
};