00001 /*************************************************************************** 00002 * GVector.cpp - Vector class * 00003 * ----------------------------------------------------------------------- * 00004 * copyright (C) 2006-2014 by Juergen Knoedlseder * 00005 * ----------------------------------------------------------------------- * 00006 * * 00007 * This program is free software: you can redistribute it and/or modify * 00008 * it under the terms of the GNU General Public License as published by * 00009 * the Free Software Foundation, either version 3 of the License, or * 00010 * (at your option) any later version. * 00011 * * 00012 * This program is distributed in the hope that it will be useful, * 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00015 * GNU General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU General Public License * 00018 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 00019 * * 00020 ***************************************************************************/ 00021 /** 00022 * @file GVector.cpp 00023 * @brief Vector class implementation 00024 * @author Juergen Knoedlseder 00025 */ 00026 00027 /* __ Includes ___________________________________________________________ */ 00028 #ifdef HAVE_CONFIG_H 00029 #include <config.h> 00030 #endif 00031 #include "GVector.hpp" 00032 #include "GTools.hpp" 00033 00034 00035 /* __ Method name definitions ____________________________________________ */ 00036 #define G_OP_ADD "GVector::operator+=(GVector&)" 00037 #define G_OP_SUB "GVector::operator-=(GVector&)" 00038 #define G_AT "GVector::at(int&)" 00039 #define G_CROSS "cross(GVector&, GVector&)" 00040 #define G_SCALAR "operator*(GVector&, GVector&)" 00041 00042 00043 /*========================================================================== 00044 = = 00045 = Constructors/destructors = 00046 = = 00047 ==========================================================================*/ 00048 00049 /***********************************************************************//** 00050 * @brief Void vector constructor 00051 ***************************************************************************/ 00052 GVector::GVector(void) 00053 { 00054 // Initialise class members 00055 init_members(); 00056 00057 // Return 00058 return; 00059 } 00060 00061 00062 /***********************************************************************//** 00063 * @brief Vector constructor 00064 * 00065 * @param[in] num Number of elements in vector. 00066 * 00067 * Initialises a vector with @p num elements. All vector elements will be 00068 * set to 0. 00069 ***************************************************************************/ 00070 GVector::GVector(const int& num) 00071 { 00072 // Initialise class members 00073 init_members(); 00074 00075 // Store vector size 00076 m_num = num; 00077 00078 // Allocate vector (filled with 0) 00079 alloc_members(); 00080 00081 // Return 00082 return; 00083 } 00084 00085 00086 /***********************************************************************//** 00087 * @brief Single element vector constructor 00088 * 00089 * @param[in] a Vector element. 00090 * 00091 * Initialises a vector with a single element. 00092 ***************************************************************************/ 00093 GVector::GVector(const double& a) 00094 { 00095 // Initialise class members 00096 init_members(); 00097 00098 // Store vector size 00099 m_num = 1; 00100 00101 // Allocate vector 00102 alloc_members(); 00103 00104 // Set value 00105 m_data[0] = a; 00106 00107 // Return 00108 return; 00109 } 00110 00111 00112 /***********************************************************************//** 00113 * @brief Two elements vector constructor 00114 * 00115 * @param[in] a First vector element. 00116 * @param[in] b Second vector element. 00117 * 00118 * Initialises a vector with two elements. 00119 ***************************************************************************/ 00120 GVector::GVector(const double& a, const double& b) 00121 { 00122 // Initialise class members 00123 init_members(); 00124 00125 // Store vector size 00126 m_num = 2; 00127 00128 // Allocate vector 00129 alloc_members(); 00130 00131 // Set values 00132 m_data[0] = a; 00133 m_data[1] = b; 00134 00135 // Return 00136 return; 00137 } 00138 00139 00140 /***********************************************************************//** 00141 * @brief Three elements vector constructor 00142 * 00143 * @param[in] a First vector element. 00144 * @param[in] b Second vector element. 00145 * @param[in] c Third vector element. 00146 * 00147 * Initialises a vector with three elements. 00148 ***************************************************************************/ 00149 GVector::GVector(const double& a, const double& b, const double& c) 00150 { 00151 // Initialise class members 00152 init_members(); 00153 00154 // Store vector size 00155 m_num = 3; 00156 00157 // Allocate vector 00158 alloc_members(); 00159 00160 // Set values 00161 m_data[0] = a; 00162 m_data[1] = b; 00163 m_data[2] = c; 00164 00165 // Return 00166 return; 00167 } 00168 00169 /***********************************************************************//** 00170 * @brief Copy constructor 00171 * 00172 * @param[in] vector Vector. 00173 ***************************************************************************/ 00174 GVector::GVector(const GVector& vector) 00175 { 00176 // Initialise class members 00177 init_members(); 00178 00179 // Copy members 00180 copy_members(vector); 00181 00182 // Return 00183 return; 00184 } 00185 00186 00187 /***********************************************************************//** 00188 * @brief Destructor 00189 ***************************************************************************/ 00190 GVector::~GVector(void) 00191 { 00192 // Free members 00193 free_members(); 00194 00195 // Return 00196 return; 00197 } 00198 00199 00200 /*========================================================================== 00201 = = 00202 = Operators = 00203 = = 00204 ==========================================================================*/ 00205 00206 /***********************************************************************//** 00207 * @brief Assignment operator 00208 * 00209 * @param[in] vector Vector. 00210 * @return Vector. 00211 ***************************************************************************/ 00212 GVector& GVector::operator=(const GVector& vector) 00213 { 00214 // Execute only if object is not identical 00215 if (this != &vector) { 00216 00217 // Free members 00218 free_members(); 00219 00220 // Initialise private members 00221 init_members(); 00222 00223 // Copy members 00224 copy_members(vector); 00225 00226 } // endif: object was not identical 00227 00228 // Return this object 00229 return *this; 00230 } 00231 00232 00233 /***********************************************************************//** 00234 * @brief Equality operator 00235 * 00236 * @param[in] vector Vector. 00237 * @return True if vectors are identical. 00238 * 00239 * Returns true if vectors are identical. Vectors are considered identical 00240 * if they have the same size and if all their elements are identical. 00241 ***************************************************************************/ 00242 bool GVector::operator==(const GVector& vector) const 00243 { 00244 // Initalise result depending on vector size identity 00245 bool result = (m_num == vector.m_num); 00246 00247 // Test for difference. Break at first difference 00248 if (result) { 00249 for (int i = 0; i < m_num; ++i) { 00250 if (m_data[i] != vector.m_data[i]) { 00251 result = false; 00252 break; 00253 } 00254 } 00255 } 00256 00257 // Return result 00258 return result; 00259 } 00260 00261 00262 /***********************************************************************//** 00263 * @brief Non-equality operator 00264 * 00265 * @param[in] vector Vector. 00266 * @return True if both vectors are different. 00267 ***************************************************************************/ 00268 bool GVector::operator!=(const GVector& vector) const 00269 { 00270 // Get negated result of equality operation 00271 bool result = !(this->operator==(vector)); 00272 00273 // Return result 00274 return result; 00275 } 00276 00277 00278 /***********************************************************************//** 00279 * @brief Unary addition operator 00280 * 00281 * @param[in] vector Vector. 00282 * @return Vector. 00283 * 00284 * @exception GException::vector_mismatch 00285 * Vectors have not the same size. 00286 * 00287 * Adds vector. 00288 ***************************************************************************/ 00289 GVector& GVector::operator+=(const GVector& vector) 00290 { 00291 // Raise exception if vectors mismatch 00292 if (m_num != vector.m_num) { 00293 throw GException::vector_mismatch(G_OP_ADD, m_num, vector.m_num); 00294 } 00295 00296 // Add elements 00297 for (int i = 0; i < m_num; ++i) { 00298 m_data[i] += vector.m_data[i]; 00299 } 00300 00301 // Return vector 00302 return *this; 00303 } 00304 00305 00306 /***********************************************************************//** 00307 * @brief Unary subtraction operator 00308 * 00309 * @param[in] vector Vector. 00310 * @return Vector. 00311 * 00312 * @exception GException::vector_mismatch 00313 * Vectors have not the same size. 00314 * 00315 * Subtracts vector. 00316 ***************************************************************************/ 00317 GVector& GVector::operator-=(const GVector& vector) 00318 { 00319 // Raise exception if vectors mismatch 00320 if (m_num != vector.m_num) { 00321 throw GException::vector_mismatch(G_OP_SUB, m_num, vector.m_num); 00322 } 00323 00324 // Subtract elements 00325 for (int i = 0; i < m_num; ++i) { 00326 m_data[i] -= vector.m_data[i]; 00327 } 00328 00329 // Return vector 00330 return *this; 00331 } 00332 00333 00334 /***********************************************************************//** 00335 * @brief Scalar assignment operator 00336 * 00337 * @param[in] scalar Scalar. 00338 * @return Vector. 00339 * 00340 * Subtracts vector. 00341 ***************************************************************************/ 00342 GVector& GVector::operator=(const double& scalar) 00343 { 00344 // Set elements 00345 for (int i = 0; i < m_num; ++i) { 00346 m_data[i] = scalar; 00347 } 00348 00349 // Return vector 00350 return *this; 00351 } 00352 00353 00354 /***********************************************************************//** 00355 * @brief Scalar unary addition operator 00356 * 00357 * @param[in] scalar Scalar. 00358 * @return Vector. 00359 * 00360 * Adds scalar to all vector elements. 00361 ***************************************************************************/ 00362 GVector& GVector::operator+=(const double& scalar) 00363 { 00364 // Add scalar to elements 00365 for (int i = 0; i < m_num; ++i) { 00366 m_data[i] += scalar; 00367 } 00368 00369 // Return vector 00370 return *this; 00371 } 00372 00373 00374 /***********************************************************************//** 00375 * @brief Scalar unary subtraction operator 00376 * 00377 * @param[in] scalar Scalar. 00378 * @return Vector. 00379 * 00380 * Subtract scalar to all vector elements. 00381 ***************************************************************************/ 00382 GVector& GVector::operator-=(const double& scalar) 00383 { 00384 // Subtract scalar from elements 00385 for (int i = 0; i < m_num; ++i) { 00386 m_data[i] -= scalar; 00387 } 00388 00389 // Return vector 00390 return *this; 00391 } 00392 00393 00394 /***********************************************************************//** 00395 * @brief Scalar unary multiplication operator 00396 * 00397 * @param[in] scalar Scalar. 00398 * @return Vector. 00399 * 00400 * Multiply all vector elements by scalar. 00401 ***************************************************************************/ 00402 GVector& GVector::operator*=(const double& scalar) 00403 { 00404 // Multiply all elements 00405 for (int i = 0; i < m_num; ++i) { 00406 m_data[i] *= scalar; 00407 } 00408 00409 // Return vector 00410 return *this; 00411 } 00412 00413 00414 /***********************************************************************//** 00415 * @brief Scalar unary division operator 00416 * 00417 * @param[in] scalar Scalar. 00418 * @return Vector. 00419 * 00420 * Divide all vector elements by scalar. 00421 ***************************************************************************/ 00422 GVector& GVector::operator/=(const double& scalar) 00423 { 00424 // Divide all elements 00425 for (int i = 0; i < m_num; ++i) { 00426 m_data[i] /= scalar; 00427 } 00428 00429 // Return vector 00430 return *this; 00431 } 00432 00433 00434 /***********************************************************************//** 00435 * @brief Unary minus operator 00436 * 00437 * @return Vector. 00438 * 00439 * Negate all vector elements. 00440 ***************************************************************************/ 00441 GVector GVector::operator-(void) const 00442 { 00443 // Copy vector 00444 GVector result = *this; 00445 00446 // Negate all elements 00447 for (int i = 0; i < m_num; ++i) { 00448 result.m_data[i] = -result.m_data[i]; 00449 } 00450 00451 // Return vector 00452 return result; 00453 } 00454 00455 00456 /*========================================================================== 00457 = = 00458 = Public methods = 00459 = = 00460 ==========================================================================*/ 00461 00462 /***********************************************************************//** 00463 * @brief Clear vector 00464 ***************************************************************************/ 00465 void GVector::clear(void) 00466 { 00467 // Free members 00468 free_members(); 00469 00470 // Initialise private members 00471 init_members(); 00472 00473 // Return 00474 return; 00475 } 00476 00477 00478 /***********************************************************************//** 00479 * @brief Clone vector 00480 * 00481 * @return Pointer to deep copy of vector. 00482 ***************************************************************************/ 00483 GVector* GVector::clone(void) const 00484 { 00485 // Clone vector 00486 return new GVector(*this); 00487 } 00488 00489 00490 /***********************************************************************//** 00491 * @brief Vector element access with range checking 00492 * 00493 * @param[in] index Element index [0,...,size()-1]. 00494 * 00495 * @exception GException::out_of_range 00496 * Element index is out of range. 00497 ***************************************************************************/ 00498 double& GVector::at(const int& index) 00499 { 00500 // Raise an exception if index is out of range 00501 if (index < 0 || index >= size()) { 00502 throw GException::out_of_range(G_AT, index, size()-1); 00503 } 00504 00505 // Return vector element 00506 return m_data[index]; 00507 } 00508 00509 00510 /***********************************************************************//** 00511 * @brief Vector element access with range checking (const variant) 00512 * 00513 * @param[in] index Element index [0,...,size()-1]. 00514 * 00515 * @exception GException::out_of_range 00516 * Element index is out of range. 00517 ***************************************************************************/ 00518 const double& GVector::at(const int& index) const 00519 { 00520 // Raise an exception if index is out of range 00521 if (index < 0 || index >= size()) { 00522 throw GException::out_of_range(G_AT, index, size()-1); 00523 } 00524 00525 // Return vector element 00526 return m_data[index]; 00527 } 00528 00529 00530 /***********************************************************************//** 00531 * @brief Returns number of non-zero elements in vector 00532 * 00533 * @return Number of non-zero elements in vector. 00534 ***************************************************************************/ 00535 int GVector::non_zeros(void) const 00536 { 00537 // Initialise number of non-zeros 00538 int non_zeros = 0; 00539 00540 // Gather all non-zero elements 00541 for (int i = 0; i < m_num; ++i) { 00542 if (m_data[i] != 0.0) { 00543 non_zeros++; 00544 } 00545 } 00546 00547 // Return number of non-zero elements 00548 return non_zeros; 00549 } 00550 00551 /***********************************************************************//** 00552 * @brief Return the index of the first non-zero element in a vector 00553 * 00554 * @return Index of first non-zero element in vector. 00555 * 00556 * Returns the index of the first non-zero element. If all elements are zero 00557 * the method will return -1. 00558 ***************************************************************************/ 00559 int GVector::first_nonzero(void) const 00560 { 00561 // Initialise index 00562 int first_non_zero = -1; 00563 00564 // Gather all non-zero elements 00565 for (int i = 0; i < m_num; ++i) { 00566 if (m_data[i] != 0.0) { 00567 first_non_zero = i; 00568 break; 00569 } 00570 } 00571 00572 // Return number of non-zero elements 00573 return first_non_zero; 00574 } 00575 00576 00577 /***********************************************************************//** 00578 * @brief Return the index of the last non-zero element in a vector 00579 * 00580 * @return Index of last non-zero element in vector. 00581 * 00582 * Returns the index of the last non-zero element. If all elements are zero 00583 * the method will return -1. 00584 ***************************************************************************/ 00585 int GVector::last_nonzero(void) const 00586 { 00587 // Initialise index 00588 int last_non_zero = -1; 00589 00590 // Gather all non-zero elements 00591 for (int i = m_num-1; i >= 0; --i) { 00592 if (m_data[i] != 0.0) { 00593 last_non_zero = i; 00594 break; 00595 } 00596 } 00597 00598 // Return number of non-zero elements 00599 return last_non_zero; 00600 } 00601 00602 00603 /***********************************************************************//** 00604 * @brief Vector slice operator 00605 * 00606 * @param[in] start Start index [0,...,size()-1] 00607 * @param[in] stop Stop index [0,...,size()-1] 00608 * @return Vector made of elements [start,...,stop]. 00609 ***************************************************************************/ 00610 GVector GVector::slice(const int& start, const int& stop) const 00611 { 00612 // Determine number of elements in vector 00613 int num = stop - start; 00614 if (num < 0) { 00615 num = 0; 00616 } 00617 00618 // Allocate vector 00619 GVector vector(num); 00620 00621 // Build vector slice between original elements a and b (inclusive) 00622 for (int i = start; i <= stop; ++i) { 00623 vector.m_data[i-start] = m_data[i]; 00624 } 00625 00626 // Return sliced vector 00627 return vector; 00628 } 00629 00630 00631 /***********************************************************************//** 00632 * @brief Print vector information 00633 * 00634 * @param[in] chatter Chattiness (defaults to NORMAL). 00635 * @return String containing vector information. 00636 ***************************************************************************/ 00637 std::string GVector::print(const GChatter& chatter) const 00638 { 00639 // Initialise result string 00640 std::string result = "("; 00641 00642 // Continue only if chatter is not silent 00643 if (chatter != SILENT) { 00644 00645 // Put all elements in stream 00646 for (int i = 0; i < m_num; ++i) { 00647 result += gammalib::str((*this)[i]); 00648 if (i != m_num-1) { 00649 result += ", "; 00650 } 00651 } 00652 00653 // Append ) 00654 result += ")"; 00655 00656 } // endif: chatter was not silent 00657 00658 // Return result 00659 return result; 00660 } 00661 00662 00663 /*========================================================================== 00664 = = 00665 = Private methods = 00666 = = 00667 ==========================================================================*/ 00668 00669 /***********************************************************************//** 00670 * @brief Initialise class members 00671 ***************************************************************************/ 00672 void GVector::init_members(void) 00673 { 00674 // Initialise members 00675 m_num = 0; 00676 m_data = NULL; 00677 00678 // Return 00679 return; 00680 } 00681 00682 00683 /***********************************************************************//** 00684 * @brief Allocate vector 00685 ***************************************************************************/ 00686 void GVector::alloc_members(void) 00687 { 00688 // Continue only if vector has non-zero length 00689 if (m_num > 0) { 00690 00691 // Allocate vector and initialize elements to 0 00692 m_data = new double[m_num]; 00693 for (int i = 0; i < m_num; ++i) { 00694 m_data[i] = 0.0; 00695 } 00696 00697 } // endif: vector had non-zero length 00698 00699 // Return 00700 return; 00701 } 00702 00703 00704 /***********************************************************************//** 00705 * @brief Copy class members 00706 * 00707 * @param[in] vector Vector from which members should be copied. 00708 ***************************************************************************/ 00709 void GVector::copy_members(const GVector& vector) 00710 { 00711 // Copy attributes 00712 m_num = vector.m_num; 00713 00714 // Copy elements 00715 if (m_num > 0) { 00716 alloc_members(); 00717 for (int i = 0; i < m_num; ++i) { 00718 m_data[i] = vector.m_data[i]; 00719 } 00720 } 00721 00722 // Return 00723 return; 00724 } 00725 00726 00727 /***********************************************************************//** 00728 * @brief Delete class members 00729 ***************************************************************************/ 00730 void GVector::free_members(void) 00731 { 00732 // Free memory 00733 if (m_data != NULL) delete[] m_data; 00734 00735 // Signal free pointers 00736 m_data = NULL; 00737 00738 // Return 00739 return; 00740 } 00741 00742 00743 /*========================================================================== 00744 = = 00745 = Friends = 00746 = = 00747 ==========================================================================*/ 00748 00749 /***********************************************************************//** 00750 * @brief Vector cross product 00751 * 00752 * @param[in] a Vector. 00753 * @param[in] b Vector. 00754 * @return Vector cross product. 00755 * 00756 * @exception GException::vector_mismatch 00757 * Mismatch between vector size. 00758 * 00759 * Computes the cross product between two 3-element vectors (note that the 00760 * cross product is only defined for 3-element vectors). 00761 ***************************************************************************/ 00762 GVector cross(const GVector& a, const GVector& b) 00763 { 00764 // Verify that vectors have same dimensions 00765 if (a.m_num != b.m_num) { 00766 throw GException::vector_mismatch(G_CROSS, a.m_num, b.m_num); 00767 } 00768 00769 // Verify that vectors have 3 elements 00770 if (a.m_num != 3) { 00771 throw GException::vector_bad_cross_dim(G_CROSS, a.m_num); 00772 } 00773 00774 // Compute cross product 00775 GVector result(3); 00776 result.m_data[0] = a.m_data[1]*b.m_data[2] - a.m_data[2]*b.m_data[1]; 00777 result.m_data[1] = a.m_data[2]*b.m_data[0] - a.m_data[0]*b.m_data[2]; 00778 result.m_data[2] = a.m_data[0]*b.m_data[1] - a.m_data[1]*b.m_data[0]; 00779 00780 // Return result 00781 return result; 00782 } 00783 00784 00785 /***********************************************************************//** 00786 * @brief Vector scalar product 00787 * 00788 * @param[in] a Vector. 00789 * @param[in] b Vector. 00790 * @return Product between vector @p a and @p b. 00791 * 00792 * @exception GException::vector_mismatch 00793 * Mismatch between vector size. 00794 * 00795 * Returns the scalar product between vector @p a and @p b. 00796 ***************************************************************************/ 00797 double operator*(const GVector& a, const GVector& b) 00798 { 00799 // Verify that vectors have same dimensions 00800 if (a.m_num != b.m_num) { 00801 throw GException::vector_mismatch(G_SCALAR, a.m_num, b.m_num); 00802 } 00803 00804 // Compute scalar product 00805 double result = 0.0; 00806 for (int i = 0; i < a.m_num; ++i) { 00807 result += (a.m_data[i] * b.m_data[i]); 00808 } 00809 00810 // Return scalar product 00811 return result; 00812 } 00813 00814 00815 /***********************************************************************//** 00816 * @brief Computes vector norm 00817 * 00818 * @param[in] vector Vector. 00819 * @return Vector norm. 00820 ***************************************************************************/ 00821 double norm(const GVector& vector) 00822 { 00823 // Initialises result 00824 double result = 0.0; 00825 00826 // Computes norm 00827 for (int i = 0; i < vector.m_num; ++i) { 00828 result += (vector.m_data[i] * vector.m_data[i]); 00829 } 00830 result = (result > 0.0) ? std::sqrt(result) : 0.0; 00831 00832 // Returns norm 00833 return result; 00834 } 00835 00836 00837 /***********************************************************************//** 00838 * @brief Computes minimum vector element 00839 * 00840 * @param[in] vector Vector. 00841 * @return Minimum vector element. 00842 ***************************************************************************/ 00843 double min(const GVector& vector) 00844 { 00845 // Initialises result 00846 double result = 0.0; 00847 00848 // Continue only if we have elements 00849 if (vector.m_num > 0) { 00850 00851 // Search for minimum 00852 result = vector.m_data[0]; 00853 for (int i = 1; i < vector.m_num; ++i) { 00854 if (vector.m_data[i] < result) { 00855 result = vector.m_data[i]; 00856 } 00857 } 00858 00859 } // endif: there were elements 00860 00861 // Returns minimum 00862 return result; 00863 } 00864 00865 00866 /***********************************************************************//** 00867 * @brief Computes maximum vector element 00868 * 00869 * @param[in] vector Vector. 00870 * @return Maximum vector element. 00871 ***************************************************************************/ 00872 double max(const GVector& vector) 00873 { 00874 // Initialises result 00875 double result = 0.0; 00876 00877 // Continue only if we have elements 00878 if (vector.m_num > 0) { 00879 00880 // Search for maximum 00881 result = vector.m_data[0]; 00882 for (int i = 1; i < vector.m_num; ++i) { 00883 if (vector.m_data[i] > result) { 00884 result = vector.m_data[i]; 00885 } 00886 } 00887 00888 } // endif: there were elements 00889 00890 // Returns maximum 00891 return result; 00892 } 00893 00894 00895 /***********************************************************************//** 00896 * @brief Computes vector sum 00897 * 00898 * @param[in] vector Vector. 00899 * @return Sum of vector elements. 00900 ***************************************************************************/ 00901 double sum(const GVector& vector) 00902 { 00903 // Compute sum 00904 double result = 0.0; 00905 for (int i = 0; i < vector.m_num; ++i) { 00906 result += vector.m_data[i]; 00907 } 00908 00909 // Returns sum 00910 return result; 00911 } 00912 00913 00914 /***********************************************************************//** 00915 * @brief Computes vector permutation 00916 * 00917 * @param[in] vector Vector. 00918 * @param[in] p Permutation array. 00919 * @return Permuted vector. 00920 ***************************************************************************/ 00921 GVector perm(const GVector& vector, const int* p) 00922 { 00923 // Initialise result vector 00924 GVector result(vector.m_num); 00925 00926 // Compute permutations 00927 if (p != NULL) { 00928 for (int i = 0; i < vector.m_num; ++i) { 00929 result.m_data[i] = vector.m_data[p[i]]; 00930 } 00931 } 00932 else { 00933 result = vector; 00934 } 00935 00936 // Return vector 00937 return result; 00938 } 00939 00940 00941 /***********************************************************************//** 00942 * @brief Computes vector inverse permutation 00943 * 00944 * @param[in] vector Vector. 00945 * @param[in] p Permutation array. 00946 * @return Inversely permuted vector. 00947 ***************************************************************************/ 00948 GVector iperm(const GVector& vector, const int* p) 00949 { 00950 // Initialise result vector 00951 GVector result(vector.m_num); 00952 00953 // Compute permutations 00954 if (p != NULL) { 00955 for (int i = 0; i < vector.m_num; ++i) { 00956 result.m_data[p[i]] = vector.m_data[i]; 00957 } 00958 } 00959 else { 00960 result = vector; 00961 } 00962 00963 // Return vector 00964 return result; 00965 } 00966 00967 00968 /***********************************************************************//** 00969 * @brief Computes arccos of vector elements 00970 * 00971 * @param[in] vector Vector. 00972 * @return Vector containing the arccos of every element. 00973 ***************************************************************************/ 00974 GVector acos(const GVector& vector) 00975 { 00976 // Initialise result vector 00977 GVector result(vector.m_num); 00978 00979 // Evaluate each vector element 00980 for (int i = 0; i < vector.m_num; ++i) { 00981 result.m_data[i] = std::acos(vector.m_data[i]); 00982 } 00983 00984 // Return vector 00985 return result; 00986 } 00987 00988 00989 /***********************************************************************//** 00990 * @brief Computes acosh of vector elements 00991 * 00992 * @param[in] vector Vector. 00993 * @return Vector containing the acosh of every element. 00994 ***************************************************************************/ 00995 GVector acosh(const GVector& vector) 00996 { 00997 // Initialise result vector 00998 GVector result(vector.m_num); 00999 01000 // Evaluate each vector element 01001 for (int i = 0; i < vector.m_num; ++i) { 01002 result.m_data[i] = acosh(vector.m_data[i]); 01003 } 01004 01005 // Return vector 01006 return result; 01007 } 01008 01009 01010 /***********************************************************************//** 01011 * @brief Computes arcsin of vector elements 01012 * 01013 * @param[in] vector Vector. 01014 * @return Vector containing the arcsin of every element. 01015 ***************************************************************************/ 01016 GVector asin(const GVector& vector) 01017 { 01018 // Initialise result vector 01019 GVector result(vector.m_num); 01020 01021 // Evaluate each vector element 01022 for (int i = 0; i < vector.m_num; ++i) { 01023 result.m_data[i] = std::asin(vector.m_data[i]); 01024 } 01025 01026 // Return vector 01027 return result; 01028 } 01029 01030 01031 /***********************************************************************//** 01032 * @brief Computes asinh of vector elements 01033 * 01034 * @param[in] vector Vector. 01035 * @return Vector containing the asinh of every element. 01036 ***************************************************************************/ 01037 GVector asinh(const GVector& vector) 01038 { 01039 // Initialise result vector 01040 GVector result(vector.m_num); 01041 01042 // Evaluate each vector element 01043 for (int i = 0; i < vector.m_num; ++i) { 01044 result.m_data[i] = asinh(vector.m_data[i]); 01045 } 01046 01047 // Return vector 01048 return result; 01049 } 01050 01051 01052 /***********************************************************************//** 01053 * @brief Computes arctan of vector elements 01054 * 01055 * @param[in] vector Vector. 01056 * @return Vector containing the arctan of every element. 01057 ***************************************************************************/ 01058 GVector atan(const GVector& vector) 01059 { 01060 // Initialise result vector 01061 GVector result(vector.m_num); 01062 01063 // Evaluate each vector element 01064 for (int i = 0; i < vector.m_num; ++i) { 01065 result.m_data[i] = std::atan(vector.m_data[i]); 01066 } 01067 01068 // Return vector 01069 return result; 01070 } 01071 01072 01073 /***********************************************************************//** 01074 * @brief Computes atanh of vector elements 01075 * 01076 * @param[in] vector Vector. 01077 * @return Vector containing the atanh of every element. 01078 ***************************************************************************/ 01079 GVector atanh(const GVector& vector) 01080 { 01081 // Initialise result vector 01082 GVector result(vector.m_num); 01083 01084 // Evaluate each vector element 01085 for (int i = 0; i < vector.m_num; ++i) { 01086 result.m_data[i] = atanh(vector.m_data[i]); 01087 } 01088 01089 // Return vector 01090 return result; 01091 } 01092 01093 01094 /***********************************************************************//** 01095 * @brief Computes cosine of vector elements 01096 * 01097 * @param[in] vector Vector. 01098 * @return Vector containing the cosine of every element. 01099 ***************************************************************************/ 01100 GVector cos(const GVector& vector) 01101 { 01102 // Initialise result vector 01103 GVector result(vector.m_num); 01104 01105 // Evaluate each vector element 01106 for (int i = 0; i < vector.m_num; ++i) { 01107 result.m_data[i] = std::cos(vector.m_data[i]); 01108 } 01109 01110 // Return vector 01111 return result; 01112 } 01113 01114 01115 /***********************************************************************//** 01116 * @brief Computes cosh of vector elements 01117 * 01118 * @param[in] vector Vector. 01119 * @return Vector containing the cosh of every element. 01120 ***************************************************************************/ 01121 GVector cosh(const GVector& vector) 01122 { 01123 // Initialise result vector 01124 GVector result(vector.m_num); 01125 01126 // Evaluate each vector element 01127 for (int i = 0; i < vector.m_num; ++i) { 01128 result.m_data[i] = std::cosh(vector.m_data[i]); 01129 } 01130 01131 // Return vector 01132 return result; 01133 } 01134 01135 01136 /***********************************************************************//** 01137 * @brief Computes exponential of vector elements 01138 * 01139 * @param[in] vector Vector. 01140 * @return Vector containing the exponential of every element. 01141 ***************************************************************************/ 01142 GVector exp(const GVector& vector) 01143 { 01144 // Initialise result vector 01145 GVector result(vector.m_num); 01146 01147 // Evaluate each vector element 01148 for (int i = 0; i < vector.m_num; ++i) { 01149 result.m_data[i] = std::exp(vector.m_data[i]); 01150 } 01151 01152 // Return vector 01153 return result; 01154 } 01155 01156 01157 /***********************************************************************//** 01158 * @brief Computes absolute of vector elements 01159 * 01160 * @param[in] vector Vector. 01161 * @return Vector containing the absolute of every element. 01162 ***************************************************************************/ 01163 GVector abs(const GVector& vector) 01164 { 01165 // Initialise result vector 01166 GVector result(vector.m_num); 01167 01168 // Evaluate each vector element 01169 for (int i = 0; i < vector.m_num; ++i) { 01170 result.m_data[i] = std::abs(vector.m_data[i]); 01171 } 01172 01173 // Return vector 01174 return result; 01175 } 01176 01177 01178 /***********************************************************************//** 01179 * @brief Computes natural logarithm of vector elements 01180 * 01181 * @param[in] vector Vector. 01182 * @return Vector containing the natural logarithm of every element. 01183 ***************************************************************************/ 01184 GVector log(const GVector& vector) 01185 { 01186 // Initialise result vector 01187 GVector result(vector.m_num); 01188 01189 // Evaluate each vector element 01190 for (int i = 0; i < vector.m_num; ++i) { 01191 result.m_data[i] = std::log(vector.m_data[i]); 01192 } 01193 01194 // Return vector 01195 return result; 01196 } 01197 01198 01199 /***********************************************************************//** 01200 * @brief Computes base10 logarithm of vector elements 01201 * 01202 * @param[in] vector Vector. 01203 * @return Vector containing the base10 logarithm of every element. 01204 ***************************************************************************/ 01205 GVector log10(const GVector& vector) 01206 { 01207 // Initialise result vector 01208 GVector result(vector.m_num); 01209 01210 // Evaluate each vector element 01211 for (int i = 0; i < vector.m_num; ++i) { 01212 result.m_data[i] = std::log10(vector.m_data[i]); 01213 } 01214 01215 // Return vector 01216 return result; 01217 } 01218 01219 01220 /***********************************************************************//** 01221 * @brief Computes sine of vector elements 01222 * 01223 * @param[in] vector Vector. 01224 * @return Vector containing the sine of every element. 01225 ***************************************************************************/ 01226 GVector sin(const GVector& vector) 01227 { 01228 // Initialise result vector 01229 GVector result(vector.m_num); 01230 01231 // Evaluate each vector element 01232 for (int i = 0; i < vector.m_num; ++i) { 01233 result.m_data[i] = std::sin(vector.m_data[i]); 01234 } 01235 01236 // Return vector 01237 return result; 01238 } 01239 01240 01241 /***********************************************************************//** 01242 * @brief Computes sinh of vector elements 01243 * 01244 * @param[in] vector Vector. 01245 * @return Vector containing the sinh of every element. 01246 ***************************************************************************/ 01247 GVector sinh(const GVector& vector) 01248 { 01249 // Initialise result vector 01250 GVector result(vector.m_num); 01251 01252 // Evaluate each vector element 01253 for (int i = 0; i < vector.m_num; ++i) { 01254 result.m_data[i] = std::sinh(vector.m_data[i]); 01255 } 01256 01257 // Return vector 01258 return result; 01259 } 01260 01261 01262 /***********************************************************************//** 01263 * @brief Computes square root of vector elements 01264 * 01265 * @param[in] vector Vector. 01266 * @return Vector containing the square root of every element. 01267 ***************************************************************************/ 01268 GVector sqrt(const GVector& vector) 01269 { 01270 // Initialise result vector 01271 GVector result(vector.m_num); 01272 01273 // Evaluate each vector element 01274 for (int i = 0; i < vector.m_num; ++i) { 01275 result.m_data[i] = std::sqrt(vector.m_data[i]); 01276 } 01277 01278 // Return vector 01279 return result; 01280 } 01281 01282 01283 /***********************************************************************//** 01284 * @brief Computes tangens of vector elements 01285 * 01286 * @param[in] vector Vector. 01287 * @return Vector containing the tangens of every element. 01288 ***************************************************************************/ 01289 GVector tan(const GVector& vector) 01290 { 01291 // Initialise result vector 01292 GVector result(vector.m_num); 01293 01294 // Evaluate each vector element 01295 for (int i = 0; i < vector.m_num; ++i) { 01296 result.m_data[i] = std::tan(vector.m_data[i]); 01297 } 01298 01299 // Return vector 01300 return result; 01301 } 01302 01303 01304 /***********************************************************************//** 01305 * @brief Computes tanh of vector elements 01306 * 01307 * @param[in] vector Vector. 01308 * @return Vector containing the tanh of every element. 01309 ***************************************************************************/ 01310 GVector tanh(const GVector& vector) 01311 { 01312 // Initialise result vector 01313 GVector result(vector.m_num); 01314 01315 // Evaluate each vector element 01316 for (int i = 0; i < vector.m_num; ++i) { 01317 result.m_data[i] = std::tanh(vector.m_data[i]); 01318 } 01319 01320 // Return vector 01321 return result; 01322 } 01323 01324 01325 /***********************************************************************//** 01326 * @brief Computes tanh of vector elements 01327 * 01328 * @param[in] vector Vector. 01329 * @param[in] power Power. 01330 * @return Vector containing the power of every element. 01331 ***************************************************************************/ 01332 GVector pow(const GVector& vector, const double& power) 01333 { 01334 // Initialise result vector 01335 GVector result(vector.m_num); 01336 01337 // Evaluate each vector element 01338 for (int i = 0; i < vector.m_num; ++i) { 01339 result.m_data[i] = std::pow(vector.m_data[i], power); 01340 } 01341 01342 // Return vector 01343 return result; 01344 }