Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpColVector.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Provide some simple operation on column vectors.
32 */
33
39
40#include <assert.h>
41#include <cmath> // std::fabs
42#include <limits> // numeric_limits
43#include <math.h>
44#include <sstream>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48
49#include <visp3/core/vpCPUFeatures.h>
50#include <visp3/core/vpColVector.h>
51#include <visp3/core/vpException.h>
52#include <visp3/core/vpMath.h>
53#include <visp3/core/vpRotationVector.h>
54
55#if defined(VISP_HAVE_SIMDLIB)
56#include <Simd/SimdLib.h>
57#endif
58
61{
62 if (getRows() != v.getRows()) {
63 throw(vpException(vpException::dimensionError, "Cannot add (%dx1) column vector to (%dx1) column vector", getRows(),
64 v.getRows()));
65 }
67
68 for (unsigned int i = 0; i < rowNum; ++i) {
69 r[i] = (*this)[i] + v[i];
70 }
71 return r;
72}
73
75{
76 const unsigned int val_3 = 3;
77 if (getRows() != val_3) {
78 throw(vpException(vpException::dimensionError, "Cannot add %d-dimension column vector to a translation vector",
79 getRows()));
80 }
82
83 for (unsigned int i = 0; i < val_3; ++i) {
84 s[i] = (*this)[i] + t[i];
85 }
86
87 return s;
88}
89
91{
92 if (getRows() != v.getRows()) {
93 throw(vpException(vpException::dimensionError, "Cannot add (%dx1) column vector to (%dx1) column vector", getRows(),
94 v.getRows()));
95 }
96
97 for (unsigned int i = 0; i < rowNum; ++i) {
98 (*this)[i] += v[i];
99 }
100 return (*this);
101}
102
104{
105 if (getRows() != t.getRows()) {
106 throw(vpException(vpException::dimensionError, "Cannot add (%dx1) translation vector to (%dx1) column vector",
107 getRows(), t.getRows()));
108 }
109
110 for (unsigned int i = 0; i < rowNum; ++i) {
111 (*this)[i] += t[i];
112 }
113 return (*this);
114}
115
117{
118 if (getRows() != v.getRows()) {
119 throw(vpException(vpException::dimensionError, "Cannot subtract (%dx1) column vector to (%dx1) column vector",
120 getRows(), v.getRows()));
121 }
122
123 for (unsigned int i = 0; i < rowNum; ++i) {
124 (*this)[i] -= v[i];
125 }
126 return (*this);
127}
128
130{
131 if (getRows() != t.getRows()) {
132 throw(vpException(vpException::dimensionError, "Cannot subtract (%dx1) translation vector to (%dx1) column vector",
133 getRows(), t.getRows()));
134 }
135
136 for (unsigned int i = 0; i < rowNum; ++i) {
137 (*this)[i] -= t[i];
138 }
139 return (*this);
140}
141
143{
144 if (size() != v.size()) {
146 "Cannot compute the dot product between column vectors "
147 "with different dimensions (%d) and (%d)",
148 size(), v.size()));
149 }
150 double r = 0;
151
152 for (unsigned int i = 0; i < rowNum; ++i) {
153 r += (*this)[i] * v[i];
154 }
155 return r;
156}
157
159{
160 vpMatrix M(rowNum, v.getCols());
161 unsigned int v_cols = v.getCols();
162 for (unsigned int i = 0; i < rowNum; ++i) {
163 for (unsigned int j = 0; j < v_cols; ++j) {
164 M[i][j] = (*this)[i] * v[j];
165 }
166 }
167 return M;
168}
169
171{
172 if (M.getRows() != 1) {
174 "Bad size during vpColVector (%dx1) and vpMatrix (%dx%d) multiplication",
175 getRows(), M.getRows(), M.getCols()));
176 }
177 vpMatrix R(rowNum, M.getCols());
178 unsigned int M_cols = M.getCols();
179 for (unsigned int i = 0; i < rowNum; ++i) {
180 for (unsigned int j = 0; j < M_cols; ++j) {
181 R[i][j] = (*this)[i] * M[0][j];
182 }
183 }
184 return R;
185}
186
188{
189 if (getRows() != m.getRows()) {
191 "Bad size during vpColVector (%dx1) and vpColVector "
192 "(%dx1) subtraction",
193 getRows(), m.getRows()));
194 }
196
197 for (unsigned int i = 0; i < rowNum; ++i) {
198 v[i] = (*this)[i] - m[i];
199 }
200 return v;
201}
202
203vpColVector::vpColVector(const vpColVector &v, unsigned int r, unsigned int nrows) : vpArray2D<double>(nrows, 1)
204{
205 init(v, r, nrows);
206}
207
208void vpColVector::init(const vpColVector &v, unsigned int r, unsigned int nrows)
209{
210 unsigned int rnrows = r + nrows;
211
212 if (rnrows > v.getRows()) {
213 throw(vpException(vpException::dimensionError, "Bad row dimension (%d > %d) used to initialize vpColVector", rnrows,
214 v.getRows()));
215 }
216 resize(nrows, false);
217
218 if (this->rowPtrs == nullptr) { // Fix coverity scan: explicit null dereferenced
219 return; // Nothing to do
220 }
221 for (unsigned int i = r; i < rnrows; ++i) {
222 (*this)[i - r] = v[i];
223 }
224}
225
227{
228 unsigned int v_size = v.size();
229 for (unsigned int i = 0; i < v_size; ++i) {
230 (*this)[i] = v[i];
231 }
232}
233
235{
236 unsigned int p_size = p.size();
237 for (unsigned int i = 0; i < p_size; ++i) {
238 (*this)[i] = p[i];
239 }
240}
241
243{
244 unsigned int v_size = v.size();
245 for (unsigned int i = 0; i < v_size; ++i) {
246 (*this)[i] = v[i];
247 }
248}
249
250vpColVector::vpColVector(const vpMatrix &M, unsigned int j) : vpArray2D<double>(M.getRows(), 1)
251{
252 unsigned int m_cols = M.getCols();
253 for (unsigned int i = 0; i < m_cols; ++i) {
254 (*this)[i] = M[i][j];
255 }
256}
257
259{
260 if (M.getCols() != 1) {
261 throw(vpException(vpException::dimensionError, "Cannot construct a (%dx1) row vector from a (%dx%d) matrix",
262 M.getRows(), M.getRows(), M.getCols()));
263 }
264
265 unsigned int m_rows = M.getRows();
266 for (unsigned int i = 0; i < m_rows; ++i) {
267 (*this)[i] = M[i][0];
268 }
269}
270
271vpColVector::vpColVector(const std::vector<double> &v) : vpArray2D<double>(static_cast<unsigned int>(v.size()), 1)
272{
273 unsigned int v_size = static_cast<unsigned int>(v.size());
274 for (unsigned int i = 0; i < v_size; ++i) {
275 (*this)[i] = v[i];
276 }
277}
278
279vpColVector::vpColVector(const std::vector<float> &v) : vpArray2D<double>(static_cast<unsigned int>(v.size()), 1)
280{
281 unsigned int v_size = static_cast<unsigned int>(v.size());
282 for (unsigned int i = 0; i < v_size; ++i) {
283 (*this)[i] = static_cast<double>(v[i]);
284 }
285}
286
287#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
289{
290
291}
292#endif
293
295{
296 vpColVector A;
297 A.resize(rowNum, false);
298
299 double *vd = A.data;
300 double *d = data;
301
302 for (unsigned int i = 0; i < rowNum; ++i) {
303 // move the d++ increment/decrement into a dedicated expression-statement
304 *vd = -(*d);
305 ++vd;
306 ++d;
307 }
308
309 return A;
310}
311
313{
315
316 double *vd = v.data;
317 double *d = data;
318
319 for (unsigned int i = 0; i < rowNum; ++i) {
320 // move the d++ increment/decrement into a dedicated expression-statement
321 *vd = (*d) * x;
322 ++vd;
323 ++d;
324 }
325 return v;
326}
327
329{
330 for (unsigned int i = 0; i < rowNum; ++i) {
331 (*this)[i] *= x;
332 }
333 return (*this);
334}
335
337{
338 for (unsigned int i = 0; i < rowNum; ++i) {
339 (*this)[i] /= x;
340 }
341 return (*this);
342}
343
345{
347
348 double *vd = v.data;
349 double *d = data;
350
351 for (unsigned int i = 0; i < rowNum; ++i) {
352 // move the d++ increment/decrement into a dedicated expression-statement
353 *vd = (*d) / x;
354 ++vd;
355 ++d;
356 }
357 return v;
358}
359
361{
362 if (M.getCols() != 1) {
363 throw(vpException(vpException::dimensionError, "Cannot transform a (%dx%d) matrix into a column vector",
364 M.getRows(), M.getCols()));
365 }
366
367 resize(M.getRows(), false);
368 memcpy(data, M.data, rowNum * sizeof(double));
369
370 return (*this);
371}
372
373vpColVector &vpColVector::operator=(const std::vector<double> &v)
374{
375 unsigned int v_size = static_cast<unsigned int>(v.size());
376 resize(v_size, false);
377 for (unsigned int i = 0; i < v_size; ++i) {
378 (*this)[i] = v[i];
379 }
380 return *this;
381}
382
383vpColVector &vpColVector::operator=(const std::vector<float> &v)
384{
385 unsigned int v_size = static_cast<unsigned int>(v.size());
386 resize(v_size, false);
387 for (unsigned int i = 0; i < v_size; ++i) {
388 (*this)[i] = static_cast<double>(v[i]);
389 }
390 return *this;
391}
392
394{
395 unsigned int k = v.rowNum;
396 if (rowNum != k) {
397 resize(k, false);
398 }
399
400 memcpy(data, v.data, rowNum * sizeof(double));
401 return *this;
402}
403
405{
406 unsigned int k = tv.getRows();
407 if (rowNum != k) {
408 resize(k, false);
409 }
410
411 memcpy(data, tv.data, rowNum * sizeof(double));
412 return *this;
413}
414
416{
417 unsigned int k = rv.getRows();
418 if (rowNum != k) {
419 resize(k, false);
420 }
421
422 memcpy(data, rv.data, rowNum * sizeof(double));
423 return *this;
424}
425
427{
428 unsigned int k = p.getRows();
429 if (rowNum != k) {
430 resize(k, false);
431 }
432
433 memcpy(data, p.data, rowNum * sizeof(double));
434 return *this;
435}
436
438{
439 *this = v;
440 return *this;
441}
442
444{
445 for (unsigned int i = 0; i < rowNum; ++i) {
446 for (unsigned int j = 0; j < colNum; ++j) {
447 rowPtrs[i][j] = *x++;
448 }
449 }
450 return *this;
451}
452
454{
455 resize(1, false);
456 data[0] = val;
457 return *this;
458}
459
461{
462 resize(rowNum + 1, false);
463 data[rowNum - 1] = val;
464 return *this;
465}
466
468{
469 double *d = data;
470
471 for (unsigned int i = 0; i < rowNum; ++i) {
472 *(d++) = x;
473 }
474 return *this;
475}
476
477std::vector<double> vpColVector::toStdVector() const
478{
479 std::vector<double> v(this->size());
480
481 unsigned int v_this_size = this->size();
482 for (unsigned int i = 0; i < v_this_size; ++i) {
483 v[i] = data[i];
484 }
485 return v;
486}
487
488#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
490{
491 vpArray2D<double>::operator=(std::move(other));
492 return *this;
493}
494
505vpColVector vpColVector::view(double *raw_data, unsigned int nrows)
506{
507 vpColVector v;
508 vpArray2D<double>::view(v, raw_data, nrows, 1);
509 return v;
510}
511
512
513void vpColVector::view(vpColVector &v, double *data, unsigned int rows)
514{
516}
517
518
519
520vpColVector &vpColVector::operator=(const std::initializer_list<double> &list)
521{
522 resize(static_cast<unsigned int>(list.size()), false);
523 std::copy(list.begin(), list.end(), data);
524 return *this;
525}
526#endif
527
529{
530 if ((rowNum != v.rowNum) || (colNum != v.colNum) /* should not happen */) {
531 return false;
532 }
533
534 for (unsigned int i = 0; i < rowNum; ++i) {
535 if (!vpMath::equal(data[i], v.data[i], std::numeric_limits<double>::epsilon())) {
536 return false;
537 }
538 }
539
540 return true;
541}
542
543bool vpColVector::operator==(double v) const
544{
545 for (unsigned int i = 0; i < rowNum; ++i) {
546 if (!vpMath::equal(data[i], v, std::numeric_limits<double>::epsilon())) {
547 return false;
548 }
549 }
550
551 return true;
552}
553
554bool vpColVector::operator!=(const vpColVector &v) const { return !(*this == v); }
555
556bool vpColVector::operator!=(double v) const { return !(*this == v); }
557
559{
561 memcpy(v.data, data, rowNum * sizeof(double));
562 return v;
563}
564
566
567void vpColVector::transpose(vpRowVector &v) const { v = t(); }
568
570{
571 if (a.data == nullptr) {
572 throw(vpException(vpException::fatalError, "Cannot compute the dot product: first vector empty"));
573 }
574 if (b.data == nullptr) {
575 throw(vpException(vpException::fatalError, "Cannot compute the dot product: second vector empty"));
576 }
577 if (a.size() != b.size()) {
579 "Cannot compute the dot product between column vectors "
580 "with different dimensions (%d) and (%d)",
581 a.size(), b.size()));
582 }
583
584 double *ad = a.data;
585 double *bd = b.data;
586
587 double c = 0;
588 unsigned int a_rows_nbr = a.getRows();
589 for (unsigned int i = 0; i < a_rows_nbr; ++i) {
590 // Move the ad++ and bd++ increment/decrement into a dedicated expression-statement
591 c += (*ad) * (*bd);
592 ++ad;
593 ++bd;
594 }
595
596 return c;
597}
598
599
601{
602 x /= sqrt(x.sumSquare());
603
604 return x;
605}
606
608{
609 double sum_square = sumSquare();
610
611 if (std::fabs(sum_square) > std::numeric_limits<double>::epsilon()) {
612 *this /= sqrt(sum_square);
613 }
614
615 // If sum = 0, we have a nul vector. So we return just.
616 return *this;
617}
618
620{
621 if (v.data == nullptr) {
622 throw(vpException(vpException::fatalError, "Cannot sort content of column vector: vector empty"));
623 }
624 vpColVector tab;
625 tab = v;
626 unsigned int nb_permutation = 1;
627 unsigned int i = 0;
628 while (nb_permutation != 0) {
629 nb_permutation = 0;
630 for (unsigned int j = v.getRows() - 1; j >= (i + 1); --j) {
631 if (tab[j] > tab[j - 1]) {
632 double tmp = tab[j];
633 tab[j] = tab[j - 1];
634 tab[j - 1] = tmp;
635 ++nb_permutation;
636 }
637 }
638 ++i;
639 }
640
641 return tab;
642}
643
645{
646 if (v.data == nullptr) {
647 throw(vpException(vpException::fatalError, "Cannot sort content of column vector: vector empty"));
648 }
649 vpColVector tab;
650 tab = v;
651 unsigned int nb_permutation = 1;
652 unsigned int i = 0;
653 while (nb_permutation != 0) {
654 nb_permutation = 0;
655 for (unsigned int j = v.getRows() - 1; j >= (i + 1); --j) {
656 if (tab[j] < tab[j - 1]) {
657 double tmp = tab[j];
658 tab[j] = tab[j - 1];
659 tab[j - 1] = tmp;
660 ++nb_permutation;
661 }
662 }
663 ++i;
664 }
665
666 return tab;
667}
668
669void vpColVector::stack(double d)
670{
671 this->resize(rowNum + 1, false);
672 (*this)[rowNum - 1] = d;
673}
674
675void vpColVector::stack(const vpColVector &v) { *this = vpColVector::stack(*this, v); }
676
678{
679 vpColVector C;
680 vpColVector::stack(A, B, C);
681 return C;
682}
683
685{
686 unsigned int nrA = A.getRows();
687 unsigned int nrB = B.getRows();
688
689 if ((nrA == 0) && (nrB == 0)) {
690 C.resize(0);
691 return;
692 }
693
694 if (nrB == 0) {
695 C = A;
696 return;
697 }
698
699 if (nrA == 0) {
700 C = B;
701 return;
702 }
703
704 // General case
705 C.resize(nrA + nrB, false);
706
707 for (unsigned int i = 0; i < nrA; ++i) {
708 C[i] = A[i];
709 }
710
711 for (unsigned int i = 0; i < nrB; ++i) {
712 C[nrA + i] = B[i];
713 }
714}
715
717{
718 if ((v.data == nullptr) || (v.size() == 0)) {
719 throw(vpException(vpException::dimensionError, "Cannot compute column vector mean: vector empty"));
720 }
721
722 // Use directly sum() function
723 double mean = v.sum();
724
725 return mean / v.getRows();
726}
727
729{
730 if ((v.data == nullptr) || (v.size() == 0)) {
731 throw(vpException(vpException::dimensionError, "Cannot compute column vector median: vector empty"));
732 }
733
734 std::vector<double> vectorOfDoubles(v.data, v.data + v.rowNum);
735
736 return vpMath::getMedian(vectorOfDoubles);
737}
738
739double vpColVector::stdev(const vpColVector &v, bool useBesselCorrection)
740{
741 if ((v.data == nullptr) || (v.size() == 0)) {
742 throw(vpException(vpException::dimensionError, "Cannot compute column vector stdev: vector empty"));
743 }
744#if defined(VISP_HAVE_SIMDLIB)
745 return SimdVectorStdev(v.data, v.rowNum, useBesselCorrection);
746#else
747 double mean_value = v.sum() / v.size();
748 double sum_squared_diff = 0.0;
749 unsigned int v_size = v.size();
750 for (size_t i = 0; i < v_size; ++i) {
751 sum_squared_diff += (v[i] - mean_value) * (v[i] - mean_value);
752 }
753
754 double divisor = static_cast<double>(v.size());
755 if (useBesselCorrection) {
756 divisor = divisor - 1;
757 }
758
759 return std::sqrt(sum_squared_diff / divisor);
760#endif
761}
762
764{
765 vpMatrix M;
766 const unsigned int rows_size = 3;
767 if (v.getRows() != rows_size) {
768 throw(vpException(vpException::dimensionError, "Cannot compute skew vector of a non 3-dimension vector (%d)",
769 v.getRows()));
770 }
771
772 M.resize(rows_size, rows_size, false, false);
773 const unsigned int index_0 = 0;
774 const unsigned int index_1 = 1;
775 const unsigned int index_2 = 2;
776 M[index_0][index_0] = 0;
777 M[index_0][index_1] = -v[index_2];
778 M[index_0][index_2] = v[index_1];
779 M[index_1][index_0] = v[index_2];
780 M[index_1][index_1] = 0;
781 M[index_1][index_2] = -v[index_0];
782 M[index_2][index_0] = -v[index_1];
783 M[index_2][index_1] = v[index_0];
784 M[index_2][index_2] = 0;
785
786 return M;
787}
788
790{
791 const unsigned int val_3 = 3;
792 if ((a.getRows() != val_3) || (b.getRows() != val_3)) {
794 "Cannot compute the cross product between column "
795 "vector with dimension %d and %d",
796 a.getRows(), b.getRows()));
797 }
798
799 return vpColVector::skew(a) * b;
800}
801
802vpMatrix vpColVector::reshape(unsigned int nrows, unsigned int ncols)
803{
804 vpMatrix M(nrows, ncols);
805 reshape(M, nrows, ncols);
806 return M;
807}
808
809void vpColVector::reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols)
810{
811 if (dsize != (nrows * ncols)) {
812 throw(vpException(vpException::dimensionError, "Cannot reshape (%dx1) column vector in (%dx%d) matrix", rowNum,
813 M.getRows(), M.getCols()));
814 }
815 if ((M.getRows() != nrows) || (M.getCols() != ncols)) {
816 M.resize(nrows, ncols, false, false);
817 }
818
819 for (unsigned int j = 0; j < ncols; ++j) {
820 for (unsigned int i = 0; i < nrows; ++i) {
821 M[i][j] = data[(j * nrows) + i];
822 }
823 }
824}
825
826void vpColVector::insert(unsigned int i, const vpColVector &v)
827{
828 if ((i + v.size()) >(this->size())) {
829 throw(vpException(vpException::dimensionError, "Unable to insert a column vector"));
830 }
831
832 if ((data != nullptr) && (v.data != nullptr) && (v.rowNum > 0)) {
833 memcpy(data + i, v.data, sizeof(double) * v.rowNum);
834 }
835}
836
837int vpColVector::print(std::ostream &s, unsigned int length, char const *intro) const
838{
839 typedef std::string::size_type size_type;
840
841 unsigned int m = getRows();
842 unsigned int n = 1;
843
844 std::vector<std::string> values(m * n);
845 std::ostringstream oss;
846 std::ostringstream ossFixed;
847 std::ios_base::fmtflags original_flags = oss.flags();
848
849 // ossFixed <<std::fixed
850 ossFixed.setf(std::ios::fixed, std::ios::floatfield);
851
852 size_type maxBefore = 0; // the length of the integral part
853 size_type maxAfter = 0; // number of decimals plus
854 // one place for the decimal point
855 for (unsigned int i = 0; i < m; ++i) {
856 oss.str("");
857 oss << (*this)[i];
858 if (oss.str().find("e") != std::string::npos) {
859 ossFixed.str("");
860 ossFixed << (*this)[i];
861 oss.str(ossFixed.str());
862 }
863
864 values[i] = oss.str();
865 size_type thislen = values[i].size();
866 size_type p = values[i].find('.');
867
868 if (p == std::string::npos) {
869 maxBefore = vpMath::maximum(maxBefore, thislen);
870 // maxAfter remains the same
871 }
872 else {
873 maxBefore = vpMath::maximum(maxBefore, p);
874 maxAfter = vpMath::maximum(maxAfter, thislen - p - 1);
875 }
876 }
877
878 size_type totalLength = length;
879 // increase totalLength according to maxBefore
880 totalLength = vpMath::maximum(totalLength, maxBefore);
881 // decrease maxAfter according to totalLength
882 maxAfter = std::min<size_type>(maxAfter, totalLength - maxBefore);
883 if (maxAfter == 1) {
884 maxAfter = 0;
885 }
886
887 // the following line is useful for debugging
888 // std::cerr <<totalLength <<" " <<maxBefore <<" " <<maxAfter <<"\n";
889
890 if (intro) {
891 s << intro;
892 }
893 s << "[" << m << "," << n << "]=\n";
894
895 for (unsigned int i = 0; i < m; ++i) {
896 s << " ";
897 size_type p = values[i].find('.');
898 s.setf(std::ios::right, std::ios::adjustfield);
899 s.width(static_cast<std::streamsize>(maxBefore));
900 s << values[i].substr(0, p).c_str();
901
902 if (maxAfter > 0) {
903 s.setf(std::ios::left, std::ios::adjustfield);
904 if (p != std::string::npos) {
905 s.width(static_cast<std::streamsize>(maxAfter));
906 s << values[i].substr(p, maxAfter).c_str();
907 }
908 else {
909 assert(maxAfter > 1);
910 s.width(static_cast<std::streamsize>(maxAfter));
911 s << ".0";
912 }
913 }
914
915 s << ' ';
916
917 s << std::endl;
918 }
919
920 s.flags(original_flags); // restore s to standard state
921
922 return static_cast<int>(maxBefore + maxAfter);
923}
924
925double vpColVector::sum() const
926{
927#if defined(VISP_HAVE_SIMDLIB)
928 return SimdVectorSum(data, rowNum);
929#else
930 double sum = 0.0;
931 for (unsigned int i = 0; i < rowNum; ++i) {
932 sum += (*this)[i];
933 }
934 return sum;
935#endif
936}
937
939{
940#if defined(VISP_HAVE_SIMDLIB)
941 return SimdVectorSumSquare(data, rowNum);
942#else
943 double sum_square = 0.0;
944 for (unsigned int i = 0; i < rowNum; ++i) {
945 sum_square += (*this)[i] * (*this)[i];
946 }
947 return sum_square;
948#endif
949}
950
952{
953 double norm = sumSquare();
954
955 return sqrt(norm);
956}
957
959{
960 if ((v.getRows() != rowNum) || (v.getCols() != colNum)) {
961 throw(vpException(vpException::dimensionError, "Hadamard product: bad dimensions!"));
962 }
963
964 vpColVector out;
965 out.resize(rowNum, false);
966#if defined(VISP_HAVE_SIMDLIB)
967 SimdVectorHadamard(data, v.data, rowNum, out.data);
968#else
969 for (unsigned int i = 0; i < dsize; ++i) {
970 out.data[i] = data[i] * v.data[i];
971 }
972#endif
973 return out;
974 }
975
977{
978 double norm = 0.0;
979 for (unsigned int i = 0; i < rowNum; ++i) {
980 double x = fabs((*this)[i]);
981 if (x > norm) {
982 norm = x;
983 }
984 }
985 return norm;
986}
987
988std::ostream &vpColVector::cppPrint(std::ostream &os, const std::string &matrixName, bool octet) const
989{
990 os << "vpColVector " << matrixName << " (" << this->getRows() << "); " << std::endl;
991
992 unsigned int rows_nbr = this->getRows();
993 for (unsigned int i = 0; i < rows_nbr; ++i) {
994
995 if (!octet) {
996 os << matrixName << "[" << i << "] = " << (*this)[i] << "; " << std::endl;
997 }
998 else {
999 for (unsigned int k = 0; k < sizeof(double); ++k) {
1000 os << "((unsigned char*)&(" << matrixName << "[" << i << "]) )[" << k << "] = 0x" << std::hex
1001 << static_cast<unsigned int>(((unsigned char *)&((*this)[i]))[k]) << "; " << std::endl;
1002 }
1003 }
1004 }
1005 std::cout << std::endl;
1006 return os;
1007}
1008
1009std::ostream &vpColVector::csvPrint(std::ostream &os) const
1010{
1011 unsigned int rows_nbr = this->getRows();
1012 for (unsigned int i = 0; i < rows_nbr; ++i) {
1013 os << (*this)[i];
1014
1015 os << std::endl;
1016 }
1017 return os;
1018}
1019
1020std::ostream &vpColVector::maplePrint(std::ostream &os) const
1021{
1022 unsigned int rows_nbr = this->getRows();
1023 os << "([ " << std::endl;
1024 for (unsigned int i = 0; i < rows_nbr; ++i) {
1025 os << "[";
1026 os << (*this)[i] << ", ";
1027 os << "]," << std::endl;
1028 }
1029 os << "])" << std::endl;
1030 return os;
1031}
1032
1033std::ostream &vpColVector::matlabPrint(std::ostream &os) const
1034{
1035 unsigned int rows_nbr = this->getRows();
1036 os << "[ ";
1037 for (unsigned int i = 0; i < rows_nbr; ++i) {
1038 os << (*this)[i] << ", ";
1039 if (this->getRows() != (i + 1)) {
1040 os << ";" << std::endl;
1041 }
1042 else {
1043 os << "]" << std::endl;
1044 }
1045 }
1046 return os;
1047}
1048
1049#if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
1050
1051void vpColVector::insert(const vpColVector &v, unsigned int i)
1052{
1053 insert(i, v);
1054}
1055
1056void vpColVector::insert(const vpColVector &v, unsigned int r, unsigned int c)
1057{
1058 (void)c;
1059 insert(r, v);
1060}
1061
1062double vpColVector::euclideanNorm() const { return frobeniusNorm(); }
1063#endif // defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
1064
1065vpColVector operator*(const double &x, const vpColVector &v)
1066{
1067 vpColVector vout;
1068 vout = v * x;
1069 return vout;
1070}
1071END_VISP_NAMESPACE
unsigned int getCols() const
Definition vpArray2D.h:423
Type * data
Address of the first element of the data array.
Definition vpArray2D.h:149
vpArray2D< Type > & operator=(Type x)
Set all the elements of the array to x.
Definition vpArray2D.h:615
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition vpArray2D.h:448
static vpArray2D< Type > view(const vpArray2D< Type > &A)
Creates a view of the Matrix A. A view shares the same underlying memory as the original array....
Definition vpArray2D.h:324
unsigned int rowNum
Definition vpArray2D.h:1201
unsigned int dsize
Definition vpArray2D.h:1207
unsigned int size() const
Definition vpArray2D.h:435
unsigned int getRows() const
Definition vpArray2D.h:433
unsigned int colNum
Definition vpArray2D.h:1203
void reshape(vpMatrix &M, const unsigned int &nrows, const unsigned int &ncols)
vpColVector & operator+=(const vpColVector &v)
double operator*(const vpColVector &v) const
static double dotProd(const vpColVector &a, const vpColVector &b)
friend class vpMatrix
vpColVector operator-() const
vpColVector & operator*=(double x)
int print(std::ostream &s, unsigned int length, char const *intro=nullptr) const
vpColVector & operator-=(const vpColVector &v)
std::ostream & matlabPrint(std::ostream &os) const
VP_DEPRECATED double euclideanNorm() const
vpColVector & operator=(const vpColVector &v)
vpColVector operator/(double x) const
vpColVector & normalize()
static double median(const vpColVector &v)
vpColVector hadamard(const vpColVector &v) const
double sumSquare() const
std::ostream & csvPrint(std::ostream &os) const
std::ostream & maplePrint(std::ostream &os) const
vpColVector & operator,(double val)
bool operator==(const vpColVector &v) const
vpColVector & operator/=(double x)
static vpColVector invSort(const vpColVector &v)
void stack(double d)
bool operator!=(const vpColVector &v) const
static vpMatrix skew(const vpColVector &v)
std::vector< double > toStdVector() const
static double mean(const vpColVector &v)
vpRowVector t() const
static vpColVector view(double *raw_data, unsigned int rows)
Create a column vector view of a raw data array. The view can modify the contents of the raw data arr...
static vpColVector crossProd(const vpColVector &a, const vpColVector &b)
double infinityNorm() const
vpColVector & operator<<(const vpColVector &v)
vpRowVector transpose() const
static double stdev(const vpColVector &v, bool useBesselCorrection=false)
std::ostream & cppPrint(std::ostream &os, const std::string &matrixName="A", bool octet=false) const
double frobeniusNorm() const
VP_DEPRECATED vpColVector rows(unsigned int first_row, unsigned int last_row) const
vpColVector operator+(const vpColVector &v) const
static vpColVector sort(const vpColVector &v)
void insert(unsigned int i, const vpColVector &v)
VP_DEPRECATED void init()
double sum() const
void resize(unsigned int i, bool flagNullify=true)
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ dimensionError
Bad dimension.
Definition vpException.h:71
@ fatalError
Fatal error.
Definition vpException.h:72
static double getMedian(const std::vector< double > &v)
Definition vpMath.cpp:343
static Type maximum(const Type &a, const Type &b)
Definition vpMath.h:257
static bool equal(double x, double y, double threshold=0.001)
Definition vpMath.h:470
Implementation of a pose vector and operations on poses.
Implementation of a generic rotation vector.
Implementation of row vector and the associated operations.
Class that consider the case of a translation vector.