Beispiel #1
0
def main():
	if len(argv) < 4:
		print "Usage: <num eigenvectors> <tolerance> <text file>"
		return
	data = load_matrix(argv[3])
	k = int(argv[1])
	epsilon = float(argv[2])
	rows = len(data)
	cols = len(data[0])
	A = Matrix(rows, cols, data)
	res, vals = power_method(k, epsilon, A)

	Vecs = Matrix(rows, k)
	for i in range(rows):
		for j in range(k):
			Vecs.data[i][j] = res[j].data[i][0]

	f1 = open('vecs.txt','w')
	f2 = open('vals.txt','w')

	# TODO fix formatting
	for i in range(rows):
		for j in range(k):
			f1.write(str(Vecs.data[i][j]) + ' ')
		f1.write('\n')

	for i in range(k):
		f2.write(str(vals[i]) + '\n')

	f1.close()
	f2.close()
Beispiel #2
0
def main():
    if len(argv) < 4:
        print "Usage: <num eigenvectors> <tolerance> <text file>"
        return
    data = load_matrix(argv[3])
    k = int(argv[1])
    epsilon = float(argv[2])
    rows = len(data)
    cols = len(data[0])
    A = Matrix(rows, cols, data)
    U, S, V = svd(k, epsilon, A)

    f1 = open("U.txt", "w")
    f2 = open("V.txt", "w")
    f3 = open("S.txt", "w")

    # output U
    for line in U.data:
        for val in line:
            f1.write(str(val) + " ")
        f1.write("\n")

        # output V
    for line in V.data:
        for val in line:
            f2.write(str(val) + " ")
        f2.write("\n")

        # output S
    for val in S:
        f3.write(str(val) + "\n")

    f1.close()
    f2.close()
    f3.close()

    # Code to test Frobenius Norm
    print "Frobenius norm:", frob_norm(A, U, S, V)