-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathSpMat_meat.hpp
More file actions
6921 lines (4918 loc) · 145 KB
/
SpMat_meat.hpp
File metadata and controls
6921 lines (4918 loc) · 145 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
//
// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
// Copyright 2008-2016 National ICT Australia (NICTA)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
//! \addtogroup SpMat
//! @{
/**
* Initialize a sparse matrix with size 0x0 (empty).
*/
template<typename eT>
inline
SpMat<eT>::SpMat()
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint_this(this);
init_cold(0,0);
}
/**
* Clean up the memory of a sparse matrix and destruct it.
*/
template<typename eT>
inline
SpMat<eT>::~SpMat()
{
arma_debug_sigprint_this(this);
if(values ) { memory::release(access::rw(values)); }
if(row_indices) { memory::release(access::rw(row_indices)); }
if(col_ptrs ) { memory::release(access::rw(col_ptrs)); }
}
/**
* Constructor with size given.
*/
template<typename eT>
inline
SpMat<eT>::SpMat(const uword in_rows, const uword in_cols)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint_this(this);
init_cold(in_rows, in_cols);
}
template<typename eT>
inline
SpMat<eT>::SpMat(const SizeMat& s)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint_this(this);
init_cold(s.n_rows, s.n_cols);
}
template<typename eT>
inline
SpMat<eT>::SpMat(const arma_reserve_indicator&, const uword in_rows, const uword in_cols, const uword new_n_nonzero)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint_this(this);
init_cold(in_rows, in_cols, new_n_nonzero);
}
template<typename eT>
template<typename eT2>
inline
SpMat<eT>::SpMat(const arma_layout_indicator&, const SpMat<eT2>& x)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint_this(this);
init_cold(x.n_rows, x.n_cols, x.n_nonzero);
if(x.n_nonzero == 0) { return; }
if(x.row_indices) { arrayops::copy(access::rwp(row_indices), x.row_indices, x.n_nonzero + 1); }
if(x.col_ptrs ) { arrayops::copy(access::rwp(col_ptrs), x.col_ptrs, x.n_cols + 1); }
// NOTE: 'values' array is not initialised
}
/**
* Assemble from text.
*/
template<typename eT>
inline
SpMat<eT>::SpMat(const char* text)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint_this(this);
init(std::string(text));
}
template<typename eT>
inline
SpMat<eT>&
SpMat<eT>::operator=(const char* text)
{
arma_debug_sigprint();
init(std::string(text));
return *this;
}
template<typename eT>
inline
SpMat<eT>::SpMat(const std::string& text)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint();
init(text);
}
template<typename eT>
inline
SpMat<eT>&
SpMat<eT>::operator=(const std::string& text)
{
arma_debug_sigprint();
init(text);
return *this;
}
template<typename eT>
inline
SpMat<eT>::SpMat(const SpMat<eT>& x)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint_this(this);
init(x);
}
template<typename eT>
inline
SpMat<eT>::SpMat(SpMat<eT>&& in_mat)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint_this(this);
arma_debug_sigprint(arma_str::format("this: %x; in_mat: %x") % this % &in_mat);
(*this).steal_mem(in_mat);
}
template<typename eT>
inline
SpMat<eT>&
SpMat<eT>::operator=(SpMat<eT>&& in_mat)
{
arma_debug_sigprint(arma_str::format("this: %x; in_mat: %x") % this % &in_mat);
(*this).steal_mem(in_mat);
return *this;
}
template<typename eT>
inline
SpMat<eT>::SpMat(const MapMat<eT>& x)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint_this(this);
init(x);
}
template<typename eT>
inline
SpMat<eT>&
SpMat<eT>::operator=(const MapMat<eT>& x)
{
arma_debug_sigprint();
init(x);
return *this;
}
//! Insert a large number of values at once.
//! locations.row[0] should be row indices, locations.row[1] should be column indices,
//! and values should be the corresponding values.
//! If sort_locations is false, then it is assumed that the locations and values
//! are already sorted in column-major ordering.
template<typename eT>
template<typename T1, typename T2>
inline
SpMat<eT>::SpMat(const Base<uword,T1>& locations_expr, const Base<eT,T2>& vals_expr, const bool sort_locations)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint_this(this);
const quasi_unwrap<T1> locs_tmp( locations_expr.get_ref() );
const quasi_unwrap<T2> vals_tmp( vals_expr.get_ref() );
const Mat<uword>& locs = locs_tmp.M;
const Mat<eT>& vals = vals_tmp.M;
arma_conform_check( (vals.is_vec() == false), "SpMat::SpMat(): given 'values' object must be a vector" );
arma_conform_check( (locs.n_rows != 2), "SpMat::SpMat(): locations matrix must have two rows" );
arma_conform_check( (locs.n_cols != vals.n_elem), "SpMat::SpMat(): number of locations is different than number of values" );
// If there are no elements in the list, max() will fail.
if(locs.n_cols == 0) { init_cold(0, 0); return; }
// Automatically determine size before pruning zeros.
uvec bounds = arma::max(locs, 1);
init_cold(bounds[0] + 1, bounds[1] + 1);
// Ensure that there are no zeros
const uword N_old = vals.n_elem;
uword N_new = 0;
for(uword i=0; i < N_old; ++i) { N_new += (vals[i] != eT(0)) ? uword(1) : uword(0); }
if(N_new != N_old)
{
Col<eT> filtered_vals( N_new, arma_nozeros_indicator());
Mat<uword> filtered_locs(2, N_new, arma_nozeros_indicator());
uword index = 0;
for(uword i = 0; i < N_old; ++i)
{
if(vals[i] != eT(0))
{
filtered_vals[index] = vals[i];
filtered_locs.at(0, index) = locs.at(0, i);
filtered_locs.at(1, index) = locs.at(1, i);
++index;
}
}
init_batch_std(filtered_locs, filtered_vals, sort_locations);
}
else
{
init_batch_std(locs, vals, sort_locations);
}
}
//! Insert a large number of values at once.
//! locations.row[0] should be row indices, locations.row[1] should be column indices,
//! and values should be the corresponding values.
//! If sort_locations is false, then it is assumed that the locations and values
//! are already sorted in column-major ordering.
//! In this constructor the size is explicitly given.
template<typename eT>
template<typename T1, typename T2>
inline
SpMat<eT>::SpMat(const Base<uword,T1>& locations_expr, const Base<eT,T2>& vals_expr, const uword in_n_rows, const uword in_n_cols, const bool sort_locations, const bool check_for_zeros)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint_this(this);
const quasi_unwrap<T1> locs_tmp( locations_expr.get_ref() );
const quasi_unwrap<T2> vals_tmp( vals_expr.get_ref() );
const Mat<uword>& locs = locs_tmp.M;
const Mat<eT>& vals = vals_tmp.M;
arma_conform_check( (vals.is_vec() == false), "SpMat::SpMat(): given 'values' object must be a vector" );
arma_conform_check( (locs.n_rows != 2), "SpMat::SpMat(): locations matrix must have two rows" );
arma_conform_check( (locs.n_cols != vals.n_elem), "SpMat::SpMat(): number of locations is different than number of values" );
init_cold(in_n_rows, in_n_cols);
// Ensure that there are no zeros, unless the user asked not to.
if(check_for_zeros)
{
const uword N_old = vals.n_elem;
uword N_new = 0;
for(uword i=0; i < N_old; ++i) { N_new += (vals[i] != eT(0)) ? uword(1) : uword(0); }
if(N_new != N_old)
{
Col<eT> filtered_vals( N_new, arma_nozeros_indicator());
Mat<uword> filtered_locs(2, N_new, arma_nozeros_indicator());
uword index = 0;
for(uword i = 0; i < N_old; ++i)
{
if(vals[i] != eT(0))
{
filtered_vals[index] = vals[i];
filtered_locs.at(0, index) = locs.at(0, i);
filtered_locs.at(1, index) = locs.at(1, i);
++index;
}
}
init_batch_std(filtered_locs, filtered_vals, sort_locations);
}
else
{
init_batch_std(locs, vals, sort_locations);
}
}
else
{
init_batch_std(locs, vals, sort_locations);
}
}
template<typename eT>
template<typename T1, typename T2>
inline
SpMat<eT>::SpMat(const bool add_values, const Base<uword,T1>& locations_expr, const Base<eT,T2>& vals_expr, const uword in_n_rows, const uword in_n_cols, const bool sort_locations, const bool check_for_zeros)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint_this(this);
const quasi_unwrap<T1> locs_tmp( locations_expr.get_ref() );
const quasi_unwrap<T2> vals_tmp( vals_expr.get_ref() );
const Mat<uword>& locs = locs_tmp.M;
const Mat<eT>& vals = vals_tmp.M;
arma_conform_check( (vals.is_vec() == false), "SpMat::SpMat(): given 'values' object must be a vector" );
arma_conform_check( (locs.n_rows != 2), "SpMat::SpMat(): locations matrix must have two rows" );
arma_conform_check( (locs.n_cols != vals.n_elem), "SpMat::SpMat(): number of locations is different than number of values" );
init_cold(in_n_rows, in_n_cols);
// Ensure that there are no zeros, unless the user asked not to.
if(check_for_zeros)
{
const uword N_old = vals.n_elem;
uword N_new = 0;
for(uword i=0; i < N_old; ++i) { N_new += (vals[i] != eT(0)) ? uword(1) : uword(0); }
if(N_new != N_old)
{
Col<eT> filtered_vals( N_new, arma_nozeros_indicator());
Mat<uword> filtered_locs(2, N_new, arma_nozeros_indicator());
uword index = 0;
for(uword i = 0; i < N_old; ++i)
{
if(vals[i] != eT(0))
{
filtered_vals[index] = vals[i];
filtered_locs.at(0, index) = locs.at(0, i);
filtered_locs.at(1, index) = locs.at(1, i);
++index;
}
}
add_values ? init_batch_add(filtered_locs, filtered_vals, sort_locations) : init_batch_std(filtered_locs, filtered_vals, sort_locations);
}
else
{
add_values ? init_batch_add(locs, vals, sort_locations) : init_batch_std(locs, vals, sort_locations);
}
}
else
{
add_values ? init_batch_add(locs, vals, sort_locations) : init_batch_std(locs, vals, sort_locations);
}
}
//! Insert a large number of values at once.
//! Per CSC format, rowind_expr should be row indices,
//! colptr_expr should column ptr indices locations,
//! and values should be the corresponding values.
//! In this constructor the size is explicitly given.
//! Values are assumed to be sorted, and the size
//! information is trusted
template<typename eT>
template<typename T1, typename T2, typename T3>
inline
SpMat<eT>::SpMat
(
const Base<uword,T1>& rowind_expr,
const Base<uword,T2>& colptr_expr,
const Base<eT, T3>& values_expr,
const uword in_n_rows,
const uword in_n_cols,
const bool check_for_zeros
)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint_this(this);
const quasi_unwrap<T1> rowind_tmp( rowind_expr.get_ref() );
const quasi_unwrap<T2> colptr_tmp( colptr_expr.get_ref() );
const quasi_unwrap<T3> vals_tmp( values_expr.get_ref() );
const Mat<uword>& rowind = rowind_tmp.M;
const Mat<uword>& colptr = colptr_tmp.M;
const Mat<eT>& vals = vals_tmp.M;
arma_conform_check( (rowind.is_vec() == false), "SpMat::SpMat(): given 'rowind' object must be a vector" );
arma_conform_check( (colptr.is_vec() == false), "SpMat::SpMat(): given 'colptr' object must be a vector" );
arma_conform_check( (vals.is_vec() == false), "SpMat::SpMat(): given 'values' object must be a vector" );
// Resize to correct number of elements (this also sets n_nonzero)
init_cold(in_n_rows, in_n_cols, vals.n_elem);
arma_conform_check( (rowind.n_elem != vals.n_elem), "SpMat::SpMat(): number of row indices is not equal to number of values" );
arma_conform_check( (colptr.n_elem != (n_cols+1) ), "SpMat::SpMat(): number of column pointers is not equal to n_cols+1" );
// copy supplied values into sparse matrix -- not checked for consistency
arrayops::copy(access::rwp(row_indices), rowind.memptr(), rowind.n_elem );
arrayops::copy(access::rwp(col_ptrs), colptr.memptr(), colptr.n_elem );
arrayops::copy(access::rwp(values), vals.memptr(), vals.n_elem );
// important: set the sentinel as well
access::rw(col_ptrs[n_cols + 1]) = std::numeric_limits<uword>::max();
// make sure no zeros are stored
if(check_for_zeros) { remove_zeros(); }
}
template<typename eT>
inline
SpMat<eT>&
SpMat<eT>::operator=(const eT val)
{
arma_debug_sigprint();
if(val != eT(0))
{
// Resize to 1x1 then set that to the right value.
init(1, 1, 1); // Sets col_ptrs to 0.
// Manually set element.
access::rw(values[0]) = val;
access::rw(row_indices[0]) = 0;
access::rw(col_ptrs[1]) = 1;
}
else
{
init(0, 0);
}
return *this;
}
template<typename eT>
inline
SpMat<eT>&
SpMat<eT>::operator*=(const eT val)
{
arma_debug_sigprint();
sync_csc();
invalidate_cache();
const uword n_nz = n_nonzero;
eT* vals = access::rwp(values);
bool has_zero = false;
for(uword i=0; i<n_nz; ++i)
{
eT& vals_i = vals[i];
vals_i *= val;
if(vals_i == eT(0)) { has_zero = true; }
}
if(has_zero) { remove_zeros(); }
return *this;
}
template<typename eT>
inline
SpMat<eT>&
SpMat<eT>::operator/=(const eT val)
{
arma_debug_sigprint();
arma_conform_check( (val == eT(0)), "element-wise division: division by zero" );
sync_csc();
invalidate_cache();
const uword n_nz = n_nonzero;
eT* vals = access::rwp(values);
bool has_zero = false;
for(uword i=0; i<n_nz; ++i)
{
eT& vals_i = vals[i];
vals_i /= val;
if(vals_i == eT(0)) { has_zero = true; }
}
if(has_zero) { remove_zeros(); }
return *this;
}
template<typename eT>
inline
SpMat<eT>&
SpMat<eT>::operator=(const SpMat<eT>& x)
{
arma_debug_sigprint();
init(x);
return *this;
}
template<typename eT>
inline
SpMat<eT>&
SpMat<eT>::operator+=(const SpMat<eT>& x)
{
arma_debug_sigprint();
sync_csc();
SpMat<eT> out = (*this) + x;
steal_mem(out);
return *this;
}
template<typename eT>
inline
SpMat<eT>&
SpMat<eT>::operator-=(const SpMat<eT>& x)
{
arma_debug_sigprint();
sync_csc();
SpMat<eT> out = (*this) - x;
steal_mem(out);
return *this;
}
template<typename eT>
inline
SpMat<eT>&
SpMat<eT>::operator*=(const SpMat<eT>& y)
{
arma_debug_sigprint();
sync_csc();
SpMat<eT> z = (*this) * y;
steal_mem(z);
return *this;
}
// This is in-place element-wise matrix multiplication.
template<typename eT>
inline
SpMat<eT>&
SpMat<eT>::operator%=(const SpMat<eT>& y)
{
arma_debug_sigprint();
sync_csc();
SpMat<eT> z = (*this) % y;
steal_mem(z);
return *this;
}
template<typename eT>
inline
SpMat<eT>&
SpMat<eT>::operator/=(const SpMat<eT>& x)
{
arma_debug_sigprint();
// NOTE: use of this function is not advised; it is implemented only for completeness
arma_conform_assert_same_size(n_rows, n_cols, x.n_rows, x.n_cols, "element-wise division");
for(uword c = 0; c < n_cols; ++c)
for(uword r = 0; r < n_rows; ++r)
{
at(r, c) /= x.at(r, c);
}
return *this;
}
// Construct a complex matrix out of two non-complex matrices
template<typename eT>
template<typename T1, typename T2>
inline
SpMat<eT>::SpMat
(
const SpBase<typename SpMat<eT>::pod_type, T1>& A,
const SpBase<typename SpMat<eT>::pod_type, T2>& B
)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint();
typedef typename T1::elem_type T;
// Make sure eT is complex and T is not (compile-time check).
arma_type_check(( is_cx<eT>::no ));
arma_type_check(( is_cx< T>::yes ));
// Compile-time abort if types are not compatible.
arma_type_check(( is_same_type< std::complex<T>, eT >::no ));
const unwrap_spmat<T1> tmp1(A.get_ref());
const unwrap_spmat<T2> tmp2(B.get_ref());
const SpMat<T>& X = tmp1.M;
const SpMat<T>& Y = tmp2.M;
arma_conform_assert_same_size(X.n_rows, X.n_cols, Y.n_rows, Y.n_cols, "SpMat()");
const uword l_n_rows = X.n_rows;
const uword l_n_cols = X.n_cols;
// Set size of matrix correctly.
init_cold(l_n_rows, l_n_cols, n_unique(X, Y, op_n_unique_count()));
// Now on a second iteration, fill it.
typename SpMat<T>::const_iterator x_it = X.begin();
typename SpMat<T>::const_iterator x_end = X.end();
typename SpMat<T>::const_iterator y_it = Y.begin();
typename SpMat<T>::const_iterator y_end = Y.end();
uword cur_pos = 0;
while((x_it != x_end) || (y_it != y_end))
{
if(x_it == y_it) // if we are at the same place
{
access::rw(values[cur_pos]) = std::complex<T>((T) *x_it, (T) *y_it);
access::rw(row_indices[cur_pos]) = x_it.row();
++access::rw(col_ptrs[x_it.col() + 1]);
++x_it;
++y_it;
}
else
{
if((x_it.col() < y_it.col()) || ((x_it.col() == y_it.col()) && (x_it.row() < y_it.row()))) // if y is closer to the end
{
access::rw(values[cur_pos]) = std::complex<T>((T) *x_it, T(0));
access::rw(row_indices[cur_pos]) = x_it.row();
++access::rw(col_ptrs[x_it.col() + 1]);
++x_it;
}
else // x is closer to the end
{
access::rw(values[cur_pos]) = std::complex<T>(T(0), (T) *y_it);
access::rw(row_indices[cur_pos]) = y_it.row();
++access::rw(col_ptrs[y_it.col() + 1]);
++y_it;
}
}
++cur_pos;
}
// Now fix the column pointers; they are supposed to be a sum.
for(uword c = 1; c <= n_cols; ++c)
{
access::rw(col_ptrs[c]) += col_ptrs[c - 1];
}
}
template<typename eT>
template<typename T1>
inline
SpMat<eT>::SpMat(const Base<eT, T1>& x)
: n_rows(0)
, n_cols(0)
, n_elem(0)
, n_nonzero(0)
, vec_state(0)
, values(nullptr)
, row_indices(nullptr)
, col_ptrs(nullptr)
{
arma_debug_sigprint_this(this);
(*this).operator=(x);
}
template<typename eT>
template<typename T1>
inline
SpMat<eT>&
SpMat<eT>::operator=(const Base<eT, T1>& expr)
{
arma_debug_sigprint();
if(is_same_type< T1, Gen<Mat<eT>, gen_zeros> >::yes)
{
const Proxy<T1> P(expr.get_ref());
(*this).zeros( P.get_n_rows(), P.get_n_cols() );
return *this;
}
if(is_same_type< T1, Gen<Mat<eT>, gen_eye> >::yes)
{
const Proxy<T1> P(expr.get_ref());
(*this).eye( P.get_n_rows(), P.get_n_cols() );
return *this;
}
const quasi_unwrap<T1> tmp(expr.get_ref());
const Mat<eT>& x = tmp.M;
const uword x_n_rows = x.n_rows;
const uword x_n_cols = x.n_cols;
const uword x_n_elem = x.n_elem;
// Count number of nonzero elements in base object.
uword n = 0;
const eT* x_mem = x.memptr();
for(uword i=0; i < x_n_elem; ++i) { n += (x_mem[i] != eT(0)) ? uword(1) : uword(0); }
init(x_n_rows, x_n_cols, n);
if(n == 0) { return *this; }
// Now the memory is resized correctly; set nonzero elements.
n = 0;
for(uword j = 0; j < x_n_cols; ++j)
for(uword i = 0; i < x_n_rows; ++i)
{
const eT val = (*x_mem); x_mem++;
if(val != eT(0))
{
access::rw(values[n]) = val;
access::rw(row_indices[n]) = i;
access::rw(col_ptrs[j + 1])++;
++n;
}
}
// Sum column counts to be column pointers.
for(uword c = 1; c <= n_cols; ++c)
{
access::rw(col_ptrs[c]) += col_ptrs[c - 1];
}
return *this;
}
template<typename eT>
template<typename T1>
inline
SpMat<eT>&
SpMat<eT>::operator+=(const Base<eT, T1>& x)
{
arma_debug_sigprint();
sync_csc();
return (*this).operator=( (*this) + x.get_ref() );
}
template<typename eT>
template<typename T1>
inline
SpMat<eT>&
SpMat<eT>::operator-=(const Base<eT, T1>& x)
{
arma_debug_sigprint();
sync_csc();
return (*this).operator=( (*this) - x.get_ref() );
}
template<typename eT>