Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小灰灰的大灰灰
Python-矩阵计算
提交
18012e5f
P
Python-矩阵计算
项目概览
小灰灰的大灰灰
/
Python-矩阵计算
与 Fork 源项目一致
Fork自
inscode / Python
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Python-矩阵计算
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
18012e5f
编写于
4月 30, 2023
作者:
6
6440e7113916fa54ba548fe6
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Auto commit
上级
df352508
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
256 addition
and
1 deletion
+256
-1
Matrix.py
Matrix.py
+194
-0
__pycache__/Matrix.cpython-38.pyc
__pycache__/Matrix.cpython-38.pyc
+0
-0
main.py
main.py
+62
-1
未找到文件。
Matrix.py
0 → 100644
浏览文件 @
18012e5f
import
copy
class
Matrix
:
def
__init__
(
self
,
matrix
):
# 列表类型检查
assert
isinstance
(
matrix
,
list
),
"参数必须为二维列表!"
for
row
in
matrix
:
assert
isinstance
(
row
,
list
),
"参数必须为二维列表!"
for
row
in
matrix
:
assert
len
(
row
)
==
len
(
matrix
[
0
]),
"参数第二维度不一致!"
for
row
in
matrix
:
for
each
in
row
:
assert
isinstance
(
each
,
(
int
,
float
,
complex
)),
"元素类型只能为int, float或complex!"
self
.
__matrix
=
matrix
self
.
__row
=
len
(
matrix
)
self
.
__col
=
len
(
matrix
[
0
])
# 矩阵间加法 +
def
__add__
(
self
,
other
):
assert
isinstance
(
other
,
Matrix
),
"加数必须是 Matrix 类"
if
self
.
__row
!=
other
.
__row
or
self
.
__col
!=
other
.
__col
:
raise
ValueError
(
"两矩阵维度不一致!"
)
result
=
Matrix
(
[[
0
for
i
in
range
(
self
.
__col
)]
for
i
in
range
(
self
.
__row
)
]
)
for
i
in
range
(
self
.
__row
):
for
j
in
range
(
self
.
__col
):
result
.
__matrix
[
i
][
j
]
=
self
.
__matrix
[
i
][
j
]
+
other
.
__matrix
[
i
][
j
]
return
result
# 矩阵间减法 -
def
__sub__
(
self
,
other
):
assert
isinstance
(
other
,
Matrix
),
"加数必须是 Matrix 类"
if
self
.
__row
!=
other
.
__row
or
self
.
__col
!=
other
.
__col
:
raise
ValueError
(
"两矩阵维度不一致!"
)
result
=
Matrix
([[
0
for
i
in
range
(
self
.
__col
)]
for
i
in
range
(
self
.
__row
)])
for
i
in
range
(
self
.
__row
):
for
j
in
range
(
self
.
__col
):
result
.
__matrix
[
i
][
j
]
=
self
.
__matrix
[
i
][
j
]
-
other
.
__matrix
[
i
][
j
]
return
result
# 矩阵乘矩阵 *
def
__mul__
(
self
,
other
):
assert
isinstance
(
other
,
Matrix
),
"右乘数必须是 Matrix 类"
if
self
.
__col
!=
other
.
__row
:
raise
ValueError
(
"两矩阵维度不一致!"
)
result
=
Matrix
([[
0
for
i
in
range
(
other
.
__col
)]
for
i
in
range
(
self
.
__row
)])
for
i
in
range
(
self
.
__row
):
for
j
in
range
(
other
.
__col
):
for
m
in
range
(
self
.
__col
):
result
.
__matrix
[
i
][
j
]
+=
self
.
__matrix
[
i
][
m
]
*
other
.
__matrix
[
m
][
j
]
return
result
# 数与矩阵乘 *
def
__rmul__
(
self
,
other
):
assert
isinstance
(
other
,
(
int
,
float
,
complex
)),
"左乘系数类型错误"
result
=
Matrix
([[
0
for
i
in
range
(
self
.
__col
)]
for
i
in
range
(
self
.
__row
)])
for
i
in
range
(
self
.
__row
):
for
j
in
range
(
self
.
__col
):
result
.
__matrix
[
i
][
j
]
=
self
.
__matrix
[
i
][
j
]
*
other
return
result
# 矩阵除以数
def
__truediv__
(
self
,
other
):
assert
isinstance
(
other
,
(
int
,
float
,
complex
)),
"被除数系数类型错误"
result
=
Matrix
([[
0
for
i
in
range
(
self
.
__col
)]
for
i
in
range
(
self
.
__row
)])
for
i
in
range
(
self
.
__row
):
for
j
in
range
(
self
.
__col
):
result
.
__matrix
[
i
][
j
]
=
self
.
__matrix
[
i
][
j
]
/
other
return
result
# 数除以矩阵
def
__rtruediv__
(
self
,
other
):
assert
isinstance
(
other
,
(
int
,
float
,
complex
)),
"除数系数类型错误"
result
=
Matrix
([[
0
for
i
in
range
(
self
.
__col
)]
for
i
in
range
(
self
.
__row
)])
for
i
in
range
(
self
.
__row
):
for
j
in
range
(
self
.
__col
):
result
.
__matrix
[
i
][
j
]
=
other
/
self
.
__matrix
[
i
][
j
]
return
result
# 矩阵与矩阵元素是否相等
def
__eq__
(
self
,
other
):
assert
isinstance
(
other
,
Matrix
),
"必须是 Matrix 类"
for
i
in
range
(
self
.
__row
):
for
j
in
range
(
self
.
__col
):
if
self
.
__matrix
[
i
][
j
]
!=
other
.
__matrix
[
i
][
j
]:
return
False
return
True
# 矩阵字符化
def
__str__
(
self
):
return
'
\n
'
.
join
(
list
(
map
(
str
,
self
.
__matrix
)))
# 行迭代器
def
__iter__
(
self
):
return
self
.
__matrix
.
__iter__
()
# 元素获取,看齐numpy
def
__getitem__
(
self
,
item
):
if
isinstance
(
item
,
int
):
return
self
.
__matrix
[
item
]
if
isinstance
(
item
,
tuple
):
row
,
col
=
item
[
0
],
item
[
1
]
if
isinstance
(
row
,
int
)
and
isinstance
(
col
,
int
):
return
self
.
__matrix
[
row
][
col
]
if
isinstance
(
row
,
slice
)
and
isinstance
(
col
,
int
):
return
Matrix
(
[
[
line
[
col
]
]
for
line
in
self
.
__matrix
[
row
]]
)
if
isinstance
(
row
,
int
)
and
isinstance
(
col
,
slice
):
return
Matrix
(
[
self
.
__matrix
[
row
][
col
]]
)
if
isinstance
(
row
,
slice
)
and
isinstance
(
col
,
slice
):
return
Matrix
(
[
line
[
col
]
for
line
in
self
.
__matrix
[
row
]
]
)
# len()
def
__len__
(
self
):
return
(
self
.
__row
)
*
(
self
.
__col
)
# 析构函数
def
__del__
(
self
):
del
self
.
__matrix
# def __copy__(self):
# 元素和
def
sum
(
self
):
value
=
0
for
i
in
range
(
self
.
__row
):
for
j
in
range
(
self
.
__col
):
value
+=
self
.
__matrix
[
i
][
j
]
return
value
# 某行某列的代数余子式
def
cofacor
(
self
,
row
,
col
):
assert
self
.
__row
==
self
.
__col
,
"必须是方阵"
mat_1
=
copy
.
deepcopy
(
self
)
temp_mat
=
[]
# 删除对应行和列
for
row_index
,
line
in
enumerate
(
mat_1
):
if
row_index
==
row
:
continue
line
.
pop
(
col
)
temp_mat
.
append
(
line
)
return
pow
(
-
1
,
row
+
col
+
2
)
*
Matrix
(
temp_mat
).
det
()
# 定义法迭代法行列式
def
det
(
self
):
assert
self
.
__row
==
self
.
__col
,
"必须是方阵"
if
self
.
__row
==
1
and
self
.
__col
==
1
:
return
self
.
__matrix
[
0
][
0
]
if
self
.
__row
==
2
and
self
.
__col
==
2
:
return
self
.
__matrix
[
0
][
0
]
*
self
.
__matrix
[
1
][
1
]
-
self
.
__matrix
[
0
][
1
]
*
self
.
__matrix
[
1
][
0
]
# 取出第一行与之代数余子式相乘
value
=
0
for
col_index
,
m
in
enumerate
(
self
.
__matrix
[
0
]):
value
+=
m
*
copy
.
deepcopy
(
self
).
cofacor
(
0
,
col_index
)
return
value
# 当前矩阵的伴随矩阵
def
adj
(
self
):
assert
self
.
__row
==
self
.
__col
,
"必须是方阵"
result
=
[[
0
for
i
in
range
(
self
.
__col
)]
for
i
in
range
(
self
.
__row
)]
for
i
in
range
(
self
.
__row
):
for
j
in
range
(
self
.
__col
):
result
[
i
][
j
]
=
self
.
cofacor
(
j
,
i
)
return
Matrix
(
result
)
# 矩阵的逆
def
inv
(
self
):
assert
self
.
__row
==
self
.
__col
,
"必须是方阵"
det
=
self
.
det
()
assert
det
!=
0
,
"矩阵必须非奇异"
return
self
.
adj
()
/
det
# 矩阵转置
def
transpose
(
self
):
return
Matrix
(
list
(
map
(
list
,
zip
(
*
self
.
__matrix
))))
# 矩阵的秩
def
rank
(
self
):
def
col_sort
(
mat
,
col_index
=
0
):
assert
isinstance
(
mat
,
Matrix
),
"对象必须为矩阵"
temp_mat
=
Matrix
(
sorted
(
mat
.
__matrix
,
key
=
lambda
x
:
abs
(
x
[
col_index
]),
reverse
=
True
)
)
return
temp_mat
def
cal_col_k
(
mat
,
col_index
=
0
):
assert
isinstance
(
mat
,
Matrix
),
"对象必须为矩阵"
k
=
[[
0
]
for
i
in
range
(
mat
.
__row
)]
if
abs
(
mat
.
__matrix
[
0
][
col_index
])
<
1e-9
:
return
0
,
None
for
i
in
range
(
1
,
mat
.
__row
):
k
[
i
][
0
]
=
mat
.
__matrix
[
i
][
col_index
]
/
mat
.
__matrix
[
0
][
col_index
]
return
1
,
Matrix
(
k
)
def
elimination
(
mat
,
k
):
assert
isinstance
(
mat
,
Matrix
),
"对象必须为矩阵"
temp_mat
=
[[
0
for
col
in
range
(
mat
.
__col
)]
for
j
in
range
(
mat
.
__row
)
]
for
i
in
range
(
mat
.
__row
):
for
j
in
range
(
mat
.
__col
):
temp_mat
[
i
][
j
]
=
mat
.
__matrix
[
i
][
j
]
-
(
mat
.
__matrix
[
0
][
j
]
*
k
[
i
][
0
])
return
Matrix
(
temp_mat
)
mat
=
copy
.
deepcopy
(
self
)
# 列排序
mat
=
col_sort
(
mat
,
col_index
=
0
)
if
mat
.
__col
==
1
or
mat
.
__row
==
1
:
return
int
(
bool
(
abs
(
mat
.
sum
())))
# 列比例
ret
,
K
=
cal_col_k
(
mat
,
col_index
=
0
)
if
ret
==
0
:
return
mat
[
1
:,
1
:].
rank
()
# 消源
mat
=
elimination
(
mat
,
K
)
return
1
+
mat
[
1
:,
1
:].
rank
()
__pycache__/Matrix.cpython-38.pyc
0 → 100644
浏览文件 @
18012e5f
文件已添加
main.py
浏览文件 @
18012e5f
from
Matrix
import
Matrix
print
(
'欢迎来到 InsCode'
)
print
(
'以下为矩阵实现演示...'
)
print
(
'矩阵变量'
.
center
(
50
,
'-'
))
A
=
Matrix
(
[
[
1
,
2
,
2
],
[
2
,
3
,
2
],
[
2
,
2
,
3
]])
M
=
Matrix
([
[
0
],
[
0
],
[
2
]])
B
=
Matrix
(
[
[
1
,
2
,
3
],
[
3
,
2
,
-
1
],
[
2
,
2
,
2
]]
)
print
(
"A:"
)
print
(
A
)
print
(
"M:"
)
print
(
M
)
print
(
"B:"
)
print
(
B
)
# 加法
print
(
"矩阵加法:A+B"
.
center
(
50
,
'-'
))
print
(
A
+
B
)
print
(
"矩阵减法:A-B"
.
center
(
50
,
'-'
))
print
(
A
-
B
)
print
(
"矩阵乘法:A*B"
.
center
(
50
,
'-'
))
print
(
A
*
B
)
print
(
"数乘矩阵:10*B"
.
center
(
50
,
'-'
))
print
(
10
*
B
)
print
(
"矩阵除以数:A/10"
.
center
(
50
,
'-'
))
print
(
A
/
10
)
print
(
"数除以矩阵(元素):10/B"
.
center
(
50
,
'-'
))
print
(
10
/
B
)
print
(
"矩阵元素访问(副本):A[1:, 1:]"
.
center
(
50
,
'-'
))
print
(
A
[
1
:,
1
:])
print
(
"代数余子式:A.cofacor(0, 0)"
.
center
(
50
,
'-'
))
print
(
A
.
cofacor
(
0
,
0
))
print
(
"行列式:A.det()"
.
center
(
50
,
'-'
))
print
(
A
.
det
())
print
(
"伴随矩阵:A.adj()"
.
center
(
50
,
'-'
))
print
(
A
.
adj
())
print
(
"矩阵逆:A.inv()"
.
center
(
50
,
'-'
))
print
(
A
.
inv
())
print
(
"矩阵转置:A.transpose()"
.
center
(
50
,
'-'
))
print
(
A
.
transpose
())
print
(
"矩阵秩:A.rank()"
.
center
(
50
,
'-'
))
print
(
A
.
rank
())
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录