multiplication of dimensional analysis

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2006
Posts: 34
Reputation: msaenz is an unknown quantity at this point 
Solved Threads: 0
msaenz msaenz is offline Offline
Light Poster

multiplication of dimensional analysis

 
0
  #1
Sep 21st, 2006
:mrgreen: hi im am tryingto create a dimensional analysis thing going on and so far i have this
  1. def mult(self,other):
  2. tempTuple1=list()
  3. tempTuple2=list()
  4.  
  5. i=0
  6. for unit,value in other.expr:
  7. for unit2,value2 in self.expr:
  8. if unit2 == unit:
  9. tempTuple1.append((unit, value+value2))
  10. #self.expr[i]=tempTuple1
  11.  
  12.  
  13. ## for unit2,value2 in self.expr:
  14. ## for unit,value in other.expr:
  15. ## if unit2 != unit:
  16. ## tempTuple1.append((unit2,value2))
  17.  
  18.  
  19. print tempTuple1

and my output is so far like this:
>>> t = dimensions([('j',2),('km',1), ('f',5)])
>>> w = dimensions([('j',2),('f',1)])
>>> w.mult(t)
[('j', 4), ('f', 6)] need to append km1.... what am i doing wrong anyone? thanks for your help
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: multiplication of dimensional analysis

 
0
  #2
Sep 22nd, 2006
You need to give us more of your code, otherwise this will be an exercise in mind reading!
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 34
Reputation: msaenz is an unknown quantity at this point 
Solved Threads: 0
msaenz msaenz is offline Offline
Light Poster

Re: multiplication of dimensional analysis

 
0
  #3
Sep 22nd, 2006
  1. class dimensions:
  2. def __init__(self, listofTuples):
  3. self.expr = listofTuples
  4.  
  5. def sub(self,other):
  6. for value in self.expr:
  7. if not value in other.expr:
  8. return "incompatible"
  9.  
  10. return self.expr
  11. def mult(self,other):
  12. tempTuple1=list()
  13. tempTuple2=list()
  14.  
  15. i=0
  16. for unit,value in other.expr:
  17. for unit2,value2 in self.expr:
  18. if unit2 == unit:
  19. tempTuple1.append((unit, value+value2))
  20. tempTuple2.append(())
  21.  
  22. ## for unit2,value2 in self.expr:
  23. ## for unit,value in other.expr:
  24. ## if unit2 != unit:
  25. ## tempTuple1.append((unit2,value2))
  26.  
  27. #print tempTuple1

here is my code now, i hope this is better
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: multiplication of dimensional analysis

 
0
  #4
Sep 22nd, 2006
I don't know what editor you are using, but for some odd reason you lost all the indentations (critical for Python to work). I doctored up your code and added a number of temporary print statements, always good for troubleshooting. I noticed that your mult() has no return statement, so result = None is the returned.
Here is the doctored code:
  1. class dimensions:
  2. def __init__(self, listofTuples):
  3. self.expr = listofTuples
  4.  
  5. def sub(self, other):
  6. for value in self.expr:
  7. if not value in other.expr:
  8. return "incompatible"
  9. return self.expr
  10.  
  11. def mult(self, other):
  12. tempTuple1=list()
  13. tempTuple2=list()
  14. print tempTuple1 # []
  15. print tempTuple2 # []
  16. print other.expr # [('j', 2), ('km', 1), ('f', 5)]
  17. print self.expr # [('j', 2), ('f', 1)]
  18.  
  19. i=0
  20. for unit,value in other.expr:
  21. for unit2,value2 in self.expr:
  22. if unit2 == unit:
  23. tempTuple1.append((unit, value+value2))
  24. tempTuple2.append(())
  25.  
  26. ## for unit2,value2 in self.expr:
  27. ## for unit,value in other.expr:
  28. ## if unit2 != unit:
  29. ## tempTuple1.append((unit2,value2))
  30.  
  31. print tempTuple1 # [('j', 4), ('f', 6)]
  32. print tempTuple2 # [(), ()]
  33. # what does mult() return?????
  34.  
  35. t = dimensions([('j',2),('km',1), ('f',5)])
  36. print t # <__main__.dimensions instance at 0x00AE6BE8>
  37. w = dimensions([('j',2),('f',1)])
  38. print w # <__main__.dimensions instance at 0x00AE6EE0>
  39. print w.mult(t) # gives None, correct since there is no return
How new are you to Python?
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 34
Reputation: msaenz is an unknown quantity at this point 
Solved Threads: 0
msaenz msaenz is offline Offline
Light Poster

Re: multiplication of dimensional analysis

 
0
  #5
Sep 22nd, 2006
sorry i just copied it and pasted it from my textedit. i was using the command line in gentoo.... i was working mostly with biopython and zope, never really persay did simple things in python. i just kinda skipped that and learned biopython syntax and the necessary python syntax to do my internship.

what i would like it to return is this: for example:
t = dimensions([('j',2),('km',1), ('f',5)])

w = dimensions([('j',2),('f',1)])

print w.mult(t) # gives None, correct since there is no return
return tempTuple1.append(the value that does not have a pair to be added with ('km',1)]

([('j',4),('f',6),('km',1)])

thanks for your help. what book would you recommend to learn python? i read some of the tutorials and they help but a book would be good.
Last edited by msaenz; Sep 22nd, 2006 at 7:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,027
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: multiplication of dimensional analysis

 
0
  #6
Sep 22nd, 2006
To get a hang of the basics of Python, I would recommend the book "Learning Python" by Mark Lutz and David Ascher, published by O'Reilly (no relation to the buffoon on TV!)
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 34
Reputation: msaenz is an unknown quantity at this point 
Solved Threads: 0
msaenz msaenz is offline Offline
Light Poster

Re: multiplication of dimensional analysis

 
0
  #7
Sep 23rd, 2006
still can't figure out how to append the last list into a tuple...does anyone stillhave any ideas. i tried popping and appending what was left and so forth. and yeah...still stuck any more ideas thanks!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC