提交 61c42da2 编写于 作者: sahduashufa's avatar sahduashufa

1.0

上级 7ab9f35b
......@@ -2,8 +2,74 @@
# Edge : 一个开源的科学计算引擎
## 目前实现的:
## 0.1 矩阵:
使用:
```c
welcome();
Matrix a = CreateMatrix(5,6);
cout<<"end"<<endl;
change_va(a,1,2,1);
change_va(a,2,3,7);
change_va(a,3,3,2);
change_va(a,4,3,4);
change_va(a,0,0,9);
change_va(a,0,2,2);
change_va(a,1,1,8);
start = clock();
cout<<"matrix sum: "<<matrix_sum(a)<<endl;
cout<<"matrix mean: "<<matrix_mean(a)<<endl;
cout<<"matrix T: "<<endl;
Matrix b = get_T(a);
for(int index_x = 0;index_x<b.row;index_x++)
{
for(int index_y=0;index_y<b.col;index_y++)
{
cout<<b.matrix[index_x][index_y]<<" ";
}
cout<<endl;
}
cout<<"-----------split line-----------"<<endl;
cout<<"matrix*n"<<endl;
Matrix c = times_mat(8,b);
for(int index_x = 0;index_x<c.row;index_x++)
{
for(int index_y=0;index_y<c.col;index_y++)
{
cout<<c.matrix[index_x][index_y]<<" ";
}
cout<<endl;
}
cout<<"-----------split line-----------"<<endl;
cout<<"matrix*matrix"<<endl;
Matrix d =mul(c,get_T(c));
for(int index_x = 0;index_x<d.row;index_x++)
{
for(int index_y=0;index_y<d.col;index_y++)
{
cout<<d.matrix[index_x][index_y]<<" ";
}
cout<<endl;
}
cout<<"-----------split line-----------"<<endl;
Matrix rs = matrix_rs(a,6,5);
for(int index_x = 0;index_x<rs.row;index_x++)
{
for(int index_y=0;index_y<rs.col;index_y++)
{
cout<<rs.matrix[index_x][index_y]<<" ";
}
cout<<endl;
}
times_mat(5,a);
stop = clock();
printf("%f\n", (double)(stop - start) / CLOCKS_PER_SEC);
return 0;
```
​ API:
​ Create a matrix : create(row,cols)
......@@ -24,6 +90,40 @@
​ Matrix1's Transposition : Matrix get_T(Matrix mid1)
​ Mul(matrix1,matrix2)
矩阵**A**
| 第1列 | 第2列 | 第3列 | 第4列 | 第5列 |
| ------- | ------- | ------- | ------- | ------- |
| 72.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 64.0000 | 0.0000 | 0.0000 | 0.0000 |
| 16.0000 | 8.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 56.0000 | 16.0000 | 32.0000 |
| 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
矩阵**B**
| 第1列 | 第2列 | 第3列 | 第4列 | 第5列 | 第6列 |
| ------- | ------- | ------- | ------- | ------ | ------ |
| 72.0000 | 0.0000 | 16.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 64.0000 | 8.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 56.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 16.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 32.0000 | 0.0000 | 0.0000 |
To
| 第1列 | 第2列 | 第3列 | 第4列 | 第5列 | 第6列 |
| --------- | --------- | --------- | --------- | ------ | ------ |
| 5184.0000 | 0.0000 | 1152.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 4096.0000 | 512.0000 | 0.0000 | 0.0000 | 0.0000 |
| 1152.0000 | 512.0000 | 320.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 4416.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
​ double* flatten(Matrix mid1)
| 1 | 2 | 3 |
......@@ -39,9 +139,11 @@ To
Return a flattened array.
Matrix matrix_rs(Matrix mid1,int rs_row,int rs_col)
double matrix_sum(Matrix mid1)
double matrix_mean(Matrix mid1)
一些细节:
......@@ -49,22 +151,18 @@ Return a flattened array.
2. 动态分配内存,防止矩阵过大
3. 可修改的地方(已完成)
4. 为了能够节省内存后期删除,采用使用指针临时开辟数组空间
5. delete(matrix);
5. 在main中使用完记得delete(matrix);
6. 利用某个地址赋值方法,更新矩阵的值
7. 已实现实现更新单个或者多个
8. 具体文档不多赘述,详情见源代码.
具体见文档,源代码.
# PS
## 0.2 矩阵相乘:
## 作者邮箱:zk@likedge.top
1. 查看数值顺序,给出不合理的问题
2. 结果储存接口
3. 矩阵均值,行列式
## 欢迎加入这个项目,一起完善自己。
## 0.3 matrix reshape操作
## QQ:2533524298
1. 重新定义矩阵大小,支持flatten和reshape操作
2. 拉伸变化后矩阵数值不变,方式:重定义
## 更新某一个值
1. 利用某个地址赋值方法,更新矩阵的值
2. 实现更新单个或者多个
文件已删除
......@@ -13,7 +13,6 @@ int main()
{
welcome();
Matrix a = CreateMatrix(5,6);
Matrix b = CreateMatrix(3,11);
cout<<"end"<<endl;
change_va(a,1,2,1);
change_va(a,2,3,7);
......@@ -23,22 +22,43 @@ int main()
change_va(a,0,2,2);
change_va(a,1,1,8);
start = clock();
Matrix rs = matrix_rs(a,6,5);
cout<<matrix_mean(a)<<endl;
/* add(a,b,1);
mul(a,b);
for(int index_x = 0;index_x<a.row;index_x++)
cout<<"matrix sum: "<<matrix_sum(a)<<endl;
cout<<"matrix mean: "<<matrix_mean(a)<<endl;
cout<<"matrix T: "<<endl;
Matrix b = get_T(a);
for(int index_x = 0;index_x<b.row;index_x++)
{
for(int index_y=0;index_y<b.col;index_y++)
{
cout<<b.matrix[index_x][index_y]<<" ";
}
cout<<endl;
}
cout<<"-----------split line-----------"<<endl;
cout<<"matrix*n"<<endl;
Matrix c = times_mat(8,b);
for(int index_x = 0;index_x<c.row;index_x++)
{
for(int index_y=0;index_y<a.col;index_y++)
for(int index_y=0;index_y<c.col;index_y++)
{
cout<<a.matrix[index_x][index_y]<<" ";
cout<<c.matrix[index_x][index_y]<<" ";
}
cout<<endl;
}
cout<<"------------"<<endl;
Matrix a_ = get_T(a);
*/
for(int index_x = 0;index_x<rs.row;index_x++)
cout<<"-----------split line-----------"<<endl;
cout<<"matrix*matrix"<<endl;
Matrix d =mul(c,get_T(c));
for(int index_x = 0;index_x<d.row;index_x++)
{
for(int index_y=0;index_y<d.col;index_y++)
{
cout<<d.matrix[index_x][index_y]<<" ";
}
cout<<endl;
}
cout<<"-----------split line-----------"<<endl;
Matrix rs = matrix_rs(a,6,5);
for(int index_x = 0;index_x<rs.row;index_x++)
{
for(int index_y=0;index_y<rs.col;index_y++)
{
......
......@@ -69,21 +69,20 @@ Matrix subtract(Matrix mid1,Matrix mid2)
Matrix mul(Matrix mid1,Matrix mid2)
{
Matrix mid3 = CreateMatrix(mid1.row,mid2.col);
for(int index_x = 0;index_x<mid1.row;index_x++)
Matrix mid3 = CreateMatrix(mid1.row,mid2.col);
for(int i = 0;i<mid1.row;i++)
{
for(int j = 0;j<mid2.col;j++)
{
for(int index_y = 0;index_y<mid2.col;index_y++)
{
mid3.matrix[index_x][index_y]=0;
for(int index_x2 = 0;index_x2<mid2.row;index_x2++)
for(int k = 0;k<mid1.col;k++)
{
mid3.matrix[index_x][index_y]+=mid1.matrix[index_x][index_y]*mid2.matrix[index_x2][index_y];
}
cout<<i<<":"<<j<<endl;
mid3.matrix[i][j]=mid3.matrix[i][j]+mid1.matrix[i][k]*mid2.matrix[k][j];
}
}
}
return mid3;
}
Matrix times_mat(int times,Matrix mid1)
{
for(int index_x=0; index_x<mid1.row; index_x++)
......@@ -119,7 +118,7 @@ double* flatten(Matrix mid1)
int index_x = i/mid1.col;
int index_y = i%mid1.col;
*(p+i) = mid1.matrix[index_x][index_y];
cout<<index_x<<","<<index_y<<*(p+i)<<endl;
// cout<<index_x<<","<<index_y<<*(p+i)<<endl;
// cout << "*(p + " << i << ") : ";
// cout << *(p + i) <<" index_x: "<<index_x<<" index_y: "<<index_y<< endl;
}
......@@ -144,15 +143,15 @@ Matrix matrix_rs(Matrix mid1,int rs_row,int rs_col)
int added = mid1.row*mid1.col;
for(int i = 0;i<size;i++)
{
cout<<"p+"<<i<<*(p+i)<<endl;
cout<<index_x<<":"<<index_y<<endl;
// cout<<"p+"<<i<<*(p+i)<<endl;
// cout<<index_x<<":"<<index_y<<endl;
if(index_y == rs_col)
{
index_y=0;
index_x+=1;
}
result.matrix[index_x][index_y]=*(p+i);
cout<<index_x<<","<<index_y<<":"<<result.matrix[index_x][index_y]<<endl;
// cout<<index_x<<","<<index_y<<":"<<result.matrix[index_x][index_y]<<endl;
index_y++;
}
return result;
......
# 矩阵滴乘法运算
# 注意:需要传入np.matrix类型数据
def Matrix_Mul(a,b):
if a.shape[1] != b.shape[0]:
print('这两个矩阵无法做乘法,请检查左边矩阵的列数是否与右边矩阵的行数相等!')
else:
c = np.zeros(a.shape[0]*b.shape[1]).reshape(a.shape[0],b.shape[1])
for i in range(a.shape[0]):
for j in range(b.shape[1]):
for k in range(a.shape[1]):
print(i,k)
c[i,j] = c[i,j] + a[i,k]*b[k,j]
return c
import numpy as np
a = np.matrix([[2,3,4],[1,0,5]])
b =a.T
print(Matrix_Mul(a=b,b=a))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册