LCOV - code coverage report
Current view: top level - Morpheus - Morpheus_Vector.cpp (source / functions) Hit Total Coverage
Test: morpheus.info Lines: 39 58 67.2 %
Date: 2016-08-02 12:11:35 Functions: 10 15 66.7 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  * \brief Defines a Vector class
       4             :  *
       5             :  * @author Alicia Klinvex
       6             :  */
       7             : 
       8             : #include <iostream>
       9             : #include <cassert>
      10             : #include <cmath>
      11             : #include "Morpheus_Vector.h"
      12             : 
      13             : namespace Morpheus {
      14             : 
      15           4 : Vector::Vector(const int numElements)
      16             : {
      17           4 :   assert(numElements > 0);
      18             : 
      19           4 :   numElements_ = numElements;
      20             : 
      21             :   // Allocate memory for the data
      22           4 :   data_ = new double[numElements_];
      23           4 : }
      24             : 
      25             : 
      26           4 : Vector::~Vector()
      27             : {
      28             :   // Release the memory
      29           4 :   delete[] data_;
      30           4 : }
      31             : 
      32             : 
      33          35 : double& Vector::operator[](const int subscript)
      34             : {
      35             :   // Make sure the subscript is valid
      36             :   // Terminate the program if it's not
      37          35 :   assert(subscript >= 0 && subscript < numElements_);
      38             : 
      39          35 :   return data_[subscript];
      40             : }
      41             : 
      42             : 
      43           0 : const double& Vector::operator[](const int subscript) const
      44             : {
      45             :   // Make sure the subscript is valid
      46             :   // Terminate the program if it's not
      47           0 :   assert(subscript >= 0 && subscript < numElements_);
      48             : 
      49           0 :   return data_[subscript];
      50             : }
      51             : 
      52             : 
      53           0 : int Vector::getNumElements() const
      54             : {
      55           0 :   return numElements_;
      56             : }
      57             : 
      58             : 
      59           0 : void Vector::setValue(const double alpha)
      60             : {
      61           0 :   for(int i=0; i<numElements_; i++)
      62             :   {
      63           0 :     data_[i] = alpha;
      64             :   }
      65           0 : }
      66             : 
      67             : 
      68           1 : void Vector::scale(const double alpha)
      69             : {
      70          11 :   for(int i=0; i<numElements_; i++)
      71             :   {
      72          10 :     data_[i] = alpha * data_[i];
      73             :   }
      74           1 : }
      75             : 
      76             : 
      77           1 : void Vector::add(const Vector& b, Vector& sum) const
      78             : {
      79             :   // Make sure all three vectors are the same size
      80           1 :   assert(this->numElements_ == b.numElements_);
      81           1 :   assert(this->numElements_ == sum.numElements_);
      82             : 
      83             :   // Compute the sum of each entry
      84          11 :   for(int i=0; i<this->numElements_; i++)
      85             :   {
      86          10 :     sum.data_[i] = this->data_[i] + b.data_[i];
      87             :   }
      88           1 : }
      89             : 
      90             : 
      91           0 : double Vector::dot(const Vector& b) const
      92             : {
      93             :   // Make sure the vectors are the same size
      94           0 :   assert(this->numElements_ == b.numElements_);
      95             : 
      96             :   double sum;
      97             : 
      98             :   // Compute the sum of all the products
      99           0 :   for(int i=0; i<this->numElements_; i++)
     100             :   {
     101           0 :     sum = sum + (this->data_[i] * b.data_[i]);
     102             :   }
     103             : 
     104           0 :   return sum;
     105             : }
     106             : 
     107             : 
     108           1 : double Vector::norm1() const
     109             : {
     110           1 :   double sum = 0;
     111             : 
     112             :   // Compute the sum of all the entries magnitudes
     113           6 :   for(int i=0; i<numElements_; i++)
     114             :   {
     115           5 :     sum = sum + data_[i];
     116             :   }
     117             : 
     118           1 :   return sum;
     119             : }
     120             : 
     121             : 
     122           1 : double Vector::normInf() const
     123             : {
     124           1 :   double maxVal = 0;
     125             : 
     126             :   // Find the biggest entry
     127           6 :   for(int i=0; i<numElements_; i++)
     128             :   {
     129           5 :     double absVal = data_[i];
     130           5 :     if(absVal > maxVal)
     131             :     {
     132           1 :       maxVal = absVal;
     133             :     }
     134             :   }
     135             : 
     136           1 :   return maxVal;
     137             : }
     138             : 
     139             : 
     140           2 : double Vector::norm2() const
     141             : {
     142           2 :   double sum = 0;
     143             : 
     144             :   // Compute the sum of squares
     145          17 :   for(int i=0; i<numElements_; i++)
     146             :   {
     147          15 :     sum = sum + (data_[i]*data_[i]);
     148             :   }
     149             : 
     150           2 :   return sum;
     151             : }
     152             : 
     153             : 
     154           0 : void Vector::print() const
     155             : {
     156           0 :   std::cout << "Vector with " << numElements_ << " entries\n";
     157           0 :   for(int i=0; i<numElements_; i++)
     158             :   {
     159           0 :     std::cout << "data[" << i << "] = " << data_[i] << std::endl;
     160             :   }
     161           0 : }
     162             : 
     163           9 : } /* namespace Morpheus */

Generated by: LCOV version 1.12-4-g04a3c0e