多线程 1
载入中...
搜索中...
未找到
矩阵.hpp
浏览该文件的文档.
1//
2// 矩阵.hpp
3// 多线程
4//
5// Created by 赵明明 on 2023/6/15.
6//
7
8#ifndef 矩阵__hpp
9#define 矩阵__hpp
10#define NDEBUG
11#include <stdio.h>
12#include <iostream>
13#include <sstream>
14#include <stdarg.h>
15#include <iomanip>
16using namespace std;
19 float * data;
20 int w;
21 int h;
22};
23
25class 矩阵{
26public:
28 float Inf=3.4*pow(10,38);
30 int N;
32 int w;
34 int h;
36 float max=3.4*pow(10,38);
38 float min=3.4*pow(10,38);
40 float * data;
41public:
43 矩阵(矩阵python * 结构体){
44 w=结构体->w;
45 h=结构体->h;
46 N=w*h;
47 data=结构体->data;
48 }
49
51 矩阵(int _h,int _w,float value){
52 N=_w*_h;
53 w=_w;
54 h=_h;
55 data = new float[N];
56 //memset(data,value,sizeof(float)*N);
57 for(int i=0;i<N;i++){
58 data[i]=value;
59 }
60 }
61
63 矩阵(int _h,int _w,矩阵 value){
64 N=_w*_h;
65 w=_w;
66 h=_h;
67 data = new float[N];
68 //memset(data,value,sizeof(float)*N);
69 for(int i=0;i<N;i++){
70 data[i]=value[i];
71 }
72 }
73
76 矩阵(int _h,int _w,float * init_data,bool COPYARRAY){
77 N=_w*_h;
78 w=_w;
79 h=_h;
80 if(COPYARRAY==1){
81 data = new float[N];
82
83 //memset(data,value,sizeof(float)*N);
84 for(int i=0;i<N;i++){
85 data[i]=init_data[i];
86 }
87 }
88 else{
89 data=init_data;
90 }
91 }
92
94 矩阵(int n,float value){
95 N=n;
96 w=n;
97 h=1;
98 data = new float[N];
99 //memset(data,value,sizeof(float)*N);
100 for(int i=0;i<N;i++){
101 data[i]=value;
102 }
103 }
104
107 N=阵.N;
108 w=阵.w;
109 h=阵.h;
110 data=new float[N];
111 for(int i=0;i<N;i++){
112 data[i]=阵[i];
113 }
114 }
115
117 void 取整(void){
118 for(int i=0;i<N;i++){
119 data[i]=(int)data[i];
120 }
121 }
122
124 void 精度(int n=5){
125 for(int i=0;i<N;i++){
126 float f = data[i];
127 float t1 = f-(int)f;
128 t1=round(t1*pow(10,n));
129 t1/=pow(10,n);
130 data[i]=t1+(int)f;
131 }
132 }
133
135 矩阵* 点积(矩阵& 矩阵1){
136 矩阵 积=operator*(矩阵1);
137 return 积.(1);
138 }
139
142 矩阵* 单位=new 矩阵(h,w,(float)0);
143 矩阵 积 = operator*(*this);
144 矩阵* 和值=积.(1);
145 //矩阵* 指数值=和值->指数(0.5);
146 for(int i=0;i<h;i++){
147 stringstream ss;
148 ss<<i<<",:";
149 矩阵* =operator[](ss.str());
150 float norm= pow((*和值)[i],0.5);
151 矩阵 长=矩阵(1,w,norm);
152 矩阵 行果=(*行)/长;
153 //cout<<行果;
154 for(int j=0;j<w;j++){
155 单位->data[i*w+j]=(行果)[j];
156 }
157 }
158 return 单位;
159 }
160
162 矩阵* (void){
163 矩阵* =new 矩阵(1,1,(float)0);
164 for(int i=0;i<N;i++){
165 (*和)[0]+=data[i];
166 }
167 return ;
168 }
169
171 矩阵* (int dim){
172 矩阵* =NULL;
173 if(dim==0){
174 =new 矩阵(1,w,(float)0);
175 for(int i=0;i<h;i++){
176 for(int j=0;j<w;j++){
177 (*和)[j]+=data[i*w+j];
178 }
179 }
180 }
181 else if(dim==1){
182 =new 矩阵(h,1,(float)0);
183 for(int i=0;i<h;i++){
184 for(int j=0;j<w;j++){
185 (*和)[i]+=data[i*w+j];
186 }
187 }
188
189 }else{
190 cout<<"仅支持对 0 或者 1 维的和操作";
191 }
192 return ;
193 }
194
197 矩阵* minmax=new 矩阵(1,2,(float)0);
198 矩阵* 排序=new 矩阵(h,w,data,1);
199 std::sort(排序->data,排序->data+N);// 对矩阵中的所有元素求最大,最小
200 (*minmax)[1]=排序->data[N-1];//最大值
201 (*minmax)[0]=排序->data[0]; //最小值
202 return minmax;
203 }
204
206 矩阵* minmax(int dim){
207 矩阵* minmax=NULL;
208 if(dim==0){
209 minmax=new 矩阵(2,w,(float)0);
210 for(int i=0;i<w;i++){
211 stringstream ss;
212 ss<<":,"<<i;
213 矩阵* 排序=(*this)[ss.str()];
214 std::sort(排序->data,排序->data+排序->N);
215 minmax->data[w+i]=排序->data[排序->N-1];//最大值
216 minmax->data[i]=排序->data[0];//最小值
217 }
218 }
219 else if(dim==1){
220 minmax=new 矩阵(2,h,(float)0);
221 for(int i=0;i<h;i++){
222 stringstream ss;
223 ss<<i<<",:";
224 矩阵* 排序=(*this)[ss.str()];
225 std::sort(排序->data,排序->data+排序->N);
226 minmax->data[w+i]=排序->data[排序->N-1];
227 minmax->data[i]=排序->data[0];
228 }
229
230 }else{
231 cout<<"维度只能是 0 或者 1";
232 }
233 return minmax;
234 }
236 矩阵* arange(int start,int end){
237 int L=end-start;
238 矩阵* =NULL;
239 =new 矩阵(1,L,(float)0);
240 for(int i=0;i<L;i++){
241 ->data[i]=start+i;
242 }
243 return ;
244 }
245
247 矩阵** meshgrid(矩阵* 行x, 矩阵* 行y,矩阵** 坐标阵){
248 //矩阵* 坐标阵[]={NULL,NULL};
249 坐标阵[0]=new 矩阵(1,行x->w,行x->data,1);
250 坐标阵[1]=new 矩阵(行y->w,1,行y->data,1);
251 矩阵* 行y转置=new 矩阵(行y->w,行y->h,行y->data,1);
252 for(int i=1;i<行y->w;i++){
253 坐标阵[0]=this->r_(坐标阵[0],行x);
254 }
255 for(int i=1;i<行x->w;i++){
256
257 坐标阵[1]=this->c_(坐标阵[1],行y转置);
258 }
259
260 return 坐标阵;
261 }
262
264 float& operator[](int i){
265 if(i>N || i<-N){
266 cout<<"下标超出范围了"<<endl;
267 }
268 if(i<0){
269 i=N+i;
270 }
271 return data[i];
272 }
273
275 int str2int(string ij){
276
277 int int_ij=0;
278 unsigned long L=ij.length();
279 for(int i=0;i<L;i++){
280 int tmp=ij[i]-'0';
281 int_ij+=tmp*pow(10,L-i-1);
282 }
283 return int_ij;
284 }
285
287 void test_str2int(void){
288 string teststr="1234";
289 cout<<teststr<<"="<<str2int(teststr)<<endl;
290 teststr="1000";
291 cout<<teststr<<"="<<str2int(teststr)<<endl;
292
293 }
294
296 矩阵* c_(矩阵* 矩阵1,矩阵* 矩阵2){
297 // 按列合并
298 assert(矩阵1->h==矩阵2->h);
299 矩阵* 矩阵3=new 矩阵(矩阵1->h,矩阵1->w+矩阵2->w,-2);
300 for(int i=0;i<矩阵1->h;i++){
301 for(int j=0;j<矩阵1->w;j++){
302 矩阵3->data[i*矩阵3->w+j]=矩阵1->data[i*矩阵1->w+j];
303 }
304 }
305 for(int i=0;i<矩阵2->h;i++){
306 for(int j=0;j<矩阵2->w;j++){
307 矩阵3->data[i*矩阵3->w+j+矩阵1->w]=矩阵2->data[i*矩阵2->w+j];
308 }
309 }
310 return 矩阵3;
311 }
312
314 矩阵* r_(矩阵* 矩阵1,矩阵* 矩阵2){
315 // 按列合并
316 assert(矩阵1->w==矩阵2->w);
317 矩阵* 矩阵3=new 矩阵(矩阵1->h+矩阵2->h,矩阵1->w,-2);
318 for(int i=0;i<矩阵1->h;i++){
319 for(int j=0;j<矩阵1->w;j++){
320 矩阵3->data[i*矩阵3->w+j]=矩阵1->data[i*矩阵1->w+j];
321 }
322 }
323 for(int i=0;i<矩阵2->h;i++){
324 for(int j=0;j<矩阵2->w;j++){
325 矩阵3->data[(i+矩阵1->h)*矩阵3->w+j]=矩阵2->data[i*矩阵2->w+j];
326 }
327 }
328 return 矩阵3;
329 }
330
333 assert(this->w==矩阵1.w);
334 assert(this->h==矩阵1.h);
335 矩阵* 果=new 矩阵(h,w,(float)0);
336 for(int i=0;i<w;i++){
337 for(int j=0;j<h;j++){
338 果->data[j*w+i]=data[j*w+i]+矩阵1.data[j*w+i];
339 }
340 }
341 return *(果);
342 }
343
345 template <typename T>
347 矩阵* 矩阵1=new 矩阵(h,w,value);
348 return operator+(*(矩阵1));
349 }
350
353 assert(this->w==矩阵1.w);
354 assert(this->h==矩阵1.h);
355 矩阵* 果=new 矩阵(h,w,(float)0);
356 for(int i=0;i<w;i++){
357 for(int j=0;j<h;j++){
358 果->data[j*w+i]=data[j*w+i]-矩阵1.data[j*w+i];
359 }
360 }
361 return *(果);
362 }
363
365 template <typename T>
367 矩阵* 矩阵1=new 矩阵(h,w,value);
368 return operator-(*(矩阵1));
369 }
370
373 assert(this->w==矩阵1.w);
374 assert(this->h==矩阵1.h);
375 矩阵* 果=new 矩阵(h,w,(float)0);
376 for(int i=0;i<w;i++){
377 for(int j=0;j<h;j++){
378 果->data[j*w+i]=data[j*w+i]*矩阵1.data[j*w+i];
379 }
380 }
381 return *(果);
382 }
383
385 template <typename T>
387 矩阵* 矩阵1=new 矩阵(h,w,value);
388 return operator*(*(矩阵1));
389 }
392 assert(this->w==矩阵1.w);
393 assert(this->h==矩阵1.h);
394 矩阵* 果=new 矩阵(h,w,(float)0);
395 for(int i=0;i<w;i++){
396 for(int j=0;j<h;j++){
397 果->data[j*w+i]=data[j*w+i]/矩阵1.data[j*w+i];
398 }
399 }
400 return *(果);
401 }
402
404 template <typename T>
406 矩阵* 矩阵1=new 矩阵(h,w,value);
407 return operator/(*(矩阵1));
408 }
409
412 assert(this->w==矩阵1.w);
413 assert(this->h==矩阵1.h);
414 矩阵* 果=new 矩阵(h,w,(float)0);
415 for(int i=0;i<w;i++){
416 for(int j=0;j<h;j++){
417 果->data[j*w+i]=0;
418 if(data[j*w+i]>=矩阵1.data[j*w+i]){
419 果->data[j*w+i]=1;
420 }
421 }
422 }
423 return *(果);
424 }
425
427 template <typename T>
429 矩阵* 矩阵1=new 矩阵(h,w,value);
430 return operator>=(*(矩阵1));
431 }
432
435 assert(this->w==矩阵1.w);
436 assert(this->h==矩阵1.h);
437 矩阵* 果=new 矩阵(h,w,(float)0);
438 for(int i=0;i<w;i++){
439 for(int j=0;j<h;j++){
440 果->data[j*w+i]=0;
441 if(data[j*w+i]==矩阵1.data[j*w+i]){
442 果->data[j*w+i]=1;
443
444 }
445 }
446 }
447 return *(果);
448 }
449
451 template <typename T>
453 矩阵* 矩阵1=new 矩阵(h,w,value);
454 return operator==(*(矩阵1));
455 }
456
459 矩阵 原矩阵 =矩阵(*this);
460 N=N;
461 w=原矩阵.h;
462 h=原矩阵.w;
463 //data=new float[N];
464 for(int i=0;i<w;i++){
465 for(int j=0;j<h;j++){
466 data[j*w+i]=原矩阵.(i,j);
467 }
468 }
469 return this;
470 }
472 矩阵* T(void){
473 return 转置();
474 }
476 矩阵* (int i){
477 stringstream ss;
478 ss<<i<<",:";
479 return operator[](ss.str());
480 }
482 矩阵* (int i,int j){
483 stringstream ss;
484 ss<<i<<":"<<j<<",:";
485 return operator[](ss.str());
486 }
487
489 矩阵* (int i){
490 stringstream ss;
491 ss<<":,"<<i;
492 return operator[](ss.str());
493 }
495 矩阵* (int i,int j){
496 stringstream ss;
497 ss<<":,"<<i<<":"<<j;
498 return operator[](ss.str());
499 }
501 矩阵* 行们(int count,...){
502 矩阵* 多行矩阵=NULL;
503 va_list v;
504 va_start(v,count);
505 //cout<<"参数个数为:"<<count<<endl;
506 for(int i=0;i<count;i++){
507 int value=va_arg(v,int);
508 (value);
509 if(!多行矩阵){
510 多行矩阵=(value);
511 }else{
512 多行矩阵=r_(多行矩阵,(value));
513 }
514 }
515 va_end(v);
516 return 多行矩阵;
517 }
519 矩阵* 列们(int count,...){
520 矩阵* 多列矩阵=NULL;
521 va_list v;
522 va_start(v,count);
523 //cout<<"参数个数为:"<<count<<endl;
524 for(int i=0;i<count;i++){
525 int value=va_arg(v,int);
526 (value);
527 if(!多列矩阵){
528 多列矩阵=(value);
529 }else{
530 多列矩阵=c_(多列矩阵,(value));
531 }
532 }
533 va_end(v);
534 return 多列矩阵;
535 }
536
538 矩阵* operator[](string ij){
539 unsigned long dou_i=ij.find(",",0);
540 unsigned long dimention_i=ij.find(":",0);
541 unsigned long dimention_ri=ij.rfind(":",ij.length());
542 // 如果有多个:,就需要返回多行或者多列
543 矩阵* 子矩阵=NULL;
544 矩阵* 维=NULL;
545 if(dimention_i<dimention_ri){
546 // 0:2,: or :,1:2
547 if(dou_i-dimention_i==1){
548 // :,1:2
549 int 始列=str2int(ij.substr(dou_i+1,dimention_ri-dou_i-1));
550 int 终列=str2int(ij.substr(dimention_ri+1,ij.length()-1-dimention_ri));
551 for(int i=始列;i<终列;i++){
552 stringstream ss;
553 ss<<":,"<<i;
554 if(i==始列){
555 子矩阵=operator[](ss.str());
556 }
557 if(i>始列){
558 子矩阵=c_(子矩阵,operator[](ss.str()));
559 }
560 }
561 //两列合并
562 return 子矩阵;
563 }
564 if(dou_i-dimention_i>1){
565 //0:100,:
566 int 始行=str2int(ij.substr(0,dimention_i));
567 int 终行=str2int(ij.substr(dimention_i+1,dou_i-dimention_i-1));
568 for(int i=始行;i<终行;i++){
569 stringstream ss;
570 ss<<i<<",:";
571 if(i==始行){
572 子矩阵=operator[](ss.str());
573 }
574 if(i>始行){
575 子矩阵=r_(子矩阵,operator[](ss.str()));
576 }
577 }
578 //两行合并
579 return 子矩阵;
580 }
581 }
582
583 unsigned long L=ij.length()-1;
584 //cout<<"dou_i,dimention_i,length="<<dou_i<<","<<dimention_i<<","<<L<<endl;
585 // 如果是取某列
586 if(dimention_i<dou_i && dimention_i>=0){// [":,0"]
587 //int j=ij[2]-'0';
588 int j = str2int(ij.substr(dou_i+1,L-dou_i));
589 //cout<<"第"<<j<<"列"<<endl;
590 维= (new 矩阵(h,1,(float)0));
591 for(int i=0;i<h;i++){
592 维->data[i]=data[i*w+j];
593 }
594 return 维;
595 }
596 // 取某行
597 if(dimention_i>dou_i && dimention_i<=L){//["1,:"]
598 维= new 矩阵(1,w,(float)0);
599 //int i=ij[0]-'0';
600 int i = str2int(ij.substr(0,dou_i));
601 for( int j=0;j<w;j++ ){
602 维->data[j]=data[i*w+j];
603 }
604 return 维;
605 }
606 int i= str2int(ij.substr(0,dou_i));
607 int j= str2int(ij.substr(dou_i+1,L-dou_i));
608 //cout<<"i="<<i<<" j="<<j<<endl;
609
610 维= (new 矩阵(1,1));
611 维->data[0]=(i,j);
612 //cout<<(*维);
613 return 维;
614 }
615 //friend ostream& operator<<(ostream &out,矩阵& m);
617 friend ostream& operator<<(ostream &out,矩阵& m){
618 cout<<"[";
619 for(int i=0;i<m.h;i++){
620 cout<<"[";
621 for(int j=0;j<m.w;j++){
622 //cout<<fixed<<setprecision(5)<<m.data[i*m.w+j];
623 cout<<m.data[i*m.w+j];
624 if(j<m.w-1){cout<<",";}
625
626 }
627 cout<<"]";
628 if(i<m.h-1){cout<<","<<endl;}
629 }
630 cout<<"]"<<endl;
631 return out;
632 }
633
635 ~矩阵(void){
636 if(data!=NULL){
637 delete []data;
638
639 }
640 }
641
643 float& (int i, int j){
644 int n=i*w+j;
645 return operator[](n);
646 }
647
648
649
650};
651#endif /* ___hpp */
基本的矩阵计算类,用于完成各种矩阵初始化、最大最小、取值、加减乘除、精度设置等矩阵计算
Definition 矩阵.hpp:25
矩阵(int _h, int _w, 矩阵 value)
复制矩阵
Definition 矩阵.hpp:63
矩阵(int _h, int _w, float *init_data, bool COPYARRAY)
Definition 矩阵.hpp:76
float max
矩阵中元素的最大值
Definition 矩阵.hpp:36
void test_str2int(void)
Definition 矩阵.hpp:287
矩阵 * c_(矩阵 *矩阵1, 矩阵 *矩阵2)
Definition 矩阵.hpp:296
float * data
储存矩阵元素的数组
Definition 矩阵.hpp:40
矩阵 operator/(T value)
Definition 矩阵.hpp:405
friend ostream & operator<<(ostream &out, 矩阵 &m)
Definition 矩阵.hpp:617
矩阵 * arange(int start, int end)
模仿numpy 的arange
Definition 矩阵.hpp:236
矩阵 operator+(T value)
Definition 矩阵.hpp:346
矩阵 * 列(int i, int j)
Definition 矩阵.hpp:495
float & operator[](int i)
Definition 矩阵.hpp:264
矩阵 * T(void)
Definition 矩阵.hpp:472
矩阵 operator-(T value)
Definition 矩阵.hpp:366
矩阵 operator>=(T value)
Definition 矩阵.hpp:428
矩阵 * 行们(int count,...)
Definition 矩阵.hpp:501
矩阵 operator*(矩阵 &矩阵1)
Definition 矩阵.hpp:372
void 取整(void)
对矩阵内的数据取整
Definition 矩阵.hpp:117
int N
矩阵中元素的个数
Definition 矩阵.hpp:30
void 精度(int n=5)
设置矩阵内的数据精度,小数点后保留5位
Definition 矩阵.hpp:124
矩阵 * 列们(int count,...)
Definition 矩阵.hpp:519
矩阵(矩阵 &阵)
复制一个矩阵
Definition 矩阵.hpp:106
矩阵 operator>=(矩阵 &矩阵1)
Definition 矩阵.hpp:411
int w
矩阵的宽度,即列数
Definition 矩阵.hpp:32
矩阵 operator*(T value)
Definition 矩阵.hpp:386
矩阵 * minmax(int dim)
Definition 矩阵.hpp:206
矩阵 * 列(int i)
Definition 矩阵.hpp:489
矩阵 * 转置(void)
Definition 矩阵.hpp:458
矩阵(int n, float value)
初始化一个1行,n列的向量
Definition 矩阵.hpp:94
矩阵 * 和(int dim)
按行求和,或者按列求和,dim=0是按列求和,dim=1是按行求和
Definition 矩阵.hpp:171
float min
矩阵中元素的最小值
Definition 矩阵.hpp:38
~矩阵(void)
Definition 矩阵.hpp:635
矩阵 operator==(T value)
Definition 矩阵.hpp:452
矩阵 operator+(矩阵 &矩阵1)
Definition 矩阵.hpp:332
矩阵 * r_(矩阵 *矩阵1, 矩阵 *矩阵2)
按行合并
Definition 矩阵.hpp:314
矩阵(int _h, int _w, float value)
初始化一个值为value的矩阵
Definition 矩阵.hpp:51
int h
矩阵的高度,即行数
Definition 矩阵.hpp:34
矩阵 operator==(矩阵 &矩阵1)
Definition 矩阵.hpp:434
矩阵 operator-(矩阵 &矩阵1)
Definition 矩阵.hpp:352
矩阵 * minmax(void)
求矩阵中所有元素的最大值,最小值
Definition 矩阵.hpp:196
矩阵 ** meshgrid(矩阵 *行x, 矩阵 *行y, 矩阵 **坐标阵)
模仿numpy 的meshgrid
Definition 矩阵.hpp:247
矩阵 * 和(void)
求矩阵内个元素的和
Definition 矩阵.hpp:162
矩阵 * 点积(矩阵 &矩阵1)
矩阵点积运算,对应位置相乘再相加
Definition 矩阵.hpp:135
矩阵 * 行(int i, int j)
Definition 矩阵.hpp:482
矩阵 * 行(int i)
Definition 矩阵.hpp:476
int str2int(string ij)
字符串转整数
Definition 矩阵.hpp:275
float Inf
表示正无穷
Definition 矩阵.hpp:28
float & 取(int i, int j)
Definition 矩阵.hpp:643
矩阵(矩阵python *结构体)
将python中的矩阵,转换为此矩阵
Definition 矩阵.hpp:43
矩阵 operator/(矩阵 &矩阵1)
Definition 矩阵.hpp:391
矩阵 * 单位化(void)
对矩阵的每一行,进行单位化
Definition 矩阵.hpp:141
矩阵 * operator[](string ij)
Definition 矩阵.hpp:538
int L[]
Definition cube.hpp:2077
此矩阵用于和python中的矩阵类进行数据对接
Definition 矩阵.hpp:18
float * data
Definition 矩阵.hpp:19