C++和Python混合编程:C++版的Numpy(Xtensor.hpp)安装和使用

简介

xtensor是一个 C++ 库,用于使用多维数组表达式进行数值分析。
c++ 的 xtensor 库对标 Python 的 Numpy 库,Numpy 在 Python 中使用很方便,但是在 C++ 中想要使用这么方便的工具就需要 stensor。xtensor 支持延迟广播。
本文介绍 xtensor.hpp 在 C++ 中的安装使用。

xtensor 说明文档:https://xtensor.readthedocs.io/en/latest/index.html
xtensor 源代码:https://github.com/xtensor-stack/xtensor

xtensor 对应 numpy 的函数表格,有很多相同的函数,不同的是C++中必须指定类型(蓝线)。会使用 Numpy 的人会很容易理解 xtensor。其他函数参考https://xtensor.readthedocs.io/en/latest/numpy.html。
在这里插入图片描述
在这里插入图片描述


安装 xtensor

有 Python 的 anaconda 的可以用 Anaconda Prompt (anaconda) 安装 xtensor。其他系统或者方法参考说明文档(https://xtensor.readthedocs.io/en/latest/installation.html)。安装过程不要科学上网。

conda install -c conda-forge xtensor

环境配置

xtensor 需要支持 C++14 的 C++ 编译器。
头文件 .hpp 必须在 Visual Studio(我用的VS2019) 的 release 中运行,要不然 VS 识别不了。


读取 npy,csv,控制台打印

https://xtensor.readthedocs.io/en/latest/api/io_index.html

矩阵操作

https://xtensor.readthedocs.io/en/latest/getting_started.html


简单代码示例1

将二维数组的第二行与一维数组相加。

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

int main(int argc, char* argv[])
{
    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;

    return 0;
}

在这里插入图片描述

项目1:将 txt 中的矩阵读入 xtensor

读取 TestData.txt ,txt 中保存的矩阵格式如下所示:

1 2 3
2 3 4
4 5 6
待更新...

项目2:将 npy 中的矩阵读入 xtensor

读取 TestData.npy,npy 中保存的矩阵格式如下所示:

1 2 3
2 3 4
4 5 6
#include <istream>
#include <fstream>
#include <iostream>
#include <xtensor/xarray.hpp>
#include <xtensor/xnpy.hpp>
#include <xtensor/xview.hpp>

int main() 
{
	std::cout << "导入.npy保存的矩阵" << std::endl;

    //文件保存目录
	const char* datapath = "D:\\TestData.npy";

    //读取矩阵
	auto data = xt::load_npy<double>(datapath);
	
	//打印矩阵行列数方法1:
	//for(const int s: data.shape())std::cout << s << std::endl;

    //打印矩阵行列数方法2:
	auto shape = data.shape();
	std::cout << "行列数:" << xt::adapt(shape)[0] << "\x20" << xt::adapt(shape)[1] << std::endl;

    //打印矩阵前两行数字
	std::cout << data(0, 0) << "\x20" << data(0, 1) << "\x20" << data(0, 2) << std::endl;
	std::cout << data(1, 0) << "\x20" << data(1, 1) << "\x20" << data(1, 2) << std::endl;

	std::cout << "第二个例子:" << std::endl;

    //截取data矩阵的所有行,前3列
	auto datab = xt::view(data, xt::all(), xt::range(0, 3));
    
    //打印矩阵的形状
	for (const int s1 : datab.shape())std::cout << s1 << std::endl;

	std::cout << datab(0, 0) << "\x20" << datab(0, 1) << "\x20" << datab(0, 2) << std::endl;
	std::cout << datab(1, 0) << "\x20" << datab(1, 1) << "\x20" << datab(1, 2) << std::endl;

	std::cout << "初始化新矩阵:" << std::endl;
	
    //初始化一个新的3行3列矩阵
	xt::xarray<double> arr
	{ 
		{1.0, 2.0, 3.0},
		{2.0, 5.0, 7.0},
		{2.0, 5.0, 7.0} 
	};
    
    //给第三行第2列的数字赋值6,和Numpy中的arr[2, 1]=6一样
	arr(2, 1) = 6.0;

    //打印矩阵的形状
	for (const int temp : arr.shape())std::cout << temp << std::endl;
	std::cout << arr(2, 1) << std::endl;

    //选取第三行第2列的数字,并打印出来
	const double t = arr(2, 1);
	std::cout << t << std::endl;
	
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_养乐多_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值