SCOREC core
Parallel unstructured mesh tools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
apfDynamicVector.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Scientific Computation Research Center
3  *
4  * This work is open source software, licensed under the terms of the
5  * BSD license as described in the LICENSE file in the top-level directory.
6  */
7 
8 #ifndef APFDYNAMICVECTOR_H
9 #define APFDYNAMICVECTOR_H
10 
14 #include "apfDynamicArray.h"
15 #include "apfVector.h"
16 #include <math.h>
17 #include <iostream>
18 
19 namespace apf {
20 
21 class DynamicVector;
22 
33 class DynamicVector : public DynamicArray<double>
34 {
35  public:
39  DynamicVector(std::size_t n):DynamicArray<double>(n) {}
44  double operator()(std::size_t i) const
45  {
46  return (*this)[i];
47  }
49  double& operator()(std::size_t i)
50  {
51  return (*this)[i];
52  }
55  {
56  for (std::size_t i=0; i < size(); ++i)
57  (*this)[i] += b[i];
58  return *this;
59  }
62  {
63  for (std::size_t i=0; i < size(); ++i)
64  (*this)[i] -= b[i];
65  return *this;
66  }
69  {
70  for (std::size_t i=0; i < size(); ++i)
71  (*this)[i] *= s;
72  return *this;
73  }
76  {
77  for (std::size_t i=0; i < size(); ++i)
78  (*this)[i] /= s;
79  return *this;
80  }
82  double operator*(DynamicVector const& b) const
83  {
84  double r=0;
85  for (std::size_t i=0; i < size(); ++i)
86  r += (*this)[i] * b[i];
87  return r;
88  }
90  double getLength() {return sqrt((*this)*(*this));}
92  void zero()
93  {
94  for (std::size_t i=0; i < size(); ++i)
95  (*this)[i] = 0;
96  }
97 };
98 
100 template <std::size_t N>
102 {
103  DynamicVector result(N);
104  for(std::size_t ii = 0; ii < N; ii++)
105  result[ii] = other[ii];
106  return result;
107 }
108 
109 } //namespace apf
110 
111 std::ostream& operator<<(std::ostream& s, apf::DynamicVector const& x);
112 
113 #endif
void zero()
Initialize all elements to zero.
DynamicVector & operator-=(DynamicVector const &b)
Subtract a vector from this vector.
DynamicVector & operator/=(double s)
Divide this vector by a scalar.
DynamicVector & operator*=(double s)
Multiply this vector by a scalar.
double & operator()(std::size_t i)
mutable index operator
std::ostream & operator<<(std::ostream &s, apf::Vector3 const &v)
write apf::Vector3 to a C++ stream
DynamicVector(std::size_t n)
construct with (n) allocated elements
double operator*(DynamicVector const &b) const
Get the vector dot product.
DynamicVector & operator+=(DynamicVector const &b)
Add a vector to this vector.
template-generic vector of N doubles
Definition: apfVector.h:26
DynamicVector()
default constructor - no allocation
double getLength()
Get the vector magnitude.
double operator()(std::size_t i) const
immutable index operator
double sqrt(double A)
wrapper for standard sqrt function
Definition: mthAD.h:588
A runtime-sized linear algebra vector of doubles.
DynamicVector fromVector(Vector< N > other)
convert an apf::Vector into an apf::DynamicVector
The APF linear algebra vector interface.