#ifndef WPC_H
#define WPC_H
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include<map>
#include<random>
#include <thread>
#include <mutex>
#include <utility>
#include<stack>
#include<queue>
#include<set>
#include <algorithm>
#include <exception>
#include <typeinfo>
#include<cmath>
#include <chrono>
#include <cstdlib>
#include<cstdio>
//本库不需要其他C++第三方库,只需要 C++ STL标准库,各种c库,其他内置库
/// 本库仅仅在符合如下条件的环境下通过测试:
// C++标准:C++ 14 C++17 C++20
// x64
// Debug/Release
// Windows 11
// Visual Studio profession 2022 (未在vscode测试过)
// 本库不兼容C++20 或C++23新增加内容
// 编译器: mingw
// 暂不支持英伟达等显卡加速
//为了保证数据安全性本库基本不提供对任何原始数据直接操作的函数
//=================================================================================================================================================================
template <typename T>//矩阵元素可以为任意类型
class juzhen {//矩阵类
private:
int hanshu;//行数
int lieshu;//列数
std::vector<std::vector<T>> data;//数据
public:
juzhen(int hanshu, int lieshu) : hanshu(hanshu), lieshu(lieshu), data(hanshu, std::vector<T>(lieshu)) {}
int juzhen_hanshu() const {
return hanshu;
}
int juzhen_lieshu() const {
return lieshu;
}
//重载符号[],方便通过下标访问矩阵合法元素
const std::vector<T>& operator[](int index) const {
if (index < 0 || index >= hanshu) {
throw std::out_of_range("超出矩阵边界");
}
return data[index];
}
std::vector<T>& operator[](int index) {
if (index < 0 || index >= hanshu) {
throw std::out_of_range("超出矩阵边界");
}
return data[index];
}
//重载符号(),方便通过下标访问矩阵合法元素
const T& operator()(int i, int j) const {
if (i < 0 || i >= hanshu || j < 0 || j >= lieshu) {
throw std::out_of_range("超出矩阵边界");
}
return data[i][j];
}
T& operator()(int i, int j) {
if (i < 0 || i >= hanshu || j < 0 || j >= lieshu) {
throw std::out_of_range("超出矩阵边界");
}
return data[i][j];
}
//juzhen类判断是否合法 注意不是juzhen_2D
bool panduan() {
int p = 1;
if (!data.empty()) {
for (int i = 0; i != hanshu; i++) {
if (data[i].size() != lieshu || data[i].empty())p = 0;
break;
}
}
return (p != 0);
}
//模仿python中.append()函数 但需要指定坐标 如 l.append(i,j,k) 意思为l[i][j]=k,而不是python的添加功能
void append(int i, int j, const T& value) {
if (i < 0 || i >= hanshu || j < 0 || j >= lieshu) {
throw std::out_of_range("超出矩阵边界");
}
data[i][j] = (value);
}
//只可以用于juzhen 类矩阵
void juzhen_print() {
for (int i = 0; i != hanshu; i++) {
for (int j = 0; j != lieshu; j++) {
std::cout << data[i][j] << " ";
}
std::cout << std::endl;
}
}
//矩阵元素初始化为任意类型(T) x
void chushihua(const T x) {
if (!data.empty()) {
for (int i = 0; i != hanshu; i++) {
for (int j = 0; j != lieshu; j++) {
data[i][j] = x;
}
}
}
}
};
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//==========================================================================================================================================================
//==========================================================================================================================================================
//
//
//与juzhen类不同的是,juzhen_2D_ 系列函数是指类型为std::vector<std::vector<T> 的二维数组,使得操作跟灵活,而juzhen类矩阵必须初始化矩阵行数,列数
template <typename T>
bool juzhen_2D_panduan(std::vector<std::vector<T>>& x) {
if (x.empty())return false;
int p = 1;
for (int i = 1; i != x.size(); i++) {
if (x[i].size() != x[0].size())p = 0;
break;
}
return p != 0;
}
//juzhen_2D行数
template <typename T>
int juzhen_2D_hanshu(std::vector<std::vector<T>>& x) {
if (juzhen_2D_panduan(x) == false)return -1;
int sum = 0;
for (int i = 0; i != x.size(); i++) {
sum++;
}
return sum;
}
//juzhen_2D列数
template <typename T>
int juzhen_2D_lieshu(std::vector<std::vector<T>>& x) {
if (juzhen_2D_panduan(x) == false)return -1;
int sum = 0;
for (int i = 0; i != x[0].size(); i++) {
sum++;
}
return sum;
}
//矩阵初始化 创造一个 m*n 矩阵 所有元素设置为zhi(任意类型)
template <typename T>
std::vector<std::vector<T>> juzhen_chushihua(int m, int n, T zhi) {
if (m <= 0 || n <= 0)return{ {0} };
std::vector<std::vector<T>> l;
std::vector<T> t;
for (int i = 0; i != m; i++) {
for (int j = 0; j != n; j++) {
t.push_back(zhi);
}
l.push_back(t);
t.clear();
}
return l;
}
//将juzhen_2D转换为juzhen类矩阵
template <typename T>
juzhen<T> juzhen_fuzhi(std::vector<std::vector<T>>& x) {
int a = vector_2D_hanshu(x);
int b = vector_2D_lieshu(x);
juzhen<T> l(a, b);
for (int i = 0; i != a; i++) {
for (int j = 0; j != b; j++) {
l.append(i, j, x[i][j]);
}
}
return l;
}
//juzhen_2D单位矩阵
std::vector<std::vector<int>> juzhen_2D_danweijuzhen(int n) {
if (n <= 0)return { {0} };
std::vector<int> t;
std::vector<std::vector<int>> l;
for (int i = 0; i != n; i++) {
for (int j = 0; j != n; j++) {
if (j == i) {
t.push_back(1);
}
else {
t.push_back(0);
}
}
l.push_back(t);
t.clear();
}
return l;
}
template <typename T>
//矩阵归一化,控制均值,标准差
std::vector<std::vector<T>> juzhen_guiyihua(std::vector<std::vector<T>>& x, T junzhi, T biaozhuncha) {
if (juzhen_2D_panduan(x) == false) {
std::cout << "输入矩阵不合法" << std::endl;
return { {0} };
}
int m = juzhen_2D_hanshu(x);
int n = juzhen_2D_lieshu(x);
double sum, Sum;
sum = 0.0;
Sum = 0.0;
for (int i = 0; i != m; i++) {
for (int j = 0; j != n; j++) {
sum += x[i][j];
Sum += x[i][j] * x[i][j];
}
}
std::vector<int> t;
std::vector<std::vector<int>> l;
double jj = sum / m * n;
if (abs(jj - junzhi) >= 1e-5) {
for (int s = 0; s != m; s++) {
for (int f = 0; f != n; f++) {
t.push_back(x[s][f] * (junzhi / jj));
}
l.push_back(t);
t.clear();
}
return l;
}
double aa = sqrt(Sum/m*n - jj * jj);
if (abs(aa - biaozhuncha) > 1e-5) {
for (int s = 0; s != m; s++) {
for (int f = 0; f != n; f++) {
t.push_back(x[s][f] * (junzhi / jj));
}
l.push_back(t);
t.clear();
}
return l;
}
return x;
}
//仅仅适合juzhen类矩阵
juzhen<int> juzhen_danweijuzhen(int n) {
juzhen<int> cuo(1, 1);
cuo.append(0, 0, 0);
if (n <= 0) {
return cuo;
}
juzhen<int> l(n, n);
for (int i = 0; i != n; i++) {
l.append(i, i, 1);
}
return l;
}
template <typename T>
juzhen<T> juzhen_zhuanzhi(juzhen<T>& x) {
juzhen<int> cuo(1, 1);
cuo.append(0, 0, 0);
if (x.panduan() == false)return cuo;
int m = x.juzhen_hanshu();
int n = x.juzhen_lieshu();
juzhen<int> l(n, m);
for (int j = 0; j != n; j++) {
for (int i = 0; i != m; i++) {
l.append(j, i, x[i][j]);
}
}
return l;
}
//坐标合法
template <typename T>
bool zuobiao_hefa(juzhen<T>& x, std::vector<int>& zuobiao) {
if (x.panduan() == false || zuobiao.size() != 2 || zuobiao[0] < 0 || zuobiao[0] >= x.juzhen_hanshu() || zuobiao[1] < 0 || zuobiao[1] >= x.juzhen_lieshu()) {
return false;
}
return true;
}
//抓取矩阵同列元素 如抓取第1列 ,第2行至第5行 所有元素,以vector<T> 类型返回函数值
template <typename T>
std::vector<T> juzhen_zhuaqu_tonlie(juzhen<T>& x, std::vector<int>& zuobiao1, std::vector<int>& zuobiao2) {
if (x.panduan() == false || zuobiao_hefa(x, zuobiao1) == false || zuobiao_hefa(x, zuobiao2) == false || zuobiao1[1] != zuobiao2[1]) return { 0 };
std::vector<T> l;
if (zuobiao1[0] <= zuobiao2[0]) {
for (int i = zuobiao1[0]; i <= zuobiao2[0]; i++) {
l.push_back(x[i][zuobiao1[1]]);
}
}
if (zuobiao1[0] > zuobiao2[0]) {
for (int j = zuobiao2[0]; j >= zuobiao1[0]; j--) {
l.push_back(x[j][zuobiao1[1]]);
}
}
return l;
}
//juzhen_2D 的初始化,指定行数(int),列数(int),任意类型数值x(T)
template <typename T>
std::vector<std::vector<T>> juzhen_2D_chushihua(int m,int n,T x) {
if (m<=0 || n <= 0) {
std::cout << "输入不合法,请重新输入" << std::endl;
return { {0} };
}
std::vector<std::vector<T> >l;
std::vector<T> t;
for (int i = 0; i != m; i++) {
for (int j = 0; j != n; j++) {
t.push_back(x);
}
l.push_back(t);
t.clear();
}
return l;
}
//将矩阵元素分类
template <typename T>
std::vector<std::vector<T>> fenlei(std::vector<T>& x) {
std::set<T> t;
for (const auto& elem : x) {
t.insert(elem);
}
std::map<T, int> counts;//整数,浮点数,字符串可以组成对
for (const auto& elem : x) {
counts[elem]++;
}
std::vector<std::vector<T>> result;
for (const auto& pair : counts) {
result.push_back({ pair.first, pair.second });
}
return result;
}
//矩阵元素随机配对 : n对
//随机从矩阵中获取两个合法且不同的元素坐标,保存在一个二维数组容器内
std::vector<std::vector<int>> juzhen_yuansu_suijipeidui(juzhen<int>& x, int n) {
if (x.panduan() == false || n <= 0)return { {0} };
int a = 0; // 区间下限
int b = x.juzhen_hanshu() - 1;
int m = x.juzhen_lieshu() - 1;
std::random_device rd; // 用于获取随机数种子
std::mt19937 gen(rd()); // 使用Mersenne Twister算法生成随机数
std::uniform_int_distribution<> dis(a, b); // 创建分布,范围是[a, b]
std::vector<std::vector<int>> result;
for (int i = 0; i < n; i++) {
std::vector<int> temp;
for (int j = 0; j < 2; j++) { // 生成两个随机数
int random_num = dis(gen); // 生成随机数
temp.push_back(random_num); // 将随机数添加到temp中
}
result.push_back(temp); // 将temp添加到结果中
}
return result; // 返回结果
}//待完善——————————————————————————————————————————————————————————————————---------------
/// 向量内积
template<typename T>
T xianlian_neiji(std::vector<T>& x, std::vector<T>& y) {
if (x.empty() || y.empty() || x.size() != y.size())return { 0 };
T sum = 0;
for (int i = 0; i != x.size(); i++) {
sum += x[i] * y[i];
}
return sum;
}
template<typename T>
//向量初始化
std::vector<T> xianlian_chushihua(int n, T zhi) {
std::vector<T> t;
if (n <= 0)return {0} ;
for (int i = 0; i != n; i++) {
t.push_back(zhi);
}
return t;
}
//矩阵点乘 001
template<typename T>
std::vector<std::vector<T>> juzhen_dianchen(std::vector<std::vector<T>>& x, std::vector<std::vector<T>>& y) {
if (juzhen_2D_panduan(x) == false || juzhen_2D_panduan(y) == false || juzhen_2D_hanshu(x) != juzhen_2D_hanshu(y) || juzhen_2D_lieshu(x) != juzhen_2D_lieshu(y)) {
std::cout << "输入矩阵不合法" << std::endl;
return { {0} };
}
std::vector<std::vector<T>> l;
std::vector<T> t;
for (int i = 0; i != juzhen_2D_hanshu(x); i++) {
for (int j = 0; j != juzhen_2D_lieshu(x); j++) {
t.push_back(x[i][j] * y[i][j]);
}
l.push_back(t);
t.clear();
}
return l;
}
//矩阵内积
template<typename T>
T juzhen_neiji(std::vector<std::vector<T>>& x, std::vector<std::vector<T>>& y) {
std::vector<std::vector<T>> l;
std::vector<T> t;
T sum = 0;
if (juzhen_2D_panduan(x) == false || juzhen_2D_panduan(y) == false || x.size()!=y.size() || x[0].size()!=y[0].size())return false;
else {
for (int i = 0; i != x.size(); i++) {
for (int j = 0; j != x[0].size(); j++) {
sum += x[i][j] * y[i][j];
}
}
return sum;
}
}
//矩阵乘法 001
template<typename T>
std::vector<std::vector<T>> juzhen_chenfa(std::vector<std::vector<T>>& x, std::vector<std::vector<T>>& y) {
if (juzhen_2D_panduan(x) == false || juzhen_2D_panduan(y) == false || juzhen_2D_lieshu(x) != juzhen_2D_hanshu(y))return { {0} };
int sum = 0;
std::vector<T> t;
std::vector<T> s;
std::vector<std::vector<T>> l;
int xl = juzhen_2D_hanshu(x);
int xd = juzhen_2D_lieshu(x);
int yl = juzhen_2D_hanshu(y);
int yd = juzhen_2D_lieshu(y);
for (int i = 0; i != xl; i++) {
for (int j = 0; j != yd; j++) {
for (int k = 0; k != yl; k++) {
s.push_back(y[k][j]);
}
sum = xianlian_neiji(x[i], s);//向量内积
s.clear();
t.push_back(sum);
sum = 0;
}
l.push_back(t);
t.clear();
}
return l;
}
//----------------------------------------------------------------------------------------------------------------------------------
// 矩阵pad填充
//默认填充0 输入:矩阵,填充位数
template<typename T>
std::vector<std::vector<T>> juzhen_pad0(std::vector<std::vector<T>>& x, int k) {
if (juzhen_2D_panduan(x) == false || k <= 0) {
std::cout << "矩阵或填充层数输入错误" << std::endl;
return { {0} };
}
int a = juzhen_2D_hanshu(x);
int b = juzhen_2D_lieshu(x);
std::vector<std::vector<T>> l;
std::vector<T> t;
for (int i = 0; i != a + 2*k ; i++) {
for (int j = 0; j != b +2* k; j++) {
if (i >= k && i <= x.size() - 1 + k && j>=k && j<=x[0].size()-1+k) {
t.push_back(x[i - k][j - k]);
}
else {
t.push_back(0);
}
}
l.push_back(t);
t.clear();
}
return l;
}
//自定义填充值,自定义填充位数
template<typename T>
// 输入:矩阵,填充值,填充位数
std::vector<std::vector<T>> juzhen_pad(std::vector<std::vector<T>>& x, T zhi, int k) {
if (juzhen_2D_panduan(x) == false || k <= 0) {
std::cout << "矩阵或填充层数输入错误" << std::endl;
return { {0} };
}
int a = juzhen_2D_hanshu(x);
int b = juzhen_2D_lieshu(x);
std::vector<std::vector<T>> l;
std::vector<T> t;
for (int i = 0; i != a + 2 * k; i++) {
for (int j = 0; j != b + 2 * k; j++) {
if (i >= k && i <= x.size() - 1 + k && j >= k && j <= x[0].size() - 1 + k) {
t.push_back(x[i - k][j - k]);
}
else {
t.push_back(zhi);
}
}
l.push_back(t);
t.clear();
}
return l;
}
//矩阵加法
template<typename T>
std::vector <std::vector<T>> juzhen_jiafa(std::vector<std::vector<T>>& x, std::vector<std::vector<T>>& y) {
if (juzhen_2D_panduan(x) == false || juzhen_2D_panduan(y) == false) {
std::cout << "输入矩阵不符合矩阵加法要求" << std::endl;
return { {0} };
}
std::vector<T> t;
std::vector<std::vector<T>> l;
int m = juzhen_2D_hanshu(x);
int n = juzhen_2D_lieshu(x);
for (int i = 0; i != m; i++) {
for (int j = 0; j != n; j++) {
t.push_back(x[i][j] + y[i][j]);
}
l.push_back(t);
t.clear();
}
return l;
}
//矩阵减法
template<typename T>
std::vector <std::vector<T>> juzhen_jianfa(std::vector<std::vector<T>>& x, std::vector<std::vector<T>>& y) {
if (juzhen_2D_panduan(x) == false || juzhen_2D_panduan(y) == false) {
std::cout << "输入矩阵不符合矩阵减法要求" << std::endl;
return { {0} };
}
std::vector<T> t;
std::vector<std::vector<T>> l;
int m = juzhen_2D_hanshu(x);
int n = juzhen_2D_lieshu(x);
for (int i = 0; i != m; i++) {
for (int j = 0; j != n; j++) {
t.push_back(x[i][j]-y[i][j]);
}
l.push_back(t);
t.clear();
}
return l;
}
//time 检测程序运行时间
template<typename Func>
auto time(Func&& func)
{
auto start = std::chrono::high_resolution_clock::now();
func();
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << "Elapsed time: " << elapsed.count() << " seconds." << std::endl;
return elapsed.count();
}
//命名空间python的应用, 应为 using namespace python; 或者python:: 暂时不支持C++23 新增加功能
//python命名空间,提供类似python语法功能。
namespace python {
//打印任意类型数据
template<typename T>
void print(T x) {
std::cout << x << std::endl;
}
//print函数第2次重载,打印一维数组
template<typename T>
void print(const std::vector<T>& x) {
for (int i = 0; i != x.size(); i++) {
std::cout << x[i] << " ";
}
std::cout << std::endl;
}
//print函数第3次重载,打印二维数组
template<typename T>
void print(const std::vector<std::vector<T>>& x) {
if (x.empty()) {
std::cout << "二维数组为空" <<std::endl;
}
int p = 1;
for (int k = 0; k != x.size(); k++) {
if (x[k].empty() || x[k].size()!=x[0].size())p = 0;
break;
}
if (p == 0) {
std::cout << "输入矩阵不合法"<<std::endl;
}
for (int i = 0; i != x.size(); i++) {
for (int j = 0; j != x[0].size(); j++) {
std::cout << x[i][j] << " ";
}
std::cout << std::endl;
}
}
//print函数第4次重载,打印矩阵类的矩阵
template<typename T>
void print(const juzhen<T>& x) {
if (x.juzhen_panduan() == false) {
std::cout << "矩阵不合法"<<std::endl;
}
else {
for (int i = 0; i != x.juzhen_hanshu(); i++) {
for (int j = 0; j != x.juzhen_lieshu(); j++) {
std::cout << x[i][j] << " ";
}
std::cout << std::endl;
}
}
}
}
#endif
#ifndef WPC_H
#define WPC_H
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include<map>
#include<random>
#include <thread>
#include <mutex>
#include <utility>
#include<stack>
#include<queue>
#include<set>
#include <algorithm>
#include <exception>
#include <typeinfo>
#include<cmath>
#include <chrono>
#include <cstdlib>
#include<cstdio>
//本库不需要其他C++第三方库,只需要 C++ STL标准库,各种c库,其他内置库
/// 本库仅仅在符合如下条件的环境下通过测试:
// C++标准:C++ 14 C++17 C++20
// x64
// Debug/Release
// Windows 11
// Visual Studio profession 2022 (未在vscode测试过)
// 本库不兼容C++20 或C++23新增加内容
// 编译器: mingw
// 暂不支持英伟达等显卡加速
//为了保证数据安全性本库基本不提供对任何原始数据直接操作的函数
//=================================================================================================================================================================
template <typename T>//矩阵元素可以为任意类型
class juzhen {//矩阵类
private:
int hanshu;//行数
int lieshu;//列数
std::vector<std::vector<T>> data;//数据
public:
juzhen(int hanshu, int lieshu) : hanshu(hanshu), lieshu(lieshu), data(hanshu, std::vector<T>(lieshu)) {}
int juzhen_hanshu() const {
return hanshu;
}
int juzhen_lieshu() const {
return lieshu;
}
//重载符号[],方便通过下标访问矩阵合法元素
const std::vector<T>& operator[](int index) const {
if (index < 0 || index >= hanshu) {
throw std::out_of_range("超出矩阵边界");
}
return data[index];
}
std::vector<T>& operator[](int index) {
if (index < 0 || index >= hanshu) {
throw std::out_of_range("超出矩阵边界");
}
return data[index];
}
//重载符号(),方便通过下标访问矩阵合法元素
const T& operator()(int i, int j) const {
if (i < 0 || i >= hanshu || j < 0 || j >= lieshu) {
throw std::out_of_range("超出矩阵边界");
}
return data[i][j];
}
T& operator()(int i, int j) {
if (i < 0 || i >= hanshu || j < 0 || j >= lieshu) {
throw std::out_of_range("超出矩阵边界");
}
return data[i][j];
}
//juzhen类判断是否合法 注意不是juzhen_2D
bool panduan() {
int p = 1;
if (!data.empty()) {
for (int i = 0; i != hanshu; i++) {
if (data[i].size() != lieshu || data[i].empty())p = 0;
break;
}
}
return (p != 0);
}
//模仿python中.append()函数 但需要指定坐标 如 l.append(i,j,k) 意思为l[i][j]=k,而不是python的添加功能
void append(int i, int j, const T& value) {
if (i < 0 || i >= hanshu || j < 0 || j >= lieshu) {
throw std::out_of_range("超出矩阵边界");
}
data[i][j] = (value);
}
//只可以用于juzhen 类矩阵
void juzhen_print() {
for (int i = 0; i != hanshu; i++) {
for (int j = 0; j != lieshu; j++) {
std::cout << data[i][j] << " ";
}
std::cout << std::endl;
}
}
//矩阵元素初始化为任意类型(T) x
void chushihua(const T x) {
if (!data.empty()) {
for (int i = 0; i != hanshu; i++) {
for (int j = 0; j != lieshu; j++) {
data[i][j] = x;
}
}
}
}
};
//-----------------------------------------------------------------------------------------------------------------------------------------------------
//==========================================================================================================================================================
//==========================================================================================================================================================
//
//
//与juzhen类不同的是,juzhen_2D_ 系列函数是指类型为std::vector<std::vector<T> 的二维数组,使得操作跟灵活,而juzhen类矩阵必须初始化矩阵行数,列数
template <typename T>
bool juzhen_2D_panduan(std::vector<std::vector<T>>& x) {
if (x.empty())return false;
int p = 1;
for (int i = 1; i != x.size(); i++) {
if (x[i].size() != x[0].size())p = 0;
break;
}
return p != 0;
}
//juzhen_2D行数
template <typename T>
int juzhen_2D_hanshu(std::vector<std::vector<T>>& x) {
if (juzhen_2D_panduan(x) == false)return -1;
int sum = 0;
for (int i = 0; i != x.size(); i++) {
sum++;
}
return sum;
}
//juzhen_2D列数
template <typename T>
int juzhen_2D_lieshu(std::vector<std::vector<T>>& x) {
if (juzhen_2D_panduan(x) == false)return -1;
int sum = 0;
for (int i = 0; i != x[0].size(); i++) {
sum++;
}
return sum;
}
//矩阵初始化 创造一个 m*n 矩阵 所有元素设置为zhi(任意类型)
template <typename T>
std::vector<std::vector<T>> juzhen_chushihua(int m, int n, T zhi) {
if (m <= 0 || n <= 0)return{ {0} };
std::vector<std::vector<T>> l;
std::vector<T> t;
for (int i = 0; i != m; i++) {
for (int j = 0; j != n; j++) {
t.push_back(zhi);
}
l.push_back(t);
t.clear();
}
return l;
}
//将juzhen_2D转换为juzhen类矩阵
template <typename T>
juzhen<T> juzhen_fuzhi(std::vector<std::vector<T>>& x) {
int a = vector_2D_hanshu(x);
int b = vector_2D_lieshu(x);
juzhen<T> l(a, b);
for (int i = 0; i != a; i++) {
for (int j = 0; j != b; j++) {
l.append(i, j, x[i][j]);
}
}
return l;
}
//juzhen_2D单位矩阵
std::vector<std::vector<int>> juzhen_2D_danweijuzhen(int n) {
if (n <= 0)return { {0} };
std::vector<int> t;
std::vector<std::vector<int>> l;
for (int i = 0; i != n; i++) {
for (int j = 0; j != n; j++) {
if (j == i) {
t.push_back(1);
}
else {
t.push_back(0);
}
}
l.push_back(t);
t.clear();
}
return l;
}
template <typename T>
//矩阵归一化,控制均值,标准差
std::vector<std::vector<T>> juzhen_guiyihua(std::vector<std::vector<T>>& x, T junzhi, T biaozhuncha) {
if (juzhen_2D_panduan(x) == false) {
std::cout << "输入矩阵不合法" << std::endl;
return { {0} };
}
int m = juzhen_2D_hanshu(x);
int n = juzhen_2D_lieshu(x);
double sum, Sum;
sum = 0.0;
Sum = 0.0;
for (int i = 0; i != m; i++) {
for (int j = 0; j != n; j++) {
sum += x[i][j];
Sum += x[i][j] * x[i][j];
}
}
std::vector<int> t;
std::vector<std::vector<int>> l;
double jj = sum / m * n;
if (abs(jj - junzhi) >= 1e-5) {
for (int s = 0; s != m; s++) {
for (int f = 0; f != n; f++) {
t.push_back(x[s][f] * (junzhi / jj));
}
l.push_back(t);
t.clear();
}
return l;
}
double aa = sqrt(Sum/m*n - jj * jj);
if (abs(aa - biaozhuncha) > 1e-5) {
for (int s = 0; s != m; s++) {
for (int f = 0; f != n; f++) {
t.push_back(x[s][f] * (junzhi / jj));
}
l.push_back(t);
t.clear();
}
return l;
}
return x;
}
//仅仅适合juzhen类矩阵
juzhen<int> juzhen_danweijuzhen(int n) {
juzhen<int> cuo(1, 1);
cuo.append(0, 0, 0);
if (n <= 0) {
return cuo;
}
juzhen<int> l(n, n);
for (int i = 0; i != n; i++) {
l.append(i, i, 1);
}
return l;
}
template <typename T>
juzhen<T> juzhen_zhuanzhi(juzhen<T>& x) {
juzhen<int> cuo(1, 1);
cuo.append(0, 0, 0);
if (x.panduan() == false)return cuo;
int m = x.juzhen_hanshu();
int n = x.juzhen_lieshu();
juzhen<int> l(n, m);
for (int j = 0; j != n; j++) {
for (int i = 0; i != m; i++) {
l.append(j, i, x[i][j]);
}
}
return l;
}
//坐标合法
template <typename T>
bool zuobiao_hefa(juzhen<T>& x, std::vector<int>& zuobiao) {
if (x.panduan() == false || zuobiao.size() != 2 || zuobiao[0] < 0 || zuobiao[0] >= x.juzhen_hanshu() || zuobiao[1] < 0 || zuobiao[1] >= x.juzhen_lieshu()) {
return false;
}
return true;
}
//抓取矩阵同列元素 如抓取第1列 ,第2行至第5行 所有元素,以vector<T> 类型返回函数值
template <typename T>
std::vector<T> juzhen_zhuaqu_tonlie(juzhen<T>& x, std::vector<int>& zuobiao1, std::vector<int>& zuobiao2) {
if (x.panduan() == false || zuobiao_hefa(x, zuobiao1) == false || zuobiao_hefa(x, zuobiao2) == false || zuobiao1[1] != zuobiao2[1]) return { 0 };
std::vector<T> l;
if (zuobiao1[0] <= zuobiao2[0]) {
for (int i = zuobiao1[0]; i <= zuobiao2[0]; i++) {
l.push_back(x[i][zuobiao1[1]]);
}
}
if (zuobiao1[0] > zuobiao2[0]) {
for (int j = zuobiao2[0]; j >= zuobiao1[0]; j--) {
l.push_back(x[j][zuobiao1[1]]);
}
}
return l;
}
//juzhen_2D 的初始化,指定行数(int),列数(int),任意类型数值x(T)
template <typename T>
std::vector<std::vector<T>> juzhen_2D_chushihua(int m,int n,T x) {
if (m<=0 || n <= 0) {
std::cout << "输入不合法,请重新输入" << std::endl;
return { {0} };
}
std::vector<std::vector<T> >l;
std::vector<T> t;
for (int i = 0; i != m; i++) {
for (int j = 0; j != n; j++) {
t.push_back(x);
}
l.push_back(t);
t.clear();
}
return l;
}
//将矩阵元素分类
template <typename T>
std::vector<std::vector<T>> fenlei(std::vector<T>& x) {
std::set<T> t;
for (const auto& elem : x) {
t.insert(elem);
}
std::map<T, int> counts;//整数,浮点数,字符串可以组成对
for (const auto& elem : x) {
counts[elem]++;
}
std::vector<std::vector<T>> result;
for (const auto& pair : counts) {
result.push_back({ pair.first, pair.second });
}
return result;
}
//矩阵元素随机配对 : n对
//随机从矩阵中获取两个合法且不同的元素坐标,保存在一个二维数组容器内
std::vector<std::vector<int>> juzhen_yuansu_suijipeidui(juzhen<int>& x, int n) {
if (x.panduan() == false || n <= 0)return { {0} };
int a = 0; // 区间下限
int b = x.juzhen_hanshu() - 1;
int m = x.juzhen_lieshu() - 1;
std::random_device rd; // 用于获取随机数种子
std::mt19937 gen(rd()); // 使用Mersenne Twister算法生成随机数
std::uniform_int_distribution<> dis(a, b); // 创建分布,范围是[a, b]
std::vector<std::vector<int>> result;
for (int i = 0; i < n; i++) {
std::vector<int> temp;
for (int j = 0; j < 2; j++) { // 生成两个随机数
int random_num = dis(gen); // 生成随机数
temp.push_back(random_num); // 将随机数添加到temp中
}
result.push_back(temp); // 将temp添加到结果中
}
return result; // 返回结果
}//待完善——————————————————————————————————————————————————————————————————---------------
/// 向量内积
template<typename T>
T xianlian_neiji(std::vector<T>& x, std::vector<T>& y) {
if (x.empty() || y.empty() || x.size() != y.size())return { 0 };
T sum = 0;
for (int i = 0; i != x.size(); i++) {
sum += x[i] * y[i];
}
return sum;
}
template<typename T>
//向量初始化
std::vector<T> xianlian_chushihua(int n, T zhi) {
std::vector<T> t;
if (n <= 0)return {0} ;
for (int i = 0; i != n; i++) {
t.push_back(zhi);
}
return t;
}
//矩阵点乘 001
template<typename T>
std::vector<std::vector<T>> juzhen_dianchen(std::vector<std::vector<T>>& x, std::vector<std::vector<T>>& y) {
if (juzhen_2D_panduan(x) == false || juzhen_2D_panduan(y) == false || juzhen_2D_hanshu(x) != juzhen_2D_hanshu(y) || juzhen_2D_lieshu(x) != juzhen_2D_lieshu(y)) {
std::cout << "输入矩阵不合法" << std::endl;
return { {0} };
}
std::vector<std::vector<T>> l;
std::vector<T> t;
for (int i = 0; i != juzhen_2D_hanshu(x); i++) {
for (int j = 0; j != juzhen_2D_lieshu(x); j++) {
t.push_back(x[i][j] * y[i][j]);
}
l.push_back(t);
t.clear();
}
return l;
}
//矩阵内积
template<typename T>
T juzhen_neiji(std::vector<std::vector<T>>& x, std::vector<std::vector<T>>& y) {
std::vector<std::vector<T>> l;
std::vector<T> t;
T sum = 0;
if (juzhen_2D_panduan(x) == false || juzhen_2D_panduan(y) == false || x.size()!=y.size() || x[0].size()!=y[0].size())return false;
else {
for (int i = 0; i != x.size(); i++) {
for (int j = 0; j != x[0].size(); j++) {
sum += x[i][j] * y[i][j];
}
}
return sum;
}
}
//矩阵乘法 001
template<typename T>
std::vector<std::vector<T>> juzhen_chenfa(std::vector<std::vector<T>>& x, std::vector<std::vector<T>>& y) {
if (juzhen_2D_panduan(x) == false || juzhen_2D_panduan(y) == false || juzhen_2D_lieshu(x) != juzhen_2D_hanshu(y))return { {0} };
int sum = 0;
std::vector<T> t;
std::vector<T> s;
std::vector<std::vector<T>> l;
int xl = juzhen_2D_hanshu(x);
int xd = juzhen_2D_lieshu(x);
int yl = juzhen_2D_hanshu(y);
int yd = juzhen_2D_lieshu(y);
for (int i = 0; i != xl; i++) {
for (int j = 0; j != yd; j++) {
for (int k = 0; k != yl; k++) {
s.push_back(y[k][j]);
}
sum = xianlian_neiji(x[i], s);//向量内积
s.clear();
t.push_back(sum);
sum = 0;
}
l.push_back(t);
t.clear();
}
return l;
}
//----------------------------------------------------------------------------------------------------------------------------------
// 矩阵pad填充
//默认填充0 输入:矩阵,填充位数
template<typename T>
std::vector<std::vector<T>> juzhen_pad0(std::vector<std::vector<T>>& x, int k) {
if (juzhen_2D_panduan(x) == false || k <= 0) {
std::cout << "矩阵或填充层数输入错误" << std::endl;
return { {0} };
}
int a = juzhen_2D_hanshu(x);
int b = juzhen_2D_lieshu(x);
std::vector<std::vector<T>> l;
std::vector<T> t;
for (int i = 0; i != a + 2*k ; i++) {
for (int j = 0; j != b +2* k; j++) {
if (i >= k && i <= x.size() - 1 + k && j>=k && j<=x[0].size()-1+k) {
t.push_back(x[i - k][j - k]);
}
else {
t.push_back(0);
}
}
l.push_back(t);
t.clear();
}
return l;
}
//自定义填充值,自定义填充位数
template<typename T>
// 输入:矩阵,填充值,填充位数
std::vector<std::vector<T>> juzhen_pad(std::vector<std::vector<T>>& x, T zhi, int k) {
if (juzhen_2D_panduan(x) == false || k <= 0) {
std::cout << "矩阵或填充层数输入错误" << std::endl;
return { {0} };
}
int a = juzhen_2D_hanshu(x);
int b = juzhen_2D_lieshu(x);
std::vector<std::vector<T>> l;
std::vector<T> t;
for (int i = 0; i != a + 2 * k; i++) {
for (int j = 0; j != b + 2 * k; j++) {
if (i >= k && i <= x.size() - 1 + k && j >= k && j <= x[0].size() - 1 + k) {
t.push_back(x[i - k][j - k]);
}
else {
t.push_back(zhi);
}
}
l.push_back(t);
t.clear();
}
return l;
}
//矩阵加法
template<typename T>
std::vector <std::vector<T>> juzhen_jiafa(std::vector<std::vector<T>>& x, std::vector<std::vector<T>>& y) {
if (juzhen_2D_panduan(x) == false || juzhen_2D_panduan(y) == false) {
std::cout << "输入矩阵不符合矩阵加法要求" << std::endl;
return { {0} };
}
std::vector<T> t;
std::vector<std::vector<T>> l;
int m = juzhen_2D_hanshu(x);
int n = juzhen_2D_lieshu(x);
for (int i = 0; i != m; i++) {
for (int j = 0; j != n; j++) {
t.push_back(x[i][j] + y[i][j]);
}
l.push_back(t);
t.clear();
}
return l;
}
//矩阵减法
template<typename T>
std::vector <std::vector<T>> juzhen_jianfa(std::vector<std::vector<T>>& x, std::vector<std::vector<T>>& y) {
if (juzhen_2D_panduan(x) == false || juzhen_2D_panduan(y) == false) {
std::cout << "输入矩阵不符合矩阵减法要求" << std::endl;
return { {0} };
}
std::vector<T> t;
std::vector<std::vector<T>> l;
int m = juzhen_2D_hanshu(x);
int n = juzhen_2D_lieshu(x);
for (int i = 0; i != m; i++) {
for (int j = 0; j != n; j++) {
t.push_back(x[i][j]-y[i][j]);
}
l.push_back(t);
t.clear();
}
return l;
}
//time 检测程序运行时间
template<typename Func>
auto time(Func&& func)
{
auto start = std::chrono::high_resolution_clock::now();
func();
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << "Elapsed time: " << elapsed.count() << " seconds." << std::endl;
return elapsed.count();
}
//命名空间python的应用, 应为 using namespace python; 或者python:: 暂时不支持C++23 新增加功能
//python命名空间,提供类似python语法功能。
namespace python {
//打印任意类型数据
template<typename T>
void print(T x) {
std::cout << x << std::endl;
}
//print函数第2次重载,打印一维数组
template<typename T>
void print(const std::vector<T>& x) {
for (int i = 0; i != x.size(); i++) {
std::cout << x[i] << " ";
}
std::cout << std::endl;
}
//print函数第3次重载,打印二维数组
template<typename T>
void print(const std::vector<std::vector<T>>& x) {
if (x.empty()) {
std::cout << "二维数组为空" <<std::endl;
}
int p = 1;
for (int k = 0; k != x.size(); k++) {
if (x[k].empty() || x[k].size()!=x[0].size())p = 0;
break;
}
if (p == 0) {
std::cout << "输入矩阵不合法"<<std::endl;
}
for (int i = 0; i != x.size(); i++) {
for (int j = 0; j != x[0].size(); j++) {
std::cout << x[i][j] << " ";
}
std::cout << std::endl;
}
}
//print函数第4次重载,打印矩阵类的矩阵
template<typename T>
void print(const juzhen<T>& x) {
if (x.juzhen_panduan() == false) {
std::cout << "矩阵不合法"<<std::endl;
}
else {
for (int i = 0; i != x.juzhen_hanshu(); i++) {
for (int j = 0; j != x.juzhen_lieshu(); j++) {
std::cout << x[i][j] << " ";
}
std::cout << std::endl;
}
}
}
}
#endif

3785

被折叠的 条评论
为什么被折叠?



