Debian Codegraph @

src/thirdparty/PoissonRecon/SparseMatrix.inl

raw
1 /* -*- C++ -*-
2 Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28
29 ///////////////////////////////////////////////////////////////
30 // SparseMatrix (unconstrained max row size specialization) //
31 ///////////////////////////////////////////////////////////////
32 template< class T , class IndexType >
33 SparseMatrix< T , IndexType , 0 >::SparseMatrix( void )
34 {
35 rowSizes = NullPointer( size_t );
36 rowNum = 0;
37 _entries = NullPointer( Pointer( MatrixEntry< T , IndexType > ) );
38 }
39
40 template< class T , class IndexType >
41 SparseMatrix< T , IndexType , 0 >::SparseMatrix( size_t rowNum )
42 {
43 this->rowNum = 0;
44 rowSizes = NullPointer( size_t );
45 _entries= NullPointer( Pointer( MatrixEntry< T , IndexType > ) );
46 resize( rowNum );
47 }
48 template< class T , class IndexType >
49 SparseMatrix< T , IndexType , 0 >::SparseMatrix( const SparseMatrix& M )
50 {
51 rowSizes = NullPointer( size_t );
52 rowNum = 0;
53 _entries = NullPointer( Pointer( MatrixEntry< T , IndexType > ) );
54 resize( M.rowNum );
55 for( size_t i=0 ; i<rowNum ; i++ )
56 {
57 setRowSize( i , M.rowSizes[i] );
58 for( size_t j=0 ; j<rowSizes[i] ; j++ ) _entries[i][j] = M._entries[i][j];
59 }
60 }
61 template< class T , class IndexType >
62 SparseMatrix< T , IndexType , 0 >::SparseMatrix( SparseMatrix&& M )
63 {
64 rowSizes = NullPointer( size_t );
65 rowNum = 0;
66 _entries = NullPointer( Pointer( MatrixEntry< T , IndexType > ) );
67
68 Swap( *this , M );
69 }
70 template< class T , class IndexType >
71 template< class T2 , class IndexType2 >
72 SparseMatrix< T , IndexType , 0 >::SparseMatrix( const SparseMatrix< T2 , IndexType2 , 0 >& M )
73 {
74 rowSizes = NullPointer( size_t );
75 rowNum = 0;
76 _entries = NULL;
77 resize( M.rowNum );
78 for( size_t i=0 ; i<rowNum ; i++ )
79 {
80 setRowSize( i , M.rowSizes[i] );
81 for( size_t j=0 ; j<rowSizes[i] ; j++ ) _entries[i][j] = MatrixEntry< T , IndexType >( M._entries[i][j].N , T( M._entries[i][j].Value ) );
82 }
83 }
84
85 template< class T , class IndexType >
86 template< class T2 , class IndexType2 >
87 SparseMatrix< T , IndexType , 0 >& SparseMatrix< T , IndexType , 0 >::copy( const SparseMatrix< T2 , IndexType2 , 0 >& M )
88 {
89 resize( M.rowNum );
90 for ( size_t i=0 ; i<rowNum ; i++)
91 {
92 setRowSize( i , M.rowSizes[i] );
93 for( size_t j=0 ; j<rowSizes[i] ; j++ )
94 {
95 IndexType2 idx = M._entries[i][j].N;
96 _entries[i][j] = MatrixEntry< T , IndexType >( (IndexType)idx , T( M[i][j].Value ) );
97 }
98 }
99 return *this;
100 }
101 template< class T , class IndexType >
102 SparseMatrix< T , IndexType , 0 >& SparseMatrix< T , IndexType , 0 >::operator = ( SparseMatrix< T , IndexType , 0 >&& M )
103 {
104 Swap( *this , M );
105 return *this;
106 }
107
108 template< class T , class IndexType >
109 SparseMatrix< T , IndexType , 0 >& SparseMatrix< T , IndexType , 0 >::operator = ( const SparseMatrix< T , IndexType , 0 >& M )
110 {
111 resize( M.rowNum );
112 for( size_t i=0 ; i<rowNum ; i++ )
113 {
114 setRowSize( i , M.rowSizes[i] );
115 for( size_t j=0 ; j<rowSizes[i] ; j++ ) _entries[i][j]=M._entries[i][j];
116 }
117 return *this;
118 }
119 template< class T , class IndexType >
120 template< class T2 , class IndexType2 >
121 SparseMatrix< T , IndexType , 0 >& SparseMatrix< T , IndexType , 0 >::operator = (const SparseMatrix< T2 , IndexType2 , 0 >& M)
122 {
123 resize( M.rowNum );
124 for( size_t i=0 ; i<rowNum ; i++ )
125 {
126 setRowSize( i , M.rowSizes[i] );
127 for( size_t j=0 ; j<rowSizes[i] ; j++ ) _entries[i][j] = MatrixEntry< T , IndexType >( M._entries[i][j].N , T( M._entries[i][j].Value ) );
128 }
129 return *this;
130 }
131
132 template< class T , class IndexType >
133 template< class T2 >
134 void SparseMatrix< T , IndexType , 0 >::operator() ( const T2* in , T2* out ) const { Interface::multiply( in , out ); }
135
136 template< class T , class IndexType > SparseMatrix< T , IndexType , 0 >::~SparseMatrix( void ) { resize( 0 ); }
137
138 template< class T , class IndexType >
139 void SparseMatrix< T , IndexType , 0 >::resize( size_t r )
140 {
141 if( rowNum>0 )
142 {
143 for( size_t i=0 ; i<rowNum ; i++ ) FreePointer( _entries[i] );
144 FreePointer( _entries );
145 FreePointer( rowSizes );
146 }
147 rowNum = r;
148 if( r )
149 {
150 rowSizes = AllocPointer< size_t >( r ) , memset( rowSizes , 0 , sizeof(size_t)*r );
151 _entries = AllocPointer< Pointer( MatrixEntry< T , IndexType > ) >( r );
152 for( size_t i=0 ; i<r ; i++ ) _entries[i] = NullPointer( MatrixEntry< T , IndexType > );
153 }
154 }
155
156 template< class T , class IndexType >
157 void SparseMatrix< T , IndexType , 0 >::setRowSize( size_t row , size_t count )
158 {
159 if( row>=0 && row<rowNum )
160 {
161 FreePointer( _entries[row] );
162 if( count>0 )
163 {
164 _entries[ row ] = AllocPointer< MatrixEntry< T , IndexType > >( count );
165 memset( _entries[ row ] , 0 , sizeof( MatrixEntry< T , IndexType > )*count );
166 }
167 rowSizes[row] = count;
168 }
169 else MK_THROW( "Row is out of bounds: 0 <= " , row , " < " , rowNum );
170 }
171 template< class T , class IndexType >
172 void SparseMatrix< T , IndexType , 0 >::resetRowSize( size_t row , size_t count )
173 {
174 if( row>=0 && row<rowNum )
175 {
176 size_t oldCount = rowSizes[row];
177 _entries[row] = ReAllocPointer< MatrixEntry< T, IndexType > >( _entries[row] , count );
178 if( count>oldCount ) memset( _entries[row]+oldCount , 0 , sizeof( MatrixEntry< T , IndexType > ) * ( count - oldCount ) );
179 rowSizes[row] = count;
180 }
181 else MK_THROW( "Row is out of bounds: 0 <= " , row , " < " , rowNum );
182 }
183
184 template< class T , class IndexType >
185 SparseMatrix< T , IndexType , 0 > SparseMatrix< T , IndexType , 0 >::Identity( size_t dim )
186 {
187 SparseMatrix I;
188 I.resize( dim );
189 for( size_t i=0 ; i<dim ; i++ ) I.setRowSize( i , 1 ) , I[i][0] = MatrixEntry< T , IndexType >( (IndexType)i , (T)1 );
190 return I;
191 }
192 template< class T , class IndexType >
193 SparseMatrix< T , IndexType , 0 >& SparseMatrix< T , IndexType , 0 >::operator *= ( T s )
194 {
195 ThreadPool::ParallelFor( 0 , rowNum , [&]( unsigned int , size_t i ){ for( size_t j=0 ; j<rowSizes[i] ; j++ ) _entries[i][j].Value *= s; } );
196 return *this;
197 }
198 template< class T , class IndexType >
199 SparseMatrix< T , IndexType , 0 >& SparseMatrix< T , IndexType , 0 >::operator /= ( T s ){ return (*this) * ( (T)1./s ); }
200 template< class T , class IndexType >
201 SparseMatrix< T , IndexType , 0 >& SparseMatrix< T , IndexType , 0 >::operator *= ( const SparseMatrix< T , IndexType , 0 >& B )
202 {
203 SparseMatrix foo = (*this) * B;
204 (*this) = foo;
205 return *this;
206 }
207 template< class T , class IndexType >
208 SparseMatrix< T , IndexType , 0 >& SparseMatrix< T , IndexType , 0 >::operator += ( const SparseMatrix< T , IndexType , 0 >& B )
209 {
210 SparseMatrix foo = (*this) + B;
211 (*this) = foo;
212 return *this;
213 }
214 template< class T , class IndexType >
215 SparseMatrix< T , IndexType , 0 >& SparseMatrix< T , IndexType , 0 >::operator -= ( const SparseMatrix< T , IndexType , 0 >& B )
216 {
217 SparseMatrix foo = (*this) - B;
218 (*this) = foo;
219 return *this;
220 }
221 template< class T , class IndexType >
222 SparseMatrix< T , IndexType , 0 > SparseMatrix< T , IndexType , 0 >::operator * ( T s ) const
223 {
224 SparseMatrix out = (*this);
225 return out *= s;
226 }
227 template< class T , class IndexType >
228 SparseMatrix< T , IndexType , 0 > SparseMatrix< T , IndexType , 0 >::operator / ( T s ) const { return (*this) * ( (T)1. / s ); }
229 template< class T , class IndexType >
230 SparseMatrix< T , IndexType , 0 > SparseMatrix< T , IndexType , 0 >::operator * ( const SparseMatrix< T , IndexType , 0 >& B ) const
231 {
232 SparseMatrix out;
233 const SparseMatrix& A = *this;
234 size_t aCols = 0 , aRows = A.rowNum;
235 size_t bCols = 0 , bRows = B.rowNum;
236 for( size_t i=0 ; i<A.rowNum ; i++ ) for( size_t j=0 ; j<A.rowSizes[i] ; j++ ) if( aCols<=A[i][j].N ) aCols = A[i][j].N+1;
237 for( size_t i=0 ; i<B.rowNum ; i++ ) for( size_t j=0 ; j<B.rowSizes[i] ; j++ ) if( bCols<=B[i][j].N ) bCols = B[i][j].N+1;
238 if( bRows<aCols ) MK_THROW( "Matrix sizes do not support multiplication " , aRows , " x " , aCols , " * " , bRows , " x " , bCols );
239
240 out.resize( aRows );
241 ThreadPool::ParallelFor( 0 , aRows , [&]( unsigned int , size_t i )
242 {
243 std::unordered_map< IndexType , T > row;
244 for( size_t j=0 ; j<A.rowSizes[i] ; j++ )
245 {
246 IndexType idx1 = A[i][j].N;
247 T AValue = A[i][j].Value;
248 for( size_t k=0 ; k<B.rowSizes[idx1] ; k++ )
249 {
250 IndexType idx2 = B[idx1][k].N;
251 T BValue = B[idx1][k].Value;
252 typename std::unordered_map< IndexType , T >::iterator iter = row.find(idx2);
253 if( iter==row.end() ) row[idx2] = AValue * BValue;
254 else iter->second += AValue * BValue;
255 }
256 }
257 out.setRowSize( i , row.size() );
258 out.rowSizes[i] = 0;
259 for( typename std::unordered_map< IndexType , T >::iterator iter=row.begin() ; iter!=row.end() ; iter++ ) out[i][ out.rowSizes[i]++ ] = MatrixEntry< T , IndexType >( iter->first , iter->second );
260 }
261 );
262 return out;
263 }
264 template< class T , class IndexType >
265 SparseMatrix< T , IndexType , 0 > SparseMatrix< T , IndexType , 0 >::operator + ( const SparseMatrix< T , IndexType , 0 >& B ) const
266 {
267 const SparseMatrix& A = *this;
268 size_t rowNum = std::max< size_t >( A.rowNum , B.rowNum );
269 SparseMatrix out;
270
271 out.resize( rowNum );
272 ThreadPool::ParallelFor( 0 , rowNum , [&]( unsigned int , size_t i )
273 {
274 std::unordered_map< IndexType , T > row;
275 if( i<A.rowNum )
276 for( size_t j=0 ; j<A.rowSizes[i] ; j++ )
277 {
278 IndexType idx = A[i][j].N;
279 typename std::unordered_map< IndexType , T >::iterator iter = row.find(idx);
280 if( iter==row.end() ) row[idx] = A[i][j].Value;
281 else iter->second += A[i][j].Value;
282 }
283 if( i<B.rowNum )
284 for( size_t j=0 ; j<B.rowSizes[i] ; j++ )
285 {
286 IndexType idx = B[i][j].N;
287 typename std::unordered_map< IndexType , T >::iterator iter = row.find(idx);
288 if( iter==row.end() ) row[idx] = B[i][j].Value;
289 else iter->second += B[i][j].Value;
290 }
291 out.setRowSize( i , row.size() );
292 out.rowSizes[i] = 0;
293 for( typename std::unordered_map< IndexType , T >::iterator iter=row.begin() ; iter!=row.end() ; iter++ ) out[i][ out.rowSizes[i]++ ] = MatrixEntry< T , IndexType >( iter->first , iter->second );
294 }
295 );
296 return out;
297 }
298 template< class T , class IndexType >
299 SparseMatrix< T , IndexType , 0 > SparseMatrix< T , IndexType , 0 >::operator - ( const SparseMatrix< T , IndexType , 0 >& B ) const
300 {
301 const SparseMatrix& A = *this;
302 size_t rowNum = std::max< size_t >( A.rowNum , B.rowNum );
303 SparseMatrix out;
304
305 out.resize( rowNum );
306 ThreadPool::ParallelFor( 0 , rowNum , [&]( unsigned int , size_t i )
307 {
308 std::unordered_map< IndexType , T > row;
309 if( i<A.rowNum )
310 for( size_t j=0 ; j<A.rowSizes[i] ; j++ )
311 {
312 IndexType idx = A[i][j].N;
313 typename std::unordered_map< IndexType , T >::iterator iter = row.find(idx);
314 if( iter==row.end() ) row[idx] = A[i][j].Value;
315 else iter->second += A[i][j].Value;
316 }
317 if( i<B.rowNum )
318 for( size_t j=0 ; j<B.rowSizes[i] ; j++ )
319 {
320 IndexType idx = B[i][j].N;
321 typename std::unordered_map< IndexType , T >::iterator iter = row.find(idx);
322 if( iter==row.end() ) row[idx] = -B[i][j].Value;
323 else iter->second -= B[i][j].Value;
324 }
325 out.setRowSize( i , row.size() );
326 out.rowSizes[i] = 0;
327 for( typename std::unordered_map< IndexType , T >::iterator iter=row.begin() ; iter!=row.end() ; iter++ ) out[i][ out.rowSizes[i]++ ] = MatrixEntry< T , IndexType >( iter->first , iter->second );
328 }
329 );
330 return out;
331 }
332
333 template< class T , class IndexType >
334 SparseMatrix< T , IndexType , 0 > SparseMatrix< T , IndexType , 0 >::transpose( T (*TransposeFunction)( const T& ) ) const
335 {
336 SparseMatrix A;
337 const SparseMatrix& At = *this;
338 size_t aRows = 0 , aCols = At.rowNum;
339 for( size_t i=0 ; i<At.rowNum ; i++ ) for( size_t j=0 ; j<At.rowSizes[i] ; j++ ) if( aRows<=At[i][j].N ) aRows = At[i][j].N+1;
340
341 A.resize( aRows );
342 const size_t One = 1;
343 for( size_t i=0 ; i<aRows ; i++ ) A.rowSizes[i] = 0;
344 ThreadPool::ParallelFor( 0 , At.rowNum , [&]( unsigned int , size_t i )
345 {
346 for( size_t j=0 ; j<At.rowSizes[i] ; j++ )
347 {
348 AddAtomic( A.rowSizes[ At[i][j].N ] , One );
349 }
350 }
351 );
352
353 ThreadPool::ParallelFor( 0 , A.rowNum , [&]( unsigned int , size_t i )
354 {
355 size_t t = A.rowSizes[i];
356 A.rowSizes[i] = 0;
357 A.setRowSize( i , t );
358 A.rowSizes[i] = 0;
359 }
360 );
361 if( TransposeFunction ) for( size_t i=0 ; i<At.rowNum ; i++ ) for( size_t j=0 ; j<At.rowSizes[i] ; j++ )
362 {
363 size_t ii = At[i][j].N;
364 A[ii][ A.rowSizes[ii]++ ] = MatrixEntry< T , IndexType >( (IndexType)i , TransposeFunction( At[i][j].Value ) );
365 }
366 else for( size_t i=0 ; i<At.rowNum ; i++ ) for( size_t j=0 ; j<At.rowSizes[i] ; j++ )
367 {
368 size_t ii = At[i][j].N;
369 A[ii][ A.rowSizes[ii]++ ] = MatrixEntry< T , IndexType >( (IndexType)i , At[i][j].Value );
370 }
371 return A;
372 }
373 template< class T , class IndexType >
374 SparseMatrix< T , IndexType , 0 > SparseMatrix< T , IndexType , 0 >::transpose( size_t aRows , T (*TransposeFunction)( const T& ) ) const
375 {
376 SparseMatrix A;
377 const SparseMatrix& At = *this;
378 size_t _aRows = 0 , aCols = At.rowNum;
379 for( size_t i=0 ; i<At.rowNum ; i++ ) for( size_t j=0 ; j<At.rowSizes[i] ; j++ ) if( aCols<=At[i][j].N ) _aRows = At[i][j].N+1;
380 if( _aRows>aRows ) MK_THROW( "Prescribed output dimension too low: " , aRows , " < " , _aRows );
381
382 A.resize( aRows );
383 for( size_t i=0 ; i<aRows ; i++ ) A.rowSizes[i] = 0;
384 const size_t One = 1;
385 ThreadPool::ParallelFor( 0 , At.rowNum , [&]( unsigned int , size_t i )
386 {
387 for( size_t j=0 ; j<At.rowSizes[i] ; j++ ) AddAtomic( A.rowSizes[ At[i][j].N ] , One );
388 }
389 );
390
391 ThreadPool::ParallelFor( 0 , A.rowNum , [&]( unsigned int , size_t i )
392 {
393 size_t t = A.rowSizes[i];
394 A.rowSizes[i] = 0;
395 A.setRowSize( i , t );
396 A.rowSizes[i] = 0;
397 }
398 );
399 if( TransposeFunction )
400 for( size_t i=0 ; i<At.rowNum ; i++ ) for( size_t j=0 ; j<At.rowSizes[i] ; j++ )
401 {
402 size_t ii = At[i][j].N;
403 A[ii][ A.rowSizes[ii]++ ] = MatrixEntry< T , IndexType >( (IndexType)i , TransposeFunction( At[i][j].Value ) );
404 }
405 else
406 for( size_t i=0 ; i<At.rowNum ; i++ ) for( size_t j=0 ; j<At.rowSizes[i] ; j++ )
407 {
408 size_t ii = At[i][j].N;
409 A[ii][ A.rowSizes[ii]++ ] = MatrixEntry< T , IndexType >( (IndexType)i , At[i][j].Value );
410 }
411 return A;
412 }
413
414 template< class T , class IndexType >
415 template< class A_const_iterator , class B_const_iterator >
416 SparseMatrix< T , IndexType , 0 > SparseMatrix< T , IndexType , 0 >::Multiply( const SparseMatrixInterface< T , A_const_iterator >& A , const SparseMatrixInterface< T , B_const_iterator >& B )
417 {
418 SparseMatrix M;
419 size_t aCols = 0 , aRows = A.rows();
420 size_t bCols = 0 , bRows = B.rows();
421 for( size_t i=0 ; i<A.rows() ; i++ ) for( A_const_iterator iter=A.begin(i) ; iter!=A.end(i) ; iter++ ) if( aCols<=iter->N ) aCols = iter->N+1;
422 for( size_t i=0 ; i<B.rows() ; i++ ) for( B_const_iterator iter=B.begin(i) ; iter!=B.end(i) ; iter++ ) if( bCols<=iter->N ) bCols = iter->N+1;
423 if( bRows<aCols ) MK_THROW( "Matrix sizes do not support multiplication " , aRows , " x " , aCols , " * " , bRows , " x " , bCols );
424
425 M.resize( aRows );
426
427 ThreadPool::ParallelFor( 0 , aRows , [&]( unsigned int , size_t i )
428 {
429 std::unordered_map< IndexType , T > row;
430 for( A_const_iterator iterA=A.begin(i) ; iterA!=A.end(i) ; iterA++ )
431 {
432 IndexType idx1 = iterA->N;
433 T AValue = iterA->Value;
434 for( B_const_iterator iterB=B.begin(idx1) ; iterB!=B.end(idx1) ; iterB++ )
435 {
436 IndexType idx2 = iterB->N;
437 T BValue = iterB->Value;
438 T temp = BValue * AValue; // temp = A( i , idx1 ) * B( idx1 , idx2 )
439 typename std::unordered_map< IndexType , T >::iterator iter = row.find(idx2);
440 if( iter==row.end() ) row[idx2] = temp;
441 else iter->second += temp;
442 }
443 }
444 M.setRowSize( i , row.size() );
445 M.rowSizes[i] = 0;
446 for( typename std::unordered_map< IndexType , T >::iterator iter=row.begin() ; iter!=row.end() ; iter++ )
447 M[i][ M.rowSizes[i]++ ] = MatrixEntry< T , IndexType >( iter->first , iter->second );
448 }
449 );
450 return M;
451 }
452 template< class T , class IndexType >
453 template< class const_iterator >
454 SparseMatrix< T , IndexType , 0 > SparseMatrix< T , IndexType , 0 >::Transpose( const SparseMatrixInterface< T , const_iterator >& At , T (*TransposeFunction)( const T& ) )
455 {
456 SparseMatrix< T , IndexType , 0 > A;
457 size_t aRows = 0 , aCols = At.rows();
458 for( size_t i=0 ; i<At.rows() ; i++ ) for( const_iterator iter=At.begin(i) ; iter!=At.end(i) ; iter++ ) if( aRows<=iter->N ) aRows = iter->N+1;
459
460 A.resize( aRows );
461 for( size_t i=0 ; i<aRows ; i++ ) A.rowSizes[i] = 0;
462 for( size_t i=0 ; i<At.rows() ; i++ ) for( const_iterator iter=At.begin(i) ; iter!=At.end(i) ; iter++ ) A.rowSizes[ iter->N ]++;
463 for( size_t i=0 ; i<A.rows() ; i++ )
464 {
465 size_t t = A.rowSizes[i];
466 A.rowSizes[i] = 0;
467 A.setRowSize( i , t );
468 A.rowSizes[i] = 0;
469 }
470 if( TransposeFunction )
471 for( size_t i=0 ; i<At.rows() ; i++ ) for( const_iterator iter=At.begin(i) ; iter!=At.end(i) ; iter++ )
472 {
473 size_t ii = (size_t)iter->N;
474 A[ii][ A.rowSizes[ii]++ ] = MatrixEntry< T , IndexType >( (IndexType)i , TransposeFunction( iter->Value ) );
475 }
476 else
477 for( size_t i=0 ; i<At.rows() ; i++ ) for( const_iterator iter=At.begin(i) ; iter!=At.end(i) ; iter++ )
478 {
479 size_t ii = (size_t)iter->N;
480 A[ii][ A.rowSizes[ii]++ ] = MatrixEntry< T , IndexType >( (IndexType)i , iter->Value );
481 }
482 return A;
483 }
484 template< class T , class IndexType >
485 template< class const_iterator >
486 SparseMatrix< T , IndexType , 0 > SparseMatrix< T , IndexType , 0 >::Transpose( const SparseMatrixInterface< T , const_iterator >& At , size_t outRows , T (*TransposeFunction)( const T& ) )
487 {
488 SparseMatrix< T , IndexType , 0 > A;
489 size_t _aRows = 0 , aCols = At.rows() , aRows = outRows;
490 for( size_t i=0 ; i<At.rows() ; i++ ) for( const_iterator iter=At.begin(i) ; iter!=At.end(i) ; iter++ ) if( aCols<=iter->N ) _aRows = iter->N+1;
491 if( _aRows>aRows ) MK_THROW( "Prescribed output dimension too low: " , aRows , " < " , _aRows );
492
493 A.resize( aRows );
494 for( size_t i=0 ; i<aRows ; i++ ) A.rowSizes[i] = 0;
495 for( size_t i=0 ; i<At.rows() ; i++ ) for( const_iterator iter=At.begin(i) ; iter!=At.end(i) ; iter++ ) A.rowSizes[ iter->N ]++;
496 for( size_t i=0 ; i<A.rows() ; i++ )
497 {
498 size_t t = A.rowSizes[i];
499 A.rowSizes[i] = 0;
500 A.setRowSize( i , t );
501 A.rowSizes[i] = 0;
502 }
503 if( TransposeFunction )
504 for( size_t i=0 ; i<At.rows() ; i++ ) for( const_iterator iter=At.begin(i) ; iter!=At.end(i) ; iter++ )
505 {
506 size_t ii = (size_t)iter->N;
507 A[ii][ A.rowSizes[ii]++ ] = MatrixEntry< T , IndexType >( (IndexType)i , TransposeFunction( iter->Value ) );
508 }
509 else
510 for( size_t i=0 ; i<At.rows() ; i++ ) for( const_iterator iter=At.begin(i) ; iter!=At.end(i) ; iter++ )
511 {
512 size_t ii = (size_t)iter->N;
513 A[ii][ A.rowSizes[ii]++ ] = MatrixEntry< T , IndexType >( (IndexType)i , iter->Value );
514 }
515 return true;
516 }
517
518 ///////////////////////////////////////////
519 // SparseMatrix (bounded max row size ) //
520 ///////////////////////////////////////////
521
522 template< class T , class IndexType , size_t MaxRowSize >
523 SparseMatrix< T , IndexType , MaxRowSize >::SparseMatrix( void )
524 {
525 _rowSizes = NullPointer( size_t );
526 _rowNum = 0;
527 _entries = NullPointer( MatrixEntry< T , IndexType > );
528 _maxRows = 0;
529 }
530
531 template< class T , class IndexType , size_t MaxRowSize >
532 SparseMatrix< T , IndexType , MaxRowSize >::SparseMatrix( size_t rowNum ) : SparseMatrix()
533 {
534 resize( rowNum );
535 }
536 template< class T , class IndexType , size_t MaxRowSize >
537 SparseMatrix< T , IndexType , MaxRowSize >::SparseMatrix( const SparseMatrix& M ) : SparseMatrix()
538 {
539 resize( M._rowNum );
540 for( size_t i=0 ; i<_rowNum ; i++ )
541 {
542 _rowSizes[i] = M._rowSizes[i];
543 for( size_t j=0 ; j<_rowSizes[i] ; j++ ) _entries[ i + MaxRowSize*j ] = M._rowEntries[ i + MaxRowSize*j ];
544 }
545 }
546 template< class T , class IndexType , size_t MaxRowSize >
547 SparseMatrix< T , IndexType , MaxRowSize >::SparseMatrix( SparseMatrix&& M ) : SparseMatrix()
548 {
549 Swap( *this , M );
550 }
551 template< class T , class IndexType , size_t MaxRowSize >
552 template< class T2 , class IndexType2 >
553 SparseMatrix< T , IndexType , MaxRowSize >::SparseMatrix( const SparseMatrix< T2 , IndexType2 , MaxRowSize >& M ) : SparseMatrix()
554 {
555 resize( M._rowNum );
556 for( size_t i=0 ; i<_rowNum ; i++ )
557 {
558 _rowSizes[i] = M._rowSizes[i];
559 for( size_t j=0 ; j<_rowSizes[i] ; j++ ) _entries[ i + MaxRowSize*j ] = MatrixEntry< T , IndexType >( M._rowEntries[i][j].N , T( M._entries[ i + MaxRowSize*j ].Value ) );
560 }
561 }
562
563 template< class T , class IndexType , size_t MaxRowSize >
564 SparseMatrix< T , IndexType , MaxRowSize >& SparseMatrix< T , IndexType , MaxRowSize >::operator = ( SparseMatrix< T , IndexType , MaxRowSize >&& M )
565 {
566 Swap( *this , M );
567 return *this;
568 }
569
570 template< class T , class IndexType , size_t MaxRowSize >
571 SparseMatrix< T , IndexType , MaxRowSize >& SparseMatrix< T , IndexType , MaxRowSize >::operator = ( const SparseMatrix< T , IndexType , MaxRowSize >& M )
572 {
573 resize( M._rowNum );
574 for( size_t i=0 ; i<_rowNum ; i++ )
575 {
576 _rowSizes[i] = M._rowSizes[i];
577 for( size_t j=0 ; j<_rowSizes[i] ; j++ ) _entries[ i + MaxRowSize*j ] = M._entries[ i + MaxRowSize*j ];
578 }
579 return *this;
580 }
581
582 template< class T , class IndexType , size_t MaxRowSize >
583 template< class T2 , class IndexType2 >
584 SparseMatrix< T , IndexType , MaxRowSize >& SparseMatrix< T , IndexType , MaxRowSize >::operator = ( const SparseMatrix< T2 , IndexType2 , MaxRowSize >& M )
585 {
586 resize( M._rowNum );
587 for( size_t i=0 ; i<_rowNum ; i++ )
588 {
589 _rowSizes[i] = M._rowSizes[i];
590 for( size_t j=0 ; j<_rowSizes[i] ; j++ ) _entries[ i + MaxRowSize*j ] = MatrixEntry< T , IndexType >( M._entries[ i + MaxRowSize*j ].N , T( M._entries[ i + MaxRowSize*j ].Value ) );
591 }
592 return *this;
593 }
594
595 template< class T , class IndexType , size_t MaxRowSize >
596 template< class T2 >
597 void SparseMatrix< T , IndexType , MaxRowSize >::operator() ( const T2* in , T2* out ) const { Interface::multiply( in , out ); }
598
599 template< class T , class IndexType , size_t MaxRowSize >
600 SparseMatrix< T , IndexType , MaxRowSize >::~SparseMatrix( void )
601 {
602 FreePointer( _rowSizes );
603 FreePointer( _entries );
604 }
605
606 template< class T , class IndexType , size_t MaxRowSize >
607 void SparseMatrix< T , IndexType , MaxRowSize >::resize( size_t rowNum )
608 {
609 _rowNum = rowNum;
610 if( rowNum>_maxRows )
611 {
612 FreePointer( _rowSizes );
613 FreePointer( _entries );
614
615 if( rowNum )
616 {
617 _rowSizes = AllocPointer< size_t >( rowNum );
618 _entries = AllocPointer< MatrixEntry< T , IndexType > >( rowNum * MaxRowSize );
619 _maxRows = rowNum;
620 }
621 }
622 if( rowNum ) memset( _rowSizes , 0 , sizeof(size_t)*rowNum );
623 }
624
625 template< class T , class IndexType , size_t MaxRowSize >
626 void SparseMatrix< T , IndexType , MaxRowSize >::setRowSize( size_t row , size_t rowSize )
627 {
628 if( row>=_rowNum ) MK_THROW( "Row is out of bounds: 0 <= " , row , " < " , _rowNum );
629 else if( rowSize>MaxRowSize ) MK_THROW( "Row size larger than max row size: " , rowSize , " < " , MaxRowSize );
630 else _rowSizes[row] = rowSize;
631 }
632 template< class T , class IndexType , size_t MaxRowSize >
633 void SparseMatrix< T , IndexType , MaxRowSize >::resetRowSize( size_t row , size_t rowSize )
634 {
635 if( row>=_rowNum ) MK_THROW( "Row is out of bounds: 0 <= " , row , " < " , _rowNum );
636 else if( rowSize>MaxRowSize ) MK_THROW( "Row size larger than max row size: " , rowSize , " < " , MaxRowSize );
637 else _rowSizes[row] = rowSize;
638 }
639
640 template< class T , class IndexType , size_t MaxRowSize >
641 SparseMatrix< T , IndexType , MaxRowSize >& SparseMatrix< T , IndexType , MaxRowSize >::operator *= ( T s )
642 {
643 ThreadPool::ParallelFor( 0 , _rowNum*MaxRowSize , [&]( unsigned int , size_t i ){ _entries[i].Value *= s; } );
644 return *this;
645 }
646
647 template< class T , class IndexType , size_t MaxRowSize >
648 SparseMatrix< T , IndexType , MaxRowSize >& SparseMatrix< T , IndexType , MaxRowSize >::operator /= ( T s ){ return (*this) * ( (T)1./s ); }
649
650 template< class T , class IndexType , size_t MaxRowSize >
651 SparseMatrix< T , IndexType , MaxRowSize > SparseMatrix< T , IndexType , MaxRowSize >::operator * ( T s ) const
652 {
653 SparseMatrix out = (*this);
654 return out *= s;
655 }
656
657 template< class T , class IndexType , size_t MaxRowSize >
658 SparseMatrix< T , IndexType , MaxRowSize > SparseMatrix< T , IndexType , MaxRowSize >::operator / ( T s ) const { return (*this) * ( (T)1. / s ); }