11111
This commit is contained in:
commit
1d12299d73
|
@ -0,0 +1,36 @@
|
||||||
|
# Algebra-with-Python
|
||||||
|
|
||||||
|
#### Description
|
||||||
|
{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**}
|
||||||
|
|
||||||
|
#### Software Architecture
|
||||||
|
Software architecture description
|
||||||
|
|
||||||
|
#### Installation
|
||||||
|
|
||||||
|
1. xxxx
|
||||||
|
2. xxxx
|
||||||
|
3. xxxx
|
||||||
|
|
||||||
|
#### Instructions
|
||||||
|
|
||||||
|
1. xxxx
|
||||||
|
2. xxxx
|
||||||
|
3. xxxx
|
||||||
|
|
||||||
|
#### Contribution
|
||||||
|
|
||||||
|
1. Fork the repository
|
||||||
|
2. Create Feat_xxx branch
|
||||||
|
3. Commit your code
|
||||||
|
4. Create Pull Request
|
||||||
|
|
||||||
|
|
||||||
|
#### Gitee Feature
|
||||||
|
|
||||||
|
1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
|
||||||
|
2. Gitee blog [blog.gitee.com](https://blog.gitee.com)
|
||||||
|
3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
|
||||||
|
4. The most valuable open source project [GVP](https://gitee.com/gvp)
|
||||||
|
5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
|
||||||
|
6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
|
|
@ -0,0 +1,39 @@
|
||||||
|
# Algebra-with-Python
|
||||||
|
|
||||||
|
#### 介绍
|
||||||
|
{**以下是 Gitee 平台说明,您可以替换此简介**
|
||||||
|
Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台
|
||||||
|
无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)}
|
||||||
|
|
||||||
|
#### 软件架构
|
||||||
|
软件架构说明
|
||||||
|
|
||||||
|
|
||||||
|
#### 安装教程
|
||||||
|
|
||||||
|
1. xxxx
|
||||||
|
2. xxxx
|
||||||
|
3. xxxx
|
||||||
|
|
||||||
|
#### 使用说明
|
||||||
|
|
||||||
|
1. xxxx
|
||||||
|
2. xxxx
|
||||||
|
3. xxxx
|
||||||
|
|
||||||
|
#### 参与贡献
|
||||||
|
|
||||||
|
1. Fork 本仓库
|
||||||
|
2. 新建 Feat_xxx 分支
|
||||||
|
3. 提交代码
|
||||||
|
4. 新建 Pull Request
|
||||||
|
|
||||||
|
|
||||||
|
#### 特技
|
||||||
|
|
||||||
|
1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md
|
||||||
|
2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com)
|
||||||
|
3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目
|
||||||
|
4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
|
||||||
|
5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help)
|
||||||
|
6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)
|
|
@ -0,0 +1,351 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "e84fd778",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"7 <class 'int'>\n",
|
||||||
|
"1.3333333333333333 <class 'float'>\n",
|
||||||
|
"1 <class 'int'>\n",
|
||||||
|
"7.0 <class 'float'>\n",
|
||||||
|
"(4+2j) <class 'float'> <class 'float'>\n",
|
||||||
|
"(0.3333333333333333+0.6666666666666666j) <class 'complex'>\n",
|
||||||
|
"False\n",
|
||||||
|
"True\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例1-19\n",
|
||||||
|
"a=4#整型\n",
|
||||||
|
"b=3#整型\n",
|
||||||
|
"c=a+b#整型\n",
|
||||||
|
"print(c,type(c))\n",
|
||||||
|
"c=a/b#浮点型\n",
|
||||||
|
"print(c,type(c))\n",
|
||||||
|
"c=a//b#整型\n",
|
||||||
|
"print(c,type(c))\n",
|
||||||
|
"b=3.0#浮点型\n",
|
||||||
|
"c=a+b#浮点型\n",
|
||||||
|
"print(c,type(c))\n",
|
||||||
|
"a=1+2j#复数型\n",
|
||||||
|
"b=3#整型\n",
|
||||||
|
"c=a+b\n",
|
||||||
|
"print(c,type(c.real),type(c.imag))\n",
|
||||||
|
"c=a/b#实部、虚部为浮点型的复数\n",
|
||||||
|
"print(c,type(c))\n",
|
||||||
|
"a=0.1+0.2\n",
|
||||||
|
"b=0.3\n",
|
||||||
|
"print(a==b)\n",
|
||||||
|
"print(abs(a-b)<1e-10)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "2023a18c",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"4/3\n",
|
||||||
|
"6004799503160661/18014398509481984\n",
|
||||||
|
"1/3\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例1-20\n",
|
||||||
|
"from fractions import Fraction as F\n",
|
||||||
|
"a=F(4,1)\n",
|
||||||
|
"b=F(3)\n",
|
||||||
|
"c=a/b\n",
|
||||||
|
"print(c)\n",
|
||||||
|
"c=F(1/3)\n",
|
||||||
|
"print(c)\n",
|
||||||
|
"print(c.limit_denominator())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "74f09dff",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"0+1/2∙x+0∙x**2+1/3∙x**3\n",
|
||||||
|
"0.0+0.5∙x+0.0∙x**2+0.3333333333333333∙x**3\n",
|
||||||
|
"1-2∙x+1∙x**2\n",
|
||||||
|
"0\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例1-21\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from fractions import Fraction as F\n",
|
||||||
|
"def exp(a):\n",
|
||||||
|
" n=len(a)\n",
|
||||||
|
" s=''\n",
|
||||||
|
" if n==0:#零多项式\n",
|
||||||
|
" s=s+'0'\n",
|
||||||
|
" else:#非零多项式\n",
|
||||||
|
" for i in range(n):\n",
|
||||||
|
" if i==0:#常数项\n",
|
||||||
|
" s=s+'%s'%a[i]\n",
|
||||||
|
" if i==1 and a[i]>=0:#非负1次项\n",
|
||||||
|
" s=s+'+%s∙x'%a[i]\n",
|
||||||
|
" if i==1 and a[i]<0:#负1次项\n",
|
||||||
|
" s=s+'%s∙x'%a[i]\n",
|
||||||
|
" if i>1 and a[i]>=0:#非负项\n",
|
||||||
|
" s=s+'+%s∙x**%d'%(a[i],i)\n",
|
||||||
|
" if i>1 and a[i]<0:#负项\n",
|
||||||
|
" s=s+'%s∙x**%d'%(a[i],i)\n",
|
||||||
|
" return s\n",
|
||||||
|
"a=[F(0),F(1,2),F(0),F(1,3)]\n",
|
||||||
|
"b=[0.0,0.5,0.0,1/3]\n",
|
||||||
|
"c=np.array([1,-2,1])\n",
|
||||||
|
"d=[0]\n",
|
||||||
|
"print(exp(a))\n",
|
||||||
|
"print(exp(b))\n",
|
||||||
|
"print(exp(c))\n",
|
||||||
|
"print(exp(d))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "ec174615",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"a=10001\n",
|
||||||
|
"b=10101\n",
|
||||||
|
"a<<2=1000100, 68\n",
|
||||||
|
"b>>2=101, 5\n",
|
||||||
|
"a|b=10101\n",
|
||||||
|
"a&b=10001\n",
|
||||||
|
"a^b=100\n",
|
||||||
|
"~a=-10010\n",
|
||||||
|
"a逐位取反=1110\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例1-22\n",
|
||||||
|
"a=17\n",
|
||||||
|
"b=21\n",
|
||||||
|
"print('a=%s'%\"{0:b}\".format(a))#a的2进制表达式\n",
|
||||||
|
"print('b=%s'%\"{0:b}\".format(b))#b的2进制表达式\n",
|
||||||
|
"print('a<<2=%s, %d'%(\"{0:b}\".format(a<<2),a<<2))#a左移2位\n",
|
||||||
|
"print('b>>2=%s, %d'%(\"{0:b}\".format(b>>2),b>>2))#b右移2位\n",
|
||||||
|
"print('a|b=%s'%\"{0:b}\".format(a|b))#a与b按位或\n",
|
||||||
|
"print('a&b=%s'%\"{0:b}\".format(a&b))#a与b按位与\n",
|
||||||
|
"print('a^b=%s'%\"{0:b}\".format(a^b))#a与b按位异或\n",
|
||||||
|
"print('~a=%s'%\"{0:b}\".format(~a))#a的带符号位补码按位反\n",
|
||||||
|
"n=a.bit_length()#a的2进制位数\n",
|
||||||
|
"b=2**n-1#n位1\n",
|
||||||
|
"print('a逐位取反=%s'%\"{0:b}\".format(a^b))#a的原码按位反"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "0139ec7f",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"('A', 119, 8523904)\n",
|
||||||
|
"('B', 34834, 4229)\n",
|
||||||
|
"('C', 12976547, 20)\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例1-23\n",
|
||||||
|
"def ipAnaly(a):\n",
|
||||||
|
" b32=2**32-1#32位1\n",
|
||||||
|
" m=255<<24#第1个字节全为1其他为0\n",
|
||||||
|
" t=(a&m)>>24#地址的第1个字节\n",
|
||||||
|
" if 1<=t<=126:#A类地址\n",
|
||||||
|
" t1='A'#地址类型\n",
|
||||||
|
" n=t#网络号\n",
|
||||||
|
" m1=m^b32#掩码255.0.0.0的反码\n",
|
||||||
|
" p=a&m1#主机号\n",
|
||||||
|
" if 128<=t<=191:#B类地址\n",
|
||||||
|
" t1='B'#地址类型\n",
|
||||||
|
" m=(2**16-1)<<16#掩码255.255.0.0\n",
|
||||||
|
" n=(a&m)>>16#网络号\n",
|
||||||
|
" m1=m^b32#掩码的反码\n",
|
||||||
|
" p=a&m1#主机号\n",
|
||||||
|
" if 192<=t<=223:#C类地址\n",
|
||||||
|
" t1='C'#地质类型\n",
|
||||||
|
" m=(2**24-1)<<8#掩码255.255.255.0\n",
|
||||||
|
" n=(a&m)>>8#网络号\n",
|
||||||
|
" m1=m^b32#掩码的反码\n",
|
||||||
|
" p=a&m1#主机号\n",
|
||||||
|
" return t1, n, p\n",
|
||||||
|
"a=2005012608#地址119.130.16.128\n",
|
||||||
|
"print(ipAnaly(a))\n",
|
||||||
|
"a=2282885253#地址136.18.16.133\n",
|
||||||
|
"print(ipAnaly(a))\n",
|
||||||
|
"a=3321996052#地址198.1.163.20\n",
|
||||||
|
"print(ipAnaly(a))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "5c3f729a",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"1-2∙x+1∙x**2\n",
|
||||||
|
"-1/2∙x+1/3∙x**3\n",
|
||||||
|
"-0.5∙x+0.3333333333333333∙x**3\n",
|
||||||
|
"False\n",
|
||||||
|
"True\n",
|
||||||
|
"1-5/2∙x+1∙x**2+1/3∙x**3\n",
|
||||||
|
"-0.75∙x+0.5∙x**3\n",
|
||||||
|
"0\n",
|
||||||
|
"9+12∙x+10∙x**2+4∙x**3+1∙x**4\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例1-24、1-25、练习1-7\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from fractions import Fraction as F\n",
|
||||||
|
"def exp(a):\n",
|
||||||
|
" n=len(a)\n",
|
||||||
|
" s=''\n",
|
||||||
|
" start=0\n",
|
||||||
|
" while start<n and a[start]==0:#查找第一个非零项下标start\n",
|
||||||
|
" start+=1\n",
|
||||||
|
" if start>=n:#零多项式\n",
|
||||||
|
" s+='0'\n",
|
||||||
|
" else:#非零多项式\n",
|
||||||
|
" if start==0:#非零常数项\n",
|
||||||
|
" s+='%s'%a[start]\n",
|
||||||
|
" if start==1:#非零1次项\n",
|
||||||
|
" s+='%s∙x'%a[start]\n",
|
||||||
|
" if start>1:#首项次数大于1\n",
|
||||||
|
" s+='%s∙x^%d'%(a[start],start)\n",
|
||||||
|
" for i in range(start+1,n):#首项后各项\n",
|
||||||
|
" if i==1:#1次项\n",
|
||||||
|
" if a[i]>0:#正1次项\n",
|
||||||
|
" s=s+'+%s∙x'%a[i]\n",
|
||||||
|
" if a[i]<0:#负1次项\n",
|
||||||
|
" s=s+'%s∙x'%a[i]\n",
|
||||||
|
" else:#非1次项\n",
|
||||||
|
" if a[i]>0:#正项\n",
|
||||||
|
" s=s+'+%s∙x**%d'%(a[i],i)\n",
|
||||||
|
" if a[i]<0:#负项\n",
|
||||||
|
" s=s+'%s∙x**%d'%(a[i],i)\n",
|
||||||
|
" return s\n",
|
||||||
|
"class myPoly:#多项式类\n",
|
||||||
|
" def __init__(self, coef):#初始化\n",
|
||||||
|
" c=np.trim_zeros(coef,trim='b')#删除高次零系数\n",
|
||||||
|
" self.coef=np.array(c)#设置多项式系数\n",
|
||||||
|
" self.degree=(self.coef).size-1#设置多项式次数\n",
|
||||||
|
" def __eq__(self, other):#相等\n",
|
||||||
|
" if self.degree!=other.degree:#次数不等\n",
|
||||||
|
" return False\n",
|
||||||
|
" return (abs(self.coef-other.coef)<1e-10).all()\n",
|
||||||
|
" #return (self.coef==other.coef).all()\n",
|
||||||
|
" def __str__(self):#输出表达式\n",
|
||||||
|
" return exp(self.coef) \n",
|
||||||
|
" def __add__(self, other):#运算符“+”\n",
|
||||||
|
" n=max(self.degree,other.degree)+1#系数个数\n",
|
||||||
|
" a=np.append(self.coef,[0]*(n-self.coef.size))#补长\n",
|
||||||
|
" b=np.append(other.coef,[0]*(n-other.coef.size))#补长\n",
|
||||||
|
" return myPoly(a+b)#创建并返回多项式和\n",
|
||||||
|
" def __rmul__(self, k):#右乘数k\n",
|
||||||
|
" c=self.coef*k#各系数与k的积\n",
|
||||||
|
" return myPoly(c)\n",
|
||||||
|
" def __neg__(self):#负多项式\n",
|
||||||
|
" return (-1)*self\n",
|
||||||
|
" def __sub__(self, other):#运算符“-”\n",
|
||||||
|
" return self+(-other)\n",
|
||||||
|
" def __mul__(self,other):#运算符“*”\n",
|
||||||
|
" m=self.degree\n",
|
||||||
|
" n=other.degree\n",
|
||||||
|
" a=np.append(self.coef,[0]*n)\n",
|
||||||
|
" b=np.append(other.coef,[0]*m)\n",
|
||||||
|
" c=[]\n",
|
||||||
|
" for s in range(1, m+n+2):\n",
|
||||||
|
" cs=(a[:s]*np.flip(b[:s])).sum()\n",
|
||||||
|
" c.append(cs)\n",
|
||||||
|
" return myPoly(c)\n",
|
||||||
|
" def val(self,x):#多项式在x处的值\n",
|
||||||
|
" n=self.degree#次数\n",
|
||||||
|
" power=np.array([x**k for k in range(n+1)])#x各次幂\n",
|
||||||
|
" v=((self.coef)*power).sum()#多项式的值\n",
|
||||||
|
"p=myPoly(np.array([1,-2,1]))\n",
|
||||||
|
"q=myPoly([F(0),F(-1,2),F(0),F(1,3)])\n",
|
||||||
|
"r=myPoly([0.0,-0.5,0.0,1/3])\n",
|
||||||
|
"k=1.5\n",
|
||||||
|
"print(p)\n",
|
||||||
|
"print(q)\n",
|
||||||
|
"print(r)\n",
|
||||||
|
"print(p==q)\n",
|
||||||
|
"print(q==r)\n",
|
||||||
|
"print(p+q)\n",
|
||||||
|
"print(k*q)\n",
|
||||||
|
"print(q-r)\n",
|
||||||
|
"f=myPoly([3, 2, 1])\n",
|
||||||
|
"g=myPoly([3, 2, 1])\n",
|
||||||
|
"print(f*g)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "807054db",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.9.7"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
|
@ -0,0 +1,766 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "4833007b",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[0 0 1 1 1 1 0 0]\n",
|
||||||
|
" [0 1 0 0 0 0 1 0]\n",
|
||||||
|
" [1 0 0 0 0 0 0 1]\n",
|
||||||
|
" [1 0 0 0 0 0 0 1]\n",
|
||||||
|
" [1 0 0 0 0 0 0 1]\n",
|
||||||
|
" [1 0 0 0 0 0 0 1]\n",
|
||||||
|
" [0 1 0 0 0 0 1 0]\n",
|
||||||
|
" [0 0 1 1 1 1 0 0]]\n",
|
||||||
|
"[[0 0 0 0 0 0 0 1]\n",
|
||||||
|
" [0 0 0 0 0 0 1 0]\n",
|
||||||
|
" [0 0 0 0 0 1 0 0]\n",
|
||||||
|
" [0 0 0 0 1 0 0 0]\n",
|
||||||
|
" [0 0 0 1 0 0 0 0]\n",
|
||||||
|
" [0 0 1 0 0 0 0 0]\n",
|
||||||
|
" [0 1 0 0 0 0 0 0]\n",
|
||||||
|
" [1 0 0 0 0 0 0 0]]\n",
|
||||||
|
"[[1. 0. 0. 0. 0.]\n",
|
||||||
|
" [0. 1. 0. 0. 0.]\n",
|
||||||
|
" [0. 0. 1. 0. 0.]\n",
|
||||||
|
" [0. 0. 0. 1. 0.]\n",
|
||||||
|
" [0. 0. 0. 0. 1.]]\n",
|
||||||
|
"[[0. 0. 0. 0. 0. 0.]\n",
|
||||||
|
" [0. 0. 0. 0. 0. 0.]\n",
|
||||||
|
" [0. 0. 0. 0. 0. 0.]\n",
|
||||||
|
" [0. 0. 0. 0. 0. 0.]\n",
|
||||||
|
" [0. 0. 0. 0. 0. 0.]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例2-3、练习2-1\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"A=np.array([[0,0,1,1,1,1,0,0],\n",
|
||||||
|
" [0,1,0,0,0,0,1,0],\n",
|
||||||
|
" [1,0,0,0,0,0,0,1],\n",
|
||||||
|
" [1,0,0,0,0,0,0,1],\n",
|
||||||
|
" [1,0,0,0,0,0,0,1],\n",
|
||||||
|
" [1,0,0,0,0,0,0,1],\n",
|
||||||
|
" [0,1,0,0,0,0,1,0],\n",
|
||||||
|
" [0,0,1,1,1,1,0,0]])\n",
|
||||||
|
"B=np.array([[0,0,0,0,0,0,0,1],\n",
|
||||||
|
" [0,0,0,0,0,0,1,0],\n",
|
||||||
|
" [0,0,0,0,0,1,0,0],\n",
|
||||||
|
" [0,0,0,0,1,0,0,0],\n",
|
||||||
|
" [0,0,0,1,0,0,0,0],\n",
|
||||||
|
" [0,0,1,0,0,0,0,0],\n",
|
||||||
|
" [0,1,0,0,0,0,0,0],\n",
|
||||||
|
" [1,0,0,0,0,0,0,0]])\n",
|
||||||
|
"I=np.eye(5)\n",
|
||||||
|
"O=np.zeros((5,6))\n",
|
||||||
|
"print(A)\n",
|
||||||
|
"print(B)\n",
|
||||||
|
"print(I)\n",
|
||||||
|
"print(O)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"id": "222e1ba5",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[1 0 0 0 0]\n",
|
||||||
|
" [0 0 0 1 0]\n",
|
||||||
|
" [0 0 1 0 0]\n",
|
||||||
|
" [0 1 0 0 0]\n",
|
||||||
|
" [0 0 0 0 1]]\n",
|
||||||
|
"[[1 0 0 0 0]\n",
|
||||||
|
" [0 1 0 0 0]\n",
|
||||||
|
" [0 0 3/2 0 0]\n",
|
||||||
|
" [0 0 0 1 0]\n",
|
||||||
|
" [0 0 0 0 1]]\n",
|
||||||
|
"[[1 0 0 0 0]\n",
|
||||||
|
" [0 1 0 0 0]\n",
|
||||||
|
" [0 0 1 0 0]\n",
|
||||||
|
" [0 3/2 0 1 0]\n",
|
||||||
|
" [0 0 0 0 1]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例2-4、练习2-4\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import P1,P2,P3\n",
|
||||||
|
"from fractions import Fraction as F\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(F(x).limit_denominator())})\n",
|
||||||
|
"E1=np.eye(5)\n",
|
||||||
|
"P1(E1,1,3)\n",
|
||||||
|
"print(E1)\n",
|
||||||
|
"E2=np.eye(5)\n",
|
||||||
|
"P2(E2,2,3/2)\n",
|
||||||
|
"print(E2)\n",
|
||||||
|
"E3=np.eye(5)\n",
|
||||||
|
"P3(E3,1,3,3/2)\n",
|
||||||
|
"print(E3)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "cd534fb6",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[-1 2 1 0]\n",
|
||||||
|
" [ 1 1 0 1]\n",
|
||||||
|
" [ 1 0 0 0]\n",
|
||||||
|
" [ 0 1 0 0]]\n",
|
||||||
|
"[1 0 0 0]\n",
|
||||||
|
"[0 1 0 0]\n",
|
||||||
|
"[-1 2 1 0]\n",
|
||||||
|
"[1 1 0 1]\n",
|
||||||
|
"[[ 1]\n",
|
||||||
|
" [ 0]\n",
|
||||||
|
" [-1]\n",
|
||||||
|
" [ 1]]\n",
|
||||||
|
"[[0]\n",
|
||||||
|
" [1]\n",
|
||||||
|
" [2]\n",
|
||||||
|
" [1]]\n",
|
||||||
|
"[[0]\n",
|
||||||
|
" [0]\n",
|
||||||
|
" [1]\n",
|
||||||
|
" [0]]\n",
|
||||||
|
"[[0]\n",
|
||||||
|
" [0]\n",
|
||||||
|
" [0]\n",
|
||||||
|
" [1]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例2-5、练习2-5\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"A=np.array([[1,0,0,0],\n",
|
||||||
|
" [0,1,0,0],\n",
|
||||||
|
" [-1,2,1,0],\n",
|
||||||
|
" [1,1,0,1]])\n",
|
||||||
|
"I=A[:2,:2]\n",
|
||||||
|
"O=A[:2,2:]\n",
|
||||||
|
"A1=A[2:,:2]\n",
|
||||||
|
"B=np.vstack((np.hstack((A1,I)),\n",
|
||||||
|
" np.hstack((I,O))))\n",
|
||||||
|
"print(B)\n",
|
||||||
|
"a1=A[0];a2=A[1];a3=A[2];a4=A[3]\n",
|
||||||
|
"print(a1)\n",
|
||||||
|
"print(a2)\n",
|
||||||
|
"print(a3)\n",
|
||||||
|
"print(a4)\n",
|
||||||
|
"b1=A[:,0];b2=A[:,1];b3=A[:,2];b4=A[:,3]\n",
|
||||||
|
"print(b1.reshape(4,1))\n",
|
||||||
|
"print(b2.reshape(4,1))\n",
|
||||||
|
"print(b3.reshape(4,1))\n",
|
||||||
|
"print(b4.reshape(4,1))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "3f972b6a",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[7 7 7]\n",
|
||||||
|
" [7 7 7]]\n",
|
||||||
|
"[[-5 -3 -1]\n",
|
||||||
|
" [ 1 3 5]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例2-10\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"A=np.array([[1, 2, 3],\n",
|
||||||
|
" [4, 5, 6]])\n",
|
||||||
|
"B=np.array([[6, 5, 4],\n",
|
||||||
|
" [3, 2, 1]])\n",
|
||||||
|
"print(A+B)\n",
|
||||||
|
"print(A-B)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 22,
|
||||||
|
"id": "6b73e9b8",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[ 1. -6. -6.]\n",
|
||||||
|
" [-2. -5. -6.]\n",
|
||||||
|
" [-2. -6. -5.]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习2-8\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"A=np.array([[1,3,3],\n",
|
||||||
|
" [1,4,3],\n",
|
||||||
|
" [1,3,4]])\n",
|
||||||
|
"I=np.eye(3)\n",
|
||||||
|
"print(3*I-2*A)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 19,
|
||||||
|
"id": "fa7855d1",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[0 0 0 0 0 0 0 1]\n",
|
||||||
|
" [0 0 0 0 0 0 1 0]\n",
|
||||||
|
" [0 0 0 0 0 1 0 0]\n",
|
||||||
|
" [0 0 0 0 1 0 0 0]\n",
|
||||||
|
" [0 0 0 1 0 0 0 0]\n",
|
||||||
|
" [0 0 1 0 0 0 0 0]\n",
|
||||||
|
" [0 1 0 0 0 0 0 0]\n",
|
||||||
|
" [1 0 0 0 0 0 0 0]]\n",
|
||||||
|
"[[0 0 0 0 0 0 0 0]\n",
|
||||||
|
" [0 0 0 0 0 0 0 0]\n",
|
||||||
|
" [0 0 0 0 0 0 0 0]\n",
|
||||||
|
" [0 0 0 0 0 0 0 0]\n",
|
||||||
|
" [0 0 0 0 0 0 0 0]\n",
|
||||||
|
" [0 0 0 0 0 0 0 0]\n",
|
||||||
|
" [0 0 0 0 0 0 0 0]\n",
|
||||||
|
" [0 0 0 0 0 0 0 0]]\n",
|
||||||
|
"[[0 0 0 0 0 0 0 0]\n",
|
||||||
|
" [0 0 0 0 0 0 0 0]\n",
|
||||||
|
" [0 0 0 0 0 0 0 0]\n",
|
||||||
|
" [0 0 0 0 0 0 0 0]\n",
|
||||||
|
" [0 0 0 0 0 0 0 0]\n",
|
||||||
|
" [0 0 0 0 0 0 0 0]\n",
|
||||||
|
" [0 0 0 0 0 0 0 0]\n",
|
||||||
|
" [0 0 0 0 0 0 0 0]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例2-11、练习2-9\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"A=np.array([[0,0,0,0,0,0,0,1],\n",
|
||||||
|
" [0,0,0,0,0,0,1,0],\n",
|
||||||
|
" [0,0,0,0,0,1,0,0],\n",
|
||||||
|
" [0,0,0,0,1,0,0,0],\n",
|
||||||
|
" [0,0,0,1,0,0,0,0],\n",
|
||||||
|
" [0,0,1,0,0,0,0,0],\n",
|
||||||
|
" [0,1,0,0,0,0,0,0],\n",
|
||||||
|
" [1,0,0,0,0,0,0,0]])\n",
|
||||||
|
"print(A)\n",
|
||||||
|
"print(A^A)\n",
|
||||||
|
"print(0&A)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "765d1b50",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[ 9 11]\n",
|
||||||
|
" [-2 15]\n",
|
||||||
|
" [-1 19]]\n",
|
||||||
|
"[[ 6 -7]\n",
|
||||||
|
" [20 -5]]\n",
|
||||||
|
"[[ 5 -2 13 12]\n",
|
||||||
|
" [ -1 1 -3 -4]\n",
|
||||||
|
" [ -1 4 -5 -12]\n",
|
||||||
|
" [ 8 4 16 0]]\n",
|
||||||
|
"[[-16 16]\n",
|
||||||
|
" [ 8 -8]]\n",
|
||||||
|
"[[ 0 0]\n",
|
||||||
|
" [ 12 -24]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例2-17\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"A=np.array([[4,-1,2,1],\n",
|
||||||
|
" [1,1,0,3],\n",
|
||||||
|
" [0,3,1,4]])\n",
|
||||||
|
"B=np.array([[1,2],\n",
|
||||||
|
" [0,1],\n",
|
||||||
|
" [3,0],\n",
|
||||||
|
" [-1,4]])\n",
|
||||||
|
"print(np.matmul(A,B))\n",
|
||||||
|
"A=np.array([[2,1,4,0],\n",
|
||||||
|
" [1,-1,3,4]])\n",
|
||||||
|
"B=np.array([[1,3],\n",
|
||||||
|
" [0,-1],\n",
|
||||||
|
" [1,-3],\n",
|
||||||
|
" [4,0]])\n",
|
||||||
|
"print(np.matmul(A,B))\n",
|
||||||
|
"print(np.matmul(B,A))\n",
|
||||||
|
"A=np.array([[-2,4],\n",
|
||||||
|
" [1,-2]])\n",
|
||||||
|
"B=np.array([[2,4],\n",
|
||||||
|
" [-3,6]])\n",
|
||||||
|
"print(np.matmul(A,B))\n",
|
||||||
|
"print(np.matmul(B,A))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"id": "e5c18c59",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[ 6 -7 8]\n",
|
||||||
|
" [20 -5 -6]]\n",
|
||||||
|
"[[ -2 13 22]\n",
|
||||||
|
" [ -2 -17 20]\n",
|
||||||
|
" [ 4 29 -2]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习2-14\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"A=np.array([[2,1,4,0],\n",
|
||||||
|
" [1,-1,3,4]])\n",
|
||||||
|
"B=np.array([[1,3,1],\n",
|
||||||
|
" [0,-1,2],\n",
|
||||||
|
" [1,-3,1],\n",
|
||||||
|
" [4,0,-2]])\n",
|
||||||
|
"print(np.matmul(A,B))\n",
|
||||||
|
"A=np.array([[1,1,1],\n",
|
||||||
|
" [1,1,-1],\n",
|
||||||
|
" [1,-1,1]])\n",
|
||||||
|
"B=np.array([[1,2,3],\n",
|
||||||
|
" [-1,-2,4],\n",
|
||||||
|
" [0,5,1]])\n",
|
||||||
|
"print(3*np.matmul(A,B)-2*A)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "ec3a81ec",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[5 6 7 8]\n",
|
||||||
|
" [1 2 3 4]\n",
|
||||||
|
" [9 10 11 12]]\n",
|
||||||
|
"[[2 1 3 4]\n",
|
||||||
|
" [6 5 7 8]\n",
|
||||||
|
" [10 9 11 12]]\n",
|
||||||
|
"[[1 2 3 4]\n",
|
||||||
|
" [5/2 3 7/2 4]\n",
|
||||||
|
" [9 10 11 12]]\n",
|
||||||
|
"[[1 1 3 4]\n",
|
||||||
|
" [5 3 7 8]\n",
|
||||||
|
" [9 5 11 12]]\n",
|
||||||
|
"[[7/2 5 13/2 8]\n",
|
||||||
|
" [5 6 7 8]\n",
|
||||||
|
" [9 10 11 12]]\n",
|
||||||
|
"[[1 5/2 3 4]\n",
|
||||||
|
" [5 17/2 7 8]\n",
|
||||||
|
" [9 29/2 11 12]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例2-18、练习2-15\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from fractions import Fraction as F\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(F(x).limit_denominator())})\n",
|
||||||
|
"from utility import P1,P2,P3\n",
|
||||||
|
"A=np.array([[1,2,3,4],\n",
|
||||||
|
" [5,6,7,8],\n",
|
||||||
|
" [9,10,11,12]])\n",
|
||||||
|
"E1=np.eye(3)\n",
|
||||||
|
"P1(E1,0,1)\n",
|
||||||
|
"print(np.matmul(E1,A))\n",
|
||||||
|
"E1=np.eye(4)\n",
|
||||||
|
"P1(E1,0,1)\n",
|
||||||
|
"print(np.matmul(A,E1))\n",
|
||||||
|
"E2=np.eye(3)\n",
|
||||||
|
"P2(E2,1,1/2)\n",
|
||||||
|
"print(np.matmul(E2,A))\n",
|
||||||
|
"E2=np.eye(4)\n",
|
||||||
|
"P2(E2,1,1/2)\n",
|
||||||
|
"print(np.matmul(A,E2))\n",
|
||||||
|
"E3=np.eye(3)\n",
|
||||||
|
"P3(E3,1,0,1/2)\n",
|
||||||
|
"print(np.matmul(E3,A))\n",
|
||||||
|
"E3=np.eye(4)\n",
|
||||||
|
"P3(E3,1,0,1/2)\n",
|
||||||
|
"print(np.matmul(A,E3))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "ebb5675a",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[5 1 3]\n",
|
||||||
|
" [8 0 3]\n",
|
||||||
|
" [-2 1 -2]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例2-19\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from fractions import Fraction as F\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(F(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[2,1,1],\n",
|
||||||
|
" [3,1,2],\n",
|
||||||
|
" [1,-1,0]])\n",
|
||||||
|
"I=np.eye(3)\n",
|
||||||
|
"f=-I-A+np.matmul(A,A)\n",
|
||||||
|
"print(f)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "7d879fe2",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[0 0]\n",
|
||||||
|
" [0 0]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习2-16\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from fractions import Fraction as F\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(F(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[2,-1],\n",
|
||||||
|
" [-3,3]])\n",
|
||||||
|
"I=np.eye(2)\n",
|
||||||
|
"f=3*I-5*A+np.matmul(A,A)\n",
|
||||||
|
"print(f)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 21,
|
||||||
|
"id": "97a99b5b",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[ 0 17]\n",
|
||||||
|
" [14 13]\n",
|
||||||
|
" [-3 10]]\n",
|
||||||
|
"[[ 0 17]\n",
|
||||||
|
" [14 13]\n",
|
||||||
|
" [-3 10]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例2-22\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"A=np.array([[2,0,-1],\n",
|
||||||
|
" [1,3,2]])\n",
|
||||||
|
"B=np.array([[1,7,-1],\n",
|
||||||
|
" [4,2,3],\n",
|
||||||
|
" [2,0,1]])\n",
|
||||||
|
"print((np.matmul(A,B)).T)\n",
|
||||||
|
"print((np.matmul(B.T, A.T)))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 19,
|
||||||
|
"id": "51fce556",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[ 0 5 8]\n",
|
||||||
|
" [ 0 -5 6]\n",
|
||||||
|
" [ 3 11 -4]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习2-18\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"A=np.array([[1,1,1],\n",
|
||||||
|
" [1,1,-2],\n",
|
||||||
|
" [1,-1,1]])\n",
|
||||||
|
"B=np.array([[1,2,3],\n",
|
||||||
|
" [-1,-2,4],\n",
|
||||||
|
" [0,5,1]])\n",
|
||||||
|
"C=np.matmul(A.T,B)\n",
|
||||||
|
"print(C)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 19,
|
||||||
|
"id": "eaa441c1",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"40.0\n",
|
||||||
|
"16.0\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例2-28、练习2-23\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"A=np.array([[3,1,-1,2],\n",
|
||||||
|
" [-5,1,3,-4],\n",
|
||||||
|
" [2,0,1,-1],\n",
|
||||||
|
" [1,-5,3,-3]])\n",
|
||||||
|
"print('%.1f'%np.linalg.det(A))\n",
|
||||||
|
"A=np.array([[1,2,3,4],\n",
|
||||||
|
" [1,3,4,1],\n",
|
||||||
|
" [1,4,1,2],\n",
|
||||||
|
" [1,1,2,3]])\n",
|
||||||
|
"print('%.1f'%np.linalg.det(A))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 15,
|
||||||
|
"id": "1e1cfde7",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[2 6 -4]\n",
|
||||||
|
" [-3 -6 5]\n",
|
||||||
|
" [2 2 -2]]\n",
|
||||||
|
"[[2 0 0]\n",
|
||||||
|
" [0 2 0]\n",
|
||||||
|
" [0 0 2]]\n",
|
||||||
|
"[[2 0 0]\n",
|
||||||
|
" [0 2 0]\n",
|
||||||
|
" [0 0 2]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例2-36、练习2-32\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import adjointMatrix\n",
|
||||||
|
"from fractions import Fraction as F\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(F(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[1,2,3],\n",
|
||||||
|
" [2,2,1],\n",
|
||||||
|
" [3,4,3]])\n",
|
||||||
|
"Am=adjointMatrix(A)\n",
|
||||||
|
"print(Am)\n",
|
||||||
|
"print(np.matmul(A,Am))\n",
|
||||||
|
"print(np.matmul(Am,A))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 13,
|
||||||
|
"id": "52d637a0",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"2.0\n",
|
||||||
|
"[[1 3 -2]\n",
|
||||||
|
" [-3/2 -3 5/2]\n",
|
||||||
|
" [1 1 -1]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例2-37\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from fractions import Fraction as F\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(F(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[1,2,3],\n",
|
||||||
|
" [2,2,1],\n",
|
||||||
|
" [3,4,3]])\n",
|
||||||
|
"print('%.1f'%np.linalg.det(A))\n",
|
||||||
|
"print(np.linalg.inv(A))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "fddc7525",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[-2 2 1]\n",
|
||||||
|
" [-8/3 5 -2/3]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习2-33\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from fractions import Fraction as F\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(F(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[2,1,-1],\n",
|
||||||
|
" [2,1,0],\n",
|
||||||
|
" [1,-1,1]])\n",
|
||||||
|
"B=np.array([[1,-1,3],\n",
|
||||||
|
" [4,3,2]])\n",
|
||||||
|
"A1=np.linalg.inv(A)\n",
|
||||||
|
"print(np.matmul(B,A1))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"id": "968e5d6d",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[4 5 6]\n",
|
||||||
|
" [1 2 3]\n",
|
||||||
|
" [7 8 9]]\n",
|
||||||
|
"[[2 1 3]\n",
|
||||||
|
" [5 4 6]\n",
|
||||||
|
" [8 7 9]]\n",
|
||||||
|
"[[1 2 3]\n",
|
||||||
|
" [2 5/2 3]\n",
|
||||||
|
" [7 8 9]]\n",
|
||||||
|
"[[1 1 3]\n",
|
||||||
|
" [4 5/2 6]\n",
|
||||||
|
" [7 4 9]]\n",
|
||||||
|
"[[3 9/2 6]\n",
|
||||||
|
" [4 5 6]\n",
|
||||||
|
" [7 8 9]]\n",
|
||||||
|
"[[1 5/2 3]\n",
|
||||||
|
" [4 7 6]\n",
|
||||||
|
" [7 23/2 9]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#初等矩阵与初等变换\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import P1,P2,P3\n",
|
||||||
|
"from fractions import Fraction as F\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(F(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[1,2,3],\n",
|
||||||
|
" [4,5,6],\n",
|
||||||
|
" [7,8,9]])\n",
|
||||||
|
"E1=np.eye(3)\n",
|
||||||
|
"P1(E1,0,1)\n",
|
||||||
|
"print(np.matmul(E1,A))\n",
|
||||||
|
"print(np.matmul(A,E1))\n",
|
||||||
|
"E2=np.eye(3)\n",
|
||||||
|
"P2(E2,1,1/2)\n",
|
||||||
|
"print(np.matmul(E2,A))\n",
|
||||||
|
"print(np.matmul(A,E2))\n",
|
||||||
|
"E3=np.eye(3)\n",
|
||||||
|
"P3(E3,1,0,1/2)\n",
|
||||||
|
"print(np.matmul(E3,A))\n",
|
||||||
|
"print(np.matmul(A,E3))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "6051aad3",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.9.7"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
|
@ -0,0 +1,322 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "1030c7c5",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[210100/12907]\n",
|
||||||
|
" [188400/12907]\n",
|
||||||
|
" [183300/12907]\n",
|
||||||
|
" [223400/12907]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例3-3\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from fractions import Fraction as F\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(F(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[9,-2,0,-1],\n",
|
||||||
|
" [-2,12,-3,0],\n",
|
||||||
|
" [0,-3,15,-4],\n",
|
||||||
|
" [-1,0,-4,10]])\n",
|
||||||
|
"b=np.array([100,100,100,100])\n",
|
||||||
|
"X=np.linalg.solve(A, b)\n",
|
||||||
|
"print(X.reshape(4,1))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "bda4b172",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[1]\n",
|
||||||
|
" [0]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习3-2\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from fractions import Fraction as F\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(F(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[1,2],\n",
|
||||||
|
" [2,5]])\n",
|
||||||
|
"b=np.array([1,2])\n",
|
||||||
|
"X=np.linalg.solve(A, b)\n",
|
||||||
|
"print(X.reshape(2,1))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"id": "4f8fd56a",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[1 0 0 5]\n",
|
||||||
|
" [0 1 0 0]\n",
|
||||||
|
" [0 0 1 3]]\n",
|
||||||
|
"3\n",
|
||||||
|
"[0 1 2]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例3-7、例3-8\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import rowLadder,simplestLadder\n",
|
||||||
|
"A=np.array([[1,-1,-1],\n",
|
||||||
|
" [2,-1,-3],\n",
|
||||||
|
" [3,2,-5]],dtype='float')\n",
|
||||||
|
"b=np.array([2,1,0])\n",
|
||||||
|
"B=np.hstack((A,b.reshape(3,1)))\n",
|
||||||
|
"rank, order=rowLadder(B,3,3)\n",
|
||||||
|
"simplestLadder(B,rank)\n",
|
||||||
|
"print(B)\n",
|
||||||
|
"print(rank)\n",
|
||||||
|
"print(order)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 55,
|
||||||
|
"id": "95fe7f86",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[1 0 0 -1 4]\n",
|
||||||
|
" [0 1 0 -1 3]\n",
|
||||||
|
" [0 0 1 0 -3]\n",
|
||||||
|
" [0 0 0 0 0]]\n",
|
||||||
|
"3\n",
|
||||||
|
"[0 1 3 2]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习3-6、练习3-7\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import rowLadder,simplestLadder\n",
|
||||||
|
"from fractions import Fraction as F\n",
|
||||||
|
"A=np.array([[2,-1,-1,1],\n",
|
||||||
|
" [1,1,-2,1],\n",
|
||||||
|
" [4,-6,2,-2],\n",
|
||||||
|
" [3,6,-9,7]],dtype='float')\n",
|
||||||
|
"b=np.array([2,4,4,9])\n",
|
||||||
|
"B=np.hstack((A,b.reshape(4,1)))\n",
|
||||||
|
"rank, order=rowLadder(B,4,4)\n",
|
||||||
|
"simplestLadder(B,rank)\n",
|
||||||
|
"print(B)\n",
|
||||||
|
"print(rank)\n",
|
||||||
|
"print(order)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "94bd3eb3",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[0 3/17 -13/17]\n",
|
||||||
|
" [0 19/17 -20/17]\n",
|
||||||
|
" [0 1 0]\n",
|
||||||
|
" [0 0 1]]\n",
|
||||||
|
"[[0 2 5/3]\n",
|
||||||
|
" [0 -2 -4/3]\n",
|
||||||
|
" [0 1 0]\n",
|
||||||
|
" [0 0 1]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例3-13、练习3-12\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import mySolve\n",
|
||||||
|
"from fractions import Fraction as Q\n",
|
||||||
|
"np.set_printoptions(formatter=\n",
|
||||||
|
" {'all':lambda x:str(Q(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[3,4,-5,7],\n",
|
||||||
|
" [2,-3,3,-2],\n",
|
||||||
|
" [4,11,-13,16],\n",
|
||||||
|
" [7,-2,1,3]],dtype='float')\n",
|
||||||
|
"b=np.array([0,0,0,0])\n",
|
||||||
|
"s=mySolve(A,b)\n",
|
||||||
|
"print(s)\n",
|
||||||
|
"A=np.array([[1,2,2,1],\n",
|
||||||
|
" [2,1,-2,-2],\n",
|
||||||
|
" [1,-1,-4,-3]],dtype='float')\n",
|
||||||
|
"b=np.array([0,0,0])\n",
|
||||||
|
"s=mySolve(A,b)\n",
|
||||||
|
"print(s)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "0cb4577c",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[5]\n",
|
||||||
|
" [0]\n",
|
||||||
|
" [3]]\n",
|
||||||
|
"[[1]\n",
|
||||||
|
" [0]\n",
|
||||||
|
" [0]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例3-14、练习3-13\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import mySolve,Q\n",
|
||||||
|
"np.set_printoptions(formatter=\n",
|
||||||
|
" {'all':lambda x: str(Q(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[1,-1,-1], #系数矩阵\n",
|
||||||
|
" [2,-1,-3],\n",
|
||||||
|
" [3,2,-5]],dtype='float')\n",
|
||||||
|
"b=np.array([2,1,0]) #常数矩阵\n",
|
||||||
|
"s=mySolve(A,b)\n",
|
||||||
|
"print(s)\n",
|
||||||
|
"A=np.array([[1,2,3], #系数矩阵\n",
|
||||||
|
" [2,1,5],\n",
|
||||||
|
" [3,5,1]],dtype='float')\n",
|
||||||
|
"b=np.array([1,2,3]) #常数矩阵\n",
|
||||||
|
"s=mySolve(A,b)\n",
|
||||||
|
"print(s)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "da97b208",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[]\n",
|
||||||
|
"[]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例3-15、练习3-14\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import Q\n",
|
||||||
|
"from utility import mySolve\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x: str(Q(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[4,2,-1], #系数矩阵\n",
|
||||||
|
" [3,-1,2],\n",
|
||||||
|
" [11,3,0]],dtype='float')\n",
|
||||||
|
"b=np.array([2,10,8]) #常数矩阵\n",
|
||||||
|
"s=mySolve(A,b)\n",
|
||||||
|
"print(s)\n",
|
||||||
|
"A=np.array([[1,-2,3,-1], #系数矩阵\n",
|
||||||
|
" [3,-1,5,-3],\n",
|
||||||
|
" [2,1,2,-2]],dtype='float')\n",
|
||||||
|
"b=np.array([1,2,3]) #常数矩阵\n",
|
||||||
|
"s=mySolve(A,b)\n",
|
||||||
|
"print(s)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "c2245d03",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[1/2 1/2 -1/2]\n",
|
||||||
|
" [0 0 1]\n",
|
||||||
|
" [0 1 0]\n",
|
||||||
|
" [0 0 0]]\n",
|
||||||
|
"[[6/7 1/7 1/7]\n",
|
||||||
|
" [-5/7 5/7 -9/7]\n",
|
||||||
|
" [0 1 0]\n",
|
||||||
|
" [0 0 1]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例3-16、练习3-15\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import Q\n",
|
||||||
|
"from utility import mySolve\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x: str(Q(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[2,1,-1,1],\n",
|
||||||
|
" [4,2,-2,1],\n",
|
||||||
|
" [2,1,-1,-1]],dtype='float')\n",
|
||||||
|
"b=np.array([1,2,1])\n",
|
||||||
|
"s=mySolve(A,b)\n",
|
||||||
|
"print(s)\n",
|
||||||
|
"A=np.array([[2,1,-1,1],\n",
|
||||||
|
" [3,-2,1,-3],\n",
|
||||||
|
" [1,4,-3,5]],dtype='float')\n",
|
||||||
|
"b=np.array([1,4,-2])\n",
|
||||||
|
"s=mySolve(A,b)\n",
|
||||||
|
"print(s)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "bb11d5b1",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.9.7"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
|
@ -0,0 +1,677 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "cc22ed7c",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[1/4 1/4 1/4]\n",
|
||||||
|
" [1/2 1/2 1/2]\n",
|
||||||
|
" [1/4 -3/4 5/4]]\n",
|
||||||
|
"[]\n",
|
||||||
|
"[[1/2 -1/2 3/2]\n",
|
||||||
|
" [1/2 1/2 1/2]]\n",
|
||||||
|
"[[1 1]\n",
|
||||||
|
" [-1 1]\n",
|
||||||
|
" [0 0]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例4-11、练习4-5\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import matrixSolve,Q\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(Q(x).limit_denominator())})\n",
|
||||||
|
"a1=np.array([0,1,2,3],dtype='float')\n",
|
||||||
|
"a2=np.array([3,0,1,2],dtype='float')\n",
|
||||||
|
"a3=np.array([2,3,0,1],dtype='float')\n",
|
||||||
|
"b1=np.array([2,1,1,2],dtype='float')\n",
|
||||||
|
"b2=np.array([0,-2,1,1],dtype='float')\n",
|
||||||
|
"b3=np.array([4,4,1,3],dtype='float')\n",
|
||||||
|
"A=np.hstack((a1.reshape(4,1),\n",
|
||||||
|
" a2.reshape(4,1),a3.reshape(4,1)))\n",
|
||||||
|
"B=np.hstack((b1.reshape(4,1),\n",
|
||||||
|
" b2.reshape(4,1),b3.reshape(4,1)))\n",
|
||||||
|
"X=matrixSolve(A,B)\n",
|
||||||
|
"print(X)\n",
|
||||||
|
"X=matrixSolve(B,A)\n",
|
||||||
|
"print(X)\n",
|
||||||
|
"a1=np.array([1,-1,1,-1],dtype='float')\n",
|
||||||
|
"a2=np.array([3,1,1,3],dtype='float')\n",
|
||||||
|
"b1=np.array([2,0,1,1],dtype='float')\n",
|
||||||
|
"b2=np.array([1,1,0,2],dtype='float')\n",
|
||||||
|
"b3=np.array([3,-1,2,0],dtype='float')\n",
|
||||||
|
"A=np.hstack((a1.reshape(4,1),a2.reshape(4,1)))\n",
|
||||||
|
"B=np.hstack((b1.reshape(4,1),\n",
|
||||||
|
" b2.reshape(4,1),b3.reshape(4,1)))\n",
|
||||||
|
"X=matrixSolve(A,B)\n",
|
||||||
|
"print(X)\n",
|
||||||
|
"X=matrixSolve(B,A)\n",
|
||||||
|
"print(X)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "05c39ad2",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[2]\n",
|
||||||
|
" [-1]\n",
|
||||||
|
" [0]]\n",
|
||||||
|
"[]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例4-12、练习4-6\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import matrixSolve,Q\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(Q(x).limit_denominator())})\n",
|
||||||
|
"a1=np.array([1,1,2,2],dtype='float')\n",
|
||||||
|
"a2=np.array([1,2,1,3],dtype='float')\n",
|
||||||
|
"a3=np.array([1,-1,4,0],dtype='float')\n",
|
||||||
|
"b=np.array([1,0,3,1],dtype='float')\n",
|
||||||
|
"A=np.hstack((a1.reshape(4,1),a2.reshape(4,1)\n",
|
||||||
|
" ,a3.reshape(4,1)))\n",
|
||||||
|
"X=matrixSolve(A,b.reshape(4,1))\n",
|
||||||
|
"print(X)\n",
|
||||||
|
"a1=np.array([1,3,2],dtype='float')\n",
|
||||||
|
"a2=np.array([-2,-1,1],dtype='float')\n",
|
||||||
|
"a3=np.array([3,5,2],dtype='float')\n",
|
||||||
|
"a4=np.array([-1,-3,-2],dtype='float')\n",
|
||||||
|
"b=np.array([1,2,3],dtype='float')\n",
|
||||||
|
"A=np.hstack((a1.reshape(3,1),a2.reshape(3,1),a3.reshape(3,1),a4.reshape(3,1)))\n",
|
||||||
|
"X=matrixSolve(A,b.reshape(3,1))\n",
|
||||||
|
"print(X)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "6af88fb5",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"a1,a2,a3线性相关。\n",
|
||||||
|
"(-1.0)a1+(-1.0)a2+(1.0)a3=o\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例4-20\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import mySolve\n",
|
||||||
|
"a1=np.array([-1,3,1]).reshape(3,1)\n",
|
||||||
|
"a2=np.array([2,1,0]).reshape(3,1)\n",
|
||||||
|
"a3=np.array([1,4,1]).reshape(3,1)\n",
|
||||||
|
"o=np.zeros((3,1))\n",
|
||||||
|
"A=np.hstack((a1,a2,a3))\n",
|
||||||
|
"X=mySolve(A,o)\n",
|
||||||
|
"_,t=X.shape\n",
|
||||||
|
"if t>1:\n",
|
||||||
|
" print('a1,a2,a3线性相关。')\n",
|
||||||
|
" print('(%s)a1+(%s)a2+(%s)a3=o'\n",
|
||||||
|
" %(X[0,1],X[1,1],X[2,1]))\n",
|
||||||
|
"else:\n",
|
||||||
|
" print('a1,a2,a3线性无关。')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "04362b31",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"a1,a2,a3线性无关。\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习4-10\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import mySolve\n",
|
||||||
|
"a1=np.array([2,3,0]).reshape(3,1)\n",
|
||||||
|
"a2=np.array([-1,4,0]).reshape(3,1)\n",
|
||||||
|
"a3=np.array([0,0,2]).reshape(3,1)\n",
|
||||||
|
"o=np.zeros((3,1))\n",
|
||||||
|
"A=np.hstack((a1,a2,a3))\n",
|
||||||
|
"X=mySolve(A,o)\n",
|
||||||
|
"_,t=X.shape\n",
|
||||||
|
"if t>1:\n",
|
||||||
|
" print('a1,a2,a3线性相关。')\n",
|
||||||
|
" print('(%s)a1+(%s)a2+(%s)a3=o'\n",
|
||||||
|
" %(X[0,1],X[1,1],X[2,1]))\n",
|
||||||
|
"else:\n",
|
||||||
|
" print('a1,a2,a3线性无关。')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "e030ac98",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"向量组B线性无关\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例4-21\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import rowLadder\n",
|
||||||
|
"A=np.array([[1,2,3],\n",
|
||||||
|
" [2,2,4],\n",
|
||||||
|
" [3,1,3]],dtype='float')\n",
|
||||||
|
"n,m=A.shape\n",
|
||||||
|
"r,_=rowLadder(A,n,m)\n",
|
||||||
|
"if r<n:\n",
|
||||||
|
" print('向量组B线性相关')\n",
|
||||||
|
"else:\n",
|
||||||
|
" print('向量组B线性无关')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "4f67bbf7",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"向量组B线性相关\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习4-11\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import rowLadder\n",
|
||||||
|
"A=np.array([[1,-1,0],\n",
|
||||||
|
" [0,2,1],\n",
|
||||||
|
" [1,1,1]],dtype='float')\n",
|
||||||
|
"n,m=A.shape\n",
|
||||||
|
"r,_=rowLadder(A,n,m)\n",
|
||||||
|
"if r<n:\n",
|
||||||
|
" print('向量组B线性相关')\n",
|
||||||
|
"else:\n",
|
||||||
|
" print('向量组B线性无关')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "dff79142",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"最大无关组:a1 a2 a4 \n",
|
||||||
|
"a3=(-1)a1+(-1)a2+(-0)a4\n",
|
||||||
|
"a5=(4)a1+(3)a2+(-3)a4\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例4-22\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import maxIndepGrp\n",
|
||||||
|
"a1=np.array([2,1,4,3],dtype='float').reshape(4,1)\n",
|
||||||
|
"a2=np.array([-1,1,-6,6],dtype='float').reshape(4,1)\n",
|
||||||
|
"a3=np.array([-1,-2,2,-9],dtype='float').reshape(4,1)\n",
|
||||||
|
"a4=np.array([1,1,-2,7],dtype='float').reshape(4,1)\n",
|
||||||
|
"a5=np.array([2,4,4,9],dtype='float').reshape(4,1)\n",
|
||||||
|
"A=np.hstack((a1,a2,a3,a4,a5))\n",
|
||||||
|
"_,n=A.shape\n",
|
||||||
|
"r,ind,expr=maxIndepGrp(A)\n",
|
||||||
|
"print('最大无关组:',end='')\n",
|
||||||
|
"for i in range(r):\n",
|
||||||
|
" print('a%d'%(ind[i]+1),end=' ')\n",
|
||||||
|
"print()\n",
|
||||||
|
"for i in range(n-r):\n",
|
||||||
|
" print('a%d=(%.0f)a%d'%(ind[r+i]+1,expr[0,i],ind[0]+1),end='')\n",
|
||||||
|
" for j in range(1,r):\n",
|
||||||
|
" print('+(%.0f)a%d'%(expr[j,i],ind[j]+1),end='')\n",
|
||||||
|
" print()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"id": "9ec7b320",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"最大无关组:a1 a2 a3 \n",
|
||||||
|
"a4=(1)a1+(3)a2+(-1)a3\n",
|
||||||
|
"a5=(0)a1+(-1)a2+(1)a3\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习4-12\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import maxIndepGrp\n",
|
||||||
|
"a1=np.array([1,0,2,1],dtype='float').reshape(4,1)\n",
|
||||||
|
"a2=np.array([1,2,0,1],dtype='float').reshape(4,1)\n",
|
||||||
|
"a3=np.array([2,1,3,0],dtype='float').reshape(4,1)\n",
|
||||||
|
"a4=np.array([2,5,-1,4],dtype='float').reshape(4,1)\n",
|
||||||
|
"a5=np.array([1,-1,3,-1],dtype='float').reshape(4,1)\n",
|
||||||
|
"A=np.hstack((a1,a2,a3,a4,a5))\n",
|
||||||
|
"_,n=A.shape\n",
|
||||||
|
"r,ind,expr=maxIndepGrp(A)\n",
|
||||||
|
"print('最大无关组:',end='')\n",
|
||||||
|
"for i in range(r):\n",
|
||||||
|
" print('a%d'%(ind[i]+1),end=' ')\n",
|
||||||
|
"print()\n",
|
||||||
|
"for i in range(n-r):\n",
|
||||||
|
" print('a%d=(%.0f)a%d'%(ind[r+i]+1,expr[0,i],ind[0]+1),end='')\n",
|
||||||
|
" for j in range(1,r):\n",
|
||||||
|
" print('+(%.0f)a%d'%(expr[j,i],ind[j]+1),end='')\n",
|
||||||
|
" print()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 50,
|
||||||
|
"id": "042dc75a",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[2/3]\n",
|
||||||
|
" [-2/3]\n",
|
||||||
|
" [-1]]\n",
|
||||||
|
"[[4/3]\n",
|
||||||
|
" [1]\n",
|
||||||
|
" [2/3]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例4-30、练习4-17\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import Q\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(Q(x).limit_denominator())})\n",
|
||||||
|
"P=np.array([[2,2,-1],\n",
|
||||||
|
" [2,-1,2],\n",
|
||||||
|
" [-1,2,2]],dtype='float')\n",
|
||||||
|
"b=np.array([1,0,-4],dtype='float').reshape(3,1)\n",
|
||||||
|
"P1=np.linalg.inv(P)\n",
|
||||||
|
"s=np.matmul(P1,b)\n",
|
||||||
|
"print(s)\n",
|
||||||
|
"b=np.array([4,3,2],dtype='float').reshape(3,1)\n",
|
||||||
|
"s=np.matmul(P1,b)\n",
|
||||||
|
"print(s)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 52,
|
||||||
|
"id": "88f4527f",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[-8]\n",
|
||||||
|
" [-1]\n",
|
||||||
|
" [5]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例4-31\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import Q\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(Q(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[1,1,1],\n",
|
||||||
|
" [1,0,0],\n",
|
||||||
|
" [1,-1,1]],dtype='float')\n",
|
||||||
|
"B=np.array([[1,2,3],\n",
|
||||||
|
" [2,3,4],\n",
|
||||||
|
" [1,4,3]],dtype='float')\n",
|
||||||
|
"A1=np.linalg.inv(A)\n",
|
||||||
|
"P=np.matmul(A1,B)\n",
|
||||||
|
"P1=np.linalg.inv(P)\n",
|
||||||
|
"x=np.array([1,1,3]).reshape(3,1)\n",
|
||||||
|
"s=np.matmul(P1,x)\n",
|
||||||
|
"print(s)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 53,
|
||||||
|
"id": "6e73212a",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[-12 -26 -11]\n",
|
||||||
|
" [6 11 3]\n",
|
||||||
|
" [1 3 2]]\n",
|
||||||
|
"[[13 19 43]\n",
|
||||||
|
" [-9 -13 -30]\n",
|
||||||
|
" [7 10 24]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习4-18\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import Q\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(Q(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[1,2,3],\n",
|
||||||
|
" [2,3,7],\n",
|
||||||
|
" [1,3,-2]],dtype='float')\n",
|
||||||
|
"B=np.array([[3,5,1],\n",
|
||||||
|
" [1,2,1],\n",
|
||||||
|
" [4,1,-6]],dtype='float')\n",
|
||||||
|
"A1=np.linalg.inv(A)\n",
|
||||||
|
"P=np.matmul(A1,B)\n",
|
||||||
|
"P1=np.linalg.inv(P)\n",
|
||||||
|
"print(P)\n",
|
||||||
|
"print(P1)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"id": "5e2f7bb4",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[ 1. -1. 1. -1.]\n",
|
||||||
|
" [ 0. 1. -2. 3.]\n",
|
||||||
|
" [ 0. 0. 1. -3.]\n",
|
||||||
|
" [ 0. 0. 0. 1.]]\n",
|
||||||
|
"[[1. 1. 1. 1.]\n",
|
||||||
|
" [0. 1. 2. 3.]\n",
|
||||||
|
" [0. 0. 1. 3.]\n",
|
||||||
|
" [0. 0. 0. 1.]]\n",
|
||||||
|
"[[10.]\n",
|
||||||
|
" [18.]\n",
|
||||||
|
" [17.]\n",
|
||||||
|
" [ 6.]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例4-32、练习4-19\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from scipy.special import comb\n",
|
||||||
|
"def polyTransMat(n,a):\n",
|
||||||
|
" P=np.zeros((n,n))\n",
|
||||||
|
" for k in range(n):\n",
|
||||||
|
" for i in range(k+1):\n",
|
||||||
|
" P[i,k]=((-a)**(k-i))*comb(k,i)\n",
|
||||||
|
" return P\n",
|
||||||
|
"n=4\n",
|
||||||
|
"a=1\n",
|
||||||
|
"P=polyTransMat(n,a)\n",
|
||||||
|
"P1=np.linalg.inv(P)\n",
|
||||||
|
"print(P)\n",
|
||||||
|
"print(P1)\n",
|
||||||
|
"f=np.array([3,2,-1,6]).reshape(4,1)\n",
|
||||||
|
"print(np.matmul(P1,f))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 20,
|
||||||
|
"id": "14878344",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"-1.0+14.0∙x-63.0∙x**2\n",
|
||||||
|
"5.0-24.0∙x+3.0∙x**2-28.0∙x**3\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import myPoly\n",
|
||||||
|
"def polyDifMat(n):\n",
|
||||||
|
" A=np.diag(np.arange(1,n))\n",
|
||||||
|
" A=np.vstack((np.hstack((np.zeros((n-1,1)),A)),\n",
|
||||||
|
" np.zeros(n)))\n",
|
||||||
|
" return A\n",
|
||||||
|
"n=4\n",
|
||||||
|
"A=polyDifMat(n)\n",
|
||||||
|
"f=np.array([5,-1,7,-21]).reshape(n,1)\n",
|
||||||
|
"f1=(np.matmul(A, f).reshape(n,))\n",
|
||||||
|
"print(myPoly(f1))\n",
|
||||||
|
"n=5\n",
|
||||||
|
"A=polyDifMat(n)\n",
|
||||||
|
"f=np.array([3,5,-12,1,-7]).flatten()\n",
|
||||||
|
"f1=(np.matmul(A, f)).reshape(n,)\n",
|
||||||
|
"print(myPoly(f1))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "88e7c817",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"值域基底:\n",
|
||||||
|
"[[1 0]\n",
|
||||||
|
" [-1 2]\n",
|
||||||
|
" [1 2]\n",
|
||||||
|
" [2 -2]]\n",
|
||||||
|
"核基底:\n",
|
||||||
|
"[[-2 -1]\n",
|
||||||
|
" [-3/2 -2]\n",
|
||||||
|
" [1 0]\n",
|
||||||
|
" [0 1]]\n",
|
||||||
|
"值域基底:\n",
|
||||||
|
"[[25 31 17]\n",
|
||||||
|
" [75 94 53]\n",
|
||||||
|
" [75 94 54]\n",
|
||||||
|
" [25 32 20]]\n",
|
||||||
|
"核基底:\n",
|
||||||
|
"[[-8/5]\n",
|
||||||
|
" [1]\n",
|
||||||
|
" [-2]\n",
|
||||||
|
" [1]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例4-45、练习4-30\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import maxIndepGrp, mySolve, Q\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(Q(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[1,0,2,1],\n",
|
||||||
|
" [-1,2,1,3],\n",
|
||||||
|
" [1,2,5,5],\n",
|
||||||
|
" [2,-2,1,-2]],dtype='float')\n",
|
||||||
|
"r,ind,_=maxIndepGrp(A)\n",
|
||||||
|
"print('值域基底:')\n",
|
||||||
|
"print(A[:,ind[:r]])\n",
|
||||||
|
"o=np.array([0,0,0,0]).reshape(4,1)\n",
|
||||||
|
"X=mySolve(A,o)\n",
|
||||||
|
"print('核基底:')\n",
|
||||||
|
"print(X[:,1:])\n",
|
||||||
|
"A=np.array([[25,31,17,43],\n",
|
||||||
|
" [75,94,53,132],\n",
|
||||||
|
" [75,94,54,134],\n",
|
||||||
|
" [25,32,20,48]],dtype='float')\n",
|
||||||
|
"r,ind,_=maxIndepGrp(A)\n",
|
||||||
|
"print('值域基底:')\n",
|
||||||
|
"print(A[:,ind[:r]])\n",
|
||||||
|
"o=np.array([0,0,0,0]).reshape(4,1)\n",
|
||||||
|
"X=mySolve(A,o)\n",
|
||||||
|
"print('核基底:')\n",
|
||||||
|
"print(X[:,1:])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "7ab22546",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"对角阵:\n",
|
||||||
|
"[[-1 0 0]\n",
|
||||||
|
" [0 -1 0]\n",
|
||||||
|
" [0 0 5]]\n",
|
||||||
|
"基:\n",
|
||||||
|
"[[-1 -1 1]\n",
|
||||||
|
" [1 0 1]\n",
|
||||||
|
" [0 1 1]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例4-46\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import mySolve,Q\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(Q(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[1,2,3],\n",
|
||||||
|
" [2,1,3],\n",
|
||||||
|
" [3,3,6]],dtype='float')\n",
|
||||||
|
"A=np.array([[1,2,2],\n",
|
||||||
|
" [2,1,2],\n",
|
||||||
|
" [2,2,1]],dtype='float')\n",
|
||||||
|
"n,_=A.shape\n",
|
||||||
|
"w=np.linalg.eigvals(A)\n",
|
||||||
|
"Lambda=np.diag(np.sort(w))\n",
|
||||||
|
"v=np.unique(np.round(w,decimals=10))\n",
|
||||||
|
"I=np.eye(n)\n",
|
||||||
|
"o=np.zeros((n,1))\n",
|
||||||
|
"lam=v[0]\n",
|
||||||
|
"P=(mySolve(lam*I-A,o))[:,1:]\n",
|
||||||
|
"for lam in v[1:]:\n",
|
||||||
|
" X=mySolve(lam*I-A,o)\n",
|
||||||
|
" P=np.hstack((P,X[:,1:n]))\n",
|
||||||
|
"print('对角阵:')\n",
|
||||||
|
"print(Lambda)\n",
|
||||||
|
"print('基:')\n",
|
||||||
|
"print(P)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"id": "e3daf629",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"对角阵:\n",
|
||||||
|
"[[-1 0 0]\n",
|
||||||
|
" [0 0 0]\n",
|
||||||
|
" [0 0 9]]\n",
|
||||||
|
"基:\n",
|
||||||
|
"[[-1 -1 1/2]\n",
|
||||||
|
" [1 -1 1/2]\n",
|
||||||
|
" [0 1 1]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习4-31\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import mySolve,Q\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(Q(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[1,2,3],\n",
|
||||||
|
" [2,1,3],\n",
|
||||||
|
" [3,3,6]],dtype='float')\n",
|
||||||
|
"n,_=A.shape\n",
|
||||||
|
"w=np.linalg.eigvals(A)\n",
|
||||||
|
"Lambda=np.diag(np.sort(w))\n",
|
||||||
|
"v=np.unique(np.round(w,decimals=10))\n",
|
||||||
|
"I=np.eye(n)\n",
|
||||||
|
"o=np.zeros((n,1))\n",
|
||||||
|
"lam=v[0]\n",
|
||||||
|
"P=(mySolve(lam*I-A,o))[:,1:]\n",
|
||||||
|
"for lam in v[1:]:\n",
|
||||||
|
" X=mySolve(lam*I-A,o)\n",
|
||||||
|
" P=np.hstack((P,X[:,1:n]))\n",
|
||||||
|
"print('对角阵:')\n",
|
||||||
|
"print(Lambda)\n",
|
||||||
|
"print('基:')\n",
|
||||||
|
"print(P)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.9.7"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
|
@ -0,0 +1,495 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "1aa104a6",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"1.5708\n",
|
||||||
|
"0.7854\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例5-9、练习5-7\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"a=np.array([2,1,3,2])\n",
|
||||||
|
"b=np.array([1,2,-2,1])\n",
|
||||||
|
"cos=np.dot(a,b)/(np.linalg.norm(a)*np.linalg.norm(b))\n",
|
||||||
|
"print('%.4f'%np.arccos(cos))\n",
|
||||||
|
"a=np.array([1,2,2,3])\n",
|
||||||
|
"b=np.array([3,1,5,1])\n",
|
||||||
|
"cos=np.dot(a,b)/(np.linalg.norm(a)*np.linalg.norm(b))\n",
|
||||||
|
"print('%.4f'%np.arccos(cos))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "3a05644d",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[-1. 0. 1.]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例5-10\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import mySolve\n",
|
||||||
|
"a1=np.array([1,1,1])\n",
|
||||||
|
"a2=np.array([1,-2,1])\n",
|
||||||
|
"A=np.vstack((a1,a2))\n",
|
||||||
|
"o=np.zeros((2,1))\n",
|
||||||
|
"X=mySolve(A,o)\n",
|
||||||
|
"print(X[:,1])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "8fd90b35",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[1 1 1] [-1. 1. 0.] [-0.5 -0.5 1. ]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习5-8\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import mySolve\n",
|
||||||
|
"a1=np.array([1,1,1])\n",
|
||||||
|
"A=np.array([a1])\n",
|
||||||
|
"o=np.zeros((1,1))\n",
|
||||||
|
"X=mySolve(A,o)\n",
|
||||||
|
"a2=X[:,1]\n",
|
||||||
|
"A=np.vstack((A,a2))\n",
|
||||||
|
"o=np.zeros((2,1))\n",
|
||||||
|
"X=mySolve(A,o)\n",
|
||||||
|
"a3=X[:,1]\n",
|
||||||
|
"print(a1,a2,a3)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "1b7b48b3",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[ 1. 0.3333 -0.2 ]\n",
|
||||||
|
" [ 0. -1. 0.6 ]\n",
|
||||||
|
" [-1. 0.6667 0.6 ]\n",
|
||||||
|
" [ 1. 0.3333 0.8 ]]\n",
|
||||||
|
"[[ 0.5774 0.2582 -0.169 ]\n",
|
||||||
|
" [ 0. -0.7746 0.5071]\n",
|
||||||
|
" [-0.5774 0.5164 0.5071]\n",
|
||||||
|
" [ 0.5774 0.2582 0.6761]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例5-11\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import orthogonalize,unitization\n",
|
||||||
|
"np.set_printoptions(precision=4, suppress=True)\n",
|
||||||
|
"A=np.array([[1,1,-1],\n",
|
||||||
|
" [0,-1,1],\n",
|
||||||
|
" [-1,0,1],\n",
|
||||||
|
" [1,1,0]],dtype='float')\n",
|
||||||
|
"B=orthogonalize(A)\n",
|
||||||
|
"print(B)\n",
|
||||||
|
"unitization(B)\n",
|
||||||
|
"print(B)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"id": "845f7ecc",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[ 0.5774 -0.7071 0.4082]\n",
|
||||||
|
" [ 0.5774 0. -0.8165]\n",
|
||||||
|
" [ 0.5774 0.7071 0.4082]]\n",
|
||||||
|
"[[ 1. 0. -0.]\n",
|
||||||
|
" [ 0. 1. 0.]\n",
|
||||||
|
" [-0. 0. 1.]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习5-9\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import orthogonalize,unitization\n",
|
||||||
|
"np.set_printoptions(precision=4, suppress=True)\n",
|
||||||
|
"A=np.array([[1,1,1],\n",
|
||||||
|
" [1,2,4],\n",
|
||||||
|
" [1,3,9]],dtype='float')\n",
|
||||||
|
"B=orthogonalize(A)\n",
|
||||||
|
"unitization(B)\n",
|
||||||
|
"print(B)\n",
|
||||||
|
"print(np.matmul(B,B.T))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "99ac2049",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[ 1. 1. 10.]\n",
|
||||||
|
"[[-0.9314 -0.1464 -0.3333]\n",
|
||||||
|
" [ 0.1231 0.7351 -0.6667]\n",
|
||||||
|
" [-0.3426 0.6619 0.6667]]\n",
|
||||||
|
"[[ 1. -0. -0.]\n",
|
||||||
|
" [ 0. 1. 0.]\n",
|
||||||
|
" [-0. 0. 10.]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例5-14\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"np.set_printoptions(precision=4, suppress=True)\n",
|
||||||
|
"A=np.array([[2,2,-2],\n",
|
||||||
|
" [2,5,-4],\n",
|
||||||
|
" [-2,-4,5]])\n",
|
||||||
|
"v,P=np.linalg.eigh(A)\n",
|
||||||
|
"print(v)\n",
|
||||||
|
"print(P)\n",
|
||||||
|
"print(np.matmul(np.matmul(P.T,A),P))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "a61abf39",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[-2 1 4]\n",
|
||||||
|
"[[-1/3 2/3 -2/3]\n",
|
||||||
|
" [-2/3 1/3 2/3]\n",
|
||||||
|
" [-2/3 -2/3 -1/3]]\n",
|
||||||
|
"[[-2 0 0]\n",
|
||||||
|
" [0 1 0]\n",
|
||||||
|
" [0 0 4]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习5-12\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import mySolve,orthogonalize,unitization\n",
|
||||||
|
"from utility import Q\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(Q(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[2,-2,0],\n",
|
||||||
|
" [-2,1,-2],\n",
|
||||||
|
" [0,-2,0]])\n",
|
||||||
|
"v,P=np.linalg.eigh(A)\n",
|
||||||
|
"print(v)\n",
|
||||||
|
"print(P)\n",
|
||||||
|
"print(np.matmul(np.matmul(P.T,A),P))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "d990b236",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[1 2 1]\n",
|
||||||
|
" [2 4 2]\n",
|
||||||
|
" [1 2 1]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例5-19\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import symmetrization\n",
|
||||||
|
"A=np.array([[1,4,2],\n",
|
||||||
|
" [0,4,4],\n",
|
||||||
|
" [0,0,1]])\n",
|
||||||
|
"symmetrization(A)\n",
|
||||||
|
"print(A)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "16b59722",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[[ 1 -1 -2]\n",
|
||||||
|
" [-1 1 -2]\n",
|
||||||
|
" [-2 -2 -7]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习5-17\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import symmetrization\n",
|
||||||
|
"A=np.array([[1,-2,-4],\n",
|
||||||
|
" [0,1,-4],\n",
|
||||||
|
" [0,0,-7]])\n",
|
||||||
|
"symmetrization(A)\n",
|
||||||
|
"print(A)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"id": "60db5c49",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[-2. 1. 1.]\n",
|
||||||
|
"[[-0.5774 -0.4225 0.6987]\n",
|
||||||
|
" [-0.5774 0.8163 0.0166]\n",
|
||||||
|
" [ 0.5774 0.3938 0.7152]]\n",
|
||||||
|
"[[-2. -0. 0.]\n",
|
||||||
|
" [-0. 1. 0.]\n",
|
||||||
|
" [ 0. 0. 1.]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例5-20\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import symmetrization\n",
|
||||||
|
"np.set_printoptions(precision=4, suppress=True)\n",
|
||||||
|
"A=np.array([[0,-2,2],\n",
|
||||||
|
" [0,0,2],\n",
|
||||||
|
" [0,0,0]])\n",
|
||||||
|
"symmetrization(A)\n",
|
||||||
|
"v,P=np.linalg.eigh(A)\n",
|
||||||
|
"print(v)\n",
|
||||||
|
"print(P)\n",
|
||||||
|
"print(np.matmul(np.matmul(P.T,A),P))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "3ef40d9c",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[1. 2. 5.]\n",
|
||||||
|
"[[ 0. 1. 0. ]\n",
|
||||||
|
" [-0.7071 0. 0.7071]\n",
|
||||||
|
" [ 0.7071 0. 0.7071]]\n",
|
||||||
|
"[[1. 0. 0.]\n",
|
||||||
|
" [0. 2. 0.]\n",
|
||||||
|
" [0. 0. 5.]]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习5-18\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import symmetrization\n",
|
||||||
|
"np.set_printoptions(precision=4, suppress=True)\n",
|
||||||
|
"A=np.array([[2,0,0],\n",
|
||||||
|
" [0,3,4],\n",
|
||||||
|
" [0,0,3]])\n",
|
||||||
|
"symmetrization(A)\n",
|
||||||
|
"v,P=np.linalg.eigh(A)\n",
|
||||||
|
"print(v)\n",
|
||||||
|
"print(P)\n",
|
||||||
|
"print(np.matmul(np.matmul(P.T,A),P))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"id": "e3eabb72",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[-8. -5. -2.]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例5-21\n",
|
||||||
|
"import numpy as np #导入numpy\n",
|
||||||
|
"from utility import symmetrization #导入symmetrization\n",
|
||||||
|
"A=np.array([[-5,4,4], #初始化A\n",
|
||||||
|
" [0,-6,0],\n",
|
||||||
|
" [0,0,-4]])\n",
|
||||||
|
"symmetrization(A) #对称化A\n",
|
||||||
|
"v=np.linalg.eigvalsh(A) #计算A的特征值\n",
|
||||||
|
"print(v)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"id": "b3411091",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[-1. 1. 2.]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习5-19\n",
|
||||||
|
"import numpy as np #导入numpy\n",
|
||||||
|
"from utility import symmetrization #导入symmetrization\n",
|
||||||
|
"A=np.array([[1,2,0], #初始化A\n",
|
||||||
|
" [0,0,-2],\n",
|
||||||
|
" [0,0,1]])\n",
|
||||||
|
"symmetrization(A) #对称化A\n",
|
||||||
|
"v=np.linalg.eigvalsh(A) #计算A的特征值\n",
|
||||||
|
"print(v)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"id": "ba9a58f8",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[9/5 -18/5 0]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#例5-23\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import mySolve\n",
|
||||||
|
"from utility import Q\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(Q(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[4,2,-1],\n",
|
||||||
|
" [3,-1,2],\n",
|
||||||
|
" [11,3,0]],dtype='float')\n",
|
||||||
|
"b=np.array([2,10,8])\n",
|
||||||
|
"n,_=A.shape\n",
|
||||||
|
"B=np.matmul(A.T,A)\n",
|
||||||
|
"c=np.matmul(A.T,b.reshape(n,1))\n",
|
||||||
|
"X=mySolve(B,c)\n",
|
||||||
|
"print(X[:,0])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"id": "c577903b",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[1 1/3 0 0]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"#练习5-21\n",
|
||||||
|
"import numpy as np\n",
|
||||||
|
"from utility import mySolve\n",
|
||||||
|
"from utility import Q\n",
|
||||||
|
"np.set_printoptions(formatter={'all':lambda x:\n",
|
||||||
|
" str(Q(x).limit_denominator())})\n",
|
||||||
|
"A=np.array([[1,-2,3,-1],\n",
|
||||||
|
" [3,-1,5,-3],\n",
|
||||||
|
" [2,1,2,-2]],dtype='float')\n",
|
||||||
|
"b=np.array([1,2,3])\n",
|
||||||
|
"n,_=A.shape\n",
|
||||||
|
"B=np.matmul(A.T,A)\n",
|
||||||
|
"c=np.matmul(A.T,b.reshape(n,1))\n",
|
||||||
|
"X=mySolve(B,c)\n",
|
||||||
|
"print(X[:,0])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "4e77374d",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.9.7"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
|
@ -0,0 +1,200 @@
|
||||||
|
import numpy as np
|
||||||
|
from fractions import Fraction as Q
|
||||||
|
def exp(a):
|
||||||
|
n=len(a)
|
||||||
|
s=''
|
||||||
|
start=0
|
||||||
|
while start<n and a[start]==0:#查找第一个非零项下标start
|
||||||
|
start+=1
|
||||||
|
if start>=n:#零多项式
|
||||||
|
s+='0'
|
||||||
|
else:#非零多项式
|
||||||
|
if start==0:#非零常数项
|
||||||
|
s+='%s'%a[start]
|
||||||
|
if start==1:#非零1次项
|
||||||
|
s+='%s∙x'%a[start]
|
||||||
|
if start>1:#首项次数大于1
|
||||||
|
s+='%s∙x^%d'%(a[start],start)
|
||||||
|
for i in range(start+1,n):#首项后各项
|
||||||
|
if i==1:#1次项
|
||||||
|
if a[i]>0:#正1次项
|
||||||
|
s=s+'+%s∙x'%a[i]
|
||||||
|
if a[i]<0:#负1次项
|
||||||
|
s=s+'%s∙x'%a[i]
|
||||||
|
else:#非1次项
|
||||||
|
if a[i]>0:#正项
|
||||||
|
s=s+'+%s∙x**%d'%(a[i],i)
|
||||||
|
if a[i]<0:#负项
|
||||||
|
s=s+'%s∙x**%d'%(a[i],i)
|
||||||
|
return s
|
||||||
|
class myPoly:#多项式类
|
||||||
|
def __init__(self, coef):#初始化
|
||||||
|
c=np.trim_zeros(coef,trim='b')#删除高次零系数
|
||||||
|
self.coef=np.array(c)#设置多项式系数
|
||||||
|
self.degree=(self.coef).size-1#设置多项式次数
|
||||||
|
def __eq__(self, other):#相等
|
||||||
|
if self.degree!=other.degree:#次数不等
|
||||||
|
return False
|
||||||
|
return (abs(self.coef-other.coef)<1e-10).all()
|
||||||
|
def __str__(self):#输出表达式
|
||||||
|
return exp(self.coef)
|
||||||
|
def __add__(self, other):#运算符“+”
|
||||||
|
n=max(self.degree,other.degree)+1#系数个数
|
||||||
|
a=np.append(self.coef,[0]*(n-self.coef.size))#补长
|
||||||
|
b=np.append(other.coef,[0]*(n-other.coef.size))#补长
|
||||||
|
return myPoly(a+b)#创建并返回多项式和
|
||||||
|
def __rmul__(self, k):#右乘数k
|
||||||
|
c=self.coef*k#各系数与k的积
|
||||||
|
return myPoly(c)
|
||||||
|
def __neg__(self):#负多项式
|
||||||
|
return (-1)*self
|
||||||
|
def __sub__(self, other):#运算符“-”
|
||||||
|
return self+(-other)
|
||||||
|
def __mul__(self,other):#运算符“*”
|
||||||
|
m=self.degree
|
||||||
|
n=other.degree
|
||||||
|
a=np.append(self.coef,[0]*n)
|
||||||
|
b=np.append(other.coef,[0]*m)
|
||||||
|
c=[]
|
||||||
|
for s in range(1, m+n+2):
|
||||||
|
cs=(a[:s]*np.flip(b[:s])).sum()
|
||||||
|
c.append(cs)
|
||||||
|
return myPoly(c)
|
||||||
|
def val(self,x):#多项式在x处的值
|
||||||
|
n=self.degree#次数
|
||||||
|
power=np.array([x**k for k in range(n+1)])#x各次幂
|
||||||
|
v=((self.coef)*power).sum()#多项式的值
|
||||||
|
def P1(A, i, j, row=True):
|
||||||
|
if row:
|
||||||
|
A[[i,j],:]=A[[j,i],:]
|
||||||
|
else:
|
||||||
|
A[:,[i,j]]=A[:,[j,i]]
|
||||||
|
def P2(A,i,k, row=True):
|
||||||
|
if row:
|
||||||
|
A[i]=k*A[i]
|
||||||
|
else:
|
||||||
|
A[:,i]=k*A[:,i]
|
||||||
|
def P3(A,i,j,k, row=True):
|
||||||
|
if row:
|
||||||
|
A[j]+=k*A[i]
|
||||||
|
else:
|
||||||
|
A[:,j]+=k*A[:,i]
|
||||||
|
def Aij(A,i,j):
|
||||||
|
up=np.hstack((A[:i,:j],A[:i,j+1:]))
|
||||||
|
lo=np.hstack((A[i+1:,:j],A[i+1:,j+1:]))
|
||||||
|
M=np.vstack((up,lo))
|
||||||
|
return ((-1)**(i+j))*np.linalg.det(M)
|
||||||
|
def adjointMatrix(A):
|
||||||
|
n,_=A.shape
|
||||||
|
Am=np.zeros((n,n))
|
||||||
|
for i in range(n):
|
||||||
|
for j in range(n):
|
||||||
|
Am[j,i]=Aij(A,i,j)
|
||||||
|
return Am
|
||||||
|
def cramer(A, b):
|
||||||
|
n=len(A)#未知数个数
|
||||||
|
A=np.hstack((A, b.reshape(n,1)))#增广矩阵
|
||||||
|
x=[]#解初始化为空
|
||||||
|
detA=np.linalg.det(A[:,:n])#系数行列式
|
||||||
|
if detA!=0:#有解
|
||||||
|
for i in range(n):#计算每一个未知数的值
|
||||||
|
A[:,[i,n]]=A[:,[n,i]]#用b替换A的第i列
|
||||||
|
detAi=np.linalg.det(A[:,:n])#行列式
|
||||||
|
x.append(detAi/detA)#第i个未知数的值
|
||||||
|
A[:,[i,n]]=A[:,[n,i]]#还原系数矩阵
|
||||||
|
return np.array(x)
|
||||||
|
def rowLadder(A, m, n):
|
||||||
|
rank=0#系数矩阵秩初始化为0
|
||||||
|
zero=m#全零行首行下标
|
||||||
|
i=0#当前行号
|
||||||
|
order=np.array(range(n))#未知数顺序
|
||||||
|
while i<min(m,n) and i<zero:#自上向下处理每一行
|
||||||
|
flag=False#B[i,i]非零标志初始化为F
|
||||||
|
index=np.where(abs(A[i:,i])>1e-10)#当前列A[i,i]及其以下的非零元素下标
|
||||||
|
if len(index[0])>0:#存在非零元素
|
||||||
|
rank+=1#累加秩
|
||||||
|
flag=True#B[i,i]非零标记
|
||||||
|
k=index[0][0]#非零元素最小下标
|
||||||
|
if k>0:#若非第i行,交换第i,k+i行
|
||||||
|
P1(A,i,i+k)
|
||||||
|
else:#A[i:,i]内全为0
|
||||||
|
index=np.where(abs(A[i,i:n])>1e-10)#当前行A[i,i:n]的非零元素下标
|
||||||
|
if len(index[0])>0:#存在非零元素,交换第i,k+i列
|
||||||
|
rank+=1
|
||||||
|
flag=True
|
||||||
|
k=index[0][0]
|
||||||
|
P1(A,i,i+k,row=False)#列交换
|
||||||
|
order[[i, k+i]]=order[[k+i, i]]#跟踪未知数顺序
|
||||||
|
if flag:#A[i,i]不为零,A[i+1:m,i]消零
|
||||||
|
P2(A,i,1/A[i,i])
|
||||||
|
for t in range(i+1, zero):
|
||||||
|
P3(A,i,t,-A[t,i])
|
||||||
|
i+=1#下一行
|
||||||
|
else:#将全零元素行交换到矩阵底部
|
||||||
|
P1(A,i,zero-1)
|
||||||
|
zero-=1#更新全零行首行下标
|
||||||
|
return rank, order
|
||||||
|
def simplestLadder(A,rank):
|
||||||
|
for i in range(rank-1,0,-1):#主对角线上方元素消零
|
||||||
|
for k in range(i-1, -1,-1):
|
||||||
|
P3(A,i,k,-A[k,i])
|
||||||
|
def mySolve(A,b):
|
||||||
|
m,n=A.shape #系数矩阵结构
|
||||||
|
b=b.reshape(b.size, 1) #常量列向量
|
||||||
|
B=np.hstack((A, b)) #构造增广矩阵
|
||||||
|
r, order=rowLadder(B, m, n) #消元
|
||||||
|
X=np.array([]) #解集初始化为空
|
||||||
|
index=np.where(abs(B[:,n])>1e-10) #常数列非零元下标
|
||||||
|
nonhomo=index[0].size>0 #判断是否非齐次
|
||||||
|
r1=r #初始化增广矩阵秩
|
||||||
|
if nonhomo: #非齐次
|
||||||
|
r1=np.max(index)+1 #修改增广阵秩
|
||||||
|
solvable=(r>=r1) #判断是否可解
|
||||||
|
if solvable: #若可解
|
||||||
|
simplestLadder(B, r)#回代
|
||||||
|
X=np.vstack((B[:r,n].reshape(r,1),
|
||||||
|
np.zeros((n-r,1))))
|
||||||
|
if r<n:
|
||||||
|
x1=np.vstack((-B[:r,r:n],np.eye(n-r)))
|
||||||
|
X=np.hstack((X,x1))
|
||||||
|
X=X[order]
|
||||||
|
return X
|
||||||
|
def matrixSolve(A,B):
|
||||||
|
m,n=A.shape
|
||||||
|
_,n1=B.shape
|
||||||
|
C=np.hstack((A, B))
|
||||||
|
r, order=rowLadder(C, m, n)
|
||||||
|
simplestLadder(C, r)
|
||||||
|
solution=[]
|
||||||
|
X=np.array([])
|
||||||
|
index=np.where(abs(C[:,n:])>1e-10)
|
||||||
|
r1=max(index[0])+1
|
||||||
|
if r==r1:
|
||||||
|
X=np.vstack((C[:r,n:],
|
||||||
|
np.zeros((n-r,n1))))
|
||||||
|
X=X[order,:]
|
||||||
|
return X
|
||||||
|
def maxIndepGrp(A):
|
||||||
|
B=A.copy()
|
||||||
|
m,n=B.shape
|
||||||
|
r,order=rowLadder(B,m,n)
|
||||||
|
simplestLadder(B,r)
|
||||||
|
return r,order,B[:r,r:]
|
||||||
|
def orthogonalize(A):
|
||||||
|
m,n=A.shape
|
||||||
|
B=A.copy()
|
||||||
|
for i in range(1,n):
|
||||||
|
for j in range(i):
|
||||||
|
B[:,i]-=(np.dot(B[:,j],A[:,i])/
|
||||||
|
np.dot(B[:,j],B[:,j]))*B[:,j]
|
||||||
|
return B
|
||||||
|
def unitization(A):
|
||||||
|
_,n=A.shape
|
||||||
|
for i in range(n):
|
||||||
|
A[:,i]/=np.linalg.norm(A[:,i])
|
||||||
|
def symmetrization(A):
|
||||||
|
n,_=A.shape
|
||||||
|
for i in range(n):
|
||||||
|
for j in range(i+1,n):
|
||||||
|
A[i,j]=(A[i,j]+A[j,i])/2
|
||||||
|
A[j,i]=A[i,j]
|
Loading…
Reference in New Issue