glucat 0.12.0
PyClical.h
Go to the documentation of this file.
1/***************************************************************************
2 GluCat : Generic library of universal Clifford algebra templates
3 PyClical.h : C++ definitions needed by PyClical
4 -------------------
5 copyright : (C) 2008-2021 by Paul C. Leopardi
6 ***************************************************************************
7
8 This library is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with this library. If not, see <http://www.gnu.org/licenses/>.
20
21 ***************************************************************************
22 This library is based on a prototype written by Arvind Raja and was
23 licensed under the LGPL with permission of the author. See Arvind Raja,
24 "Object-oriented implementations of Clifford algebras in C++: a prototype",
25 in Ablamowicz, Lounesto and Parra (eds.)
26 "Clifford algebras with numeric and symbolic computations", Birkhauser, 1996.
27 ***************************************************************************
28 See also Arvind Raja's original header comments in glucat/glucat.h
29 ***************************************************************************/
30// References for algorithms:
31// [DL]:
32// C. Doran and A. Lasenby, "Geometric algebra for physicists", Cambridge, 2003.
33
35#include "glucat/glucat.h"
36#include "glucat/glucat_imp.h"
37#include <iostream>
38#include <sstream>
39#include <iomanip>
40#include <limits>
41
44template <typename Scalar_T>
45inline PyObject* PyFloat_FromDouble(Scalar_T v)
46{ return ::PyFloat_FromDouble(glucat::numeric_traits<Scalar_T>::to_double(v)); }
47
48
49// String representations for use by PyClical Python classes.
50
51using String = std::string;
52
54
56template<typename Index_Set_T>
57inline String index_set_to_repr(const Index_Set_T& ist)
58{
59 std::ostringstream os;
60 os << "index_set(" << ist << ")";
61 return os.str();
62}
63
65template<typename Index_Set_T>
66inline String index_set_to_str(const Index_Set_T& ist)
67{
68 std::ostringstream os;
69 os << ist;
70 return os.str();
71}
72
74template<typename Multivector_T>
75inline String clifford_to_repr(const Multivector_T& mv)
76{
77 using scalar_t = typename Multivector_T::scalar_t;
78 std::ostringstream os;
79 os << std::setprecision(std::numeric_limits<scalar_t>::digits10 + 1);
80 os << "clifford(\"" << mv << "\")";
81 return os.str();
82}
83
85template<typename Multivector_T>
86inline String clifford_to_str(const Multivector_T& mv)
87{
88 using scalar_t = typename Multivector_T::scalar_t;
89 std::ostringstream os;
90 if (abs(mv) < std::numeric_limits<scalar_t>::epsilon())
91 os << 0.0;
92 else
93 os << std::setprecision(4) << mv.truncated(scalar_t(1.0e-4));
94 return os.str();
95}
96
97
99namespace cga3
100{
102 template<typename Multivector_T>
103 inline Multivector_T cga3(const Multivector_T& x)
104 {
105 using cl = Multivector_T;
106 using ist = typename cl::index_set_t;
107 static const cl ninf3 = cl(ist(4)) + cl(ist(-1));
108
109 return (cl(ist(4)) - x) * ninf3 * (x - cl(ist(4)));
110 }
111
113 template<typename Multivector_T>
114 inline Multivector_T cga3std(const Multivector_T& X)
115 {
116 using cl = Multivector_T;
117 using ist = typename cl::index_set_t;
118 using scalar_t = typename cl::scalar_t;
119 static const cl ninf3 = cl(ist(4)) + cl(ist(-1));
120
121 return scalar_t(-2.0) * X / (X & ninf3);
122 }
123
125 template<typename Multivector_T>
126 inline Multivector_T agc3(const Multivector_T& X)
127 {
128 using cl = Multivector_T;
129 using ist = typename cl::index_set_t;
130 using scalar_t = typename cl::scalar_t;
131
132 const cl& cga3stdX = cga3std(X);
133 return (cl(ist(1))*cga3stdX[ist(1)] +
134 cl(ist(2))*cga3stdX[ist(2)] +
135 cl(ist(3))*cga3stdX[ist(3)]) / scalar_t(2.0);
136 }
137}
138
139
140// Specifications of the IndexSet and Clifford C++ classes for use with PyClical.
141
142using namespace glucat;
143const index_t lo_ndx = DEFAULT_LO;
146
147using scalar_t = double;
149
150const scalar_t epsilon = std::numeric_limits<scalar_t>::epsilon();
151
152// Do not warn about unused values. This affects clang++ as well as g++.
153
154#pragma GCC diagnostic ignored "-Wunused-value"
155
156#if defined(__clang__)
157// Do not warn about unused functions. The affects clang++ only.
158
159# pragma clang diagnostic ignored "-Wunused-function"
160
161// Do not warn about unneeded internal declarations. The affects clang++ only.
162
163# pragma clang diagnostic ignored "-Wunneeded-internal-declaration"
164#endif
const index_t lo_ndx
Definition: PyClical.h:143
String clifford_to_str(const Multivector_T &mv)
The "informal" string representation of Multivector_T mv.
Definition: PyClical.h:86
double scalar_t
Definition: PyClical.h:147
const index_t hi_ndx
Definition: PyClical.h:144
String glucat_package_version
Definition: PyClical.h:53
String clifford_to_repr(const Multivector_T &mv)
The “official” string representation of Multivector_T mv.
Definition: PyClical.h:75
const scalar_t epsilon
Definition: PyClical.h:150
std::string String
Definition: PyClical.h:51
String index_set_to_str(const Index_Set_T &ist)
The "informal" string representation of Index_Set_T ist.
Definition: PyClical.h:66
String index_set_to_repr(const Index_Set_T &ist)
The “official” string representation of Index_Set_T ist.
Definition: PyClical.h:57
PyObject * PyFloat_FromDouble(Scalar_T v)
Definition: PyClical.h:45
Index set class based on std::bitset<> in Gnu standard C++ library.
Definition: index_set.h:75
A matrix_multi<Scalar_T,LO,HI,Tune_P> is a matrix approximation to a multivector.
Definition: matrix_multi.h:139
Extra traits which extend numeric limits.
Definition: scalar.h:48
#define GLUCAT_PACKAGE_VERSION
Definitions for 3D Conformal Geometric Algebra [DL].
Definition: PyClical.h:100
Multivector_T cga3std(const Multivector_T &X)
Convert CGA3 null vector to standard Conformal Geometric Algebra null vector [DL (10....
Definition: PyClical.h:114
Multivector_T agc3(const Multivector_T &X)
Convert CGA3 null vector to Euclidean 3D vector [DL (10.50)].
Definition: PyClical.h:126
int index_t
Size of index_t should be enough to represent LO, HI.
Definition: global.h:77
auto abs(const Multivector< Scalar_T, LO, HI, Tune_P > &val) -> Scalar_T
Absolute value == sqrt(norm)
const index_t DEFAULT_HI
Default highest index in an index set.
Definition: global.h:111