xio: pretty printing

Defined in xtensor/xio.hpp

This file defines functions for pretty printing xexpressions. It defines appropriate overloads for the << operator for std::ostreams and xexpressions.

#include <xtensor/xio.hpp>
#include <xtensor/xarray.hpp>

int main()
{
    xt::xarray<double> a = {{1,2,3}, {4,5,6}};
    std::cout << a << std::endl;
    return 0;
}

Will print

{{ 1., 2., 3.},
 { 4., 5., 6.}}

With the following functions, the global print options can be set:

inline void xt::print_options::set_line_width(int line_width)

Sets the line width.

After line_width chars, a new line is added.

Parameters:

line_width – The line width

inline void xt::print_options::set_threshold(int threshold)

Sets the threshold after which summarization is triggered (default: 1000).

Parameters:

threshold – The number of elements in the xexpression that triggers summarization in the output

inline void xt::print_options::set_edge_items(int edge_items)

Sets the number of edge items.

If the summarization is triggered, this value defines how many items of each dimension are printed.

Parameters:

edge_items – The number of edge items

inline void xt::print_options::set_precision(int precision)

Sets the precision for printing floating point values.

Parameters:

precision – The number of digits for floating point output

On can also locally overwrite the print options with io manipulators:

class line_width

io manipulator used to set the width of the lines when printing an expression.

using po = xt::print_options;
xt::xarray<double> a = {{1, 2, 3}, {4, 5, 6}};
std::cout << po::line_width(100) << a << std::endl;
class threshold

io manipulator used to set the threshold after which summarization is triggered.

using po = xt::print_options;
xt::xarray<double> a = xt::rand::randn<double>({2000, 500});
std::cout << po::threshold(50) << a << std::endl;
class edge_items

io manipulator used to set the number of egde items if the summarization is triggered.

using po = xt::print_options;
xt::xarray<double> a = xt::rand::randn<double>({2000, 500});
std::cout << po::edge_items(5) << a << std::endl;
class precision

io manipulator used to set the precision of the floating point values when printing an expression.

using po = xt::print_options;
xt::xarray<double> a = xt::rand::randn<double>({2000, 500});
std::cout << po::precision(5) << a << std::endl;