~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to src/mod/mth/shl/Rmatrix.hpp

  • Committer: Package Import Robot
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2015-07-11 02:00:35 UTC
  • mfrom: (10.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150711020035-2nhpztq7s15qyc0v
Tags: 2.5.1-1
* New upstream release. (Closes: #789968)
* Update debian/control.
  - Update Standards-Version to 3.9.6.
* Add support mips64(el) and ppc64el. (Closes: #741508, #748146)
* Add patches/support-gcc-5.x.patch. (Closes: #777767)
  - Fix build with gcc-5.x.
* Add patches/Disable-NET0001.als.patch.
  - Disable test of NET0001.als.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
// - the copyright holder be liable for any  direct, indirect, incidental or -
12
12
// - special damages arising in any way out of the use of this software.     -
13
13
// ---------------------------------------------------------------------------
14
 
// - copyright (c) 1999-2012 amaury darsch                                   -
 
14
// - copyright (c) 1999-2015 amaury darsch                                   -
15
15
// ---------------------------------------------------------------------------
16
16
 
17
17
#ifndef  AFNIX_RMATRIX_HPP
20
20
#ifndef  AFNIX_RMI_HPP
21
21
#include "Rmi.hpp"
22
22
#endif
 
23
 
 
24
#ifndef  AFNIX_MUTEX_HPP
 
25
#include "Mutex.hpp"
 
26
#endif
23
27
 
24
28
#ifndef  AFNIX_RVECTOR_HPP
25
29
#include "Rvector.hpp"
28
32
namespace afnix {
29
33
 
30
34
  /// This Rmatrix class is the default implementation of the real matrix
31
 
  /// interface. Internally, the matrix is represented as an array of rows.
32
 
  /// Each row is represented as an array of t_real.
 
35
  /// interface. The matrix is designed to support a sparse representation
 
36
  /// the form of blocks aggregation. The block index is obtained from the
 
37
  /// matrix index with the help of field index. The lower index is derived
 
38
  /// from the lower 32 bits while the higher index is derived from the higher
 
39
  /// 32 bits if needed. The implementation automatically adapt itself with
 
40
  /// a 32 bits or 64 bits long integer. The lower 32 bits indexes are derived
 
41
  /// as LB[4] (lower bottom - 4 bits), CB[8] (center-bottom 8 bits), CC[8]
 
42
  /// (center-center 8 bits), CT[8] (center-top - 8 bits) and LT[4] (lower-top
 
43
  /// 4 bits). The upper block (HB) has a variable size.
33
44
  /// @author amaury darsch
34
45
 
35
46
  class Rmatrix : public Rmi {
36
47
  public:
37
48
    /// generate a random matrix by size
38
49
    /// @param size the matrix size
 
50
    /// @param rmin the minimum value
 
51
    /// @param rmax the maximum value
 
52
    static Rmatrix* random (const t_long size, const t_real rmin,
 
53
                            const t_real rmax);
 
54
 
 
55
    /// generate a random matrix by size
 
56
    /// @param size the matrix size
 
57
    /// @param rmin the minimum value
 
58
    /// @param rmax the maximum value
39
59
    /// @param ddom the diagonally dominant flag
 
60
    static Rmatrix* random (const t_long size, const t_real rmin,
 
61
                            const t_real rmax, const bool   ddom);
 
62
 
 
63
    /// generate a sparse matrix by size
 
64
    /// @param size the matrix size
40
65
    /// @param rmin the minimum value
41
66
    /// @param rmax the maximum value
42
 
    static Rmatrix random (const t_long size, const bool   ddom,
43
 
                           const t_real rmin, const t_real rmax);
44
 
 
 
67
    /// @param nzsz the non zero size
 
68
    static Rmatrix* sparse (const t_long size, const t_real rmin,
 
69
                            const t_real rmax, const t_long nzsz);
 
70
    
45
71
    /// add a matrix with another one
46
72
    /// @param mx the matrix argument
47
73
    /// @param my the matrix argument
61
87
    /// @param mx the matrix argument
62
88
    /// @param my the matrix argument
63
89
    friend Rmatrix operator * (const Rmatrix& mx, const Rmatrix& my);
64
 
    
 
90
 
 
91
  public:
 
92
    /// the internal lb type
 
93
    typedef t_real* t_rmlb;
 
94
    /// the internal cb type
 
95
    typedef t_rmlb* t_rmcb;
 
96
    /// the internal cc type
 
97
    typedef t_rmcb* t_rmcc;
 
98
    /// the internal ct type
 
99
    typedef t_rmcc* t_rmct;
 
100
    /// the internal lt type
 
101
    typedef t_rmct* t_rmlt;
 
102
    /// the internal hb type
 
103
    typedef t_rmlt* t_rmhb;
 
104
 
65
105
  private:
66
 
    /// the row array
67
 
    t_real** p_rtab;
68
 
 
 
106
    /// the sparse matrix
 
107
    t_rmhb p_rmhb;
 
108
    /// the block mutex
 
109
    Mutex* p_rmbm;
 
110
  
69
111
  public:
70
112
    /// create a null matrix
71
113
    Rmatrix (void);
79
121
    /// @param csiz the column size
80
122
    Rmatrix (const t_long rsiz, const t_long csiz);
81
123
 
 
124
    /// create a matrix by vector multiply
 
125
    /// @param u the vector argument
 
126
    /// @param v the vector argument
 
127
    Rmatrix (const Rvi& u, const Rvi& v);
 
128
 
82
129
    /// copy construct this matrix
83
130
    /// @param that the matrix to copy
84
131
    Rmatrix (const Rmatrix& that);
96
143
    /// @param that the matrix to assign
97
144
    Rmatrix& operator = (const Rmatrix& that);
98
145
 
99
 
    /// set a matrix by position
 
146
    /// @return the matrix row barrier
 
147
    t_long getrowb (void) const;
 
148
 
 
149
    /// @return the matrix column barrier
 
150
    t_long getcolb (void) const;
 
151
 
 
152
    /// return true if the matrix is null
 
153
    bool isnil (void) const;
 
154
 
 
155
    /// reset this matrix
 
156
    void reset (void);
 
157
 
 
158
    /// clear this matrix
 
159
    void clear (void);
 
160
 
 
161
    /// clear this matrix lower triangular part
 
162
    void clt (void);
 
163
 
 
164
    /// resize this matrix
 
165
    /// @param rsiz the new matrix row size
 
166
    /// @param csiz the new matrix column size
 
167
    void resize (const t_long rsiz, const t_long csiz);
 
168
 
 
169
    /// compress the matrix if possible
 
170
    bool compress (void);
 
171
 
 
172
    /// create a zero identical matrix
 
173
    Rmi* zeros (void) const;
 
174
 
 
175
    /// copy a matrix row into a vector
 
176
    /// @param row the row to copy
 
177
    Rvi* cpr (const t_long row) const;
 
178
 
 
179
    /// copy a matrix column into a vector
 
180
    /// @param col the column to copy
 
181
    Rvi* cpc (const t_long col) const;
 
182
 
 
183
    /// @return a new iterator for this matrix
 
184
    Iterator* makeit (void);
 
185
 
 
186
    /// permutate this matrix
 
187
    /// @param p the permutation object
 
188
    Rmi* permutate (const Rpi& p) const;
 
189
 
 
190
    /// reverse permutate this matrix
 
191
    /// @param p the permutation object
 
192
    Rmi* reverse (const Rpi& p) const;
 
193
 
 
194
  public:
 
195
    /// no lock - clear a matrix zone
 
196
    /// @param row the stating row
 
197
    /// @param col the starting column
 
198
    /// @param rsz the row size
 
199
    /// @param csz the column size
 
200
    void nlclear (const t_long row, const t_long col,
 
201
                  const t_long rsz, const t_long csz);
 
202
 
 
203
    /// no lock - set a matrix by position
100
204
    /// @param row the row position
101
205
    /// @param col the column position
102
206
    /// @param val the value to set
103
 
    void set (const t_long row, const t_long col, const t_real val);
 
207
    void nlset (const t_long row, const t_long col, const t_real val);
104
208
 
105
 
    /// get a matrix value by position
 
209
    /// no lock - get a matrix value by position
106
210
    /// @param row the row position
107
211
    /// @param col the column position
108
 
    t_real get (const t_long row, const t_long col) const;
109
 
 
110
 
    /// compare two matrices upto a precision
111
 
    /// @param mx the matrix argument
112
 
    bool cmp (const Rmi& x) const;
113
 
    
114
 
    /// @return the matrix frobenius norm
115
 
    t_real norm (void) const;
116
 
 
117
 
    /// multiply a matrix with a vector and a scaling factor
118
 
    /// @param r the result vector
119
 
    /// @param x the vector argument
120
 
    /// @param s the scaling factor
121
 
    Rvi& mul (Rvi& r, const Rvi& x, const t_real s) const;
122
 
 
123
 
  protected:
124
 
    /// internally multiply a matrix with a vector, no lock
125
 
    /// @param r the result vector
126
 
    /// @param x the vector argument
127
 
    /// @param s the scaling factor
128
 
    Rvector& imul (Rvector& r, const Rvector& x, const t_real s) const;
 
212
    t_real nlget (const t_long row, const t_long col) const;
 
213
 
 
214
    /// no lock - perform a givens matrix update
 
215
    /// @param i the row coordinate
 
216
    /// @param j the column coordinate
 
217
    /// @param c the givens ii/jj coefficient
 
218
    /// @param s the givens ij/ji coefficient
 
219
    /// @param pflg the partial flag
 
220
    void nlgivens (const t_long i, const t_long j, const t_real c, 
 
221
                   const t_real s, const bool pflg);
 
222
 
 
223
  private:
 
224
    // make the matrix iterator a friend
 
225
    friend class Rmatrixit;
129
226
 
130
227
  public:
131
228
    /// create a new object in a generic way
132
229
    /// @param argv the argument vector
133
230
    static Object* mknew (Vector* argv);
134
231
 
 
232
    /// @return true if the given quark is defined
 
233
    bool isquark (const long quark, const bool hflg) const;
 
234
 
135
235
    /// operate this object with another object
136
236
    /// @param type   the operator type
137
237
    /// @param object the operand object
138
238
    Object* oper (t_oper type, Object* object);
 
239
 
 
240
    /// apply this object with a set of arguments and a quark
 
241
    /// @param robj  the current runnable
 
242
    /// @param nset  the current nameset    
 
243
    /// @param quark the quark to apply these arguments
 
244
    /// @param argv  the arguments to apply
 
245
    Object* apply (Runnable* robj, Nameset* nset, const long quark,
 
246
                   Vector* argv);
 
247
  };
 
248
 
 
249
  /// The Rmatrixit class is the iterator for the real matrix class. Such 
 
250
  /// iterator is constructed with the "makeit" method. The iterator
 
251
  /// is reset to the beginning of the matrix.
 
252
  /// @author amaury darsch
 
253
 
 
254
  class Rmatrixit : public Rmit {
 
255
  private:
 
256
    /// the matrix to iterate
 
257
    Rmatrix* p_mobj;
 
258
    /// the hb size    
 
259
    long d_hbsz;
 
260
    /// the original move row
 
261
    t_long d_mrow;
 
262
    /// the original move column
 
263
    t_long d_mcol;
 
264
    /// the hb block index
 
265
    long d_bihb;
 
266
    /// the lt block index
 
267
    long d_bilt;
 
268
    /// the ct block index
 
269
    long d_bict;
 
270
    /// the cc block index
 
271
    long d_bicc;
 
272
    /// the cl block index
 
273
    long d_bicb;
 
274
    /// the lb block index
 
275
    long d_bilb;
 
276
    /// the hb mcnext barrier 
 
277
    long d_hbib;
 
278
    /// the lt mcnext barrier 
 
279
    long d_ltib;
 
280
    /// the ct mcnext barrier 
 
281
    long d_ctib;
 
282
    /// the cc index barrier 
 
283
    long d_ccib;
 
284
    /// the cb index barrier 
 
285
    long d_cbib;
 
286
    /// the lb index barrier 
 
287
    long d_lbib;
 
288
    /// the lt col max barrier 
 
289
    long d_ltcm;
 
290
    /// the ct col max barrier 
 
291
    long d_ctcm;
 
292
    /// the cc col max barrier 
 
293
    long d_cccm;
 
294
    /// the cb col max barrier 
 
295
    long d_cbcm;
 
296
    /// the lb col max barrier 
 
297
    long d_lbcm;
 
298
    /// the lt col reset barrier 
 
299
    long d_ltrb;
 
300
    /// the ct col reset barrier 
 
301
    long d_ctrb;
 
302
    /// the cc col reset barrier 
 
303
    long d_ccrb;
 
304
    /// the cb col reset barrier 
 
305
    long d_cbrb;
 
306
    /// the lb col reset barrier 
 
307
    long d_lbrb;
 
308
    
 
309
  public:
 
310
    /// create a new iterator from a matrix
 
311
    /// @param mobj the matrix to iterate
 
312
    Rmatrixit (Rmatrix* vobj);
 
313
 
 
314
    /// create a new iterator by type
 
315
    /// @param mobj the matrix to iterate
 
316
    /// @param rmit the matrix iterator type
 
317
    Rmatrixit (Rmatrix* vobj, const t_rmit rmit);
 
318
    
 
319
    /// destroy this matrix iterator
 
320
    ~Rmatrixit (void);
 
321
    
 
322
    /// @return the class name
 
323
    String repr (void) const;
 
324
    
 
325
    /// reset the iterator to the begining
 
326
    void begin (void);
 
327
    
 
328
    /// reset the iterator to the end
 
329
    void end (void);
 
330
        
 
331
    /// @return the object at the current position
 
332
    Object* getobj (void) const;
 
333
    
 
334
  public:
 
335
    /// no lock - move the matrix iterator to the next position
 
336
    void nlnext (void);
 
337
    
 
338
    /// no lock - move the matrix iterator to the previous position
 
339
    void nlprev (void);
 
340
 
 
341
    /// @return true if the iterator is at the end (no lock)
 
342
    bool nlend (void) const;
 
343
    
 
344
    /// no lock - move the iterator to a point
 
345
    /// @param row the matrix row coordinate
 
346
    /// @param col the matrix col coordinate
 
347
    void nlmove (const t_long row, const t_long col);
 
348
 
 
349
    /// @return the row coordinate at the iterator position (no lock)
 
350
    t_long nlgrow (void) const;
 
351
 
 
352
    /// @return the column coordinate at the iterator position (no lock)
 
353
    t_long nlgcol (void) const;
 
354
 
 
355
    /// set the matrix at the iterator position (no lock)
 
356
    /// @param val the value to set
 
357
    void nlsval (const t_real val);
 
358
 
 
359
    /// @return the value at the iterator position (no lock)
 
360
    t_real nlgval (void) const;
 
361
 
 
362
  private:
 
363
    // move the iterator to the next position (no lock)
 
364
    void   mvnext (void);
 
365
    // move the iterator to the next row position (no lock)
 
366
    void   mrnext (void);
 
367
    // move the iterator to the next column position (no lock)
 
368
    void   mcnext (void);
 
369
    // move the iterator to the previous position (no lock)
 
370
    void   mvprev (void);
 
371
    // move the iterator to the previous row position (no lock)
 
372
    void   mrprev (void);
 
373
    // move the iterator to the previous column position (no lock)
 
374
    void   mcprev (void);
 
375
 
 
376
  private:
 
377
    // make the copy constructor private
 
378
    Rmatrixit (const Rmatrixit&);
 
379
    // make the assignment operator private
 
380
    Rmatrixit& operator = (const Rmatrixit&);
139
381
  };
140
382
}
141
383