flexiblesusy is hosted by Hepforge, IPPP Durham
FlexibleSUSY
mathlink_utils.hpp
Go to the documentation of this file.
1// ====================================================================
2// This file is part of FlexibleSUSY.
3//
4// FlexibleSUSY is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published
6// by the Free Software Foundation, either version 3 of the License,
7// or (at your option) any later version.
8//
9// FlexibleSUSY is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with FlexibleSUSY. If not, see
16// <http://www.gnu.org/licenses/>.
17// ====================================================================
18
19#ifndef MATHLINK_UTILS_H
20#define MATHLINK_UTILS_H
21
22#include <mathlink.h>
23
24#include <complex>
25#include <string>
26#include <vector>
27#include <Eigen/Core>
28
29/********************* put types *********************/
30
31inline void MLPut(MLINK link, const std::string& s)
32{
33 MLPutSymbol(link, s.c_str());
34}
35
36inline void MLPut(MLINK link, int c)
37{
38 MLPutInteger(link, c);
39}
40
41inline void MLPut(MLINK link, double c)
42{
43 MLPutReal(link, c);
44}
45
46inline void MLPut(MLINK link, std::complex<double> c)
47{
48 if (std::imag(c) == 0.) {
49 MLPutReal(link, std::real(c));
50 } else {
51 MLPutFunction(link, "Complex", 2);
52 MLPutReal(link, std::real(c));
53 MLPutReal(link, std::imag(c));
54 }
55}
56
57template <int M>
58void MLPut(MLINK link, const Eigen::Array<double,M,1>& a)
59{
60 double v[M];
61 for (int i = 0; i < M; i++)
62 v[i] = a(i);
63 MLPutRealList(link, v, M);
64}
65
66template <int M>
67void MLPut(MLINK link, const Eigen::Matrix<double,M,1>& m)
68{
69 const Eigen::Array<double,M,1> a(m.array());
70 MLPut(link, a);
71}
72
73template <int M, int N>
74void MLPut(MLINK link, const Eigen::Matrix<double,M,N>& m)
75{
76 double mat[M][N];
77 for (int i = 0; i < M; i++)
78 for (int k = 0; k < N; k++)
79 mat[i][k] = m(i, k);
80
81 long dims[] = { M, N };
82 MLPutDoubleArray(link, (double*)mat, dims, NULL, 2);
83}
84
85template <int M>
86void MLPut(MLINK link, const Eigen::Array<std::complex<double>,M,1>& a)
87{
88 MLPutFunction(link, "List", M);
89 for (int i = 0; i < M; i++)
90 MLPut(link, a(i));
91}
92
93template <int M>
94void MLPut(MLINK link, const Eigen::Matrix<std::complex<double>,M,1>& m)
95{
96 const Eigen::Array<std::complex<double>,M,1> a(m.array());
97 MLPut(link, a);
98}
99
100template <int M, int N>
101void MLPut(MLINK link, const Eigen::Matrix<std::complex<double>,M,N>& m)
102{
103 MLPutFunction(link, "List", M);
104 for (int i = 0; i < M; i++) {
105 MLPutFunction(link, "List", N);
106 for (int k = 0; k < N; k++)
107 MLPut(link, m(i,k));
108 }
109}
110
111/********************* put single heads *********************/
112
113inline void MLPutHeads(MLINK link, const std::vector<std::string>& heads)
114{
115 for (const auto& h: heads)
116 MLPutFunction(link, h.c_str(), 1);
117}
118
119/********************* put rules to types *********************/
120
121inline void MLPutRule(MLINK link, const std::string& name, const std::vector<std::string>& heads = {})
122{
123 MLPutFunction(link, "Rule", 2);
124 MLPutHeads(link, heads);
125 MLPutUTF8Symbol(link, reinterpret_cast<const unsigned char*>(name.c_str()), name.size());
126}
127
128inline void MLPutRule(MLINK link, int number, const std::vector<std::string>& heads = {})
129{
130 MLPutFunction(link, "Rule", 2);
131 MLPutHeads(link, heads);
132 MLPutInteger(link, number);
133}
134
135inline void MLPutRule(MLINK link, long number, const std::vector<std::string>& heads = {})
136{
137 MLPutFunction(link, "Rule", 2);
138 MLPutHeads(link, heads);
139 MLPutLongInteger(link, number);
140}
141
142template <class T1, class T2>
143void MLPutRuleTo(MLINK link, T1 t, const T2& name, const std::vector<std::string>& heads = {})
144{
145 MLPutRule(link, name, heads);
146 MLPut(link, t);
147}
148
149/********************* get types *********************/
150
151inline void MLGet(MLINK link, int *c)
152{
153 MLGetInteger(link, c);
154}
155
156inline void MLGet(MLINK link, long *c)
157{
158 MLGetLongInteger(link, c);
159}
160
161inline void MLGet(MLINK link, short *c)
162{
163 MLGetShortInteger(link, c);
164}
165
166#endif // MATHLINK_UTILS_H
#define M(i)
Definition: defs.h:629