KASKADE 7 development version
Loading...
Searching...
No Matches
vectorOps.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2025 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef VECTOROPS_HH
14#define VECTOROPS_HH
15
16#include <cmath>
17
19// forward declarations
20namespace Dune
21{
22 template <class Entry, int dim>
23 class FieldVector;
24
25 template <class Entry, class A>
26 class BlockVector;
27}
29
30namespace Kaskade
31{
32 template <class Entry, class A>
35 {
36 Dune::BlockVector<Entry,A> z(x.N()+y.N());
37
38 for (int i=0; i<x.N(); ++i)
39 z[i] = x[i];
40
41 for (int i=0; i<y.N(); ++i)
42 z[x.N()+i] = y[i];
43
44 return z;
45 }
46
51 template <class Scalar, int n>
53 {
54 static_assert(n>1,"No orthogonal vector in R^1");
55
56 if constexpr (n==2) // apply rotation
57 {
58 Dune::FieldVector<Scalar,n> y{x[1],-x[0]};
59 return y;
60 }
61 else
62 {
63 // Find the two largest (by magnitude) entries and rotate in that plane.
64 std::pair<int,Scalar> m1(0,std::abs(x[0])), m2(1,std::abs(x[1]));
65
66 for (int k=2; k<n; ++k)
67 {
68 if (m1.second < m2.second) // sort such that m1 is larger
69 std::swap(m1,m2);
70 if (std::abs(x[k]) > m2.second) // if x[k] is larger than the smaller m2,
71 m2 = std::pair(k,std::abs(x[k])); // replace the smaller one by x[k]
72 }
73
74 Dune::FieldVector<Scalar,n> y = x; // rotate in the plane of the two largest
75 y[m1.first] = x[m2.first]; // entries
76 y[m2.first] = -x[m1.first];
77
78 // Now, y is still not orthogonal to x, but almost, so we can do Gram-Schmidt.
79 y -= (x*y)/y.two_norm()/x.two_norm2() * x;
80 return y;
81 }
82 }
83
84}
85
86#endif
Dune::FieldVector< Scalar, n > orthogonalTo(Dune::FieldVector< Scalar, n > const &x)
Find an (arbitrary) vector orthogonal to the given one.
Definition vectorOps.hh:52
Dune::BlockVector< Entry, A > vertcat(Dune::BlockVector< Entry, A > const &x, Dune::BlockVector< Entry, A > const &y)
Definition vectorOps.hh:33