# IDA_test.py
import mymatrix
data = [
[5,6,7,5,4,7,8,0],
[2,1,5,9,7,4,2,9],
[3,4,6,7,4,3,2,7],
[8,7,6,8,9,4,6,3]]
print 'data'
for i in data: print i
vand=[[i**j for j in range(len(data))] for i in range(1,9)]
print '\nvandermonde: '
for i in vand: print i
result = mymatrix.m_m(vand,data)
print 'result: '
for i in result: print i
# ---suppose the 1,2,3,4 of sensors damaged
r = result[4:]
v = vand[4:]
print '\npart of result:'
for i in r: print i
print '\nrespectvie part in vand:'
for i in v: print i
i_v = mymatrix.inverse(v)
d = mymatrix.m_m(i_v,r)
print '\n\ndata recovered:'
for i in d: print i
IDLE 1.2.2 ==== No Subprocess ====
>>>
data
[5, 6, 7, 5, 4, 7, 8, 0]
[2, 1, 5, 9, 7, 4, 2, 9]
[3, 4, 6, 7, 4, 3, 2, 7]
[8, 7, 6, 8, 9, 4, 6, 3]
vandermonde:
[1, 1, 1, 1]
[1, 2, 4, 8]
[1, 3, 9, 27]
[1, 4, 16, 64]
[1, 5, 25, 125]
[1, 6, 36, 216]
[1, 7, 49, 343]
[1, 8, 64, 512]
result:
[18, 18, 24, 29, 24, 18, 18, 19]
[85, 80, 89, 115, 106, 59, 68, 70]
[254, 234, 238, 311, 304, 154, 194, 171]
[573, 522, 507, 665, 672, 327, 432, 340]
[1090, 986, 932, 1225, 1264, 602, 818, 595]
[1853, 1668, 1549, 2039, 2134, 1003, 1388, 954]
[2910, 2610, 2394, 3155, 3336, 1554, 2178, 1435]
[4309, 3854, 3503, 4621, 4924, 2279, 3224, 2056]
part of result:
[1090, 986, 932, 1225, 1264, 602, 818, 595]
[1853, 1668, 1549, 2039, 2134, 1003, 1388, 954]
[2910, 2610, 2394, 3155, 3336, 1554, 2178, 1435]
[4309, 3854, 3503, 4621, 4924, 2279, 3224, 2056]
respectvie part in vand:
[1, 5, 25, 125]
[1, 6, 36, 216]
[1, 7, 49, 343]
[1, 8, 64, 512]
data recovered:
[5.0, 6.0, 7.0, 5.0, 4.0, 7.0, 8.0, 0.0]
[2.0, 1.0, 5.000000000007276, 9.0, 7.0, 4.0, 2.000000000007276, 9.0]
[3.0, 4.0, 6.0, 7.0, 4.0, 3.0, 2.0, 7.0]
[8.0, 7.0, 6.0, 8.0, 9.0, 4.0, 6.0, 3.0]
>>>
# mymatrix.py
def v_v(a,b):
""" return the product of two vector a and b """
return sum(map(lambda (i,j):i*j, zip(a,b)))
def v_m(a,B):
""" return the product of vector a and matrix B"""
return [v_v(a,i) for i in transp(B)]
def m_m(A,B):
""" return the product of two matrixes A and B """
return [v_m(i,B) for i in A]
def transp(A):
""" return the transpose of matrix A """
return [[i[j] for i in A] for j in xrange(len(A[0]))]
def det(A):
""" return the determinant of square matrix A
Notice that the 1-order matrix should be like [[x]],
Not [x] """
if len(A[0])==1:
return A[0][0]
return sum([A[0][j]*alg_com(A,0,j) for j in xrange(len(A))])
def inverse(A):
""" return the inversion of square matrix A
A must be invertible """
d=det(A)
if not d:
print 'No inversion for singular matrix !'; return
d=1.0/d
return transp([[alg_com(A,i,j)*d for j in xrange(len(A[0]))] for i in xrange(len(A))])
def complement(A,i,j):
""" return the complement of element A[j] in square matrix A,
To get algebraic complement use alg_com """
return det(cut(A,i,j))
def alg_com(A,i,j):
""" return the algebraic complement of square matrix A """
return (-1)**(i+j)*complement(A,i,j)
def cut(A,i,j):
""" return a copy of A that have no row i and column j """
return [[A[x][y] for y in xrange(len(A[0])) if y!=j] for x in xrange(len(A)) if x!=i]
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/54441/showart_1881147.html |