forked from 3dem/relion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix1d.h
1317 lines (1186 loc) · 31 KB
/
matrix1d.h
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
/***************************************************************************
*
* Author: "Sjors H.W. Scheres"
* MRC Laboratory of Molecular Biology
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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.
*
* This complete copyright notice must be included in any revised version of the
* source code. Additional authorship citations may be added, but existing
* author citations must be preserved.
***************************************************************************/
/***************************************************************************
*
* Authors: Carlos Oscar S. Sorzano ([email protected])
*
* Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*
* All comments concerning this program package may be sent to the
* e-mail address '[email protected]'
***************************************************************************/
#ifndef MATRIX1D_H_
#define MATRIX1D_H_
#include "src/funcs.h"
#include "src/filename.h"
extern int bestPrecision(float F, int _width);
extern std::string floatToString(float F, int _width, int _prec);
template <typename T> class Matrix2D;
/** @defgroup Vectors Matrix1D Vectors
* @ingroup DataLibrary
*/
//@{
/** @name Vectors speed up macros
*
* This macros are defined to allow high speed in critical parts of your
* program. They shouldn't be used systematically as usually there is no
* checking on the correctness of the operation you are performing. Speed comes
* from three facts: first, they are macros and no function call is performed
* (although most of the critical functions are inline functions), there is no
* checking on the correctness of the operation (it could be wrong and you are
* not warned of it), and destination vectors are not returned saving time in
* the copy constructor and in the creation/destruction of temporary vectors.
*/
//@{
/** Array access.
* This macro gives you access to the array (T)
*/
#define MATRIX1D_ARRAY(v) ((v).vdata)
/** For all elements in the array
* This macro is used to generate loops for the vector in an easy manner. It
* defines an internal index 'i' which ranges the vector using its mathematical
* definition (ie, logical access).
*
* @code
* FOR_ALL_ELEMENTS_IN_MATRIX1D(v)
* {
* std::cout << v(i) << " ";
* }
* @endcode
*/
#define FOR_ALL_ELEMENTS_IN_MATRIX1D(v) \
for (int i=0; i<v.vdim; i++)
/** X dimension of the matrix
*/
#define VEC_XSIZE(m) ((m).vdim)
/** Access to X component
* @code
* XX(v) = 1;
* val = XX(v);
* @endcode
*/
#define XX(v) (v).vdata[0]
/** Access to Y component
* @code
* YY(v) = 1;
* val = YY(v);
* @endcode
*/
#define YY(v) (v).vdata[1]
/** Access to Z component
* @code
* ZZ(v) = 1;
* val = ZZ(v);
* @endcode
*/
#define ZZ(v) (v).vdata[2]
/** Creates vector in R2
* The vector must be created beforehand to the correct size. After this macro
* the vector is (x, y) in R2.
*
* @code
* MultidimArray< RFLOAT > v(2);
* VECTOR_R2(v, 1, 2);
* @endcode
*/
#define VECTOR_R2(v, x, y) { \
XX(v) = x; YY(v) = y; }
/** Creates vector in R3
* The vector must be created beforehand to the correct size. After this macro
* the vector is (x, y, z) in R3.
*
* @code
* MultidimArray< RFLOAT > v(3);
* VECTOR_R2(v, 1, 2, 1);
* @endcode
*/
#define VECTOR_R3(v, x, y, z) { \
XX(v) = x; YY(v) = y; ZZ(v) = z;}
/** Adding two R2 vectors (a=b+c)
* @code
* MultidimArray< RFLOAT > a(2), b(2), c(2);
* ...;
* V2_PLUS_V2(a, b, c);
* @endcode
*/
#define V2_PLUS_V2(a, b, c) { \
XX(a) = XX(b) + XX(c); \
YY(a) = YY(b) + YY(c); }
/** Substracting two R2 vectors (a=b-c)
* @code
* MultidimArray< RFLOAT > a(2), b(2), c(2);
* ...;
* V2_MINUS_V2(a, b, c);
* @endcode
*/
#define V2_MINUS_V2(a, b, c) { \
XX(a) = XX(b) - XX(c); \
YY(a) = YY(b) - YY(c); }
/** Adding/substracting a constant to a R2 vector (a=b-k).
* @code
* MultidimArray< RFLOAT > a(2), b(2);
* RFLOAT k;
* ...;
* V2_PLUS_CT(a, b, k);
*
* MultidimArray< RFLOAT > a(2), b(2);
* RFLOAT k;
* ...;
* V2_PLUS_CT(a, b, -k);
* @endcode
*/
#define V2_PLUS_CT(a, b, k) { \
XX(a) = XX(b) + (k); \
YY(a) = YY(b) + (k); }
/** Multiplying/dividing by a constant a R2 vector (a=b*k)
* @code
* MultidimArray< RFLOAT > a(2), b(2);
* RFLOAT k;
* ...;
* V2_BY_CT(a, b, k);
*
* MultidimArray< RFLOAT > a(2), b(2);
* RFLOAT k;
* ...;
* V2_BY_CT(a, b, 1/k);
* @endcode
*/
#define V2_BY_CT(a, b, k) { \
XX(a) = XX(b) * (k); \
YY(a) = YY(b) * (k); }
/** Adding two R3 vectors (a=b+c)
* @code
* MultidimArray< RFLOAT > a(3), b(3), c(3);
* ...;
* V3_PLUS_V3(a, b, c);
* @endcode
*/
#define V3_PLUS_V3(a, b, c) { \
XX(a) = XX(b) + XX(c); \
YY(a) = YY(b) + YY(c); \
ZZ(a) = ZZ(b) + ZZ(c); }
/** Substracting two R3 vectors (a=b-c)
* @code
* MultidimArray< RFLOAT > a(3), b(3), c(3);
* ...;
* V3_MINUS_V3(a, b, c);
* @endcode
*/
#define V3_MINUS_V3(a, b, c) { \
XX(a) = XX(b) - XX(c); \
YY(a) = YY(b) - YY(c); \
ZZ(a) = ZZ(b) - ZZ(c); }
/** Adding/substracting a constant to a R3 vector (a=b-k)
* @code
* MultidimArray< RFLOAT > a(3), b(3);
* RFLOAT k;
* ...;
* V3_PLUS_CT(a, b, k);
*
* MultidimArray< RFLOAT > a(3), b(3);
* RFLOAT k;
* ...;
* V3_PLUS_CT(a, b, -k);
* @endcode
*/
#define V3_PLUS_CT(a, b, c) { \
XX(a) = XX(b) + (c); \
YY(a) = YY(b) + (c); \
ZZ(a) = ZZ(b) + (c); }
/** Multiplying/dividing by a constant a R3 vector (a=b*k)
* @code
* MultidimArray< RFLOAT > a(3), b(3);
* RFLOAT k;
* ...;
* V3_BY_CT(a, b, k);
*
* MultidimArray< RFLOAT > a(3), b(3);
* RFLOAT k;
* ...;
* V3_BY_CT(a, b, 1/k);
* @endcode
*/
#define V3_BY_CT(a, b, c) { \
XX(a) = XX(b) * (c); \
YY(a) = YY(b) * (c); \
ZZ(a) = ZZ(b) * (c); }
/** Direct access to vector element
*/
#define VEC_ELEM(v,i) ((v).vdata[(i)])
//@}
/** Matrix1D class.*/
template<typename T>
class Matrix1D
{
public:
/// The array itself
T* vdata;
/// Destroy data
bool destroyData;
/// Number of elements
int vdim;
/// <0=column vector (default), 1=row vector
bool row;
/// @name Constructors
//@{
/** Empty constructor
*
* The empty constructor creates a vector with no memory associated,
* origin=0, size=0, no statistics, ... You can choose between a column
* vector (by default), or a row one.
*
* @code
* Matrix1D< RFLOAT > v1;
* Matrix1D< RFLOAT > v1(true);
* // both are examples of empty column vectors
*
* Matrix1D< int > v1(false);
* // empty row vector
* @endcode
*/
Matrix1D(bool column = true)
{
coreInit();
row = ! column;
}
/** Dimension constructor
*
* The dimension constructor creates a vector with memory associated (but
* not assigned to anything, could be full of garbage) origin=0, size=the
* given one. You can choose between a column vector (by default), or a row
* one.
*
* @code
* Matrix1D< RFLOAT > v1(6);
* Matrix1D< RFLOAT > v1(6, 'y');
* // both are examples of column vectors of dimensions 6
*
* Matrix1D< int > v1('n');
* // empty row vector
* @endcode
*/
Matrix1D(int dim, bool column = true)
{
coreInit();
row = ! column;
resize(dim);
}
/** Copy constructor
*
* The created vector is a perfect copy of the input vector but with a
* different memory assignment.
*
* @code
* Matrix1D< RFLOAT > v2(v1);
* @endcode
*/
Matrix1D(const Matrix1D<T>& v)
{
coreInit();
*this = v;
}
/** Destructor.
*/
~Matrix1D()
{
coreDeallocate();
}
/** Assignment.
*
* You can build as complex assignment expressions as you like. Multiple
* assignment is allowed.
*
* @code
* v1 = v2 + v3;
* v1 = v2 = v3;
* @endcode
*/
Matrix1D<T>& operator=(const Matrix1D<T>& op1)
{
if (&op1 != this)
{
resize(op1);
for (int i = 0; i < vdim; i++)
vdata[i] = op1.vdata[i];
row=op1.row;
}
return *this;
}
//@}
/// @name Core memory operations for Matrix1D
//@{
/** Clear.
*/
void clear()
{
coreDeallocate();
coreInit();
}
/** Core init.
* Initialize everything to 0
*/
void coreInit()
{
vdim=0;
row=false;
vdata=NULL;
destroyData=true;
}
/** Core allocate.
*/
inline void coreAllocate(int _vdim)
{
if (_vdim<=0)
{
clear();
return;
}
vdim=_vdim;
vdata = new T [vdim];
if (vdata == NULL)
REPORT_ERROR("Allocate: No space left");
}
/** Core deallocate.
* Free all vdata.
*/
inline void coreDeallocate()
{
if (vdata != NULL && destroyData)
delete[] vdata;
vdata=NULL;
}
//@}
///@name Size and shape of Matrix1D
//@{
/** Resize to a given size
*
* This function resize the actual array to the given size. The origin is
* not modified. If the actual array is larger than the pattern then the
* values outside the new size are lost, if it is smaller then 0's are
* added. An exception is thrown if there is no memory.
*
* @code
* V1.resize(3, 3, 2);
* @endcode
*/
inline void resize(int Xdim)
{
if (Xdim == vdim)
return;
if (Xdim <= 0)
{
clear();
return;
}
T * new_vdata;
try
{
new_vdata = new T [Xdim];
}
catch (std::bad_alloc &)
{
REPORT_ERROR("Allocate: No space left");
}
// Copy needed elements, fill with 0 if necessary
for (int j = 0; j < Xdim; j++)
{
T val;
if (j >= vdim)
val = 0;
else
val = vdata[j];
new_vdata[j] = val;
}
// deallocate old vector
coreDeallocate();
// assign *this vector to the newly created
vdata = new_vdata;
vdim = Xdim;
}
/** Resize according to a pattern.
*
* This function resize the actual array to the same size
* as the input pattern. If the actual array is larger than the pattern
* then the trailing values are lost, if it is smaller then 0's are
* added at the end
*
* @code
* v2.resize(v1);
* // v2 has got now the same structure as v1
* @endcode
*/
template<typename T1>
void resize(const Matrix1D<T1> &v)
{
if (vdim != v.vdim)
resize(v.vdim);
}
/** Same shape.
*
* Returns true if this object has got the same shape (origin and size)
* than the argument
*/
template <typename T1>
bool sameShape(const Matrix1D<T1>& op) const
{
return (vdim == op.vdim);
}
/** Returns the size of this vector
*
* @code
* int nn = a.size();
* @endcode
*/
inline int size() const
{
return vdim;
}
/** True if vector is a row.
*
* @code
* if (v.isRow())
* std::cout << "v is a row vector\n";
* @endcode
*/
int isRow() const
{
return row;
}
/** True if vector is a column
*
* @code
* if (v.isCol())
* std::cout << "v is a column vector\n";
* @endcode
*/
int isCol() const
{
return !row;
}
/** Forces the vector to be a row vector
*
* @code
* v.setRow();
* @endcode
*/
void setRow()
{
row = true;
}
/** Forces the vector to be a column vector
*
* @code
* v.setCol();
* @endcode
*/
void setCol()
{
row = false;
}
//@}
/// @name Initialization of Matrix1D values
//@{
/** Same value in all components.
*
* The constant must be of a type compatible with the array type, ie,
* you cannot assign a RFLOAT to an integer array without a casting.
* It is not an error if the array is empty, then nothing is done.
*
* @code
* v.initConstant(3.14);
* @endcode
*/
void initConstant(T val)
{
for (int j = 0; j < vdim; j++)
{
vdata[j] = val;
}
}
/** Initialize to zeros with current size.
*
* All values are set to 0. The current size and origin are kept. It is not
* an error if the array is empty, then nothing is done.
*
* @code
* v.initZeros();
* @endcode
*/
void initZeros()
{
memset(vdata,0,vdim*sizeof(T));
}
/** Initialize to zeros with a given size.
*/
void initZeros(int Xdim)
{
if (vdim!=Xdim)
resize(Xdim);
memset(vdata,0,vdim*sizeof(T));
}
/** Initialize to zeros following a pattern.
*
* All values are set to 0, and the origin and size of the pattern are
* adopted.
*
* @code
* v2.initZeros(v1);
* @endcode
*/
template <typename T1>
void initZeros(const Matrix1D<T1>& op)
{
if (vdim!=op.vdim)
resize(op);
memset(vdata,0,vdim*sizeof(T));
}
//@}
/// @name Matrix1D operators
//@{
/** v3 = v1 * k.
*/
Matrix1D<T> operator*(T op1) const
{
Matrix1D<T> tmp(*this);
for (int i=0; i < vdim; i++)
tmp.vdata[i] = vdata[i] * op1;
return tmp;
}
/** v3 = v1 / k.
*/
Matrix1D<T> operator/(T op1) const
{
Matrix1D<T> tmp(*this);
for (int i=0; i < vdim; i++)
tmp.vdata[i] = vdata[i] / op1;
return tmp;
}
/** v3 = v1 + k.
*/
Matrix1D<T> operator+(T op1) const
{
Matrix1D<T> tmp(*this);
for (int i=0; i < vdim; i++)
tmp.vdata[i] = vdata[i] + op1;
return tmp;
}
/** v3 = v1 - k.
*/
Matrix1D<T> operator-(T op1) const
{
Matrix1D<T> tmp(*this);
for (int i=0; i < vdim; i++)
tmp.vdata[i] = vdata[i] - op1;
return tmp;
}
/** v3 = k * v2.
*/
friend Matrix1D<T> operator*(T op1, const Matrix1D<T>& op2)
{
Matrix1D<T> tmp(op2);
for (int i=0; i < op2.vdim; i++)
tmp.vdata[i] = op1 * op2.vdata[i];
return tmp;
}
/** v3 = k / v2.
*/
friend Matrix1D<T> operator/(T op1, const Matrix1D<T>& op2)
{
Matrix1D<T> tmp(op2);
for (int i=0; i < op2.vdim; i++)
tmp.vdata[i] = op1 / op2.vdata[i];
return tmp;
}
/** v3 = k + v2.
*/
friend Matrix1D<T> operator+(T op1, const Matrix1D<T>& op2)
{
Matrix1D<T> tmp(op2);
for (int i=0; i < op2.vdim; i++)
tmp.vdata[i] = op1 + op2.vdata[i];
return tmp;
}
/** Vector summation
*
* @code
* A += B;
* @endcode
*/
void operator+=(const Matrix1D<T>& op1) const
{
if (vdim != op1.vdim)
REPORT_ERROR("Not same sizes in vector summation");
for (int i = 0; i < vdim; i++)
vdata[i] += op1.vdata[i];
}
/** v3 = k - v2.
*/
friend Matrix1D<T> operator-(T op1, const Matrix1D<T>& op2)
{
Matrix1D<T> tmp(op2);
for (int i=0; i < op2.vdim; i++)
tmp.vdata[i] = op1 - op2.vdata[i];
return tmp;
}
/** Vector substraction
*
* @code
* A -= B;
* @endcode
*/
void operator-=(const Matrix1D<T>& op1) const
{
if (vdim != op1.vdim)
REPORT_ERROR("Not same sizes in vector summation");
for (int i = 0; i < vdim; i++)
vdata[i] -= op1.vdata[i];
}
/** v3 *= k.
*/
void operator*=(T op1)
{
for (int i=0; i < vdim; i++)
vdata[i] *= op1;
}
/** v3 /= k.
*/
void operator/=(T op1)
{
for (int i=0; i < vdim; i++)
vdata[i] /= op1;
}
/** v3 += k.
*/
void operator+=(T op1)
{
for (int i=0; i < vdim; i++)
vdata[i] += op1;
}
/** v3 -= k.
*/
void operator-=(T op1)
{
for (int i=0; i < vdim; i++)
vdata[i] -= op1;
}
/** v3 = v1 * v2.
*/
Matrix1D<T> operator*(const Matrix1D<T>& op1) const
{
if (vdim != op1.vdim)
REPORT_ERROR("Not same sizes in vector multiplication");
Matrix1D<T> tmp(op1);
for (int i=0; i < vdim; i++)
tmp.vdata[i] = vdata[i] * op1.vdata[i];
return tmp;
}
/** v3 = v1 / v2.
*/
Matrix1D<T> operator/(const Matrix1D<T>& op1) const
{
if (vdim != op1.vdim)
REPORT_ERROR("Not same sizes in vector division");
Matrix1D<T> tmp(op1);
for (int i=0; i < vdim; i++)
tmp.vdata[i] = vdata[i] / op1.vdata[i];
return tmp;
}
/** v3 = v1 + v2.
*/
Matrix1D<T> operator+(const Matrix1D<T>& op1) const
{
if (vdim != op1.vdim)
REPORT_ERROR("Not same sizes in vector summation");
Matrix1D<T> tmp(op1);
for (int i=0; i < vdim; i++)
tmp.vdata[i] = vdata[i] + op1.vdata[i];
return tmp;
}
/** v3 = v1 - v2.
*/
Matrix1D<T> operator-(const Matrix1D<T>& op1) const
{
if (vdim != op1.vdim)
REPORT_ERROR("Not same sizes in vector subtraction");
Matrix1D<T> tmp(op1);
for (int i=0; i < vdim; i++)
tmp.vdata[i] = vdata[i] - op1.vdata[i];
return tmp;
}
/** v3 *= v2.
*/
void operator*=(const Matrix1D<T>& op1)
{
if (vdim != op1.vdim)
REPORT_ERROR("Not same sizes in vector multiplication");
for (int i=0; i < vdim; i++)
vdata[i] *= op1.vdata[i];
}
/** v3 /= v2.
*/
void operator/=(const Matrix1D<T>& op1)
{
if (vdim != op1.vdim)
REPORT_ERROR("Not same sizes in vector division");
for (int i=0; i < vdim; i++)
vdata[i] /= op1.vdata[i];
}
/** v3 += v2.
*/
void operator+=(const Matrix1D<T>& op1)
{
if (vdim != op1.vdim)
REPORT_ERROR("Not same sizes in vector summation");
for (int i=0; i < vdim; i++)
vdata[i] += op1.vdata[i];
}
/** v3 -= v2.
*/
void operator-=(const Matrix1D<T>& op1)
{
if (vdim != op1.vdim)
REPORT_ERROR("Not same sizes in vector subtraction");
for (int i=0; i < vdim; i++)
vdata[i] -= op1.vdata[i];
}
/** Unary minus.
*
* It is used to build arithmetic expressions. You can make a minus
* of anything as long as it is correct semantically.
*
* @code
* v1 = -v2;
* v1 = -v2.transpose();
* @endcode
*/
Matrix1D<T> operator-() const
{
Matrix1D<T> tmp(*this);
for (int i=0; i < vdim; i++)
tmp.vdata[i] = - vdata[i];
return tmp;
}
/** Vector by matrix
*
* Algebraic vector by matrix multiplication. This function is actually
* implemented in xmippMatrices2D
*/
Matrix1D<T> operator*(const Matrix2D<T>& M);
/** Vector element access
*
* Returns the value of a vector logical position. In our example we could
* access from v(-2) to v(2). The elements can be used either by value or by
* reference.
*
* @code
* v(-2) = 1;
* val = v(-2);
* @endcode
*/
T& operator()(int i) const
{
return vdata[i];
}
//@}
/// @name Utilities for Matrix1D
//@{
/** Produce a vector suitable for working with Numerical Recipes
*
* This function must be used only as a preparation for routines which need
* that the first physical index is 1 and not 0 as it usually is in C. In
* fact the vector provided for Numerical recipes is exactly this same one
* but with the indexes changed.
*
* This function is not ported to Python.
*/
T* adaptForNumericalRecipes() const
{
return MATRIX1D_ARRAY(*this) - 1;
}
/** Kill an array produced for Numerical Recipes.
*
* Nothing needs to be done in fact.
*
* This function is not ported to Python.
*/
void killAdaptationForNumericalRecipes(T* m) const
{}
/** CEILING
*
* Applies a CEILING (look for the nearest larger integer) to each
* array element.
*/
void selfCEIL()
{
for (int i=0; i < vdim; i++)
vdata[i] = CEIL(vdata[i]);
}
/** FLOOR
*
* Applies a FLOOR (look for the nearest larger integer) to each
* array element.
*/
void selfFLOOR()
{
for (int i=0; i < vdim; i++)
vdata[i] = FLOOR(vdata[i]);
}
/** ROUND
*
* Applies a ROUND (look for the nearest larger integer) to each
* array element.
*/
void selfROUND()
{
for (int i=0; i < vdim; i++)
vdata[i] = ROUND(vdata[i]);
}
/** Index for the maximum element.
*
* This function returns the index of the maximum element of an matrix1d.
* Returns -1 if the array is empty
*/
void maxIndex(int& jmax) const
{
if (vdim == 0)
{
jmax = -1;
return;
}
jmax = 0;
T maxval = (*this)(0);
for (int j = 0; j < vdim; j++)
if ( (*this)(j) > maxval )
jmax =j;
}
/** Index for the minimum element.
*
* This function returns the index of the minimum element of an matrix1d.
* Returns -1 if the array is empty
*/
void minIndex(int& jmin) const
{
if (vdim == 0)
{
jmin = -1;
return;
}
jmin = 0;
T minval = (*this)(0);