MyraMath
swaps.h
Go to the documentation of this file.
1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
6 #ifndef MYRAMATH_DENSE_SWAPS_H
7 #define MYRAMATH_DENSE_SWAPS_H
8 
14 #include <myramath/MYRAMATH_EXPORT.h>
15 
18 #include <myramath/utility/detail/ssize.h>
19 #include <myramath/utility/detail/sdistance.h>
20 
22 
23 #include <vector>
24 
25 namespace myra {
26 
27 // Forward declarations.
28 class intRange;
29 class intCRange;
30 template<class Number> class VectorRange;
31 template<class Number> class MatrixRange;
32 template<class Number> class Matrix;
33 
34 // --------------------------------------- Generating and manipulating permutations.
35 
37 MYRAMATH_EXPORT void inverse_perm(const intCRange& perm, const intRange& iperm);
39 MYRAMATH_EXPORT std::vector<int> inverse_perm(const intCRange& perm);
41 
43 MYRAMATH_EXPORT void swaps2iswaps(const intCRange& swaps, const intRange& iswaps);
45 MYRAMATH_EXPORT std::vector<int> swaps2iswaps(const intCRange& swaps);
47 
49 MYRAMATH_EXPORT void perm2swaps(const intCRange& perm, const intRange& swaps);
51 MYRAMATH_EXPORT std::vector<int> perm2swaps(const intCRange& perm);
53 
55 MYRAMATH_EXPORT void swaps2perm (const intCRange& swaps, const intRange& perm);
57 MYRAMATH_EXPORT std::vector<int> swaps2perm (const intCRange& swaps);
59 
61 // .. same result to inverse_perm(swaps2perm(swaps)), but more efficient
63 MYRAMATH_EXPORT void swaps2iperm (const intCRange& swaps, const intRange& perm);
64 MYRAMATH_EXPORT std::vector<int> swaps2iperm (const intCRange& swaps);
66 
68 template<class Number> Matrix<Number> swaps2Matrix (const intCRange& swaps);
70 template<> MYRAMATH_EXPORT Matrix<NumberS> swaps2Matrix<NumberS> (const intCRange& swaps);
71 template<> MYRAMATH_EXPORT Matrix<NumberD> swaps2Matrix<NumberD> (const intCRange& swaps);
72 template<> MYRAMATH_EXPORT Matrix<NumberC> swaps2Matrix<NumberC> (const intCRange& swaps);
73 template<> MYRAMATH_EXPORT Matrix<NumberZ> swaps2Matrix<NumberZ> (const intCRange& swaps);
75 
76 // --------------------------------------- Applying swap sequences to numeric containers.
77 
79 MYRAMATH_EXPORT void swap_rows(const intCRange& swaps, const MatrixRange<NumberS>& X);
81 MYRAMATH_EXPORT void swap_rows(const intCRange& swaps, const MatrixRange<NumberD>& X);
82 MYRAMATH_EXPORT void swap_rows(const intCRange& swaps, const MatrixRange<NumberC>& X);
83 MYRAMATH_EXPORT void swap_rows(const intCRange& swaps, const MatrixRange<NumberZ>& X);
85 
87 MYRAMATH_EXPORT void iswap_rows(const intCRange& swaps, const MatrixRange<NumberS>& X);
89 MYRAMATH_EXPORT void iswap_rows(const intCRange& swaps, const MatrixRange<NumberD>& X);
90 MYRAMATH_EXPORT void iswap_rows(const intCRange& swaps, const MatrixRange<NumberC>& X);
91 MYRAMATH_EXPORT void iswap_rows(const intCRange& swaps, const MatrixRange<NumberZ>& X);
93 
95 MYRAMATH_EXPORT void swap_columns(const intCRange& swaps, const MatrixRange<NumberS>& X);
97 MYRAMATH_EXPORT void swap_columns(const intCRange& swaps, const MatrixRange<NumberD>& X);
98 MYRAMATH_EXPORT void swap_columns(const intCRange& swaps, const MatrixRange<NumberC>& X);
99 MYRAMATH_EXPORT void swap_columns(const intCRange& swaps, const MatrixRange<NumberZ>& X);
101 
103 MYRAMATH_EXPORT void iswap_columns(const intCRange& swaps, const MatrixRange<NumberS>& X);
105 MYRAMATH_EXPORT void iswap_columns(const intCRange& swaps, const MatrixRange<NumberD>& X);
106 MYRAMATH_EXPORT void iswap_columns(const intCRange& swaps, const MatrixRange<NumberC>& X);
107 MYRAMATH_EXPORT void iswap_columns(const intCRange& swaps, const MatrixRange<NumberZ>& X);
109 
111 MYRAMATH_EXPORT void swap_vector(const intCRange& swaps, const VectorRange<NumberS>& x);
113 MYRAMATH_EXPORT void swap_vector(const intCRange& swaps, const VectorRange<NumberD>& x);
114 MYRAMATH_EXPORT void swap_vector(const intCRange& swaps, const VectorRange<NumberC>& x);
115 MYRAMATH_EXPORT void swap_vector(const intCRange& swaps, const VectorRange<NumberZ>& x);
117 
119 MYRAMATH_EXPORT void swap_vector(const intCRange& swaps, const intRange& x);
120 
122 MYRAMATH_EXPORT void iswap_vector(const intCRange& swaps, const VectorRange<NumberS>& x);
124 MYRAMATH_EXPORT void iswap_vector(const intCRange& swaps, const VectorRange<NumberD>& x);
125 MYRAMATH_EXPORT void iswap_vector(const intCRange& swaps, const VectorRange<NumberC>& x);
126 MYRAMATH_EXPORT void iswap_vector(const intCRange& swaps, const VectorRange<NumberZ>& x);
128 
130 MYRAMATH_EXPORT void iswap_vector(const intCRange& swaps, const intRange& x);
131 
132 
134 template<class T> void swap_range(const intCRange& swaps, T* begin, T* end)
135  {
136  if (swaps.N != sdistance(begin,end))
137  throw eprintf("swap_range(): size mismatch, swaps.N != range size [%d != %d]", swaps.N, sdistance(begin,end));
138  int N = swaps.N;
139  for (int n = 0; n < N; ++n)
140  std::swap( begin[n], begin[swaps[n]] );
141  }
142 
144 template<class T> void iswap_range(const intCRange& swaps, T* begin, T* end)
145  {
146  if (swaps.N != sdistance(begin,end))
147  throw eprintf("iswap_range(): size mismatch, swaps.N != range size [%d != %d]", swaps.N, sdistance(begin,end));
148  int N = swaps.N;
149  for (int n = N-1; n >= 0; --n)
150  std::swap( begin[n], begin[swaps[n]] );
151  }
152 
154 template<class T> void swap_vector(const intCRange& swaps, std::vector<T>& v)
155  { swap_range(swaps, v.data(),v.data()+v.size() ); }
156 
158 template<class T> void iswap_vector(const intCRange& swaps, std::vector<T>& v)
159  { iswap_range(swaps, v.data(),v.data()+v.size() ); }
160 
161 
162 } // namespace
163 
164 #endif
Returns a std::runtime_error() whose message has been populated using printf()-style formatting...
Matrix< Number > swaps2Matrix(const intCRange &swaps)
Given a swaps sequence, returns an equivalent permutation Matrix, P.
void iswap_range(const intCRange &swaps, T *begin, T *end)
Inverts swaps on a range of T*.
Definition: swaps.h:144
int size() const
Size inspector.
Definition: intRange.cpp:205
void swap_range(const intCRange &swaps, T *begin, T *end)
Applies swaps on a range of T*.
Definition: swaps.h:134
Definition: syntax.dox:1
Various utility functions/classes related to scalar Number types.
int N
---------— Data members, all public ----------------—
Definition: intRange.h:149
Represents a const intRange.
Definition: intRange.h:142
Interface class for representing subranges of contiguous int&#39;s.