简易实现
导包@常量准备
| import re |
| import numpy as np |
| from functools import reduce |
| ddots = r'\ddots' |
| cdots = r'\cdots' |
| vdots = r'\vdots' |
| pass_strs=[cdots, vdots,ddots,""] |
| type = "v"+"matrix" |
| begin = r"\begin{"+type+"}" |
| end = r"\end{"+type+"}" |
| |
| nrows=6 |
| ncols=6 |
| size = [nrows,ncols] |
| |
| s=nrows*ncols |
| |
| |
| mat = np.arange(s).reshape(size) |
| |
| |
一般数字阵🎈
| \begin{pmatrix} |
| 3& 1& -1& 2 \\ |
| -5& 1& 3& -4 \\ |
| 2& 0& 1& -1 \\ |
| 1& -5& 3& -3 \\ |
| \end{pmatrix} |
| |
填充你的矩阵😊
| |
| |
| |
| |
| |
| |
| |
| mat=np.array( |
| [[3,1,-1,2], |
| [-5,1,3,-4], |
| [2,0,1,-1], |
| [1,-5,3,-3]] |
| ) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| integer = False |
| integer = True |
| |
| |
| |
| |
| |
| def print_num_matrix(mat,integer=True): |
| print(begin) |
| for i in mat: |
| l = [ |
| str(j) if not integer else str(int(float(j)) if j not in pass_strs else j) |
| for j in i |
| ] |
| line = "&\t".join(l) + "\t" + r"\\" |
| print("\t" + line) |
| print(end) |
| |
| print_num_matrix(mat) |
| \begin{vmatrix} |
| 3& 1& -1& 2 \\ |
| -5& 1& 3& -4 \\ |
| 2& 0& 1& -1 \\ |
| 1& -5& 3& -3 \\ |
| \end{vmatrix} |
| |
| array([[ 3, 1, -1, 2], |
| [-5, 1, 3, -4], |
| [ 2, 0, 1, -1], |
| [ 1, -5, 3, -3]]) |
| |
| mat[:,[0,1]]=mat[:,[1,0]] |
| mat[1]-=mat[0] |
| mat[3]+=5*mat[0] |
| mat |
| array([[ 1, 3, -1, 2], |
| [ 0, -8, 4, -6], |
| [ 0, 2, 1, -1], |
| [ 0, 16, -2, 7]]) |
| mat[[1,2]]=mat[[2,1]] |
| print_num_matrix(mat) |
| \begin{vmatrix} |
| 1& 3& -1& 2 \\ |
| 0& 2& 1& -1 \\ |
| 0& -8& 4& -6 \\ |
| 0& 16& -2& 7 \\ |
| \end{vmatrix} |
| |
| mat[2]+=4*mat[1] |
| mat[3]-=8*mat[1] |
| mat |
| print_num_matrix(mat) |
| \begin{vmatrix} |
| 1& 3& -1& 2 \\ |
| 0& 2& 1& -1 \\ |
| 0& 0& 8& -10 \\ |
| 0& 0& -10& 15 \\ |
| \end{vmatrix} |
| mat=mat.astype('float64') |
| mat[2]/=2 |
| mat[3]/=5 |
| print_num_matrix(mat) |
| \begin{vmatrix} |
| 1& 3& -1& 2 \\ |
| 0& 2& 1& -1 \\ |
| 0& 0& 4& -5 \\ |
| 0& 0& -2& 3 \\ |
| \end{vmatrix} |
| mat[3]+=mat[2]/2 |
| print_num_matrix(mat) |
| \begin{vmatrix} |
| 1& 3& -1& 2 \\ |
| 0& 2& 1& -1 \\ |
| 0& 0& 4& -5 \\ |
| 0& 0& 0& 0 \\ |
| \end{vmatrix} |
使用array-to-latex库🎈
| import array_to_latex as a2l |
| a2l.to_ltx(mat) |
| \begin{bmatrix} |
| 1.00 & 3.00 & -1.00 & 2.00\\ |
| 0.00 & 2.00 & 1.00 & -1.00\\ |
| 0.00 & 0.00 & 4.00 & -5.00\\ |
| 0.00 & 0.00 & 0.00 & 0.50 |
| \end{bmatrix} |
其他矩阵
单位阵生成@对角阵🎈
| mat=np.identity(4) |
| |
| mat=mat.astype('int32') |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| mat = mat.astype('U7') |
| mat[2,:]=vdots |
| mat[:,2]=cdots |
| mat[2,2]=ddots |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| print(begin) |
| for i in mat: |
| show_zero=False |
| show_zero=True |
| l = [format(str(j), "^2") if j != 0 or show_zero else " " for j in i] |
| line = "&".join(l)+"\t"*0+r"\\" |
| print("\t"+line) |
| print(end) |
字母阵
| char = r'\alpha' |
| m, n = 5,5 |
| size = [m, n] |
| cell_gap = 8 |
| |
| |
| mat = np.array([["{}{}".format("", j) for j in range(1, n+1)] |
| for i in range(1, m+1)], dtype='U7') |
| |
| |
| |
| |
| |
| dots_line=4-1 |
| mat[:, dots_line] = cdots |
| |
| new_parts=np.where(mat[dots_line] == cdots, "", vdots) |
| mat[3-1]=new_parts |
| |
| mat[-1]='n' |
| mat[:,-1]='n' |
| |
| print(mat, "\n"*3) |
| print(begin) |
| for i in mat: |
| l = [format(char+'_{'+str(j)+'}', "<"+str(cell_gap)) if str(j) |
| not in pass_strs else format(str(j), "<"+str(cell_gap)) for j in i] |
| line = "&".join(l)+"\t"+r"\\" |
| print("\t"+line) |
| print(end) |
向量表示法@向量阵
| char0='A' |
| char = 'B' |
| m, n = 4,4 |
| size = [m, n] |
| cell_gap = 8 |
| |
| mat = np.array([["{}".format(i) for i in range(1, n+1)] for j in range(1,m+1)], dtype='U7') |
| |
| mat[:, 3-1] = cdots |
| |
| |
| |
| |
| |
| |
| |
| |
| mat2=np.array([["{}".format(j) for i in range(1, n+1)] for j in range(1,m+1)], dtype='U7') |
| print(mat,"\n","-"*20 ,"\n"*2) |
| print(begin) |
| r=0 |
| for i in mat: |
| l = [format(char0+'_{'+ mat2[r, c]+'}'+char+'_{'+mat[r, c]+'}', "<"+str(cell_gap)) if str(mat[r,c]) |
| not in [cdots, vdots,ddots] else format(mat[r, c], "<"+str(cell_gap)) for c in range(len(i))] |
| line = "&".join(l)+"\t"+r"\\" |
| r+=1 |
| print("\t"+line) |
| print(end) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2022-05-28 CN_@DNS@HTTP
2022-05-28 http_认证机制&https加密&TLS&SSL&密钥对(公钥&私钥)
2021-05-28 dataStructure_图的遍历算法(广度优先搜索BFS/深度优先搜索 DFS with Stack)&归纳推理和认识规律的方法论
2021-05-28 Android_on开头命名的方法
2021-05-28 android_关于logcat内容不变化现象