20#if defined(_MSC_VER) && (_MSC_VER < 1800)
22#define isfinite _finite
39template<
typename dtype>
59 const int s = v.
Size();
65 for (
int i = 0; i <
s; i++)
80 const int s = v.
Size();
86 for (
int i = 0; i <
s; i++)
132 void Load(std::istream **in,
int np,
int *
dim)
137 for (i = 0; i < np; i++)
145 for (i = 0; i < np; i++)
147 for (j = 0; j <
dim[i]; j++)
150 data[
p++] = dtype(tmpd);
160 for (
int i = 0; i <
size; i++)
163 data[i] = dtype(tmpd);
257 return const_cast<dtype *
>((
const dtype *)
data);
263 inline operator dtype *() {
return data; }
268 inline operator const dtype *()
const {
return data; }
299 MFEM_ASSERT(
data && i >= 0 && i <
size,
300 "index [" << i <<
"] is out of range [0," <<
size <<
")");
309 MFEM_ASSERT(
data && i >= 0 && i <
size,
310 "index [" << i <<
"] is out of range [0," <<
size <<
")");
319 for (
int i = 0; i <
size; i++)
321 dot +=
data[i] * v[i];
329 MFEM_ASSERT(
size == v.
Size(),
"incompatible Vectors!");
331 for (
int i = 0; i <
size; i++)
333 dot +=
data[i] * v[i];
340 MFEM_ASSERT(
size == v.
Size(),
"incompatible Vectors!");
342 for (
int i = 0; i <
size; i++)
344 dot +=
data[i] * v[i];
352 for (
int i = 0; i <
size; i++)
365 for (
int i = 0; i <
size; i++)
375 for (
int i = 0; i <
size; i++)
383 template<
typename ivtype>
386 for (
int i = 0; i <
size; i++)
388 data[i] = (dtype) value;
393 template<
typename ivtype>
396 for (
int i = 0; i <
size; i++)
403 template<
typename ivtype>
406 for (
int i = 0; i <
size; i++)
415 MFEM_ASSERT(
size == v.
Size(),
"incompatible Vectors!");
416 for (
int i = 0; i <
size; i++)
423 template<
typename ivtype>
426 for (
int i = 0; i <
size; i++)
435 MFEM_ASSERT(
size == v.
Size(),
"incompatible Vectors!");
436 for (
int i = 0; i <
size; i++)
443 template<
typename ivtype>
446 for (
int i = 0; i <
size; i++)
454 template<
typename ivtype,
typename vtype>
457 MFEM_ASSERT(
size == v.Size(),
"incompatible Vectors!");
458 for (
int i = 0; i <
size; i++)
466 template<
typename ivtype,
typename vtype>
469 MFEM_ASSERT(
size == v.Size(),
"incompatible Vectors!");
470 for (
int i = 0; i <
size; i++)
477 template<
typename vtype>
480 MFEM_ASSERT(v.Size() + offset <=
size,
"invalid sub-vector");
481 for (
int i = 0; i <
size; i++)
483 data[i + offset] = v[i];
490 for (
int i = 0; i <
size; i++)
505 template<
typename vtype1,
typename vtype2>
508 MFEM_ASSERT(v1.Size() == v.
Size(),
"incompatible Vectors!");
509 MFEM_ASSERT(v2.Size() == v.
Size(),
"incompatible Vectors!");
510 for (
int i = 0; i < v.
Size(); i++)
512 v[i] = v1[i] + v2[i];
517 template<
typename vtype1,
typename vtype2>
518 friend void add(
const vtype1 &v1,
523 MFEM_ASSERT(v1.Size() == v.
Size(),
"incompatible Vectors!");
524 MFEM_ASSERT(v2.Size() == v.
Size(),
"incompatible Vectors!");
525 for (
int i = 0; i < v.
Size(); i++)
527 v[i] = v1[i] +
alpha * v2[i];
531 template<
typename vtype1,
typename vtype2>
538 MFEM_ASSERT(x.Size() == y.Size() && x.Size() == z.
Size(),
539 "incompatible Vectors!");
541 for (
int i = 0; i < z.
Size(); i++)
543 z[i] =
a * x[i] +
b * y[i];
547 template<
typename vtype1,
typename vtype2>
553 MFEM_ASSERT(x.Size() == y.Size() && x.Size() == z.
Size(),
554 "incompatible Vectors!");
556 for (
int i = 0; i < z.
Size(); i++)
558 z[i] =
a * x[i] + y[i];
562 template<
typename vtype1,
typename vtype2>
563 friend void subtract(
const vtype1 &x,
const vtype2 &y,
566 MFEM_ASSERT(x.Size() == y.Size() && x.Size() == z.
Size(),
567 "incompatible Vectors!");
568 for (
int i = 0; i < z.
Size(); i++)
574 template<
typename ivtype,
typename vtype1,
typename vtype2>
580 MFEM_ASSERT(x.Size() == y.Size() && x.Size() == z.
Size(),
581 "incompatible Vectors!");
582 for (
int i = 0; i < z.
Size(); i++)
584 z[i] =
a * (x[i] - y[i]);
625 seed = (int) time(0);
629 srand((
unsigned) seed);
631 for (
int i = 0; i <
size; i++)
655 for (
int i = 0; i <
size; i++)
659 const dtype absdata = abs(
data[i]);
660 if (scale <= absdata)
662 const dtype sqr_arg = scale / absdata;
663 sum = 1.0 + sum * (sqr_arg * sqr_arg);
667 const dtype sqr_arg = absdata / scale;
668 sum += (sqr_arg * sqr_arg);
671 return scale * sqrt(sum);
678 for (
int i = 0; i <
size; i++)
680 max = max(abs(
data[i]), max);
688 for (
int i = 0; i <
size; i++)
Templated vector data type.
TAutoDiffVector & operator/=(ivtype c)
dtype Normlinf() const
Returns the l_infinity norm of the vector.
bool OwnsData() const
Read the Vector data (host pointer) ownership flag.
dtype * GetData() const
Return a pointer to the beginning of the Vector data.
void NewDataAndSize(dtype *d, int s)
Set the Vector data and size, deleting the old data, if owned.
void Destroy()
Destroy a vector.
void Swap(TAutoDiffVector< dtype > &other)
Swap the contents of two Vectors.
TAutoDiffVector(int s)
Creates vector of size s.
int Capacity() const
Return the size of the currently allocated data array.
TAutoDiffVector< dtype > & operator=(const dtype *v)
Copy Size() entries from v.
dtype operator*(const Vector &v) const
void StealData(dtype **p)
Changes the ownership of the data; after the call the Vector is empty.
TAutoDiffVector & Add(const ivtype a, const vtype &v)
(*this) += a * Va
void MakeRef(TAutoDiffVector< dtype > &base, int offset)
Reset the Vector to be a reference to a sub-vector of base without changing its current size.
const dtype & Elem(int i) const
Read only access to Vector entries. Index i = 0 .. size-1.
TAutoDiffVector & operator=(ivtype value)
Redefine '=' for vector = constant.
TAutoDiffVector & operator+=(const TAutoDiffVector< dtype > &v)
TAutoDiffVector & Set(const ivtype a, const vtype &v)
(*this) = a * x
dtype operator*(const TAutoDiffVector< dtype > &v) const
Return the inner-product.
friend void subtract(const ivtype a, const vtype1 &x, const vtype2 &y, TAutoDiffVector< dtype > &z)
dtype & operator()(int i)
Access Vector entries using () for 0-based indexing.
void Print(std::ostream &os=mfem::out, int width=8) const
Prints vector to stream os with width entries per line.
TAutoDiffVector(const TAutoDiffVector< dtype > &v)
Copy constructor. Allocates a new data array and copies the data.
void Load(std::istream &in, int Size)
Load a vector from an input stream.
void MakeRef(TAutoDiffVector< dtype > &base, int offset, int size_)
Reset the Vector to be a reference to a sub-vector of base.
TAutoDiffVector & operator+=(ivtype v)
TAutoDiffVector & operator*=(ivtype c)
void Randomize(int seed=0)
Set random values in the vector.
TAutoDiffVector< dtype > & operator=(const TAutoDiffVector< dtype > &v)
Copy assignment.
dtype Norml2() const
Returns the l2 norm of the vector.
TAutoDiffVector(const Vector &v)
friend void subtract(const vtype1 &x, const vtype2 &y, TAutoDiffVector< dtype > &z)
void Load(std::istream &in)
Load a vector from an input stream, reading the size from the stream.
~TAutoDiffVector()
Destroys vector.
friend void add(const vtype1 &v1, const vtype2 &v2, TAutoDiffVector< dtype > &v)
Set v = v1 + v2.
void Neg()
(*this) = -(*this)
void SetVector(const vtype &v, int offset)
dtype & Elem(int i)
Access Vector entries. Index i = 0 .. size-1.
TAutoDiffVector & operator-=(ivtype v)
dtype Norml1() const
Returns the l_1 norm of the vector.
TAutoDiffVector()
Default constructor for Vector. Sets size = 0 and data = NULL.
const dtype & operator()(int i) const
Read only access to Vector entries using () for 0-based indexing.
friend void add(const dtype a, const vtype1 &x, const dtype b, const vtype2 &y, TAutoDiffVector< dtype > &z)
friend void add(const dtype a, const vtype1 &x, const vtype2 &y, TAutoDiffVector< dtype > &z)
int Size() const
Returns the size of the vector.
void SetSize(int s)
Resize the vector to size s.
void SetDataAndSize(dtype *d, int s)
Set the Vector data and size.
TAutoDiffVector & operator-=(const TAutoDiffVector< dtype > &v)
dtype * StealData()
Changes the ownership of the data; after the call the Vector is empty.
TAutoDiffVector(dtype *_data, int _size)
Creates a vector referencing an array of doubles, owned by someone else.
void Load(std::istream **in, int np, int *dim)
Reads a vector from multiple files.
dtype operator*(const dtype *v) const
Dot product with a dtype * array.
TAutoDiffVector< dtype > & operator=(const Vector &v)
friend void add(const vtype1 &v1, dtype alpha, const vtype2 &v2, TAutoDiffVector< dtype > &v)
Set v = v1 + alpha * v2.
int Size() const
Returns the size of the vector.
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
real_t rand_real()
Generate a random real_t number in the interval [0,1) using rand().
real_t p(const Vector &x, real_t t)