12 #ifndef MFEM_TADVECTOR
13 #define MFEM_TADVECTOR
20 #if defined(_MSC_VER) && (_MSC_VER < 1800)
22 #define isfinite _finite
39 template<
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]);
622 const double max = (double) (RAND_MAX) + 1.;
626 seed = (int) time(0);
630 srand((
unsigned) seed);
632 for (
int i = 0; i <
size; i++)
634 data[i] = std::abs(rand() / max);
656 for (
int i = 0; i <
size; i++)
660 const dtype absdata = abs(
data[i]);
661 if (scale <= absdata)
663 const dtype sqr_arg = scale / absdata;
664 sum = 1.0 + sum * (sqr_arg * sqr_arg);
668 const dtype sqr_arg = absdata / scale;
669 sum += (sqr_arg * sqr_arg);
672 return scale * sqrt(sum);
679 for (
int i = 0; i <
size; i++)
681 max = max(abs(
data[i]), max);
689 for (
int i = 0; i <
size; i++)
void Print(std::ostream &os=mfem::out, int width=8) const
Prints vector to stream os with width entries per line.
TAutoDiffVector & operator+=(const TAutoDiffVector< dtype > &v)
dtype * GetData() const
Return a pointer to the beginning of the Vector data.
TAutoDiffVector & operator-=(const TAutoDiffVector< dtype > &v)
void Load(std::istream **in, int np, int *dim)
Reads a vector from multiple files.
void NewDataAndSize(dtype *d, int s)
Set the Vector data and size, deleting the old data, if owned.
dtype & operator()(int i)
Access Vector entries using () for 0-based indexing.
TAutoDiffVector< dtype > & operator=(const dtype *v)
Copy Size() entries from v.
dtype Norml1() const
Returns the l_1 norm of the vector.
dtype operator*(const Vector &v) const
int Size() const
Returns the size of the vector.
~TAutoDiffVector()
Destroys vector.
TAutoDiffVector & operator=(ivtype value)
Redefine '=' for vector = constant.
TAutoDiffVector()
Default constructor for Vector. Sets size = 0 and data = NULL.
TAutoDiffVector< dtype > & operator=(const Vector &v)
const dtype & operator()(int i) const
Read only access to Vector entries using () for 0-based indexing.
TAutoDiffVector(int s)
Creates vector of size s.
int Capacity() const
Return the size of the currently allocated data array.
friend void subtract(const vtype1 &x, const vtype2 &y, TAutoDiffVector< dtype > &z)
void StealData(dtype **p)
Changes the ownership of the data; after the call the Vector is empty.
friend void add(const vtype1 &v1, dtype alpha, const vtype2 &v2, TAutoDiffVector< dtype > &v)
Set v = v1 + alpha * v2.
void Randomize(int seed=0)
Set random values in the vector.
void SetVector(const vtype &v, int offset)
int Size() const
Returns the size of the vector.
TAutoDiffVector(const TAutoDiffVector< dtype > &v)
Copy constructor. Allocates a new data array and copies the data.
friend void add(const dtype a, const vtype1 &x, const dtype b, const vtype2 &y, TAutoDiffVector< dtype > &z)
TAutoDiffVector & Add(const ivtype a, const vtype &v)
(*this) += a * Va
bool OwnsData() const
Read the Vector data (host pointer) ownership flag.
dtype Norml2() const
Returns the l2 norm of the vector.
dtype operator*(const dtype *v) const
Dot product with a dtype * array.
void Neg()
(*this) = -(*this)
void SetSize(int s)
Resize the vector to size s.
double p(const Vector &x, double t)
TAutoDiffVector & operator/=(ivtype c)
const dtype & Elem(int i) const
Read only access to Vector entries. Index i = 0 .. size-1.
TAutoDiffVector & operator+=(ivtype v)
friend void add(const vtype1 &v1, const vtype2 &v2, TAutoDiffVector< dtype > &v)
Set v = v1 + v2.
friend void add(const dtype a, const vtype1 &x, const vtype2 &y, TAutoDiffVector< dtype > &z)
void SetDataAndSize(dtype *d, int s)
Set the Vector data and size.
dtype operator*(const TAutoDiffVector< dtype > &v) const
Return the inner-product.
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...
dtype * StealData()
Changes the ownership of the data; after the call the Vector is empty.
TAutoDiffVector & Set(const ivtype a, const vtype &v)
(*this) = a * x
void Load(std::istream &in, int Size)
Load a vector from an input stream.
void Swap(TAutoDiffVector< dtype > &other)
Swap the contents of two Vectors.
friend void subtract(const ivtype a, const vtype1 &x, const vtype2 &y, TAutoDiffVector< dtype > &z)
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 c)
void Load(std::istream &in)
Load a vector from an input stream, reading the size from the stream.
TAutoDiffVector & operator-=(ivtype v)
Templated vector data type.
void Destroy()
Destroy a vector.
TAutoDiffVector(dtype *_data, int _size)
Creates a vector referencing an array of doubles, owned by someone else.
TAutoDiffVector(const Vector &v)
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
TAutoDiffVector< dtype > & operator=(const TAutoDiffVector< dtype > &v)
Copy assignment.
dtype Normlinf() const
Returns the l_infinity norm of the vector.
dtype & Elem(int i)
Access Vector entries. Index i = 0 .. size-1.