Read Xtensor Source Code2 -- examples

This article is the second one of the Read Xtensor Source Code searies. In the previous article, I build and install xtensor and xtl on my windows system desktop, and in this article, I'll try to explore the xtensor document, write some examples and try to understand the phylosophy and idioms behind the xtensor design.

After using xtensor a while, it leave me a very complex impression. On one hand it's well designed and try hard to use C++11 features and C++11 idioms, which ensure the code base quality; it uses very seemingly syntax to Python which make it very easy use. On the other hand, it has implicit bugs, I'll talk about them in this article.

This article will keep growing, all experimenting examples will be dumped here.

1. getting started

Pythoner can easily guess what the following code will output.

void getting_started()
{
    xt::xarray<double> arr1{
        { 1.0, 2.0, 3.0 },
        { 2.0, 5.0, 7.0 },
        { 2.0, 5.0, 7.0 }
    };

    xt::xarray<double> arr2{
        5.0, 6.0, 7.0
    };

    xt::xarray<double> res = xt::view(arr1, 1) + arr2;

    std::cout << res << std::endl;

    std::cout << arr1(1, 1) << std::endl;
    std::cout << arr2(1) << std::endl;

    // do pow of arr2 for arr1
    arr2.reshape({3, 1});
    res = xt::pow(arr1, arr2);
    std::cout << res << std::endl;

    arr1.reshape({1, 9});
    std::cout << arr1;
}

2. lazy evaluation

Xtensor is featured for lazy evaluation, which make it very efficient. It utilize a very complexed expression and semantic machism. I wrote the following example, but can't see explicitly how much it can speed up.

void expression()
{
    xt::xarray<double> x{1.0, 2.0, 3.0, 4.0};
    xt::xarray<double> y{5.0, 6.0, 7.0, 8.0};
    xt::xarray<double> z{0, 3.14, 6.28, 9.42};
    xt::xarray<double> res = x + y + xt::sin(z);

    std::cout << res << std::endl;
}

void expression_interface()
{
    using array_type = xt::xarray<double>;
    using shape_type = array_type::shape_type;

    shape_type shape = {3, 2, 4};
    array_type arr{shape};

    size_t d = arr.dimension();
    std::cout << "dimension: " << d << std::endl;

    const shape_type& s = arr.shape();
    bool res = (d == shape.size()) && (s == shape);

    std::cout << res << std::endl;
}

3. sliced view

The slice machism is exactly same to Python, but it won't check the range of the range, in the following example, I wrong a very large upper bound, the output will just siliently trunck the container, no even a warning message is emited out.

Another thing bothored me is that one can assign a scalar to a container, which will resize the container to a 0-D container, it's very error prone. For example, in the following code, I want to assign sequence numbers to array, it should be item = i++, but I mistyped it as a = i++, it just work, but of course not what I want.

void view_sliced()
{
    std::vector<size_t> shape{ 3, 2, 4 };
    xt::xarray<int> a{ shape };

    int i = 0;
    for (auto &item: a) {
        item = i++; // a is assigned to a scalar, then a is resized to a scalar?!
    }
    std::cout << a << std::endl;
    std::cout << "----------" << std::endl;

    auto v1 = xt::view(a, xt::range(1, 3)); // shape: 2, 2, 4
    std::cout << v1.dimension() << std::endl;
    // auto v1 = xt::view(a, xt::range(1, 3), xt::all(), xt::range(1, 3));
    std::cout << v1 << std::endl;

    std::cout << "----------" << std::endl;
    auto v2 = xt::view(a, xt::all(), xt::range(1, 100), xt::all()); // bug or feature? range error
    std::cout << v2 << std::endl;

    std::cout << "----------" << std::endl;
    auto v3 = xt::view(a, xt::all(), xt::all(), xt::range(1, 100)); // the upper bound don't work
    std::cout << v3 << std::endl;
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,279评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,355评论 0 23
  • 2017年12月11日 星期一 睛有风 今天的天真冷啊,大风呼呼的刮。下班后到托辅接了儿子回家,检查作业...
    圈圈370阅读 183评论 0 0
  • 隔了一年多重新找回了简书的账号,看着以前写的文字,竟然有了好些的陌生感。谁会想到,一年后的我竟然连一个可以讲话的人...
    郑鲍勃阅读 432评论 0 0
  • 01 “二子,我和你爸思考了一宿,还是给你念个本三吧!”母亲缓缓地说道。 “上什么三本,学费这么高,哥哥也要读书,...
    苏子游阅读 3,385评论 71 91