1 /*
2    Copyright (c) 2009-2014, Jack Poulson
3    All rights reserved.
4 
5    This file is part of Elemental and is under the BSD 2-Clause License,
6    which can be found in the LICENSE file in the root directory, or at
7    http://opensource.org/licenses/BSD-2-Clause
8 */
9 #pragma once
10 #ifndef ELEM_GEAR_HPP
11 #define ELEM_GEAR_HPP
12 
13 #include "./Zeros.hpp"
14 
15 namespace elem {
16 
17 template<typename T>
18 inline void
Gear(Matrix<T> & G,Int n,Int s,Int t)19 Gear( Matrix<T>& G, Int n, Int s, Int t )
20 {
21     DEBUG_ONLY(CallStackEntry cse("Gear"))
22     if( s == 0 || s > n || s < -n )
23         LogicError("Invalid s value");
24     if( t == 0 || t > n || t < -n )
25         LogicError("Invalid t value");
26 
27     Zeros( G, n, n );
28     for( Int j=0; j<n-1; ++j )
29     {
30         G.Set( j, j+1, T(1) );
31         G.Set( j+1, j, T(1) );
32     }
33 
34     if( s > 0 )
35         G.Set( 0, s-1, T(1) );
36     else
37         G.Set( 0, (-s)-1, T(-1) );
38 
39     if( t > 0 )
40         G.Set( n-1, n-t, T(1) );
41     else
42         G.Set( n-1, n+t, T(-1) );
43 }
44 
45 template<typename T,Dist U,Dist V>
46 inline void
Gear(DistMatrix<T,U,V> & G,Int n,Int s,Int t)47 Gear( DistMatrix<T,U,V>& G, Int n, Int s, Int t )
48 {
49     DEBUG_ONLY(CallStackEntry cse("Gear"))
50     if( s == 0 || s > n || s < -n )
51         LogicError("Invalid s value");
52     if( t == 0 || t > n || t < -n )
53         LogicError("Invalid t value");
54 
55     Zeros( G, n, n );
56     for( Int j=0; j<n-1; ++j )
57     {
58         G.Set( j, j+1, T(1) );
59         G.Set( j+1, j, T(1) );
60     }
61 
62     if( s > 0 )
63         G.Set( 0, s-1, T(1) );
64     else
65         G.Set( 0, (-s)-1, T(-1) );
66 
67     if( t > 0 )
68         G.Set( n-1, n-t, T(1) );
69     else
70         G.Set( n-1, n+t, T(-1) );
71 }
72 
73 template<typename T,Dist U,Dist V>
74 inline void
Gear(BlockDistMatrix<T,U,V> & G,Int n,Int s,Int t)75 Gear( BlockDistMatrix<T,U,V>& G, Int n, Int s, Int t )
76 {
77     DEBUG_ONLY(CallStackEntry cse("Gear"))
78     if( s == 0 || s > n || s < -n )
79         LogicError("Invalid s value");
80     if( t == 0 || t > n || t < -n )
81         LogicError("Invalid t value");
82 
83     Zeros( G, n, n );
84     for( Int j=0; j<n-1; ++j )
85     {
86         G.Set( j, j+1, T(1) );
87         G.Set( j+1, j, T(1) );
88     }
89 
90     if( s > 0 )
91         G.Set( 0, s-1, T(1) );
92     else
93         G.Set( 0, (-s)-1, T(-1) );
94 
95     if( t > 0 )
96         G.Set( n-1, n-t, T(1) );
97     else
98         G.Set( n-1, n+t, T(-1) );
99 }
100 
101 } // namespace elem
102 
103 #endif // ifndef ELEM_GEAR_HPP
104