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