本文整理汇总了Python中matutil.coldict2mat函数的典型用法代码示例。如果您正苦于以下问题:Python coldict2mat函数的具体用法?Python coldict2mat怎么用?Python coldict2mat使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了coldict2mat函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: aug_orthonormalize
def aug_orthonormalize(L):
'''
Input:
- L: a list of Vecs
Output:
- A pair Qlist, Rlist such that:
* coldict2mat(L) == coldict2mat(Qlist) * coldict2mat(Rlist)
* Qlist = orthonormalize(L)
>>> from vec import Vec
>>> D={'a','b','c','d'}
>>> L = [Vec(D, {'a':4,'b':3,'c':1,'d':2}), Vec(D, {'a':8,'b':9,'c':-5,'d':-5}), Vec(D, {'a':10,'b':1,'c':-1,'d':5})]
>>> Qlist, Rlist = aug_orthonormalize(L)
'''
Ql, Rl = aug_orthogonalize(L)
l=len(Ql)
Qlist=[]
Am=[]
for v in Ql:
A=sqrt(sum([v[i]**2 for i in range(len(v.D))]))
Am.append(A)
Qlist.append(1/A*v)
D=set(range(l))
AmV=[Vec(D,{i:Am[i]}) for i in range(l)]
AmM=coldict2mat(AmV)
Rlist=AmM*coldict2mat(Rl)
return Qlist, Rlist
示例2: direct_sum_decompose
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
'''
UV = coldict2mat(U_basis + V_basis)
W = solve(UV, w)
U = coldict2mat(U_basis)
V = coldict2mat(V_basis)
Wu = Vec(set(range(len(U_basis))),{x: W[x] for x in range(len(U_basis))})
Wv = Vec(set(range(len(V_basis))),{x: W[len(U_basis) + x] for x in range(len(V_basis))})
u = U * Wu
v = V * Wv
return (u,v)
示例3: is_superfluous
def is_superfluous(L, i):
'''
Input:
- L: list of vectors as instances of Vec class
- i: integer in range(len(L))
Output:
True if the span of the vectors of L is the same
as the span of the vectors of L, excluding L[i].
False otherwise.
Examples:
>>> 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})
>>> is_superfluous(L, 3)
True
>>> is_superfluous([a0,a1,a2,a3], 3)
True
>>> is_superfluous([a0,a1,a2,a3], 0)
True
>>> is_superfluous([a0,a1,a2,a3], 1)
False
'''
if len(L) == 1:
return False
tList = copy.deepcopy(L)
v_i = tList.pop(i)
x = solve(coldict2mat(tList),v_i)
res = v_i - coldict2mat(tList) * x
if res * res < pow(10,-14) :
return True
return False
pass
示例4: is_superfluous
def is_superfluous(L, i):
'''
Input:
- L: list of vectors as instances of Vec class
- i: integer in range(len(L))
Output:
True if the span of the vectors of L is the same
as the span of the vectors of L, excluding L[i].
False otherwise.
Examples:
>>> 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})
>>> is_superfluous(L, 3)
True
>>> is_superfluous([a0,a1,a2,a3], 3)
True
>>> is_superfluous([ a0,a1,a2,a3], 0)
True
>>> is_superfluous([a0,a1,a2,a3], 1)
False
'''
b = L.pop(i)
u = solve(coldict2mat(L),b)
residual = b - coldict2mat(L)*u
if residual * residual < 10e-14:
return True
else:
return False
示例5: direct_sum_decompose
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
'''
from hw4 import vec2rep
U = coldict2mat(U_basis)
V = coldict2mat(V_basis)
sum = U_basis + V_basis
sol_w = vec2rep(sum,w)
lenU = len(U_basis)
wu = list2vec ([ v for i, v in sol_w.f.items () if i < lenU ])
wv = list2vec ([ v for i, v in sol_w.f.items () if i >= lenU ])
u = U*wu
v = V*wv
return (u,v)
示例6: is_superfluous
def is_superfluous(L, i):
'''
Input:
- L: list of vectors as instances of Vec class
- i: integer in range(len(L))
Output:
True if the span of the vectors of L is the same
as the span of the vectors of L, excluding L[i].
False otherwise.
Examples:
>>> 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})
>>> is_superfluous(L, 3)
True
>>> is_superfluous([a0,a1,a2,a3], 3)
True
>>> is_superfluous([a0,a1,a2,a3], 0)
True
>>> is_superfluous([a0,a1,a2,a3], 1)
False
'''
b = L[i]
if i == 0:
A = coldict2mat(L[i+1:])
elif i == len(L)-1:
A = coldict2mat(L[0:i])
else:
A = coldict2mat(L[0:i-1]+L[i+1:])
u = solve(A, b)
r = b - A * u
return True if r*r < 1.0e-14 else False
示例7: QR_factor
def QR_factor(A):
col_labels = sorted(A.D[1], key=repr)
Acols = dict2list(mat2coldict(A),col_labels)
Qlist, Rlist = aug_orthonormalize(Acols)
#Now make Mats
Q = coldict2mat(Qlist)
R = coldict2mat(list2dict(Rlist, col_labels))
return Q,R
示例8: direct_sum_decompose
def direct_sum_decompose(U_basis, V_basis, w):
UV = coldict2mat(U_basis+V_basis)
U = coldict2mat(U_basis)
V = coldict2mat(V_basis)
W = solve(UV,w)
Wu = list2vec([v for i, v in W.f.items() if i < len(U_basis)])
Wv = list2vec([v for i, v in W.f.items() if i >= len(U_basis)])
u = U * Wu
v = V * Wv
return (u,v)
示例9: direct_sum_decompose
def direct_sum_decompose(U_basis, V_basis, w):
'''
Input:
- U_basis: a list of Vecs forming a basis for a vector space U
- V_basis: a list of Vecs forming a basis for a vector space V
- w: a Vec in the direct sum of U and V
Output:
- a pair (u, v) such that u + v = w, u is in U, v is in V
Example:
>>> D = {0,1,2,3,4,5}
>>> U_basis = [Vec(D,{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec(D,{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec(D,{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})]
>>> V_basis = [Vec(D,{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec(D,{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})]
>>> w = Vec(D,{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0})
>>> (u, v) = direct_sum_decompose(U_basis, V_basis, w)
>>> (u + v - w).is_almost_zero()
True
>>> U_matrix = coldict2mat(U_basis)
>>> V_matrix = coldict2mat(V_basis)
>>> (u - U_matrix*solve(U_matrix, u)).is_almost_zero()
True
>>> (v - V_matrix*solve(V_matrix, v)).is_almost_zero()
True
>>> ww = Vec(D,{0: 2, 1: 5, 2: 51, 4: 1, 5: 7})
>>> (u, v) = direct_sum_decompose(U_basis, V_basis, ww)
>>> (u + v - ww).is_almost_zero()
True
>>> (u - U_matrix*solve(U_matrix, u)).is_almost_zero()
True
>>> (v - V_matrix*solve(V_matrix, v)).is_almost_zero()
True
>>> U_basis == [Vec(D,{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec(D,{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec(D,{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})]
True
>>> V_basis == [Vec(D,{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec(D,{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})]
True
>>> w == Vec(D,{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0})
True
'''
union_basis = U_basis.copy()
union_basis.extend(V_basis)
union_matrix = coldict2mat(union_basis)
U_matrix = coldict2mat(U_basis)
V_matrix = coldict2mat(V_basis)
# calculate the w representation in u and v terms
w_rep = solve(union_matrix, w)
# extracting the u an v representation on U_basis and V_basis respectively
u_rep = Vec(U_matrix.D[1], {idx:w_rep[idx] for idx in range(len(U_basis))})
v_rep = Vec(V_matrix.D[1], {idx:w_rep[idx + len(U_basis)] for idx in range(len(V_basis))})
# calculate u and v vectors
u = U_matrix * u_rep
v = V_matrix * v_rep
return (u, v)
示例10: aug_orthonormalize
def aug_orthonormalize(L):
'''
Input:
- L: a list of Vecs
Output:
- A pair Qlist, Rlist such that:
* coldict2mat(L) == coldict2mat(Qlist) * coldict2mat(Rlist)
* Qlist = orthonormalize(L)
'''
Qlist = orthonormalize(L)
Rlist = mat2coldict(transpose(coldict2mat(Qlist)) * coldict2mat(L))
newRlist = [Rlist[k] for k in Rlist]
return Qlist,newRlist
示例11: aug_orthonormalize
def aug_orthonormalize(L):
'''
Input:
- L: a list of Vecs
Output:
- A pair Qlist, Rlist such that:
* coldict2mat(L) == coldict2mat(Qlist) * coldict2mat(Rlist)
* Qlist = orthonormalize(L)
'''
V, S = aug_orthogonalize(L)
Q = orthonormalize(L)
R = mat2coldict(transpose(coldict2mat(V)) * coldict2mat(Q) * coldict2mat(S))
return (Q,[x for x in R.values()])
示例12: aug_orthonormalize
def aug_orthonormalize(L):
"""
Input:
- L: a list of Vecs
Output:
- A pair Qlist, Rlist such that:
* coldict2mat(L) == coldict2mat(Qlist) * coldict2mat(Rlist)
* Qlist = orthonormalize(L)
"""
from orthogonalization import aug_orthogonalize
from matutil import mat2coldict, coldict2mat
Qlist = orthonormalize(L)
Rlist = mat2coldict(coldict2mat(Qlist).transpose() * coldict2mat(L))
return Qlist, list(Rlist.values())
示例13: mat_move2board
def mat_move2board(Y):
'''
Input:
- Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec
giving the whiteboard coordinates of a point q.
Output:
- a Mat each column of which is the corresponding point in the
whiteboard plane (the point of intersection with the whiteboard plane
of the line through the origin and q).
Example:
>>> Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}),
... {('y1',0):2, ('y2',0):4, ('y3',0):8,
... ('y1',1):10, ('y2',1):5, ('y3',1):5,
... ('y1',2):4, ('y2',2):25, ('y3',2):2,
... ('y1',3):5, ('y2',3):10, ('y3',3):4})
>>> print(Y_in)
<BLANKLINE>
0 1 2 3
------------
y1 | 2 10 4 5
y2 | 4 5 25 10
y3 | 8 5 2 4
<BLANKLINE>
>>> print(mat_move2board(Y_in))
<BLANKLINE>
0 1 2 3
------------------
y1 | 0.25 2 2 1.25
y2 | 0.5 1 12.5 2.5
y3 | 1 1 1 1
<BLANKLINE>
'''
coldict = mat2coldict(Y)
return coldict2mat({key:move2board(val) for key, val in coldict.items()})
示例14: direct_sum_decompose
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
'''
sum_base = U_basis + V_basis
# find coefficients
composed_result = solve(coldict2mat(sum_base),w)
# calculate u
u = Vec(U_basis[0].D,{})
for i in range(len(U_basis)):
u += composed_result[i] * U_basis[i]
# calculate v
v = Vec(V_basis[0].D,{})
for i in range(len(U_basis), len(sum_base)):
v += composed_result[i] * V_basis[i-len(U_basis)]
return (u,v)
示例15: direct_sum_decompose
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
"""
testMat = coldict2mat(U_basis + V_basis)
coef = solve(testMat, w)
Nu = len(U_basis)
Nv = len(V_basis)
retU = Vec(U_basis[0].D, {})
retV = Vec(V_basis[0].D, {})
for i in range(Nu):
retU += coef[i] * U_basis[i]
for i in range(Nv):
retV += coef[i + Nu] * V_basis[i]
return (retU, retV)