10#ifndef XTENSOR_MIME_HPP
11#define XTENSOR_MIME_HPP
18#include <nlohmann/json.hpp>
24 template <
class P,
class T>
25 void compute_0d_table(std::stringstream& out, P& ,
const T& expr)
27 out <<
"<table style='border-style:solid;border-width:1px;'><tbody>";
28 out <<
"<tr><td style='font-family:monospace;'><pre>";
30 out <<
"</pre></td></tr>";
31 out <<
"</tbody></table>";
35 void compute_1d_row(std::stringstream& out, P& printer,
const std::size_t& row_idx)
37 out <<
"<tr><td style='font-family:monospace;' title='" << row_idx <<
"'><pre>";
38 printer.print_next(out);
39 out <<
"</pre></td></tr>";
42 template <
class P,
class T>
43 void compute_1d_table(std::stringstream& out, P& printer,
const T& expr,
const std::size_t& edgeitems)
45 const auto& dim = expr.shape()[0];
47 out <<
"<table style='border-style:solid;border-width:1px;'><tbody>";
48 if (edgeitems == 0 || 2 * edgeitems >= dim)
50 for (std::size_t row_idx = 0; row_idx < dim; ++row_idx)
52 compute_1d_row(out, printer, row_idx);
57 for (std::size_t row_idx = 0; row_idx < edgeitems; ++row_idx)
59 compute_1d_row(out, printer, row_idx);
61 out <<
"<tr><td><center>\u22ee</center></td></tr>";
62 for (std::size_t row_idx = dim - edgeitems; row_idx < dim; ++row_idx)
64 compute_1d_row(out, printer, row_idx);
67 out <<
"</tbody></table>";
71 void compute_2d_element(
72 std::stringstream& out,
74 const std::string& idx_str,
75 const std::size_t& row_idx,
76 const std::size_t& column_idx
79 out <<
"<td style='font-family:monospace;' title='(" << idx_str << row_idx <<
", " << column_idx
81 printer.print_next(out);
85 template <
class P,
class T>
87 std::stringstream& out,
90 const std::size_t& edgeitems,
91 const std::string& idx_str,
92 const std::size_t& row_idx
95 const auto& dim = expr.shape()[expr.dimension() - 1];
98 if (edgeitems == 0 || 2 * edgeitems >= dim)
100 for (std::size_t column_idx = 0; column_idx < dim; ++column_idx)
102 compute_2d_element(out, printer, idx_str, row_idx, column_idx);
107 for (std::size_t column_idx = 0; column_idx < edgeitems; ++column_idx)
109 compute_2d_element(out, printer, idx_str, row_idx, column_idx);
111 out <<
"<td><center>\u22ef</center></td>";
112 for (std::size_t column_idx = dim - edgeitems; column_idx < dim; ++column_idx)
114 compute_2d_element(out, printer, idx_str, row_idx, column_idx);
120 template <
class P,
class T,
class I>
121 void compute_2d_table(
122 std::stringstream& out,
125 const std::size_t& edgeitems,
126 const std::vector<I>& idx
129 const auto& dim = expr.shape()[expr.dimension() - 2];
130 const auto& last_dim = expr.shape()[expr.dimension() - 1];
135 [&idx_str](
const auto& i)
137 idx_str += std::to_string(i) +
", ";
141 std::size_t nb_ellipsis = 2 * edgeitems + 1;
142 if (last_dim <= 2 * edgeitems + 1)
144 nb_ellipsis = last_dim;
147 out <<
"<table style='border-style:solid;border-width:1px;'><tbody>";
148 if (edgeitems == 0 || 2 * edgeitems >= dim)
150 for (std::size_t row_idx = 0; row_idx < dim; ++row_idx)
152 compute_2d_row(out, printer, expr, edgeitems, idx_str, row_idx);
157 for (std::size_t row_idx = 0; row_idx < edgeitems; ++row_idx)
159 compute_2d_row(out, printer, expr, edgeitems, idx_str, row_idx);
162 for (std::size_t column_idx = 0; column_idx < nb_ellipsis; ++column_idx)
164 if (column_idx == edgeitems && nb_ellipsis != last_dim)
166 out <<
"<td><center>\u22f1</center></td>";
170 out <<
"<td><center>\u22ee</center></td>";
174 for (std::size_t row_idx = dim - edgeitems; row_idx < dim; ++row_idx)
176 compute_2d_row(out, printer, expr, edgeitems, idx_str, row_idx);
179 out <<
"</tbody></table>";
182 template <
class P,
class T,
class I>
184 std::stringstream& out,
187 const std::size_t& edgeitems,
188 const std::vector<I>& idx
192 compute_nd_table_impl(out, printer, expr, edgeitems, idx);
196 template <
class P,
class T,
class I>
197 void compute_nd_table_impl(
198 std::stringstream& out,
201 const std::size_t& edgeitems,
202 const std::vector<I>& idx
205 const auto& displayed_dimension = idx.size();
206 const auto& expr_dim = expr.dimension();
207 const auto& dim = expr.shape()[displayed_dimension];
209 if (expr_dim - displayed_dimension == 2)
211 return compute_2d_table(out, printer, expr, edgeitems, idx);
214 std::vector<I> idx2 = idx;
215 idx2.resize(displayed_dimension + 1);
217 out <<
"<table style='border-style:solid;border-width:1px;'>";
218 if (edgeitems == 0 || 2 * edgeitems >= dim)
220 for (std::size_t i = 0; i < dim; ++i)
222 idx2[displayed_dimension] = i;
223 compute_nd_row(out, printer, expr, edgeitems, idx2);
228 for (std::size_t i = 0; i < edgeitems; ++i)
230 idx2[displayed_dimension] = i;
231 compute_nd_row(out, printer, expr, edgeitems, idx2);
233 out <<
"<tr><td><center>\u22ef</center></td></tr>";
234 for (std::size_t i = dim - edgeitems; i < dim; ++i)
236 idx2[displayed_dimension] = i;
237 compute_nd_row(out, printer, expr, edgeitems, idx2);
243 template <
class P,
class T>
244 void compute_nd_table(std::stringstream& out, P& printer,
const T& expr,
const std::size_t& edgeitems)
246 if (expr.dimension() == 0)
248 compute_0d_table(out, printer, expr);
250 else if (expr.dimension() == 1)
252 compute_1d_table(out, printer, expr, edgeitems);
256 std::vector<std::size_t> empty_vector;
257 compute_nd_table_impl(out, printer, expr, edgeitems, empty_vector);
262 nlohmann::json mime_bundle_repr_impl(
const E& expr)
264 std::stringstream out;
266 std::size_t edgeitems = 0;
267 std::size_t size = compute_size(expr.shape());
268 if (size >
static_cast<std::size_t
>(print_options::print_options().threshold))
270 edgeitems =
static_cast<std::size_t
>(print_options::print_options().edge_items);
273 if (print_options::print_options().precision != -1)
275 out.precision(print_options::print_options().precision);
278 detail::printer<E> printer(out.precision());
281 detail::recurser_run(printer, expr, slice_vector, edgeitems);
284 compute_nd_table(out, printer, expr, edgeitems);
286 auto bundle = nlohmann::json::object();
287 bundle[
"text/html"] = out.str();
291 template <
class F,
class CT>
294 template <
class F,
class CT>
295 nlohmann::json mime_bundle_repr(
const xfunctor_view<F, CT>& expr)
297 return mime_bundle_repr_impl(expr);
300 template <
class F,
class... CT>
303 template <
class F,
class... CT>
304 nlohmann::json mime_bundle_repr(
const xfunction<F, CT...>& expr)
306 return mime_bundle_repr_impl(expr);
309 template <
class EC, layout_type L,
class SC,
class Tag>
310 class xarray_container;
312 template <
class EC, layout_type L,
class SC,
class Tag>
313 nlohmann::json mime_bundle_repr(
const xarray_container<EC, L, SC, Tag>& expr)
315 return mime_bundle_repr_impl(expr);
318 template <
class EC, std::
size_t N, layout_type L,
class Tag>
319 class xtensor_container;
321 template <
class EC, std::
size_t N, layout_type L,
class Tag>
322 nlohmann::json mime_bundle_repr(
const xtensor_container<EC, N, L, Tag>& expr)
324 return mime_bundle_repr_impl(expr);
327 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
328 class xfixed_container;
330 template <
class ET,
class S, layout_type L,
bool SH,
class Tag>
331 nlohmann::json mime_bundle_repr(
const xfixed_container<ET, S, L, SH, Tag>& expr)
333 return mime_bundle_repr_impl(expr);
336 template <
class F,
class CT,
class X,
class O>
339 template <
class F,
class CT,
class X,
class O>
340 nlohmann::json mime_bundle_repr(
const xreducer<F, CT, X, O>& expr)
342 return mime_bundle_repr_impl(expr);
345 template <
class VE,
class FE>
346 class xoptional_assembly;
348 template <
class VE,
class FE>
349 nlohmann::json mime_bundle_repr(
const xoptional_assembly<VE, FE>& expr)
351 return mime_bundle_repr_impl(expr);
354 template <
class VEC,
class FEC>
355 class xoptional_assembly_adaptor;
357 template <
class VEC,
class FEC>
358 nlohmann::json mime_bundle_repr(
const xoptional_assembly_adaptor<VEC, FEC>& expr)
360 return mime_bundle_repr_impl(expr);
367 nlohmann::json mime_bundle_repr(
const xscalar<CT>& expr)
369 return mime_bundle_repr_impl(expr);
372 template <
class CT,
class X>
375 template <
class CT,
class X>
376 nlohmann::json mime_bundle_repr(
const xbroadcast<CT, X>& expr)
378 return mime_bundle_repr_impl(expr);
381 template <
class F,
class R,
class S>
384 template <
class F,
class R,
class S>
385 nlohmann::json mime_bundle_repr(
const xgenerator<F, R, S>& expr)
387 return mime_bundle_repr_impl(expr);
390 template <
class CT,
class... S>
393 template <
class CT,
class... S>
394 nlohmann::json mime_bundle_repr(
const xview<CT, S...>& expr)
396 return mime_bundle_repr_impl(expr);
399 template <
class CT,
class S, layout_type L,
class FST>
402 template <
class CT,
class S, layout_type L,
class FST>
403 nlohmann::json mime_bundle_repr(
const xstrided_view<CT, S, L, FST>& expr)
405 return mime_bundle_repr_impl(expr);
408 template <
class CTD,
class CTM>
411 template <
class CTD,
class CTM>
412 nlohmann::json mime_bundle_repr(
const xmasked_view<CTD, CTM>& expr)
414 return mime_bundle_repr_impl(expr);
417 template <
class T,
class B>
420 template <
class T,
class B>
423 auto bundle = nlohmann::json::object();
424 std::stringstream
tmp;
standard mathematical functions for xexpressions
std::vector< xstrided_slice< std::ptrdiff_t > > xstrided_slice_vector
vector of slices used to build a xstrided_view