GammaLib  1.7.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-2014 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 and initialize elements to 0
692  m_data = new double[m_num];
693  for (int i = 0; i < m_num; ++i) {
694  m_data[i] = 0.0;
695  }
696 
697  } // endif: vector had non-zero length
698 
699  // Return
700  return;
701 }
702 
703 
704 /***********************************************************************//**
705  * @brief Copy class members
706  *
707  * @param[in] vector Vector from which members should be copied.
708  ***************************************************************************/
709 void GVector::copy_members(const GVector& vector)
710 {
711  // Copy attributes
712  m_num = vector.m_num;
713 
714  // Copy elements
715  if (m_num > 0) {
716  alloc_members();
717  for (int i = 0; i < m_num; ++i) {
718  m_data[i] = vector.m_data[i];
719  }
720  }
721 
722  // Return
723  return;
724 }
725 
726 
727 /***********************************************************************//**
728  * @brief Delete class members
729  ***************************************************************************/
731 {
732  // Free memory
733  if (m_data != NULL) delete[] m_data;
734 
735  // Signal free pointers
736  m_data = NULL;
737 
738  // Return
739  return;
740 }
741 
742 
743 /*==========================================================================
744  = =
745  = Friends =
746  = =
747  ==========================================================================*/
748 
749 /***********************************************************************//**
750  * @brief Vector cross product
751  *
752  * @param[in] a Vector.
753  * @param[in] b Vector.
754  * @return Vector cross product.
755  *
756  * @exception GException::vector_mismatch
757  * Mismatch between vector size.
758  *
759  * Computes the cross product between two 3-element vectors (note that the
760  * cross product is only defined for 3-element vectors).
761  ***************************************************************************/
762 GVector cross(const GVector& a, const GVector& b)
763 {
764  // Verify that vectors have same dimensions
765  if (a.m_num != b.m_num) {
767  }
768 
769  // Verify that vectors have 3 elements
770  if (a.m_num != 3) {
772  }
773 
774  // Compute cross product
775  GVector result(3);
776  result.m_data[0] = a.m_data[1]*b.m_data[2] - a.m_data[2]*b.m_data[1];
777  result.m_data[1] = a.m_data[2]*b.m_data[0] - a.m_data[0]*b.m_data[2];
778  result.m_data[2] = a.m_data[0]*b.m_data[1] - a.m_data[1]*b.m_data[0];
779 
780  // Return result
781  return result;
782 }
783 
784 
785 /***********************************************************************//**
786  * @brief Vector scalar product
787  *
788  * @param[in] a Vector.
789  * @param[in] b Vector.
790  * @return Product between vector @p a and @p b.
791  *
792  * @exception GException::vector_mismatch
793  * Mismatch between vector size.
794  *
795  * Returns the scalar product between vector @p a and @p b.
796  ***************************************************************************/
797 double operator*(const GVector& a, const GVector& b)
798 {
799  // Verify that vectors have same dimensions
800  if (a.m_num != b.m_num) {
802  }
803 
804  // Compute scalar product
805  double result = 0.0;
806  for (int i = 0; i < a.m_num; ++i) {
807  result += (a.m_data[i] * b.m_data[i]);
808  }
809 
810  // Return scalar product
811  return result;
812 }
813 
814 
815 /***********************************************************************//**
816  * @brief Computes vector norm
817  *
818  * @param[in] vector Vector.
819  * @return Vector norm.
820  ***************************************************************************/
821 double norm(const GVector& vector)
822 {
823  // Initialises result
824  double result = 0.0;
825 
826  // Computes norm
827  for (int i = 0; i < vector.m_num; ++i) {
828  result += (vector.m_data[i] * vector.m_data[i]);
829  }
830  result = (result > 0.0) ? std::sqrt(result) : 0.0;
831 
832  // Returns norm
833  return result;
834 }
835 
836 
837 /***********************************************************************//**
838  * @brief Computes minimum vector element
839  *
840  * @param[in] vector Vector.
841  * @return Minimum vector element.
842  ***************************************************************************/
843 double min(const GVector& vector)
844 {
845  // Initialises result
846  double result = 0.0;
847 
848  // Continue only if we have elements
849  if (vector.m_num > 0) {
850 
851  // Search for minimum
852  result = vector.m_data[0];
853  for (int i = 1; i < vector.m_num; ++i) {
854  if (vector.m_data[i] < result) {
855  result = vector.m_data[i];
856  }
857  }
858 
859  } // endif: there were elements
860 
861  // Returns minimum
862  return result;
863 }
864 
865 
866 /***********************************************************************//**
867  * @brief Computes maximum vector element
868  *
869  * @param[in] vector Vector.
870  * @return Maximum vector element.
871  ***************************************************************************/
872 double max(const GVector& vector)
873 {
874  // Initialises result
875  double result = 0.0;
876 
877  // Continue only if we have elements
878  if (vector.m_num > 0) {
879 
880  // Search for maximum
881  result = vector.m_data[0];
882  for (int i = 1; i < vector.m_num; ++i) {
883  if (vector.m_data[i] > result) {
884  result = vector.m_data[i];
885  }
886  }
887 
888  } // endif: there were elements
889 
890  // Returns maximum
891  return result;
892 }
893 
894 
895 /***********************************************************************//**
896  * @brief Computes vector sum
897  *
898  * @param[in] vector Vector.
899  * @return Sum of vector elements.
900  ***************************************************************************/
901 double sum(const GVector& vector)
902 {
903  // Compute sum
904  double result = 0.0;
905  for (int i = 0; i < vector.m_num; ++i) {
906  result += vector.m_data[i];
907  }
908 
909  // Returns sum
910  return result;
911 }
912 
913 
914 /***********************************************************************//**
915  * @brief Computes vector permutation
916  *
917  * @param[in] vector Vector.
918  * @param[in] p Permutation array.
919  * @return Permuted vector.
920  ***************************************************************************/
921 GVector perm(const GVector& vector, const int* p)
922 {
923  // Initialise result vector
924  GVector result(vector.m_num);
925 
926  // Compute permutations
927  if (p != NULL) {
928  for (int i = 0; i < vector.m_num; ++i) {
929  result.m_data[i] = vector.m_data[p[i]];
930  }
931  }
932  else {
933  result = vector;
934  }
935 
936  // Return vector
937  return result;
938 }
939 
940 
941 /***********************************************************************//**
942  * @brief Computes vector inverse permutation
943  *
944  * @param[in] vector Vector.
945  * @param[in] p Permutation array.
946  * @return Inversely permuted vector.
947  ***************************************************************************/
948 GVector iperm(const GVector& vector, const int* p)
949 {
950  // Initialise result vector
951  GVector result(vector.m_num);
952 
953  // Compute permutations
954  if (p != NULL) {
955  for (int i = 0; i < vector.m_num; ++i) {
956  result.m_data[p[i]] = vector.m_data[i];
957  }
958  }
959  else {
960  result = vector;
961  }
962 
963  // Return vector
964  return result;
965 }
966 
967 
968 /***********************************************************************//**
969  * @brief Computes arccos of vector elements
970  *
971  * @param[in] vector Vector.
972  * @return Vector containing the arccos of every element.
973  ***************************************************************************/
974 GVector acos(const GVector& vector)
975 {
976  // Initialise result vector
977  GVector result(vector.m_num);
978 
979  // Evaluate each vector element
980  for (int i = 0; i < vector.m_num; ++i) {
981  result.m_data[i] = std::acos(vector.m_data[i]);
982  }
983 
984  // Return vector
985  return result;
986 }
987 
988 
989 /***********************************************************************//**
990  * @brief Computes acosh of vector elements
991  *
992  * @param[in] vector Vector.
993  * @return Vector containing the acosh of every element.
994  ***************************************************************************/
995 GVector acosh(const GVector& vector)
996 {
997  // Initialise result vector
998  GVector result(vector.m_num);
999 
1000  // Evaluate each vector element
1001  for (int i = 0; i < vector.m_num; ++i) {
1002  result.m_data[i] = acosh(vector.m_data[i]);
1003  }
1004 
1005  // Return vector
1006  return result;
1007 }
1008 
1009 
1010 /***********************************************************************//**
1011  * @brief Computes arcsin of vector elements
1012  *
1013  * @param[in] vector Vector.
1014  * @return Vector containing the arcsin of every element.
1015  ***************************************************************************/
1016 GVector asin(const GVector& vector)
1017 {
1018  // Initialise result vector
1019  GVector result(vector.m_num);
1020 
1021  // Evaluate each vector element
1022  for (int i = 0; i < vector.m_num; ++i) {
1023  result.m_data[i] = std::asin(vector.m_data[i]);
1024  }
1025 
1026  // Return vector
1027  return result;
1028 }
1029 
1030 
1031 /***********************************************************************//**
1032  * @brief Computes asinh of vector elements
1033  *
1034  * @param[in] vector Vector.
1035  * @return Vector containing the asinh of every element.
1036  ***************************************************************************/
1037 GVector asinh(const GVector& vector)
1038 {
1039  // Initialise result vector
1040  GVector result(vector.m_num);
1041 
1042  // Evaluate each vector element
1043  for (int i = 0; i < vector.m_num; ++i) {
1044  result.m_data[i] = asinh(vector.m_data[i]);
1045  }
1046 
1047  // Return vector
1048  return result;
1049 }
1050 
1051 
1052 /***********************************************************************//**
1053  * @brief Computes arctan of vector elements
1054  *
1055  * @param[in] vector Vector.
1056  * @return Vector containing the arctan of every element.
1057  ***************************************************************************/
1058 GVector atan(const GVector& vector)
1059 {
1060  // Initialise result vector
1061  GVector result(vector.m_num);
1062 
1063  // Evaluate each vector element
1064  for (int i = 0; i < vector.m_num; ++i) {
1065  result.m_data[i] = std::atan(vector.m_data[i]);
1066  }
1067 
1068  // Return vector
1069  return result;
1070 }
1071 
1072 
1073 /***********************************************************************//**
1074  * @brief Computes atanh of vector elements
1075  *
1076  * @param[in] vector Vector.
1077  * @return Vector containing the atanh of every element.
1078  ***************************************************************************/
1079 GVector atanh(const GVector& vector)
1080 {
1081  // Initialise result vector
1082  GVector result(vector.m_num);
1083 
1084  // Evaluate each vector element
1085  for (int i = 0; i < vector.m_num; ++i) {
1086  result.m_data[i] = atanh(vector.m_data[i]);
1087  }
1088 
1089  // Return vector
1090  return result;
1091 }
1092 
1093 
1094 /***********************************************************************//**
1095  * @brief Computes cosine of vector elements
1096  *
1097  * @param[in] vector Vector.
1098  * @return Vector containing the cosine of every element.
1099  ***************************************************************************/
1100 GVector cos(const GVector& vector)
1101 {
1102  // Initialise result vector
1103  GVector result(vector.m_num);
1104 
1105  // Evaluate each vector element
1106  for (int i = 0; i < vector.m_num; ++i) {
1107  result.m_data[i] = std::cos(vector.m_data[i]);
1108  }
1109 
1110  // Return vector
1111  return result;
1112 }
1113 
1114 
1115 /***********************************************************************//**
1116  * @brief Computes cosh of vector elements
1117  *
1118  * @param[in] vector Vector.
1119  * @return Vector containing the cosh of every element.
1120  ***************************************************************************/
1121 GVector cosh(const GVector& vector)
1122 {
1123  // Initialise result vector
1124  GVector result(vector.m_num);
1125 
1126  // Evaluate each vector element
1127  for (int i = 0; i < vector.m_num; ++i) {
1128  result.m_data[i] = std::cosh(vector.m_data[i]);
1129  }
1130 
1131  // Return vector
1132  return result;
1133 }
1134 
1135 
1136 /***********************************************************************//**
1137  * @brief Computes exponential of vector elements
1138  *
1139  * @param[in] vector Vector.
1140  * @return Vector containing the exponential of every element.
1141  ***************************************************************************/
1142 GVector exp(const GVector& vector)
1143 {
1144  // Initialise result vector
1145  GVector result(vector.m_num);
1146 
1147  // Evaluate each vector element
1148  for (int i = 0; i < vector.m_num; ++i) {
1149  result.m_data[i] = std::exp(vector.m_data[i]);
1150  }
1151 
1152  // Return vector
1153  return result;
1154 }
1155 
1156 
1157 /***********************************************************************//**
1158  * @brief Computes absolute of vector elements
1159  *
1160  * @param[in] vector Vector.
1161  * @return Vector containing the absolute of every element.
1162  ***************************************************************************/
1163 GVector abs(const GVector& vector)
1164 {
1165  // Initialise result vector
1166  GVector result(vector.m_num);
1167 
1168  // Evaluate each vector element
1169  for (int i = 0; i < vector.m_num; ++i) {
1170  result.m_data[i] = std::abs(vector.m_data[i]);
1171  }
1172 
1173  // Return vector
1174  return result;
1175 }
1176 
1177 
1178 /***********************************************************************//**
1179  * @brief Computes natural logarithm of vector elements
1180  *
1181  * @param[in] vector Vector.
1182  * @return Vector containing the natural logarithm of every element.
1183  ***************************************************************************/
1184 GVector log(const GVector& vector)
1185 {
1186  // Initialise result vector
1187  GVector result(vector.m_num);
1188 
1189  // Evaluate each vector element
1190  for (int i = 0; i < vector.m_num; ++i) {
1191  result.m_data[i] = std::log(vector.m_data[i]);
1192  }
1193 
1194  // Return vector
1195  return result;
1196 }
1197 
1198 
1199 /***********************************************************************//**
1200  * @brief Computes base10 logarithm of vector elements
1201  *
1202  * @param[in] vector Vector.
1203  * @return Vector containing the base10 logarithm of every element.
1204  ***************************************************************************/
1205 GVector log10(const GVector& vector)
1206 {
1207  // Initialise result vector
1208  GVector result(vector.m_num);
1209 
1210  // Evaluate each vector element
1211  for (int i = 0; i < vector.m_num; ++i) {
1212  result.m_data[i] = std::log10(vector.m_data[i]);
1213  }
1214 
1215  // Return vector
1216  return result;
1217 }
1218 
1219 
1220 /***********************************************************************//**
1221  * @brief Computes sine of vector elements
1222  *
1223  * @param[in] vector Vector.
1224  * @return Vector containing the sine of every element.
1225  ***************************************************************************/
1226 GVector sin(const GVector& vector)
1227 {
1228  // Initialise result vector
1229  GVector result(vector.m_num);
1230 
1231  // Evaluate each vector element
1232  for (int i = 0; i < vector.m_num; ++i) {
1233  result.m_data[i] = std::sin(vector.m_data[i]);
1234  }
1235 
1236  // Return vector
1237  return result;
1238 }
1239 
1240 
1241 /***********************************************************************//**
1242  * @brief Computes sinh of vector elements
1243  *
1244  * @param[in] vector Vector.
1245  * @return Vector containing the sinh of every element.
1246  ***************************************************************************/
1247 GVector sinh(const GVector& vector)
1248 {
1249  // Initialise result vector
1250  GVector result(vector.m_num);
1251 
1252  // Evaluate each vector element
1253  for (int i = 0; i < vector.m_num; ++i) {
1254  result.m_data[i] = std::sinh(vector.m_data[i]);
1255  }
1256 
1257  // Return vector
1258  return result;
1259 }
1260 
1261 
1262 /***********************************************************************//**
1263  * @brief Computes square root of vector elements
1264  *
1265  * @param[in] vector Vector.
1266  * @return Vector containing the square root of every element.
1267  ***************************************************************************/
1268 GVector sqrt(const GVector& vector)
1269 {
1270  // Initialise result vector
1271  GVector result(vector.m_num);
1272 
1273  // Evaluate each vector element
1274  for (int i = 0; i < vector.m_num; ++i) {
1275  result.m_data[i] = std::sqrt(vector.m_data[i]);
1276  }
1277 
1278  // Return vector
1279  return result;
1280 }
1281 
1282 
1283 /***********************************************************************//**
1284  * @brief Computes tangens of vector elements
1285  *
1286  * @param[in] vector Vector.
1287  * @return Vector containing the tangens of every element.
1288  ***************************************************************************/
1289 GVector tan(const GVector& vector)
1290 {
1291  // Initialise result vector
1292  GVector result(vector.m_num);
1293 
1294  // Evaluate each vector element
1295  for (int i = 0; i < vector.m_num; ++i) {
1296  result.m_data[i] = std::tan(vector.m_data[i]);
1297  }
1298 
1299  // Return vector
1300  return result;
1301 }
1302 
1303 
1304 /***********************************************************************//**
1305  * @brief Computes tanh of vector elements
1306  *
1307  * @param[in] vector Vector.
1308  * @return Vector containing the tanh of every element.
1309  ***************************************************************************/
1310 GVector tanh(const GVector& vector)
1311 {
1312  // Initialise result vector
1313  GVector result(vector.m_num);
1314 
1315  // Evaluate each vector element
1316  for (int i = 0; i < vector.m_num; ++i) {
1317  result.m_data[i] = std::tanh(vector.m_data[i]);
1318  }
1319 
1320  // Return vector
1321  return result;
1322 }
1323 
1324 
1325 /***********************************************************************//**
1326  * @brief Computes tanh of vector elements
1327  *
1328  * @param[in] vector Vector.
1329  * @param[in] power Power.
1330  * @return Vector containing the power of every element.
1331  ***************************************************************************/
1332 GVector pow(const GVector& vector, const double& power)
1333 {
1334  // Initialise result vector
1335  GVector result(vector.m_num);
1336 
1337  // Evaluate each vector element
1338  for (int i = 0; i < vector.m_num; ++i) {
1339  result.m_data[i] = std::pow(vector.m_data[i], power);
1340  }
1341 
1342  // Return vector
1343  return result;
1344 }
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:995
double norm(const GVector &vector)
Computes vector norm.
Definition: GVector.cpp:821
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:1247
GVector abs(const GVector &vector)
Computes absolute of vector elements.
Definition: GVector.cpp:1163
GVector cos(const GVector &vector)
Computes cosine of vector elements.
Definition: GVector.cpp:1100
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:901
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:843
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:1268
GVector tan(const GVector &vector)
Computes tangens of vector elements.
Definition: GVector.cpp:1289
#define G_OP_SUB
Definition: GVector.cpp:37
GVector perm(const GVector &vector, const int *p)
Computes vector permutation.
Definition: GVector.cpp:921
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:762
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:1184
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:872
GVector asin(const GVector &vector)
Computes arcsin of vector elements.
Definition: GVector.cpp:1016
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:709
GVector tanh(const GVector &vector)
Computes tanh of vector elements.
Definition: GVector.cpp:1310
GVector atanh(const GVector &vector)
Computes atanh of vector elements.
Definition: GVector.cpp:1079
GVector cosh(const GVector &vector)
Computes cosh of vector elements.
Definition: GVector.cpp:1121
GVector asinh(const GVector &vector)
Computes asinh of vector elements.
Definition: GVector.cpp:1037
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:1332
#define G_CROSS
Definition: GVector.cpp:39
GVector sin(const GVector &vector)
Computes sine of vector elements.
Definition: GVector.cpp:1226
#define G_OP_ADD
Definition: GVector.cpp:36
GVector exp(const GVector &vector)
Computes exponential of vector elements.
Definition: GVector.cpp:1142
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:948
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:730
GVector atan(const GVector &vector)
Computes arctan of vector elements.
Definition: GVector.cpp:1058
GVector log10(const GVector &vector)
Computes base10 logarithm of vector elements.
Definition: GVector.cpp:1205
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:413