GammaLib 2.1.0.dev
Loading...
Searching...
No Matches
GVector.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GVector.cpp - Vector class *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2006-2025 by Juergen Knoedlseder *
5 * ----------------------------------------------------------------------- *
6 * *
7 * This program is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21/**
22 * @file GVector.cpp
23 * @brief Vector class implementation
24 * @author Juergen Knoedlseder
25 */
26
27/* __ Includes ___________________________________________________________ */
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include "GVector.hpp"
32#include "GTools.hpp"
33
34
35/* __ Method name definitions ____________________________________________ */
36#define G_CONSTRUCTOR "GVector::GVector(int&)"
37#define G_OP_ADD "GVector::operator+=(GVector&)"
38#define G_OP_SUB "GVector::operator-=(GVector&)"
39#define G_OP_MULT "GVector::operator*=(GVector&)"
40#define G_OP_DIV "GVector::operator/=(GVector&)"
41#define G_AT "GVector::at(int&)"
42#define G_CROSS "cross(GVector&, GVector&)"
43#define G_SCALAR "operator*(GVector&, GVector&)"
44#define G_ANGLE "angle(GVector&, GVector&)"
45
46
47/*==========================================================================
48 = =
49 = Constructors/destructors =
50 = =
51 ==========================================================================*/
52
53/***********************************************************************//**
54 * @brief Void vector constructor
55 ***************************************************************************/
57{
58 // Initialise class members
60
61 // Return
62 return;
63}
64
65
66/***********************************************************************//**
67 * @brief Vector constructor
68 *
69 * @param[in] num Number of elements in vector [>=0].
70 *
71 * @exception GException::invalid_argument
72 * Number of elements is negative.
73 *
74 * Initialises a vector with @p num elements. All vector elements will be
75 * set to 0.
76 ***************************************************************************/
77GVector::GVector(const int& num)
78{
79 // Compile option: raise an exception if number of rows or columns is
80 // negative
81 #if defined(G_RANGE_CHECK)
82 if (num < 0) {
83 std::string msg = "Number of elements "+gammalib::str(num)+" is "
84 "negative. Please specify a non-negative number of "
85 "elements.";
87 }
88 #endif
89
90 // Initialise class members
92
93 // Store vector size
94 m_num = num;
95
96 // Allocate vector (filled with 0)
98
99 // Return
100 return;
101}
102
103
104/***********************************************************************//**
105 * @brief Single element vector constructor
106 *
107 * @param[in] a Vector element.
108 *
109 * Initialises a vector with a single element.
110 ***************************************************************************/
111GVector::GVector(const double& a)
112{
113 // Initialise class members
114 init_members();
115
116 // Store vector size
117 m_num = 1;
118
119 // Allocate vector
121
122 // Set value
123 m_data[0] = a;
124
125 // Return
126 return;
127}
128
129
130/***********************************************************************//**
131 * @brief Two elements vector constructor
132 *
133 * @param[in] a First vector element.
134 * @param[in] b Second vector element.
135 *
136 * Initialises a vector with two elements.
137 ***************************************************************************/
138GVector::GVector(const double& a, const double& b)
139{
140 // Initialise class members
141 init_members();
142
143 // Store vector size
144 m_num = 2;
145
146 // Allocate vector
148
149 // Set values
150 m_data[0] = a;
151 m_data[1] = b;
152
153 // Return
154 return;
155}
156
157
158/***********************************************************************//**
159 * @brief Three elements vector constructor
160 *
161 * @param[in] a First vector element.
162 * @param[in] b Second vector element.
163 * @param[in] c Third vector element.
164 *
165 * Initialises a vector with three elements.
166 ***************************************************************************/
167GVector::GVector(const double& a, const double& b, const double& c)
168{
169 // Initialise class members
170 init_members();
171
172 // Store vector size
173 m_num = 3;
174
175 // Allocate vector
177
178 // Set values
179 m_data[0] = a;
180 m_data[1] = b;
181 m_data[2] = c;
182
183 // Return
184 return;
185}
186
187/***********************************************************************//**
188 * @brief Copy constructor
189 *
190 * @param[in] vector Vector.
191 ***************************************************************************/
193{
194 // Initialise class members
195 init_members();
196
197 // Copy members
198 copy_members(vector);
199
200 // Return
201 return;
202}
203
204
205/***********************************************************************//**
206 * @brief Destructor
207 ***************************************************************************/
209{
210 // Free members
211 free_members();
212
213 // Return
214 return;
215}
216
217
218/*==========================================================================
219 = =
220 = Operators =
221 = =
222 ==========================================================================*/
223
224/***********************************************************************//**
225 * @brief Assignment operator
226 *
227 * @param[in] vector Vector.
228 * @return Vector.
229 ***************************************************************************/
231{
232 // Execute only if object is not identical
233 if (this != &vector) {
234
235 // Free members
236 free_members();
237
238 // Initialise private members
239 init_members();
240
241 // Copy members
242 copy_members(vector);
243
244 } // endif: object was not identical
245
246 // Return this object
247 return *this;
248}
249
250
251/***********************************************************************//**
252 * @brief Equality operator
253 *
254 * @param[in] vector Vector.
255 * @return True if vectors are identical.
256 *
257 * Returns true if vectors are identical. Vectors are considered identical
258 * if they have the same size and if all their elements are identical.
259 ***************************************************************************/
260bool GVector::operator==(const GVector& vector) const
261{
262 // Initalise result depending on vector size identity
263 bool result = (m_num == vector.m_num);
264
265 // Test for difference. Break at first difference
266 if (result) {
267 for (int i = 0; i < m_num; ++i) {
268 if (m_data[i] != vector.m_data[i]) {
269 result = false;
270 break;
271 }
272 }
273 }
274
275 // Return result
276 return result;
277}
278
279
280/***********************************************************************//**
281 * @brief Non-equality operator
282 *
283 * @param[in] vector Vector.
284 * @return True if both vectors are different.
285 ***************************************************************************/
286bool GVector::operator!=(const GVector& vector) const
287{
288 // Get negated result of equality operation
289 bool result = !(this->operator==(vector));
290
291 // Return result
292 return result;
293}
294
295
296/***********************************************************************//**
297 * @brief Unary addition operator
298 *
299 * @param[in] vector Vector.
300 * @return Vector.
301 *
302 * @exception GException::invalid_argument
303 * Vectors have not the same size.
304 *
305 * Adds vector elements.
306 ***************************************************************************/
308{
309 // Raise exception if vectors mismatch
310 if (m_num != vector.m_num) {
311 std::string msg = "Vector size "+gammalib::str(vector.m_num)+
312 " differs from expected size "+gammalib::str(m_num)+
313 ". Please specify a vector of size "+
314 gammalib::str(m_num)+".";
316 }
317
318 // Add elements
319 for (int i = 0; i < m_num; ++i) {
320 m_data[i] += vector.m_data[i];
321 }
322
323 // Return vector
324 return *this;
325}
326
327
328/***********************************************************************//**
329 * @brief Unary subtraction operator
330 *
331 * @param[in] vector Vector.
332 * @return Vector.
333 *
334 * @exception GException::vector_mismatch
335 * Vectors have not the same size.
336 *
337 * Subtracts vector elements.
338 ***************************************************************************/
340{
341 // Raise exception if vectors mismatch
342 if (m_num != vector.m_num) {
343 std::string msg = "Vector size "+gammalib::str(vector.m_num)+
344 " differs from expected size "+gammalib::str(m_num)+
345 ". Please specify a vector of size "+
346 gammalib::str(m_num)+".";
348 }
349
350 // Subtract elements
351 for (int i = 0; i < m_num; ++i) {
352 m_data[i] -= vector.m_data[i];
353 }
354
355 // Return vector
356 return *this;
357}
358
359
360/***********************************************************************//**
361 * @brief Unary multiplication operator
362 *
363 * @param[in] vector Vector.
364 * @return Vector.
365 *
366 * @exception GException::vector_mismatch
367 * Vectors have not the same size.
368 *
369 * Multiplies vector elements.
370 ***************************************************************************/
372{
373 // Raise exception if vectors mismatch
374 if (m_num != vector.m_num) {
375 std::string msg = "Vector size "+gammalib::str(vector.m_num)+
376 " differs from expected size "+gammalib::str(m_num)+
377 ". Please specify a vector of size "+
378 gammalib::str(m_num)+".";
380 }
381
382 // Multiply elements
383 for (int i = 0; i < m_num; ++i) {
384 m_data[i] *= vector.m_data[i];
385 }
386
387 // Return vector
388 return *this;
389}
390
391
392/***********************************************************************//**
393 * @brief Unary division operator
394 *
395 * @param[in] vector Vector.
396 * @return Vector.
397 *
398 * @exception GException::vector_mismatch
399 * Vectors have not the same size.
400 *
401 * Divide vector elements.
402 *
403 * @todo Handle division by zero errors
404 ***************************************************************************/
406{
407 // Raise exception if vectors mismatch
408 if (m_num != vector.m_num) {
409 std::string msg = "Vector size "+gammalib::str(vector.m_num)+
410 " differs from expected size "+gammalib::str(m_num)+
411 ". Please specify a vector of size "+
412 gammalib::str(m_num)+".";
414 }
415
416 // Divide elements
417 for (int i = 0; i < m_num; ++i) {
418 m_data[i] /= vector.m_data[i];
419 }
420
421 // Return vector
422 return *this;
423}
424
425
426/***********************************************************************//**
427 * @brief Scalar assignment operator
428 *
429 * @param[in] scalar Scalar.
430 * @return Vector.
431 *
432 * Subtracts vector.
433 ***************************************************************************/
434GVector& GVector::operator=(const double& scalar)
435{
436 // Set elements
437 for (int i = 0; i < m_num; ++i) {
438 m_data[i] = scalar;
439 }
440
441 // Return vector
442 return *this;
443}
444
445
446/***********************************************************************//**
447 * @brief Scalar unary addition operator
448 *
449 * @param[in] scalar Scalar.
450 * @return Vector.
451 *
452 * Adds scalar to all vector elements.
453 ***************************************************************************/
454GVector& GVector::operator+=(const double& scalar)
455{
456 // Add scalar to elements
457 for (int i = 0; i < m_num; ++i) {
458 m_data[i] += scalar;
459 }
460
461 // Return vector
462 return *this;
463}
464
465
466/***********************************************************************//**
467 * @brief Scalar unary subtraction operator
468 *
469 * @param[in] scalar Scalar.
470 * @return Vector.
471 *
472 * Subtract scalar to all vector elements.
473 ***************************************************************************/
474GVector& GVector::operator-=(const double& scalar)
475{
476 // Subtract scalar from elements
477 for (int i = 0; i < m_num; ++i) {
478 m_data[i] -= scalar;
479 }
480
481 // Return vector
482 return *this;
483}
484
485
486/***********************************************************************//**
487 * @brief Scalar unary multiplication operator
488 *
489 * @param[in] scalar Scalar.
490 * @return Vector.
491 *
492 * Multiply all vector elements by scalar.
493 ***************************************************************************/
494GVector& GVector::operator*=(const double& scalar)
495{
496 // Multiply all elements
497 for (int i = 0; i < m_num; ++i) {
498 m_data[i] *= scalar;
499 }
500
501 // Return vector
502 return *this;
503}
504
505
506/***********************************************************************//**
507 * @brief Scalar unary division operator
508 *
509 * @param[in] scalar Scalar.
510 * @return Vector.
511 *
512 * Divide all vector elements by scalar.
513 ***************************************************************************/
514GVector& GVector::operator/=(const double& scalar)
515{
516 // Divide all elements
517 for (int i = 0; i < m_num; ++i) {
518 m_data[i] /= scalar;
519 }
520
521 // Return vector
522 return *this;
523}
524
525
526/***********************************************************************//**
527 * @brief Unary minus operator
528 *
529 * @return Vector.
530 *
531 * Negate all vector elements.
532 ***************************************************************************/
534{
535 // Copy vector
536 GVector result = *this;
537
538 // Negate all elements
539 for (int i = 0; i < m_num; ++i) {
540 result.m_data[i] = -result.m_data[i];
541 }
542
543 // Return vector
544 return result;
545}
546
547
548/*==========================================================================
549 = =
550 = Public methods =
551 = =
552 ==========================================================================*/
553
554/***********************************************************************//**
555 * @brief Clear vector
556 ***************************************************************************/
558{
559 // Free members
560 free_members();
561
562 // Initialise private members
563 init_members();
564
565 // Return
566 return;
567}
568
569
570/***********************************************************************//**
571 * @brief Clone vector
572 *
573 * @return Pointer to deep copy of vector.
574 ***************************************************************************/
576{
577 // Clone vector
578 return new GVector(*this);
579}
580
581
582/***********************************************************************//**
583 * @brief Vector element access with range checking
584 *
585 * @param[in] index Element index [0,...,size()[.
586 *
587 * @exception GException::out_of_range
588 * Element index is out of range.
589 ***************************************************************************/
590double& GVector::at(const int& index)
591{
592 // Raise an exception if index is out of range
593 if (index < 0 || index >= size()) {
594 throw GException::out_of_range(G_AT, "Vector element index", index, size());
595 }
596
597 // Return vector element
598 return m_data[index];
599}
600
601
602/***********************************************************************//**
603 * @brief Vector element access with range checking (const variant)
604 *
605 * @param[in] index Element index [0,...,size()[.
606 *
607 * @exception GException::out_of_range
608 * Element index is out of range.
609 ***************************************************************************/
610const double& GVector::at(const int& index) const
611{
612 // Raise an exception if index is out of range
613 if (index < 0 || index >= size()) {
614 throw GException::out_of_range(G_AT, "Vector element index", index, size());
615 }
616
617 // Return vector element
618 return m_data[index];
619}
620
621
622/***********************************************************************//**
623 * @brief Returns number of non-zero elements in vector
624 *
625 * @return Number of non-zero elements in vector.
626 ***************************************************************************/
627int GVector::non_zeros(void) const
628{
629 // Initialise number of non-zeros
630 int non_zeros = 0;
631
632 // Gather all non-zero elements
633 for (int i = 0; i < m_num; ++i) {
634 if (m_data[i] != 0.0) {
635 non_zeros++;
636 }
637 }
638
639 // Return number of non-zero elements
640 return non_zeros;
641}
642
643/***********************************************************************//**
644 * @brief Return the index of the first non-zero element in a vector
645 *
646 * @return Index of first non-zero element in vector.
647 *
648 * Returns the index of the first non-zero element. If all elements are zero
649 * the method will return -1.
650 ***************************************************************************/
652{
653 // Initialise index
654 int first_non_zero = -1;
655
656 // Gather all non-zero elements
657 for (int i = 0; i < m_num; ++i) {
658 if (m_data[i] != 0.0) {
659 first_non_zero = i;
660 break;
661 }
662 }
663
664 // Return number of non-zero elements
665 return first_non_zero;
666}
667
668
669/***********************************************************************//**
670 * @brief Return the index of the last non-zero element in a vector
671 *
672 * @return Index of last non-zero element in vector.
673 *
674 * Returns the index of the last non-zero element. If all elements are zero
675 * the method will return -1.
676 ***************************************************************************/
678{
679 // Initialise index
680 int last_non_zero = -1;
681
682 // Gather all non-zero elements
683 for (int i = m_num-1; i >= 0; --i) {
684 if (m_data[i] != 0.0) {
685 last_non_zero = i;
686 break;
687 }
688 }
689
690 // Return number of non-zero elements
691 return last_non_zero;
692}
693
694
695/***********************************************************************//**
696 * @brief Vector slice operator
697 *
698 * @param[in] start Start index [0,...,size()-1]
699 * @param[in] stop Stop index [0,...,size()-1]
700 * @return Vector made of elements [start,...,stop].
701 ***************************************************************************/
702GVector GVector::slice(const int& start, const int& stop) const
703{
704 // Determine number of elements in vector
705 int num = stop - start;
706 if (num < 0) {
707 num = 0;
708 }
709
710 // Allocate vector
711 GVector vector(num);
712
713 // Build vector slice between original elements a and b (inclusive)
714 for (int i = start; i <= stop; ++i) {
715 vector.m_data[i-start] = m_data[i];
716 }
717
718 // Return sliced vector
719 return vector;
720}
721
722
723/***********************************************************************//**
724 * @brief Print vector information
725 *
726 * @param[in] chatter Chattiness.
727 * @return String containing vector information.
728 ***************************************************************************/
729std::string GVector::print(const GChatter& chatter) const
730{
731 // Initialise result string
732 std::string result = "(";
733
734 // Continue only if chatter is not silent
735 if (chatter != SILENT) {
736
737 // Put all elements in stream
738 for (int i = 0; i < m_num; ++i) {
739 result += gammalib::str((*this)[i]);
740 if (i != m_num-1) {
741 result += ", ";
742 }
743 }
744
745 // Append )
746 result += ")";
747
748 } // endif: chatter was not silent
749
750 // Return result
751 return result;
752}
753
754
755/*==========================================================================
756 = =
757 = Private methods =
758 = =
759 ==========================================================================*/
760
761/***********************************************************************//**
762 * @brief Initialise class members
763 ***************************************************************************/
765{
766 // Initialise members
767 m_num = 0;
768 m_data = NULL;
769
770 // Return
771 return;
772}
773
774
775/***********************************************************************//**
776 * @brief Allocate vector
777 ***************************************************************************/
779{
780 // Continue only if vector has non-zero size
781 if (m_num > 0) {
782
783 // Allocate vector
784 m_data = new double[m_num];
785
786 // Initialize elements to 0
787 for (int i = 0; i < m_num; ++i) {
788 m_data[i] = 0.0;
789 }
790
791 } // endif: vector had non-zero size
792
793 // Return
794 return;
795}
796
797
798/***********************************************************************//**
799 * @brief Copy class members
800 *
801 * @param[in] vector Vector from which members should be copied.
802 ***************************************************************************/
804{
805 // Copy attributes
806 m_num = vector.m_num;
807
808 // Continue only if vector has non-zero size
809 if (m_num > 0) {
810
811 // Allocate vector
812 m_data = new double[m_num];
813
814 // Copy elements
815 for (int i = 0; i < m_num; ++i) {
816 m_data[i] = vector.m_data[i];
817 }
818
819 } // endif: vector had non-zero size
820
821 // Return
822 return;
823}
824
825
826/***********************************************************************//**
827 * @brief Delete class members
828 ***************************************************************************/
830{
831 // Free memory
832 if (m_data != NULL) delete[] m_data;
833
834 // Signal free pointers
835 m_data = NULL;
836
837 // Return
838 return;
839}
840
841
842/*==========================================================================
843 = =
844 = Friends =
845 = =
846 ==========================================================================*/
847
848/***********************************************************************//**
849 * @brief Vector cross product
850 *
851 * @param[in] a Vector.
852 * @param[in] b Vector.
853 * @return Vector cross product.
854 *
855 * @exception GException::invalid_argument
856 * Mismatch between vector size or vector not of size 3.
857 *
858 * Computes the cross product between two 3-element vectors (note that the
859 * cross product is only defined for 3-element vectors).
860 ***************************************************************************/
861GVector cross(const GVector& a, const GVector& b)
862{
863 // Verify that vectors have same dimensions
864 if (a.m_num != b.m_num) {
865 std::string msg = "Size "+gammalib::str(a.m_num)+" of first vector "
866 "differs from size "+gammalib::str(b.m_num)+" of "
867 "second vector. Please specify vectors of identical "
868 "size.";
870 }
871
872 // Verify that vectors have 3 elements
873 if (a.m_num != 3) {
874 std::string msg = "Vector size "+gammalib::str(a.m_num)+" is not 3. "
875 "The vector cross product is only defined for "
876 "vectors of size 3. Please specify vectors of "
877 "size 3.";
879 }
880
881 // Compute cross product
882 GVector result(3);
883 result.m_data[0] = a.m_data[1]*b.m_data[2] - a.m_data[2]*b.m_data[1];
884 result.m_data[1] = a.m_data[2]*b.m_data[0] - a.m_data[0]*b.m_data[2];
885 result.m_data[2] = a.m_data[0]*b.m_data[1] - a.m_data[1]*b.m_data[0];
886
887 // Return result
888 return result;
889}
890
891
892/***********************************************************************//**
893 * @brief Vector scalar product
894 *
895 * @param[in] a Vector.
896 * @param[in] b Vector.
897 * @return Product between vector @p a and @p b.
898 *
899 * @exception GException::invalid_argument
900 * Mismatch between vector size.
901 *
902 * Returns the scalar product between vector @p a and @p b.
903 ***************************************************************************/
904double operator*(const GVector& a, const GVector& b)
905{
906 // Verify that vectors have same size
907 if (a.m_num != b.m_num) {
908 std::string msg = "Size "+gammalib::str(a.m_num)+" of first vector "
909 "differs from size "+gammalib::str(b.m_num)+" of "
910 "second vector. Please specify vectors of identical "
911 "size.";
913 }
914
915 // Compute scalar product
916 double result = 0.0;
917 for (int i = 0; i < a.m_num; ++i) {
918 result += (a.m_data[i] * b.m_data[i]);
919 }
920
921 // Return scalar product
922 return result;
923}
924
925
926/***********************************************************************//**
927 * @brief Computes vector norm
928 *
929 * @param[in] vector Vector.
930 * @return Vector norm.
931 ***************************************************************************/
932double norm(const GVector& vector)
933{
934 // Initialises result
935 double result = 0.0;
936
937 // Computes norm
938 for (int i = 0; i < vector.m_num; ++i) {
939 result += (vector.m_data[i] * vector.m_data[i]);
940 }
941 result = (result > 0.0) ? std::sqrt(result) : 0.0;
942
943 // Returns norm
944 return result;
945}
946
947
948/***********************************************************************//**
949 * @brief Computes minimum vector element
950 *
951 * @param[in] vector Vector.
952 * @return Minimum vector element.
953 ***************************************************************************/
954double min(const GVector& vector)
955{
956 // Initialises result
957 double result = 0.0;
958
959 // Continue only if we have elements
960 if (vector.m_num > 0) {
961
962 // Search for minimum
963 result = vector.m_data[0];
964 for (int i = 1; i < vector.m_num; ++i) {
965 if (vector.m_data[i] < result) {
966 result = vector.m_data[i];
967 }
968 }
969
970 } // endif: there were elements
971
972 // Returns minimum
973 return result;
974}
975
976
977/***********************************************************************//**
978 * @brief Computes maximum vector element
979 *
980 * @param[in] vector Vector.
981 * @return Maximum vector element.
982 ***************************************************************************/
983double max(const GVector& vector)
984{
985 // Initialises result
986 double result = 0.0;
987
988 // Continue only if we have elements
989 if (vector.m_num > 0) {
990
991 // Search for maximum
992 result = vector.m_data[0];
993 for (int i = 1; i < vector.m_num; ++i) {
994 if (vector.m_data[i] > result) {
995 result = vector.m_data[i];
996 }
997 }
998
999 } // endif: there were elements
1000
1001 // Returns maximum
1002 return result;
1003}
1004
1005
1006/***********************************************************************//**
1007 * @brief Computes vector sum
1008 *
1009 * @param[in] vector Vector.
1010 * @return Sum of vector elements.
1011 ***************************************************************************/
1012double sum(const GVector& vector)
1013{
1014 // Compute sum
1015 double result = 0.0;
1016 for (int i = 0; i < vector.m_num; ++i) {
1017 result += vector.m_data[i];
1018 }
1019
1020 // Returns sum
1021 return result;
1022}
1023
1024
1025/***********************************************************************//**
1026 * @brief Computes angle between vectors
1027 *
1028 * @param[in] a Vector.
1029 * @param[in] b Vector.
1030 * @return Angle between vector @p a and @p b in radians.
1031 *
1032 * @exception GException::invalid_argument
1033 * Mismatch between vector size.
1034 *
1035 * Returns the angle \f$\alpha\f$ between vector @p a and @p b in radians.
1036 * The computation is done using
1037 *
1038 * \f[
1039 * \alpha = \arccos{\frac{\vec{a} \cdot \vec{b}}{|\vec{a}||\vec{b}|}}
1040 * \f]
1041 ***************************************************************************/
1042double angle(const GVector& a, const GVector& b)
1043{
1044 // Verify that vectors have same size
1045 if (a.m_num != b.m_num) {
1046 std::string msg = "Size "+gammalib::str(a.m_num)+" of first vector "
1047 "differs from size "+gammalib::str(b.m_num)+" of "
1048 "second vector. Please specify vectors of identical "
1049 "size.";
1051 }
1052
1053 // Compute angle
1054 double angle = 0.0;
1055 double denom = norm(a) * norm(b);
1056 if (denom != 0.0) {
1057 double arg = (a * b) / denom;
1058 if (arg < -1.0) {
1059 arg = -1.0;
1060 }
1061 if (arg > 1.0) {
1062 arg = 1.0;
1063 }
1064 angle = std::acos(arg);
1065 }
1066
1067 // Return angle
1068 return angle;
1069}
1070
1071
1072/***********************************************************************//**
1073 * @brief Computes vector permutation
1074 *
1075 * @param[in] vector Vector.
1076 * @param[in] p Permutation array.
1077 * @return Permuted vector.
1078 ***************************************************************************/
1079GVector perm(const GVector& vector, const int* p)
1080{
1081 // Initialise result vector
1082 GVector result(vector.m_num);
1083
1084 // Compute permutations
1085 if (p != NULL) {
1086 for (int i = 0; i < vector.m_num; ++i) {
1087 result.m_data[i] = vector.m_data[p[i]];
1088 }
1089 }
1090 else {
1091 result = vector;
1092 }
1093
1094 // Return vector
1095 return result;
1096}
1097
1098
1099/***********************************************************************//**
1100 * @brief Computes vector inverse permutation
1101 *
1102 * @param[in] vector Vector.
1103 * @param[in] p Permutation array.
1104 * @return Inversely permuted vector.
1105 ***************************************************************************/
1106GVector iperm(const GVector& vector, const int* p)
1107{
1108 // Initialise result vector
1109 GVector result(vector.m_num);
1110
1111 // Compute permutations
1112 if (p != NULL) {
1113 for (int i = 0; i < vector.m_num; ++i) {
1114 result.m_data[p[i]] = vector.m_data[i];
1115 }
1116 }
1117 else {
1118 result = vector;
1119 }
1120
1121 // Return vector
1122 return result;
1123}
1124
1125
1126/***********************************************************************//**
1127 * @brief Computes arccos of vector elements
1128 *
1129 * @param[in] vector Vector.
1130 * @return Vector containing the arccos of every element.
1131 ***************************************************************************/
1132GVector acos(const GVector& vector)
1133{
1134 // Initialise result vector
1135 GVector result(vector.m_num);
1136
1137 // Evaluate each vector element
1138 for (int i = 0; i < vector.m_num; ++i) {
1139 result.m_data[i] = std::acos(vector.m_data[i]);
1140 }
1141
1142 // Return vector
1143 return result;
1144}
1145
1146
1147/***********************************************************************//**
1148 * @brief Computes acosh of vector elements
1149 *
1150 * @param[in] vector Vector.
1151 * @return Vector containing the acosh of every element.
1152 ***************************************************************************/
1153GVector acosh(const GVector& vector)
1154{
1155 // Initialise result vector
1156 GVector result(vector.m_num);
1157
1158 // Evaluate each vector element
1159 for (int i = 0; i < vector.m_num; ++i) {
1160 result.m_data[i] = acosh(vector.m_data[i]);
1161 }
1162
1163 // Return vector
1164 return result;
1165}
1166
1167
1168/***********************************************************************//**
1169 * @brief Computes arcsin of vector elements
1170 *
1171 * @param[in] vector Vector.
1172 * @return Vector containing the arcsin of every element.
1173 ***************************************************************************/
1174GVector asin(const GVector& vector)
1175{
1176 // Initialise result vector
1177 GVector result(vector.m_num);
1178
1179 // Evaluate each vector element
1180 for (int i = 0; i < vector.m_num; ++i) {
1181 result.m_data[i] = std::asin(vector.m_data[i]);
1182 }
1183
1184 // Return vector
1185 return result;
1186}
1187
1188
1189/***********************************************************************//**
1190 * @brief Computes asinh of vector elements
1191 *
1192 * @param[in] vector Vector.
1193 * @return Vector containing the asinh of every element.
1194 ***************************************************************************/
1195GVector asinh(const GVector& vector)
1196{
1197 // Initialise result vector
1198 GVector result(vector.m_num);
1199
1200 // Evaluate each vector element
1201 for (int i = 0; i < vector.m_num; ++i) {
1202 result.m_data[i] = asinh(vector.m_data[i]);
1203 }
1204
1205 // Return vector
1206 return result;
1207}
1208
1209
1210/***********************************************************************//**
1211 * @brief Computes arctan of vector elements
1212 *
1213 * @param[in] vector Vector.
1214 * @return Vector containing the arctan of every element.
1215 ***************************************************************************/
1216GVector atan(const GVector& vector)
1217{
1218 // Initialise result vector
1219 GVector result(vector.m_num);
1220
1221 // Evaluate each vector element
1222 for (int i = 0; i < vector.m_num; ++i) {
1223 result.m_data[i] = std::atan(vector.m_data[i]);
1224 }
1225
1226 // Return vector
1227 return result;
1228}
1229
1230
1231/***********************************************************************//**
1232 * @brief Computes atanh of vector elements
1233 *
1234 * @param[in] vector Vector.
1235 * @return Vector containing the atanh of every element.
1236 ***************************************************************************/
1237GVector atanh(const GVector& vector)
1238{
1239 // Initialise result vector
1240 GVector result(vector.m_num);
1241
1242 // Evaluate each vector element
1243 for (int i = 0; i < vector.m_num; ++i) {
1244 result.m_data[i] = atanh(vector.m_data[i]);
1245 }
1246
1247 // Return vector
1248 return result;
1249}
1250
1251
1252/***********************************************************************//**
1253 * @brief Computes cosine of vector elements
1254 *
1255 * @param[in] vector Vector.
1256 * @return Vector containing the cosine of every element.
1257 ***************************************************************************/
1258GVector cos(const GVector& vector)
1259{
1260 // Initialise result vector
1261 GVector result(vector.m_num);
1262
1263 // Evaluate each vector element
1264 for (int i = 0; i < vector.m_num; ++i) {
1265 result.m_data[i] = std::cos(vector.m_data[i]);
1266 }
1267
1268 // Return vector
1269 return result;
1270}
1271
1272
1273/***********************************************************************//**
1274 * @brief Computes cosh of vector elements
1275 *
1276 * @param[in] vector Vector.
1277 * @return Vector containing the cosh of every element.
1278 ***************************************************************************/
1279GVector cosh(const GVector& vector)
1280{
1281 // Initialise result vector
1282 GVector result(vector.m_num);
1283
1284 // Evaluate each vector element
1285 for (int i = 0; i < vector.m_num; ++i) {
1286 result.m_data[i] = std::cosh(vector.m_data[i]);
1287 }
1288
1289 // Return vector
1290 return result;
1291}
1292
1293
1294/***********************************************************************//**
1295 * @brief Computes exponential of vector elements
1296 *
1297 * @param[in] vector Vector.
1298 * @return Vector containing the exponential of every element.
1299 ***************************************************************************/
1300GVector exp(const GVector& vector)
1301{
1302 // Initialise result vector
1303 GVector result(vector.m_num);
1304
1305 // Evaluate each vector element
1306 for (int i = 0; i < vector.m_num; ++i) {
1307 result.m_data[i] = std::exp(vector.m_data[i]);
1308 }
1309
1310 // Return vector
1311 return result;
1312}
1313
1314
1315/***********************************************************************//**
1316 * @brief Computes absolute of vector elements
1317 *
1318 * @param[in] vector Vector.
1319 * @return Vector containing the absolute of every element.
1320 ***************************************************************************/
1321GVector abs(const GVector& vector)
1322{
1323 // Initialise result vector
1324 GVector result(vector.m_num);
1325
1326 // Evaluate each vector element
1327 for (int i = 0; i < vector.m_num; ++i) {
1328 result.m_data[i] = std::abs(vector.m_data[i]);
1329 }
1330
1331 // Return vector
1332 return result;
1333}
1334
1335
1336/***********************************************************************//**
1337 * @brief Computes natural logarithm of vector elements
1338 *
1339 * @param[in] vector Vector.
1340 * @return Vector containing the natural logarithm of every element.
1341 ***************************************************************************/
1342GVector log(const GVector& vector)
1343{
1344 // Initialise result vector
1345 GVector result(vector.m_num);
1346
1347 // Evaluate each vector element
1348 for (int i = 0; i < vector.m_num; ++i) {
1349 result.m_data[i] = std::log(vector.m_data[i]);
1350 }
1351
1352 // Return vector
1353 return result;
1354}
1355
1356
1357/***********************************************************************//**
1358 * @brief Computes base10 logarithm of vector elements
1359 *
1360 * @param[in] vector Vector.
1361 * @return Vector containing the base10 logarithm of every element.
1362 ***************************************************************************/
1363GVector log10(const GVector& vector)
1364{
1365 // Initialise result vector
1366 GVector result(vector.m_num);
1367
1368 // Evaluate each vector element
1369 for (int i = 0; i < vector.m_num; ++i) {
1370 result.m_data[i] = std::log10(vector.m_data[i]);
1371 }
1372
1373 // Return vector
1374 return result;
1375}
1376
1377
1378/***********************************************************************//**
1379 * @brief Computes sine of vector elements
1380 *
1381 * @param[in] vector Vector.
1382 * @return Vector containing the sine of every element.
1383 ***************************************************************************/
1384GVector sin(const GVector& vector)
1385{
1386 // Initialise result vector
1387 GVector result(vector.m_num);
1388
1389 // Evaluate each vector element
1390 for (int i = 0; i < vector.m_num; ++i) {
1391 result.m_data[i] = std::sin(vector.m_data[i]);
1392 }
1393
1394 // Return vector
1395 return result;
1396}
1397
1398
1399/***********************************************************************//**
1400 * @brief Computes sinh of vector elements
1401 *
1402 * @param[in] vector Vector.
1403 * @return Vector containing the sinh of every element.
1404 ***************************************************************************/
1405GVector sinh(const GVector& vector)
1406{
1407 // Initialise result vector
1408 GVector result(vector.m_num);
1409
1410 // Evaluate each vector element
1411 for (int i = 0; i < vector.m_num; ++i) {
1412 result.m_data[i] = std::sinh(vector.m_data[i]);
1413 }
1414
1415 // Return vector
1416 return result;
1417}
1418
1419
1420/***********************************************************************//**
1421 * @brief Computes square root of vector elements
1422 *
1423 * @param[in] vector Vector.
1424 * @return Vector containing the square root of every element.
1425 ***************************************************************************/
1426GVector sqrt(const GVector& vector)
1427{
1428 // Initialise result vector
1429 GVector result(vector.m_num);
1430
1431 // Evaluate each vector element
1432 for (int i = 0; i < vector.m_num; ++i) {
1433 result.m_data[i] = std::sqrt(vector.m_data[i]);
1434 }
1435
1436 // Return vector
1437 return result;
1438}
1439
1440
1441/***********************************************************************//**
1442 * @brief Computes tangens of vector elements
1443 *
1444 * @param[in] vector Vector.
1445 * @return Vector containing the tangens of every element.
1446 ***************************************************************************/
1447GVector tan(const GVector& vector)
1448{
1449 // Initialise result vector
1450 GVector result(vector.m_num);
1451
1452 // Evaluate each vector element
1453 for (int i = 0; i < vector.m_num; ++i) {
1454 result.m_data[i] = std::tan(vector.m_data[i]);
1455 }
1456
1457 // Return vector
1458 return result;
1459}
1460
1461
1462/***********************************************************************//**
1463 * @brief Computes tanh of vector elements
1464 *
1465 * @param[in] vector Vector.
1466 * @return Vector containing the tanh of every element.
1467 ***************************************************************************/
1468GVector tanh(const GVector& vector)
1469{
1470 // Initialise result vector
1471 GVector result(vector.m_num);
1472
1473 // Evaluate each vector element
1474 for (int i = 0; i < vector.m_num; ++i) {
1475 result.m_data[i] = std::tanh(vector.m_data[i]);
1476 }
1477
1478 // Return vector
1479 return result;
1480}
1481
1482
1483/***********************************************************************//**
1484 * @brief Computes tanh of vector elements
1485 *
1486 * @param[in] vector Vector.
1487 * @param[in] power Power.
1488 * @return Vector containing the power of every element.
1489 ***************************************************************************/
1490GVector pow(const GVector& vector, const double& power)
1491{
1492 // Initialise result vector
1493 GVector result(vector.m_num);
1494
1495 // Evaluate each vector element
1496 for (int i = 0; i < vector.m_num; ++i) {
1497 result.m_data[i] = std::pow(vector.m_data[i], power);
1498 }
1499
1500 // Return vector
1501 return result;
1502}
#define G_AT
#define G_CONSTRUCTOR
Definition GMatrix.cpp:41
#define G_OP_ADD
Definition GMatrix.cpp:43
#define G_OP_SUB
Definition GMatrix.cpp:44
Gammalib tools definition.
GChatter
Definition GTypemaps.hpp:33
@ SILENT
Definition GTypemaps.hpp:34
GVector cos(const GVector &vector)
Computes cosine of vector elements.
Definition GVector.cpp:1258
#define G_OP_DIV
Definition GVector.cpp:40
GVector cosh(const GVector &vector)
Computes cosh of vector elements.
Definition GVector.cpp:1279
double norm(const GVector &vector)
Computes vector norm.
Definition GVector.cpp:932
GVector log(const GVector &vector)
Computes natural logarithm of vector elements.
Definition GVector.cpp:1342
GVector abs(const GVector &vector)
Computes absolute of vector elements.
Definition GVector.cpp:1321
double angle(const GVector &a, const GVector &b)
Computes angle between vectors.
Definition GVector.cpp:1042
double min(const GVector &vector)
Computes minimum vector element.
Definition GVector.cpp:954
GVector pow(const GVector &vector, const double &power)
Computes tanh of vector elements.
Definition GVector.cpp:1490
double operator*(const GVector &a, const GVector &b)
Vector scalar product.
Definition GVector.cpp:904
GVector iperm(const GVector &vector, const int *p)
Computes vector inverse permutation.
Definition GVector.cpp:1106
GVector tan(const GVector &vector)
Computes tangens of vector elements.
Definition GVector.cpp:1447
GVector asinh(const GVector &vector)
Computes asinh of vector elements.
Definition GVector.cpp:1195
#define G_ANGLE
Definition GVector.cpp:44
GVector acos(const GVector &vector)
Computes arccos of vector elements.
Definition GVector.cpp:1132
GVector perm(const GVector &vector, const int *p)
Computes vector permutation.
Definition GVector.cpp:1079
GVector exp(const GVector &vector)
Computes exponential of vector elements.
Definition GVector.cpp:1300
GVector atanh(const GVector &vector)
Computes atanh of vector elements.
Definition GVector.cpp:1237
GVector tanh(const GVector &vector)
Computes tanh of vector elements.
Definition GVector.cpp:1468
GVector sin(const GVector &vector)
Computes sine of vector elements.
Definition GVector.cpp:1384
GVector log10(const GVector &vector)
Computes base10 logarithm of vector elements.
Definition GVector.cpp:1363
double sum(const GVector &vector)
Computes vector sum.
Definition GVector.cpp:1012
GVector sqrt(const GVector &vector)
Computes square root of vector elements.
Definition GVector.cpp:1426
GVector atan(const GVector &vector)
Computes arctan of vector elements.
Definition GVector.cpp:1216
#define G_CROSS
Definition GVector.cpp:42
double max(const GVector &vector)
Computes maximum vector element.
Definition GVector.cpp:983
GVector asin(const GVector &vector)
Computes arcsin of vector elements.
Definition GVector.cpp:1174
GVector sinh(const GVector &vector)
Computes sinh of vector elements.
Definition GVector.cpp:1405
GVector acosh(const GVector &vector)
Computes acosh of vector elements.
Definition GVector.cpp:1153
#define G_SCALAR
Definition GVector.cpp:43
GVector cross(const GVector &a, const GVector &b)
Vector cross product.
Definition GVector.cpp:861
#define G_OP_MULT
Definition GVector.cpp:39
Vector class interface definition.
Vector class.
Definition GVector.hpp:46
bool operator==(const GVector &vector) const
Equality operator.
Definition GVector.cpp:260
void alloc_members(void)
Allocate vector.
Definition GVector.cpp:778
std::string print(const GChatter &chatter=NORMAL) const
Print vector information.
Definition GVector.cpp:729
GVector & operator+=(const GVector &vector)
Unary addition operator.
Definition GVector.cpp:307
void copy_members(const GVector &vector)
Copy class members.
Definition GVector.cpp:803
GVector slice(const int &start, const int &stop) const
Vector slice operator.
Definition GVector.cpp:702
int last_nonzero(void) const
Return the index of the last non-zero element in a vector.
Definition GVector.cpp:677
GVector & operator/=(const GVector &vector)
Unary division operator.
Definition GVector.cpp:405
GVector & operator-=(const GVector &vector)
Unary subtraction operator.
Definition GVector.cpp:339
void clear(void)
Clear vector.
Definition GVector.cpp:557
void init_members(void)
Initialise class members.
Definition GVector.cpp:764
const int & size(void) const
Return size of vector.
Definition GVector.hpp:180
GVector * clone(void) const
Clone vector.
Definition GVector.cpp:575
GVector operator-(void) const
Unary minus operator.
Definition GVector.cpp:533
double * m_data
Vector array.
Definition GVector.hpp:128
int m_num
Number of elements in vector.
Definition GVector.hpp:127
bool operator!=(const GVector &vector) const
Non-equality operator.
Definition GVector.cpp:286
int first_nonzero(void) const
Return the index of the first non-zero element in a vector.
Definition GVector.cpp:651
GVector & operator=(const GVector &vector)
Assignment operator.
Definition GVector.cpp:230
virtual ~GVector(void)
Destructor.
Definition GVector.cpp:208
int non_zeros(void) const
Returns number of non-zero elements in vector.
Definition GVector.cpp:627
GVector & operator*=(const GVector &vector)
Unary multiplication operator.
Definition GVector.cpp:371
void free_members(void)
Delete class members.
Definition GVector.cpp:829
double & at(const int &index)
Vector element access with range checking.
Definition GVector.cpp:590
GVector(void)
Void vector constructor.
Definition GVector.cpp:56
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition GTools.cpp:508