Intrepid
Intrepid_MiniTensor_Vector.i.h
1 // @HEADER
2 // ************************************************************************
3 //
4 // Intrepid Package
5 // Copyright (2007) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions: Alejandro Mota (amota@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #if !defined(Intrepid_MiniTensor_Vector_i_h)
43 #define Intrepid_MiniTensor_Vector_i_h
44 
45 namespace Intrepid
46 {
47 
48 //
49 // Default constructor
50 //
51 template<typename T>
52 inline
55 {
56  return;
57 }
58 
59 //
60 // Constructor that initializes to NaNs
61 //
62 template<typename T>
63 inline
64 Vector<T>::Vector(Index const dimension) :
65 TensorBase<T>::TensorBase(dimension, order)
66 {
67  return;
68 }
69 
73 template<typename T>
74 inline
75 Vector<T>::Vector(Index const dimension, ComponentValue value) :
76 TensorBase<T>::TensorBase(dimension, order, value)
77 {
78  return;
79 }
80 
81 //
82 // Create vector from a scalar
83 //
84 template<typename T>
85 inline
86 Vector<T>::Vector(Index const dimension, T const & s) :
87 TensorBase<T>::TensorBase(dimension, order, s)
88 {
89  return;
90 }
91 
92 //
93 // Create vector specifying components
94 //
95 template<typename T>
96 inline
97 Vector<T>::Vector(T const & s0, T const & s1)
98 {
99  Vector<T> &
100  self = (*this);
101 
102  self.set_dimension(2);
103 
104  self[0] = s0;
105  self[1] = s1;
106 
107  return;
108 }
109 
110 //
111 // Create vector specifying components
112 //
113 template<typename T>
114 inline
115 Vector<T>::Vector(T const & s0, T const & s1, T const & s2)
116 {
117  Vector<T> &
118  self = (*this);
119 
120  self.set_dimension(3);
121 
122  self[0] = s0;
123  self[1] = s1;
124  self[2] = s2;
125 
126  return;
127 }
128 
129 //
130 // Create vector from array
131 //
132 template<typename T>
133 inline
134 Vector<T>::Vector(Index const dimension, T const * data_ptr) :
135 TensorBase<T>::TensorBase(dimension, order, data_ptr)
136 {
137  return;
138 }
139 
140 //
141 // Copy constructor
142 //
143 template<typename T>
144 inline
146 TensorBase<T>::TensorBase(v)
147 {
148  return;
149 }
150 
151 //
152 // Simple destructor
153 //
154 template<typename T>
155 inline
157 {
158  return;
159 }
160 
161 //
162 // Indexing for constant vector
163 //
164 template<typename T>
165 inline
166 T const &
167 Vector<T>::operator()(Index const i) const
168 {
169  return (*this)[i];
170 }
171 
172 //
173 // Vector indexing
174 //
175 template<typename T>
176 inline
177 T &
178 Vector<T>::operator()(Index const i)
179 {
180  return (*this)[i];
181 }
182 
183 //
184 // Vector addition
185 //
186 template<typename S, typename T>
187 inline
189 operator+(Vector<S> const & u, Vector<T> const & v)
190 {
192  w;
193 
194  add(u, v, w);
195 
196  return w;
197 }
198 
199 //
200 // Vector subtraction
201 //
202 template<typename S, typename T>
203 inline
204 Vector<typename Promote<S, T>::type>
205 operator-(Vector<S> const & u, Vector<T> const & v)
206 {
207  Vector<typename Promote<S, T>::type>
208  w;
209 
210  subtract(u, v, w);
211 
212  return w;
213 }
214 
215 //
216 // Vector minus
217 //
218 template<typename T>
219 inline
220 Vector<T>
221 operator-(Vector<T> const & u)
222 {
223  Vector<T>
224  v;
225 
226  minus(u, v);
227 
228  return v;
229 }
230 
231 //
232 // Vector dot product
233 //
234 template<typename S, typename T>
235 inline
236 typename Promote<S, T>::type
237 operator*(Vector<S> const & u, Vector<T> const & v)
238 {
239  return dot(u, v);
240 }
241 
242 //
243 // Vector equality tested by components
244 //
245 template<typename T>
246 inline
247 bool
248 operator==(Vector<T> const & u, Vector<T> const & v)
249 {
250  return equal(u, v);
251 }
252 
253 //
254 // Vector inequality tested by components
255 //
256 template<typename T>
257 inline
258 bool
259 operator!=(Vector<T> const & u, Vector<T> const & v)
260 {
261  return not_equal(u, v);
262 }
263 
264 //
265 // Scalar vector product
266 //
267 template<typename S, typename T>
268 inline
269 typename lazy_disable_if< order_1234<S>, apply_vector< Promote<S,T> > >::type
270 operator*(S const & s, Vector<T> const & u)
271 {
272  Vector<typename Promote<S, T>::type>
273  v;
274 
275  scale(u, s, v);
276 
277  return v;
278 }
279 
280 //
281 // Vector scalar product
282 //
283 template<typename S, typename T>
284 inline
285 typename lazy_disable_if< order_1234<S>, apply_vector< Promote<S,T> > >::type
286 operator*(Vector<T> const & u, S const & s)
287 {
288  Vector<typename Promote<S, T>::type>
289  v;
290 
291  scale(u, s, v);
292 
293  return v;
294 }
295 
296 //
297 // Vector scalar division
298 //
299 template<typename S, typename T>
300 inline
301 Vector<typename Promote<S, T>::type>
302 operator/(Vector<T> const & u, S const & s)
303 {
304  Vector<typename Promote<S, T>::type>
305  v;
306 
307  divide(u, s, v);
308 
309  return v;
310 }
311 
312 //
313 // Vector dot product
314 //
315 template<typename S, typename T>
316 inline
317 typename Promote<S, T>::type
318 dot(Vector<S> const & u, Vector<T> const & v)
319 {
320  Index const
321  N = u.get_dimension();
322 
323  assert(v.get_dimension() == N);
324 
325  typename Promote<S, T>::type
326  s = 0.0;
327 
328  switch (N) {
329 
330  default:
331  for (Index i = 0; i < N; ++i) {
332  s += u(i) * v(i);
333  }
334  break;
335 
336  case 3:
337  s = u(0) * v(0) + u(1) * v(1) + u(2) * v(2);
338  break;
339 
340  case 2:
341  s = u(0) * v(0) + u(1) * v(1);
342  break;
343 
344  }
345 
346  return s;
347 }
348 
349 //
350 // Cross product only valid for R^3.
351 //
352 template<typename S, typename T>
353 inline
354 Vector<typename Promote<S, T>::type>
355 cross(Vector<S> const & u, Vector<T> const & v)
356 {
357  Index const
358  N = u.get_dimension();
359 
360  assert(v.get_dimension() == N);
361 
362  Vector<typename Promote<S, T>::type>
363  w(N);
364 
365  switch (N) {
366 
367  case 3:
368  w(0) = u(1) * v(2) - u(2) * v(1);
369  w(1) = u(2) * v(0) - u(0) * v(2);
370  w(2) = u(0) * v(1) - u(1) * v(0);
371  break;
372 
373  default:
374  std::cerr << "ERROR: " << __PRETTY_FUNCTION__;
375  std::cerr << std::endl;
376  std::cerr << "Cross product undefined for R^" << N;
377  std::cerr << std::endl;
378  exit(1);
379  break;
380 
381  }
382 
383  return w;
384 }
385 
386 //
387 // R^N vector 2-norm
388 // \return \f$ \sqrt{u \cdot u} \f$
389 //
390 template<typename T>
391 inline
392 T
393 norm(Vector<T> const & u)
394 {
395  Index const
396  N = u.get_dimension();
397 
398  T
399  s = 0.0;
400 
401  switch (N) {
402 
403  default:
404  s = std::sqrt(dot(u, u));
405  break;
406 
407  case 3:
408  s = std::sqrt(u(0) * u(0) + u(1) * u(1) + u(2) * u(2));
409  break;
410 
411  case 2:
412  s = std::sqrt(u(0) * u(0) + u(1) * u(1));
413  break;
414 
415  }
416 
417  return s;
418 }
419 
420 //
421 // R^N vector 2-norm square for fast distance calculations.
422 // \return \f$ u \cdot u \f$
423 //
424 template<typename T>
425 inline
426 T
427 norm_square(Vector<T> const & u)
428 {
429  Index const
430  N = u.get_dimension();
431 
432  T
433  s = 0.0;
434 
435  switch (N) {
436 
437  default:
438  s = dot(u, u);
439  break;
440 
441  case 3:
442  s = u(0) * u(0) + u(1) * u(1) + u(2) * u(2);
443  break;
444 
445  case 2:
446  s = u(0) * u(0) + u(1) * u(1);
447  break;
448 
449  }
450 
451  return s;
452 }
453 
454 //
455 // R^N vector 1-norm
456 // \return \f$ \sum_i |u_i| \f$
457 //
458 template<typename T>
459 inline
460 T
461 norm_1(Vector<T> const & u)
462 {
463  Index const
464  N = u.get_dimension();
465 
466  T
467  s = 0.0;
468 
469  switch (N) {
470 
471  default:
472  for (Index i = 0; i < N; ++i) {
473  s += std::abs(u(i));
474  }
475  break;
476 
477  case 3:
478  s = std::abs(u(0)) + std::abs(u(1)) + std::abs(u(2));
479  break;
480 
481  case 2:
482  s = std::abs(u(0)) + std::abs(u(1));
483  break;
484 
485  }
486 
487  return s;
488 }
489 
490 //
491 // R^N vector infinity-norm
492 // \return \f$ \max(|u_0|,...|u_i|,...|u_N|) \f$
493 //
494 template<typename T>
495 inline
496 T
497 norm_infinity(Vector<T> const & u)
498 {
499  Index const
500  N = u.get_dimension();
501 
502  T
503  s = 0.0;
504 
505  switch (N) {
506 
507  default:
508  for (Index i = 0; i < N; ++i) {
509  s = std::max(s, std::abs(u(i)));
510  }
511  break;
512 
513  case 3:
514  s = std::max(std::max(std::abs(u(0)), std::abs(u(1))), std::abs(u(2)));
515  break;
516 
517  case 2:
518  s = std::max(std::abs(u(0)), std::abs(u(1)));
519  break;
520 
521  }
522 
523  return s;
524 }
525 
526 } // namespace Intrepid
527 
528 #endif // Intrepid_MiniTensor_Vector_i_h
void set_dimension(Index const dimension)
T const & operator()(Index const i) const