42 inline std::vector<double> uniform(idx N) {
46 throw exception::ZeroSize(
"qpp::uniform()");
49 return std::vector<double>(N, 1. / N);
60 inline std::vector<double> marginalX(
const dmat& probXY) {
63 if (!internal::check_nonzero_size(probXY))
64 throw exception::ZeroSize(
"qpp::marginalX()");
67 std::vector<double> result(probXY.rows(), 0);
68 for (idx i = 0; i < static_cast<idx>(probXY.rows()); ++i) {
69 for (idx j = 0; j < static_cast<idx>(probXY.cols()); ++j) {
70 result[i] += probXY(i, j);
85 inline std::vector<double> marginalY(
const dmat& probXY) {
88 if (!internal::check_nonzero_size(probXY))
89 throw exception::ZeroSize(
"qpp::marginalY()");
92 return marginalX(probXY.transpose());
104 template <
typename Container>
106 avg(
const std::vector<double>& prob,
const Container& X,
107 typename std::enable_if<is_iterable<Container>::value>::type* =
nullptr) {
110 if (!internal::check_nonzero_size(prob))
111 throw exception::ZeroSize(
"qpp::avg()");
112 if (!internal::check_matching_sizes(prob, X))
113 throw exception::SizeMismatch(
"qpp::avg()");
117 for (idx i = 0; i < prob.size(); ++i)
118 result += prob[i] * X[i];
133 template <
typename Container>
135 cov(
const dmat& probXY,
const Container& X,
const Container& Y,
136 typename std::enable_if<is_iterable<Container>::value>::type* =
nullptr) {
139 if (!internal::check_nonzero_size(X) || !internal::check_nonzero_size(Y))
140 throw exception::ZeroSize(
"qpp::cov()");
141 if (static_cast<idx>(probXY.rows()) != X.size() ||
142 static_cast<idx
>(probXY.cols()) != Y.size())
143 throw exception::SizeMismatch(
"qpp::cov()");
146 std::vector<double> probX = marginalX(probXY);
147 std::vector<double> probY = marginalY(probXY);
150 for (idx i = 0; i < X.size(); ++i) {
151 for (idx j = 0; j < Y.size(); ++j) {
152 result += probXY(i, j) * X[i] * Y[j];
156 return result - avg(probX, X) * avg(probY, Y);
167 template <
typename Container>
169 var(
const std::vector<double>& prob,
const Container& X,
170 typename std::enable_if<is_iterable<Container>::value>::type* =
nullptr) {
173 if (!internal::check_nonzero_size(prob))
174 throw exception::ZeroSize(
"qpp::var()");
175 if (!internal::check_matching_sizes(prob, X))
176 throw exception::SizeMismatch(
"qpp::var()");
179 Eigen::VectorXd diag(prob.size());
180 for (idx i = 0; i < prob.size(); ++i)
182 dmat probXX = diag.asDiagonal();
184 return cov(probXX, X, X);
195 template <
typename Container>
197 sigma(
const std::vector<double>& prob,
const Container& X,
198 typename std::enable_if<is_iterable<Container>::value>::type* =
nullptr) {
201 if (!internal::check_nonzero_size(prob))
202 throw exception::ZeroSize(
"qpp::sigma()");
203 if (!internal::check_matching_sizes(prob, X))
204 throw exception::SizeMismatch(
"qpp::sigma()");
207 return std::sqrt(var(prob, X));
220 template <
typename Container>
222 cor(
const dmat& probXY,
const Container& X,
const Container& Y,
223 typename std::enable_if<is_iterable<Container>::value>::type* =
nullptr) {
226 if (!internal::check_nonzero_size(X) || !internal::check_nonzero_size(Y))
227 throw exception::ZeroSize(
"qpp::cor()");
228 if (static_cast<idx>(probXY.rows()) != X.size() ||
229 static_cast<idx
>(probXY.cols()) != Y.size())
230 throw exception::SizeMismatch(
"qpp::cor()");
233 return cov(probXY, X, Y) /
234 (sigma(marginalX(probXY), X) * sigma(marginalY(probXY), Y));
Quantum++ main namespace.
Definition: circuits.h:35