Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 3rd, 2012, 05:37 PM
rebork rebork is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 rebork User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 43 sec
Reputation Power: 0
Reading text file containing square matrix

Hi everyone,

I am a beginner at python and so far it's been great learning this language. I'm currently working on an assignment which is asking me to do some operations on a multidimensional square matrix. My problem is getting the matrix out of a text file because I just can't figure out how to get it out. I can print the matrix but I can never do anything to it like find its dot product or transpose.

My code so far...
Code:
from numpy import *

textfile=raw_input('What is your textfile name? (Refrain from putting .txt):')
print 'You have entered', textfile+'.txt'
new_copy=[]
filename=open(textfile+'.txt')
for line in filename:
	new_copy.append(line)
print new_copy


Any help would be gladly appreciated!

Reply With Quote
  #2  
Old December 3rd, 2012, 06:39 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,372 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 39 m 38 sec
Reputation Power: 383
A solution (without matrix product)
Code:
import pprint

textfile=raw_input('What is your textfile name? (Refrain from putting .txt):')
name = textfile + '.txt'
print 'loading',name

with open(name,'r') as inf:
	text_data = inf.readlines()

table = []
for Line in text_data:
    row = []
    for field in Line.strip().split():
        row.append(float(field))          # convert to float
    table.append(row)

print('the table is:')
pprint.pprint(table)

def transpose(A):
    return list(zip(*A))

print('transposed')
pprint.pprint(transpose(table))
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old December 3rd, 2012, 06:44 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,372 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 39 m 38 sec
Reputation Power: 383
Simpler with numpy

Code:
>>> import numpy
>>> numpy.loadtxt('a.txt')
array([[ 1.,  2.],
       [ 3.,  4.]])
>>> A = numpy.loadtxt('a.txt')
>>> A.transpose()
array([[ 1.,  3.],
       [ 2.,  4.]])
>>> A.dot(A)
array([[  7.,  10.],
       [ 15.,  22.]])
>>> 

Reply With Quote
  #4  
Old December 4th, 2012, 12:41 AM
rebork rebork is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 rebork User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 43 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
A solution (without matrix product)
Code:
import pprint

textfile=raw_input('What is your textfile name? (Refrain from putting .txt):')
name = textfile + '.txt'
print 'loading',name

with open(name,'r') as inf:
	text_data = inf.readlines()

table = []
for Line in text_data:
    row = []
    for field in Line.strip().split():
        row.append(float(field))          # convert to float
    table.append(row)

print('the table is:')
pprint.pprint(table)

def transpose(A):
    return list(zip(*A))

print('transposed')
pprint.pprint(transpose(table))


Hi I looked through this code and I tried it out using PyLab. Everything worked up till I got the value error stating that I could not convert the string to float at row.append(float(field)). I tried the numpy way too but that also had the problem of converting string to float. Am I doing something wrong?

Reply With Quote
  #5  
Old December 4th, 2012, 07:42 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,372 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 39 m 38 sec
Reputation Power: 383
Yes, you did something wrong. Specifically you didn't share your input file. My test data:
Code:
1 2
3 4

Reply With Quote
  #6  
Old December 4th, 2012, 01:42 PM
rebork rebork is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 rebork User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 43 sec
Reputation Power: 0
Ohhh I see. My text file was wrong the enter time! My file contained [[1,1],[2,3]]. But now that I think about it my program probably didn't need the matrix to be displayed as such in a text file. My apologies. Thanks for all your help though I understand it now!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Reading text file containing square matrix

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap