这一周的作业,刚压线写完。Problem3 没有写,不想证明了。从Problem 9 开始一直到最后难度都挺大的,我是在论坛上看过了别人的讨论才写出来的,挣扎了很久。

Problem 9在给定的基上分解向量,里面调用了hw4的一些函数,通过solve函数获得矩阵方程的解

Problem 10判断矩阵是不是可逆的,注意判断矩阵是不是square的

Problem 11和Problem 12 都是求逆,也是解方程,只是函数的参数需要参考一下源码

发现一个有趣的事情,Coding the Matrix的论坛上有个老头胡子都白了也在学这个课程,好励志的感觉,不过人家貌似是教授来着。

# version code 941
# Please fill out this stencil and submit using the provided submission script. from vecutil import list2vec
from solver import solve
from matutil import *
from mat import Mat
from GF2 import one
from vec import Vec
from independence import *
from triangular import *
from hw4 import * ## Problem 1
w0 = list2vec([1,0,0])
w1 = list2vec([0,1,0])
w2 = list2vec([0,0,1]) v0 = list2vec([1,2,3])
v1 = list2vec([1,3,3])
v2 = list2vec([0,3,3]) # Fill in exchange_S1 and exchange_S2
# with appropriate lists of 3 vectors exchange_S0 = [w0,w1,w2]
exchange_S1 = [v0,w1,w2]
exchange_S2 = [v0,v1,w2]
exchange_S3 = [v0,v1,v2] ## Problem 2
w0 = list2vec([0,one,0])
w1 = list2vec([0,0,one])
w2 = list2vec([one,one,one]) v0 = list2vec([one,0,one])
v1 = list2vec([one,0,0])
v2 = list2vec([one,one,0]) exchange_2_S0 = [w0, w1, w2]
exchange_2_S1 = [w0,w1,v1]
exchange_2_S2 = [w0,v0,v1]
exchange_2_S3 = [v0, v1, v2] ## Problem 3
def morph(S, B):
'''
Input:
- S: a list of distinct Vec instances
- B: a list of linearly independent Vec instances
- Span S == Span B
Output: a list of pairs of vectors to inject and eject
Example:
>>> #This is how our morph works. Yours may yield different results.
>>> S = [list2vec(v) for v in [[1,0,0],[0,1,0],[0,0,1]]]
>>> B = [list2vec(v) for v in [[1,1,0],[0,1,1],[1,0,1]]]
>>> morph(S, B)
[(Vec({0, 1, 2},{0: 1, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 1, 1: 0, 2: 0})), (Vec({0, 1, 2},{0: 0, 1: 1, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 0})), (Vec({0, 1, 2},{0: 1, 1: 0, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 0, 2: 1}))] '''
pass ## Problem 4
# Please express each solution as a list of vectors (Vec instances) row_space_1 = [list2vec([1,2,0]),list2vec([0,2,1])]
col_space_1 = [list2vec([1,0]),list2vec([0,1])] row_space_2 = [list2vec([1,4,0,0]),list2vec([0,2,2,0]),list2vec([0,0,1,1])]
col_space_2 = [list2vec([1,0,0]),list2vec([4,2,0]),list2vec([0,0,1])] row_space_3 = [list2vec([1])]
col_space_3 = [list2vec([1,2,3])] row_space_4 = [list2vec([1,0]),list2vec([2,1])]
col_space_4 = [list2vec([1,2,3]),list2vec([0,1,4])] ## Problem 5
def my_is_independent(L):
'''
input: A list, L, of Vecs
output: A boolean indicating if the list is linearly independent >>> L = [Vec({0, 1, 2},{0: 1, 1: 0, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 0, 2: 1}), Vec({0, 1, 2},{0: 1, 1: 1, 2: 1}), Vec({0, 1, 2},{0: 1, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 1})]
>>> my_is_independent(L)
False
>>> my_is_independent(L[:2])
True
>>> my_is_independent(L[:3])
True
>>> my_is_independent(L[1:4])
True
>>> my_is_independent(L[0:4])
False
>>> my_is_independent(L[2:])
False
>>> my_is_independent(L[2:5])
False
'''
if rank(L)==len(L):return True
else:return False ## Problem 6
def subset_basis(T):
'''
input: A list, T, of Vecs
output: A list, S, containing Vecs from T, that is a basis for the
space spanned by T. >>> a0 = Vec({'a','b','c','d'}, {'a':1})
>>> a1 = Vec({'a','b','c','d'}, {'b':1})
>>> a2 = Vec({'a','b','c','d'}, {'c':1})
>>> a3 = Vec({'a','b','c','d'}, {'a':1,'c':3})
>>> subset_basis([a0,a1,a2,a3]) == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})]
True
'''
r=[]
for x in T:
r.append(x)
#print(x)
if my_is_independent(r)==False:r.remove(x)
#print(r)
return r ## Problem 7
def my_rank(L):
'''
input: A list, L, of Vecs
output: The rank of the list of Vecs >>> my_rank([list2vec(v) for v in [[1,2,3],[4,5,6],[1.1,1.1,1.1]]])
2
'''
return len(subset_basis(L)) ## Problem 8
# Please give each answer as a boolean only_share_the_zero_vector_1 = True
only_share_the_zero_vector_2 = True
only_share_the_zero_vector_3 = True ## Problem 9
def direct_sum_decompose(U_basis, V_basis, w):
'''
input: A list of Vecs, U_basis, containing a basis for a vector space, U.
A list of Vecs, V_basis, containing a basis for a vector space, V.
A Vec, w, that belongs to the direct sum of these spaces.
output: A pair, (u, v), such that u+v=w and u is an element of U and
v is an element of V. >>> U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})]
>>> V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})]
>>> w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0})
>>> direct_sum_decompose(U_basis, V_basis, w) == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0}))
True
'''
T=U_basis + V_basis
x=vec2rep(T,w)
#print(T,w,x)
rep=list(x.f.values())
u1=list2vec(rep[0:len(U_basis)])
v1=list2vec(rep[len(U_basis):len(T)])
u=rep2vec(u1,U_basis)
v=rep2vec(v1,V_basis)
return (u,v) ## Problem 10
def is_invertible(M):
'''
input: A matrix, M
outpit: A boolean indicating if M is invertible. >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0})
>>> is_invertible(M)
True
'''
L=mat2coldict(M)
L=list(L.values())
if len(L)!=len(L[0].D):return False
else:return rank(L)==len(L) ## Problem 11
def find_matrix_inverse(A):
'''
input: An invertible matrix, A, over GF(2)
output: Inverse of A >>> M = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (1, 2): 0, (0, 0): 0, (2, 0): 0, (1, 0): one, (2, 2): one, (0, 2): 0, (2, 1): 0, (1, 1): 0})
>>> find_matrix_inverse(M) == Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (2, 0): 0, (0, 0): 0, (2, 2): one, (1, 0): one, (1, 2): 0, (1, 1): 0, (2, 1): 0, (0, 2): 0})
True
'''
B=[]
for i in range(len(A.D[0])):
b=Vec(A.D[0],{})
b[i]=one
t=solve(A,b)
B.append(t)
B=coldict2mat(B)
return B ## Problem 12
def find_triangular_matrix_inverse(A):
'''
input: An upper triangular Mat, A, with nonzero diagonal elements
output: Inverse of A
>>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]])
>>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0})
True
'''
B=[]
C=mat2rowdict(A)
for i in range(len(A.D[0])):
b=Vec(A.D[0],{})
b[i]=1
t=triangular_solve(C,range(len(C)),b)
B.append(t)
B=coldict2mat(B)
return B

【Python】Coding the Matrix:Week 5: Dimension Homework 5的更多相关文章

  1. 【Python】Coding the Matrix:Week 5 Perspective Lab

    这个Lab的内容光是说明就有7页之巨,我反复看了很久才看懂一点点,Lab主要完成的是从不同坐标系表示之间变换的方法. 原始的图片,从Camera basis的表示转换成WhiteBoard basis ...

  2. 【python】SQLAlchemy

    来源:廖雪峰 对比:[python]在python中调用mysql 注意连接数据库方式和数据操作方式! 今天发现了个处理数据库的好东西:SQLAlchemy 一般python处理mysql之类的数据库 ...

  3. 【Python】内置数据类型

    参考资料: http://sebug.net/paper/books/dive-into-python3/native-datatypes.html http://blog.csdn.net/hazi ...

  4. 【Python】 零碎知识积累 II

    [Python] 零碎知识积累 II ■ 函数的参数默认值在函数定义时确定并保存在内存中,调用函数时不会在内存中新开辟一块空间然后用参数默认值重新赋值,而是单纯地引用这个参数原来的地址.这就带来了一个 ...

  5. 【Python】【BugList12】python自带IDLE执行print(req.text)报错:UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 93204-93204

    [代码] # -*- coding:UTF-8 -*- import requests if __name__ == '__main__': target = 'https://unsplash.co ...

  6. 【Python】【内置函数】

    [fromkeys()] -- coding: utf-8 -- python 27 xiaodeng python之函数用法fromkeys() fromkeys() 说明:用于创建一个新字典,以序 ...

  7. 【Python】-NO.98.Note.3.Python -【Python3 解释器、运算符】

    1.0.0 Summary Tittle:[Python]-NO.98.Note.3.Python -[Python3 解释器] Style:Python Series:Python Since:20 ...

  8. 【Python】【容器 | 迭代对象 | 迭代器 | 生成器 | 生成器表达式 | 协程 | 期物 | 任务】

    Python 的 asyncio 类似于 C++ 的 Boost.Asio. 所谓「异步 IO」,就是你发起一个 IO 操作,却不用等它结束,你可以继续做其他事情,当它结束时,你会得到通知. Asyn ...

  9. 【Python】无须numpy,利用map函数与zip(*)函数对数组转置(转)

    http://blog.csdn.net/yongh701/article/details/50283689 在Python的numpy中,对类似array=[[1,2,3],[4,5,6],[7,8 ...

随机推荐

  1. 【POJ1581】A Contesting Decision(简单模拟)

    没有什么弯路,直接模拟即可.水题. #include <iostream> #include <cstring> #include <cstdlib> #inclu ...

  2. ios 导航栏和旋屏

    1,状态栏(UIStatusBar) http://my.oschina.net/shede333/blog/304560 2,visibleViewController和topViewControl ...

  3. [置顶] 【其他部分 第一章 矩阵】The C Programming Language 程序研究 【持续更新】

    其他部分 第一章 矩阵 一.矩阵的转置   问题描述: 编写函数,把给定的任意一个二维整型矩阵转换为其转置矩阵. 输入: 1 2 3 4 5 6 输出: 1 4 2 5 3 6 分析 题目要求编写一个 ...

  4. hdu 4400 Mines(离散化+bfs+枚举)

    Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all ...

  5. Scala-Partial Functions(偏函数)

    如果你想定义一个函数,而让它只接受和处理其参数定义域范围内的子集,对于这个参数范围外的参数则抛出异常,这样的函数就是偏函数(顾名思异就是这个函数只处理传入来的部分参数). 偏函数是个特质其的类型为Pa ...

  6. Java之Static静态修饰符详解

    Java之Static静态修饰符详解 Java之Static静态修饰符详解 一.特点 1.随着类的加载而加载,随着类的消失而消失,生命周期最长 2.优先于对象存在 3.被所有类的对象共享 4.可以直接 ...

  7. hdu 4923 Room and Moor (单调栈+思维)

    题意: 给一个0和1组成的序列a,要构造一个相同长度的序列b.b要满足非严格单调,且 值为0到1的实数.最后使得  sum((ai-bi)^2)最小. 算法: 首先a序列開始的连续0和末尾的连续1是能 ...

  8. 几个检查当前运行的LINUX是在VM还是在实体机中的方法

    昨天提到了VM中的逃逸问题,要想逃逸,首先要检测当前操作系统是否为VM,下面提供几个LINUX下的检查方法: 第一,首推facter virtual ,权限为普通用户,约定,普通用户命令提示符用$表示 ...

  9. [HeadFirst-HTMLCSS入门][第十章div,span]

    新元素 <div>逻辑容器 能进行分组,等于用一个大的盒子进行包装 <span> 内联字符的逻辑分组 text-align 改变所有内联元素位置. 属性 center 居中 行 ...

  10. 关于UIWebview的属性的介绍

    /*    ViewController.h 文件               */ #import <UIKit/UIKit.h> @interface ViewController : ...