473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with pure functions

Xentrik
I am a beginner working my way through How To Think Like A Computer Scientist. I am having a problem with an exercise that multiplies a matrix by a scalar. def scalar_mult(n, m), n being the scalar and m being the matrix.
My code works but I guess its not a pure function because if I put in

>>> m = [[1, 2], [3, 4]]
>>> scalar_mult(3, m)

my return is [[3, 6], [9, 12]] which is correct, but when i check m after I run my function I get [[3, 6], [9, 12]] instead of [[1, 2], [3, 4]]. I don't understand this because I cloned m and used mx throughout the function. This is the first time I have posted on a forum so please excuse me if my format is incorrect.

Expand|Select|Wrap|Line Numbers
  1. def scalar_mult(n, m):
  2.     mx = m[:]
  3.   (my code is here)
  4.  
Mar 11 '10 #1

✓ answered by bvdet

The problem is that you have a list of lists. You made a copy of the original list, but you did not make copies of the internal lists. Do this:
Expand|Select|Wrap|Line Numbers
  1. def scalar_mult(n, m):
  2.     mx = [item[:] for item in m]
  3.     x = 0
  4.     while x < len(mx):
  5.         for index, value in enumerate(mx[x]):
  6.             mx[x][index] = mx[x][index] * n
  7.         x += 1
  8.     return mx 
Following is an example that makes a new list. It uses recursion to get at nested lists also.
Expand|Select|Wrap|Line Numbers
  1. def scalar_mult(seq, n):
  2.     results = []
  3.     for elem in seq:
  4.         if isinstance(elem, (list, tuple)):
  5.             results.append(scalar_mult(elem, n))
  6.         else:
  7.             results.append(elem * n)
  8.     return results
  9.  
  10. seq = [[1, 2], [3, 4]]
  11. seq1 = scalar_mult(seq, 3)
  12. print seq
  13. print seq1
  14.  
  15. seq2 = ((1, 2), (3, (4, 5, 6), ((6, 7, 8), (9, 20))))
  16. seq3 = scalar_mult(seq2, 3)
  17. print seq2
  18. print seq3
  19.  
  20. ''' The output:
  21. >>> [[1, 2], [3, 4]]
  22. [[3, 6], [9, 12]]
  23. ((1, 2), (3, (4, 5, 6), ((6, 7, 8), (9, 20))))
  24. [[3, 6], [9, [12, 15, 18], [[18, 21, 24], [27, 60]]]]
  25. '''

3 2126
Glenton
391 Expert 256MB
Hi

Welcome to the forum. Please go ahead and post the rest of your code.

Thanks

Glenton
Mar 11 '10 #2
@Glenton
Glenton, here is my code.

Expand|Select|Wrap|Line Numbers
  1. def scalar_mult(n, m):
  2.     mx = m[:]
  3.     x = 0
  4.     while x < len(mx):
  5.         for index, value in enumerate(mx[x]):
  6.             mx[x][index] = mx[x][index] * n
  7.         x += 1
  8.     return mx 
  9.  
Mar 11 '10 #3
bvdet
2,851 Expert Mod 2GB
The problem is that you have a list of lists. You made a copy of the original list, but you did not make copies of the internal lists. Do this:
Expand|Select|Wrap|Line Numbers
  1. def scalar_mult(n, m):
  2.     mx = [item[:] for item in m]
  3.     x = 0
  4.     while x < len(mx):
  5.         for index, value in enumerate(mx[x]):
  6.             mx[x][index] = mx[x][index] * n
  7.         x += 1
  8.     return mx 
Following is an example that makes a new list. It uses recursion to get at nested lists also.
Expand|Select|Wrap|Line Numbers
  1. def scalar_mult(seq, n):
  2.     results = []
  3.     for elem in seq:
  4.         if isinstance(elem, (list, tuple)):
  5.             results.append(scalar_mult(elem, n))
  6.         else:
  7.             results.append(elem * n)
  8.     return results
  9.  
  10. seq = [[1, 2], [3, 4]]
  11. seq1 = scalar_mult(seq, 3)
  12. print seq
  13. print seq1
  14.  
  15. seq2 = ((1, 2), (3, (4, 5, 6), ((6, 7, 8), (9, 20))))
  16. seq3 = scalar_mult(seq2, 3)
  17. print seq2
  18. print seq3
  19.  
  20. ''' The output:
  21. >>> [[1, 2], [3, 4]]
  22. [[3, 6], [9, 12]]
  23. ((1, 2), (3, (4, 5, 6), ((6, 7, 8), (9, 20))))
  24. [[3, 6], [9, [12, 15, 18], [[18, 21, 24], [27, 60]]]]
  25. '''
Mar 11 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
0
by: Kevin Bracey | last post by:
I've been implemening Annex F in my compiler, and one issue to do with "pure" functions has come to my attention. The compiler has for a long time had a __pure keyword (and a related pragma...
13
by: junky_fellow | last post by:
I have read certain articles that encourage to use/write pure functions (if possible) as they are better suited for optimization. I got one example that expalins how the code can be optimised....
5
by: Michael C | last post by:
I'm trying to convert this interface over to C# but can't get any of the functions to work at all. I'm just trying as a start to get the get_Name function to work but can't get anything out of it...
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
10
by: Rahul | last post by:
Hi, I tried to create a abstract class by having a non-virtual member function as pure, but i got a compilation error saying "only virtual member functions can be pure"... I'm trying to think...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.