File: m_print.py

package info (click to toggle)
shogun 0.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 8,688 kB
  • ctags: 6,563
  • sloc: cpp: 61,677; python: 5,233; sh: 2,767; makefile: 555; objc: 37
file content (38 lines) | stat: -rw-r--r-- 916 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from numpy import*

#writes matrix km into the opened m-file fileobj
def print_mat(km, fileobj, mat_name='km'):
	line= list()
	lis = list()
	for x in range(km.shape[0]):
		for y in range(km.shape[1]):
			line.append('%.9g' %km[x,y])	
		lis.append(', '.join(line))
		line = list()
	kmstr = ';'.join(lis)
	kmstr = ''.join([mat_name,' = [',kmstr, ']'])
	kmstr = kmstr.replace('\n','')
	fileobj.write(kmstr)
	fileobj.write('\n')
		
def read_mat(line):
	str   = (line.split('[')[1]).split(']')[0]
	lines = str.split(';')
	lis   = list()
	lis2d = list()
	for x in lines:
		for y in x.split(','):
			lis.append(float(y))
		lis2d.append(lis)
		lis = list()
		
	return array(lis2d)

	#for x in range(km.shape[0]):
	#	line = repr(km[x])
	#	line = line.strip()
	#	remove '...[' and ']...'
	#	line = (line.split('[')[1]).split(']')[0]
	#	lis.append(line)		
	#kmstr = ';'.join(lis)
	#kmstr = ''.join([mat_name,' = [',kmstr, ']'])