Arrays and copy semantics

Assignment Help Basic Computer Science
Reference no: EM132197714

Part A) Arrays and copy semantics Consider the following Python code segment, which uses built-in Python lists and NumPy lists to perform similar operations, albeit with differing results.

Using built-in Python lists:

>>> data = [1, 2, 3, 4]

>>> print data [1, 2, 3, 4]

>>> otherData = data

>>> otherData[1] = -2

>>> print otherData [1, -2, 3, 4]

>>> print data [1, -2, 3, 4]

>>>

>>> otherData = data[1:3]

>>> print otherData [-2, 3]

>>> otherData[0] = 0

>>> print otherData [0, 3]

>>> print data Using NumPy arrays:

>>> import numpy as np

>>> data = np.array([1, 2, 3, 4])

>>> print data [1 2 3 4]

>>> otherData = data

>>> otherData[1] = -2

>>> print otherData [1 -2 3 4]

>>> print data [1 -2 3 4]

>>>

>>> otherData = data[1:3]

>>> print otherData [-2 3]

>>> otherData[0] = 0

>>> print otherData [0 3]

>>> print data [1 0 3 4]

Describe similarities and differences between copying and assignment semantics of built-in Python lists and NumPy arrays. Explain why the code behaves differently for the two.

Part B) Matrices NumPy also supports matrices. However, there are some important differences between two-dimensional arrays and matrices. Consider the following two code segments that are similar, but produce different results: Using NumPy two-dimensional arrays:

>>> A = np.array([[1,2], [3,4]])

>>> B = np.array([[2,1], [-1,2]])

>>> A * B array([[ 2, 2], [-3, 8]])

>>> A ** 3 array([[ 1, 8], [27, 64]])

Using NumPy matrices:

>>> A = np.matrix([[1,2], [3,4]])

>>> B = np.matrix([[2,1], [-1,2]])

>>> A * B matrix([[ 0, 5], [ 2, 11]])

>>> A**3 matrix([[ 37, 54], [ 81, 118]])

Describe similarities and differences between NumPy two-dimensional arrays and matrices. Explain why the code behaves differently for the two.

Reference no: EM132197714

Questions Cloud

Write a code to make a calculator which can perform : Write a code in C++ to make a calculator which can perform (Basic Arithmetic Operations, Factorial , addition of N terms,multiplication of N terms)
Describe how you will promote the new business : Identify the target market of a food truck and develop a strategy for appealing to it. Justify your proposed location for this business.
Create stand-alone presentation : Stand-alone Professional Presentation Design a professional presentation. Create a Stand-alone Presentation using either Prezi or PowerPoint
Write code that creates three radio buttons : Write code that creates three radio buttons with the text " Option 1", "Option 2", and "Option 3".
Arrays and copy semantics : Consider the following Python code segment, which uses built-in Python lists and NumPy lists to perform similar operations, albeit with differing results.
What are the possible alternative responses : How could Ms. Rodriquez have better responded to Ms. Tang? What are 2-3 possible alternative responses? Out of the possible alternatives.
What is the average holding cost per year : What is the optimal size of the production run? What is the average holding cost per year?
Compare the value of a variable named age to the number 16 : Write the code for an if-else structure in a console application that compares the value of a variable named age to the number 16.
Which functional dependency causes bcnf violation : List candidate key(s), and how you found the key(s). Which functional dependency causes BCNF violation?

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Energy and linear versus angular impulse and momentum

What is the relationship with the three common types of energy and linear versus angular impulse and momentum. What is the conservation of momentum?

  Performance of the memory access instructions

What will the speedup be if only the performance of the memory access instructions is improved? What will the speedup be if both improvements are made?

  Answer the specific multipart question and label

Answer the specific multipart question and label the part you are answering ie. part a,b,c etc.

  Write an essay on turing machine explain with examples

Write an essay on turing machine explain with examples

  How much will jeff have at month 61

Assuming his investments earn an interest rate of 5%, how much will Jeff have at month 61?

  Assumption on word size

Consider a computer that you have used or now use. (You may make an assumption on word size if needed.)

  Focusing on return on investment

Why is it important to analyze profitability, specifically focusing on return on investment? Invoke the breakdown of ROI in thinking about your response.

  Write an output statement that displays the memory location

Write an output statement that displays the memory location of the value 4 stored in intArray[] in base-16 integer format.

  Identify the three primary areas a project manager

Identify the three primary areas a project manager must focus on to ensure success. Explain which area you feel is most important and why.

  Why might moore''s law come to an end soon

a. Why might Moore's Law come to an end soon? Explain based on currently technologies.

  God grace most clearly depicted or described

Where in the in the New Testament do you see God's grace most clearly depicted or described? Cite some specific examples in scripture.

  Boyer-moore algorithm for string

How are dynamic programming and the Boyer-Moore algorithm for string matching similar?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd