PSCF v1.1
List of all members
Util::Vector Class Reference

A Vector is a Cartesian vector. More...

#include <Vector.h>

Public Member Functions

Constructors
 Vector ()
 Default constructor. More...
 
 Vector (const Vector &v)
 Copy constructor. More...
 
 Vector (double scalar)
 Constructor, initialize all elements to a scalar value. More...
 
 Vector (const double *v)
 Construct Vector from C double[3] array. More...
 
 Vector (double x, double y, double z=0.0)
 Construct Vector from its coordinates. More...
 
Vectorzero ()
 Set all elements of a 3D vector to zero. More...
 
template<class Archive >
void serialize (Archive &ar, const unsigned int version)
 Serialize to/from an archive. More...
 
Assignment
Vectoroperator= (const Vector &v)
 Copy assignment. More...
 
Vectoroperator= (const double *v)
 Assignment from C double[3] array. More...
 
Arithmetic Assignment
void operator+= (const Vector &dv)
 Add vector dv to this vector. More...
 
void operator-= (const Vector &dv)
 Subtract vector dv from this vector. More...
 
void operator*= (double s)
 Multiply this vector by scalar s. More...
 
void operator/= (double s)
 Divide this vector by scalar s. More...
 
Array Subscript
const double & operator[] (int i) const
 Return one Cartesian element by value. More...
 
double & operator[] (int i)
 Return one element of the vector by references. More...
 
Scalar-valued functions
double square () const
 Return square magnitude of this vector. More...
 
double abs () const
 Return absolute magnitude of this vector. More...
 
double dot (const Vector &v) const
 Return dot product of this vector and vector v. More...
 
double projection (const Vector &p) const
 Return projection of this vector along vector p. More...
 
Vector valued functions (result assigned to invoking object)
Vectoradd (const Vector &v1, const Vector &v2)
 Add vectors v1 and v2. More...
 
Vectorsubtract (const Vector &v1, const Vector &v2)
 Subtract vector v2 from v1. More...
 
Vectormultiply (const Vector &v, double s)
 Multiply a vector v by a scalar s. More...
 
Vectordivide (const Vector &v, double s)
 Divide vector v by scalar s. More...
 
Vectorcross (const Vector &v1, const Vector &v2)
 Calculate cross product of vectors v1 and v2. More...
 
Vectorversor (const Vector &v)
 Calculate unit vector parallel to input vector v. More...
 
Vectorparallel (const Vector &v, const Vector &p)
 Calculate component of vector v parallel to vector p. More...
 
Vectortransverse (const Vector &v, const Vector &p)
 Calculate component of vector v transverse to vector p. More...
 
int minId (const Vector &v)
 Computes the index corresponding to minimum element in a vector. More...
 
int maxId (const Vector &v)
 Computes the index corresponding to maximum element in a vector. More...
 

Static Members

static const Vector Zero = Vector(0.0)
 Zero Vector = {0.0, 0.0, 0.0}. More...
 
static void initStatic ()
 Initialize Zero Vector. More...
 
static void commitMpiType ()
 Commit MPI datatype MpiTraits<Vector>::type. More...
 
bool operator== (const Vector &v1, const Vector &v2)
 Equality for Vectors. More...
 
bool operator== (const Vector &v1, const double *v2)
 Equality of Vector and C array. More...
 
std::istream & operator>> (std::istream &in, Vector &vector)
 istream extractor for a Vector. More...
 
std::ostream & operator<< (std::ostream &out, const Vector &vector)
 ostream inserter for a Vector. More...
 

Detailed Description

A Vector is a Cartesian vector.

The Cartesian elements of a Vector can be accessed using array notation: The elements of a three dimensional Vector v are v[0], v[1], and v[2]. The subscript operator [] returns elements as references, which can be used on either the left or right side of an assignment operator.

The arithmetic assignment operators +=, -=, *=, and /= are overloaded. The operators += and -= represent increment or decrement by a vector, while *= and /= represent multiplication or division by a scalar.

All other unary and binary mathematical operations are implemented as methods. Operations that yield a scalar result, such as a dot product, are implemented as methods that return the resulting value. Operations that yield a Vector, such as vector addition, are implemented by methods that assign the result to the invoking vector, and return a reference to the invoking vector. For example,

Vector a, b, c;
double s;
a[0] = 0.0
a[1] = 1.0
a[2] = 2.0
b[0] = 0.5
b[1] = -0.5
b[2] = -1.5
// Set s = a.b
s = a.dot(b)
// Set c = a + b
c.add(a, b)
// Set a = a + b
a += b
// Set b = b*2
b *= 2
A Vector is a Cartesian vector.
Definition: Vector.h:76
double dot(const Vector &v) const
Return dot product of this vector and vector v.
Definition: Vector.h:632
Vector & add(const Vector &v1, const Vector &v2)
Add vectors v1 and v2.
Definition: Vector.h:657

This syntax for Vector valued operations avoids dynamic allocation of temporary Vector objects, by requiring that the invoking function provide an object to hold the result.

For efficiency, all methods in this class are declared inline.

Definition at line 75 of file Vector.h.

Constructor & Destructor Documentation

◆ Vector() [1/5]

Util::Vector::Vector ( )
inline

Default constructor.

Definition at line 463 of file Vector.h.

◆ Vector() [2/5]

Util::Vector::Vector ( const Vector v)
inline

Copy constructor.

Parameters
vVector to be copied

Definition at line 470 of file Vector.h.

◆ Vector() [3/5]

Util::Vector::Vector ( double  scalar)
inlineexplicit

Constructor, initialize all elements to a scalar value.

Parameters
scalarinitial value for all elements.

Definition at line 481 of file Vector.h.

◆ Vector() [4/5]

Util::Vector::Vector ( const double *  v)
inlineexplicit

Construct Vector from C double[3] array.

Parameters
varray of 3 coordinates

Definition at line 492 of file Vector.h.

◆ Vector() [5/5]

Util::Vector::Vector ( double  x,
double  y,
double  z = 0.0 
)
inline

Construct Vector from its coordinates.

Parameters
xx-axis coordinate, v[0]
yy-axis coordinate, v[1]
zz-axis coordinate, v[2]

Definition at line 503 of file Vector.h.

Member Function Documentation

◆ zero()

Vector & Util::Vector::zero ( )
inline

Set all elements of a 3D vector to zero.

Definition at line 514 of file Vector.h.

Referenced by Util::setToZero().

◆ serialize()

template<class Archive >
void Util::Vector::serialize ( Archive &  ar,
const unsigned int  version 
)
inline

Serialize to/from an archive.

Parameters
ararchive
versionarchive version id

Definition at line 818 of file Vector.h.

◆ operator=() [1/2]

Vector & Util::Vector::operator= ( const Vector v)
inline

Copy assignment.

Parameters
vVector to assign.

Definition at line 526 of file Vector.h.

◆ operator=() [2/2]

Vector & Util::Vector::operator= ( const double *  v)
inline

Assignment from C double[3] array.

Parameters
vC-array of components

Definition at line 538 of file Vector.h.

◆ operator+=()

void Util::Vector::operator+= ( const Vector dv)
inline

Add vector dv to this vector.

Upon return, *this = this + dv.

Parameters
dvvector increment (input)

Definition at line 550 of file Vector.h.

◆ operator-=()

void Util::Vector::operator-= ( const Vector dv)
inline

Subtract vector dv from this vector.

Upon return, *this = this + dv.

Parameters
dvvector increment (input)

Definition at line 561 of file Vector.h.

◆ operator*=()

void Util::Vector::operator*= ( double  s)
inline

Multiply this vector by scalar s.

Upon return, *this = (*this)*s.

Parameters
sscalar multiplier

Definition at line 572 of file Vector.h.

◆ operator/=()

void Util::Vector::operator/= ( double  s)
inline

Divide this vector by scalar s.

Upon return, *this = (*this)/s.

Parameters
sscalar divisor (input)

Definition at line 583 of file Vector.h.

◆ operator[]() [1/2]

const double & Util::Vector::operator[] ( int  i) const
inline

Return one Cartesian element by value.

Parameters
ielement index
Returns
element i of the vector

Definition at line 594 of file Vector.h.

References Util::Dimension.

◆ operator[]() [2/2]

double & Util::Vector::operator[] ( int  i)
inline

Return one element of the vector by references.

Parameters
ielement index
Returns
element i of this vector

Definition at line 605 of file Vector.h.

References Util::Dimension.

◆ square()

double Util::Vector::square ( ) const
inline

Return square magnitude of this vector.

Returns
square magnitude of this vector

Definition at line 616 of file Vector.h.

Referenced by abs(), parallel(), and transverse().

◆ abs()

double Util::Vector::abs ( ) const
inline

Return absolute magnitude of this vector.

Returns
absolute magnitude (norm) of this vector.

Definition at line 625 of file Vector.h.

References square().

Referenced by projection(), and versor().

◆ dot()

double Util::Vector::dot ( const Vector v) const
inline

Return dot product of this vector and vector v.

Parameters
vinput vector
Returns
dot product of this vector and vector v

Definition at line 632 of file Vector.h.

Referenced by parallel(), Util::product(), projection(), and transverse().

◆ projection()

double Util::Vector::projection ( const Vector p) const
inline

Return projection of this vector along vector p.

Parameters
pvector parallel to direction along which to project
Returns
scalar projection this->dot(p)/p.abs()

Definition at line 641 of file Vector.h.

References abs(), and dot().

◆ add()

Vector & Util::Vector::add ( const Vector v1,
const Vector v2 
)
inline

Add vectors v1 and v2.

Upon return, *this = v1 + v2.

Parameters
v1vector (input)
v2vector (input)
Returns
modified invoking vector

Definition at line 657 of file Vector.h.

◆ subtract()

Vector & Util::Vector::subtract ( const Vector v1,
const Vector v2 
)
inline

Subtract vector v2 from v1.

Upon return, *this = v1 - v2.

Parameters
v1vector (input)
v2vector (input)
Returns
modified invoking vector

Definition at line 672 of file Vector.h.

◆ multiply()

Vector & Util::Vector::multiply ( const Vector v,
double  s 
)
inline

Multiply a vector v by a scalar s.

Upon return, *this = v*s.

Parameters
vvector input
sscalar input
Returns
modified invoking vector

Definition at line 686 of file Vector.h.

◆ divide()

Vector & Util::Vector::divide ( const Vector v,
double  s 
)
inline

Divide vector v by scalar s.

Upon return, *this = v/s;

Parameters
vvector input
sscalar input
Returns
modified invoking vector

Definition at line 700 of file Vector.h.

◆ cross()

Vector & Util::Vector::cross ( const Vector v1,
const Vector v2 
)
inline

Calculate cross product of vectors v1 and v2.

Upon return, *this = v1 x v2.

Parameters
v1input vector
v2input vector
Returns
modified invoking vector

Definition at line 714 of file Vector.h.

◆ versor()

Vector & Util::Vector::versor ( const Vector v)
inline

Calculate unit vector parallel to input vector v.

Upon return *this = unit vector.

Parameters
vinput vector
Returns
modified invoking Vector

Definition at line 726 of file Vector.h.

References abs().

◆ parallel()

Vector & Util::Vector::parallel ( const Vector v,
const Vector p 
)
inline

Calculate component of vector v parallel to vector p.

Upon return, the invoking vector is equal to the vector projection of vector v along a direction parallel to vector p.

The vector projection of v along p is parallel to p and has an absolute magnitude equal to the scalar projection of v along p.

Parameters
vvector to project
pvector along which to project
Returns
modified invoking Vector

Definition at line 749 of file Vector.h.

References dot(), and square().

◆ transverse()

Vector & Util::Vector::transverse ( const Vector v,
const Vector p 
)
inline

Calculate component of vector v transverse to vector p.

Upon return, the invoking vector is equal to the vector projection of vector v perpendicular to vector p.

Parameters
vinput vector
pvector perpendicular to which to project.
Returns
modified invoking Vector

Definition at line 771 of file Vector.h.

References dot(), and square().

◆ minId()

int Util::Vector::minId ( const Vector v)

Computes the index corresponding to minimum element in a vector.

Parameters
vinput vector
Returns
index of the minimum element.

◆ maxId()

int Util::Vector::maxId ( const Vector v)

Computes the index corresponding to maximum element in a vector.

Parameters
vinput vector
Returns
index of the maximum element.

◆ initStatic()

void Util::Vector::initStatic ( )
static

Initialize Zero Vector.

Definition at line 119 of file Vector.cpp.

Referenced by Util::initStatic().

◆ commitMpiType()

void Util::Vector::commitMpiType ( )
static

Commit MPI datatype MpiTraits<Vector>::type.

Commit MPI Datatype.

Definition at line 97 of file Vector.cpp.

References Util::MpiStructBuilder::addMember(), Util::MpiStructBuilder::commit(), and Util::MpiStructBuilder::setBase().

Friends And Related Function Documentation

◆ operator== [1/2]

bool operator== ( const Vector v1,
const Vector v2 
)
friend

Equality for Vectors.

Definition at line 26 of file Vector.cpp.

◆ operator== [2/2]

bool operator== ( const Vector v1,
const double *  v2 
)
friend

Equality of Vector and C array.

Definition at line 36 of file Vector.cpp.

◆ operator>>

std::istream & operator>> ( std::istream &  in,
Vector vector 
)
friend

istream extractor for a Vector.

Input elements of a vector from stream, without line breaks.

Parameters
ininput stream
vectorVector to be read from stream
Returns
modified input stream

Definition at line 65 of file Vector.cpp.

◆ operator<<

std::ostream & operator<< ( std::ostream &  out,
const Vector vector 
)
friend

ostream inserter for a Vector.

Output elements of a vector to stream, without line breaks.

Parameters
outoutput stream
vectorVector to be written to stream
Returns
modified output stream

Definition at line 76 of file Vector.cpp.

Member Data Documentation

◆ Zero

const Vector Util::Vector::Zero = Vector(0.0)
static

Zero Vector = {0.0, 0.0, 0.0}.

Definition at line 369 of file Vector.h.


The documentation for this class was generated from the following files: