/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Problem 8 Suppose arrays a and b are defin... [FREE SOLUTION] | 91Ó°ÊÓ

91Ó°ÊÓ

Suppose arrays a and b are defined as follows: $$ \begin{aligned} &\text { from numpy import array } \\ &a=\operatorname{array}([1,2,3,4], \text { int }) \\ &b=\operatorname{array}([2,4,6,8], \text { int }) \end{aligned} $$ What will the computer print upon executing the following lines? (Try to work out the answer before trying it on the computer.) a) print \((b / a+1)\) b) print \((b /(a+1))\) c) print(1/a)

Short Answer

Expert verified
a) [3, 3, 3, 3]b) [1.0, 1.33333333, 1.5, 1.6]c) [1.0, 0.5, 0.33333333, 0.25]

Step by step solution

01

Import and Define Arrays

'import array' module and define arrays 'a' and 'b'. \(\text{from numpy import array}\) \(a=\text{array}([1,2,3,4], \text{int})\) \(b=\text{array}([2,4,6,8], \text{int})\).
02

Solve for Part a

Calculate the expression \(\frac{b}{a} + 1\) for each element:\(b = [2,4,6,8]\) \(a = [1,2,3,4]\) \(\frac{b}{a} = [2/1, 4/2, 6/3, 8/4]\) \(\frac{b}{a} = [2, 2, 2, 2]\).Now add 1 to each element: \([2+1, 2+1, 2+1, 2+1]\) which gives \([3, 3, 3, 3]\) Result: \(\text{[3, 3, 3, 3]}\).
03

Solve for Part b

Calculate the expression \(\frac{b}{(a+1)}\) for each element: \(b = [2, 4, 6, 8]\) \(a = [1, 2, 3, 4]\) Now, add 1 to each element of array a: \(a+1 = [2, 3, 4, 5]\).Divide b by (a+1) element-wise: \(\frac{b}{a+1} = [2/2, 4/3, 6/4, 8/5]\) which gives: \([1.0, 1.33333333, 1.5, 1.6]\) Result: \(\text{[1.0, 1.33333333, 1.5, 1.6]}\).
04

Solve for Part c

Calculate the expression \(\frac{1}{a}\) for each element: \(a = [1, 2, 3, 4]\) \(\frac{1}{a} = [1/1, 1/2, 1/3, 1/4]\) which gives: \([1.0, 0.5, 0.33333333, 0.25]\) Result: \(\text{[1.0, 0.5, 0.33333333, 0.25]}\).

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with 91Ó°ÊÓ!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

numpy arrays
Numpy arrays are a fundamental concept in computational physics and numerical computations. They provide an efficient way to store large data sets and perform many mathematical operations. In Python, these arrays are built using the numpy library, which must be imported with the command:
from numpy import array.

Using numpy arrays, we can create multi-dimensional data structures that support various operations like addition, subtraction, division, and more, directly on the data sets.
For example, consider the arrays in our exercise:
a = array([1, 2, 3, 4], int)
b = array([2, 4, 6, 8], int).
Here, a and b are one-dimensional numpy arrays containing integer values. These arrays are more efficient than Python lists for numerical computations, especially when working with large datasets.
element-wise operations
One of the most powerful features of numpy arrays is their ability to perform element-wise operations. This means that operations are applied to each element independently and simultaneously.

Let's explore this with our example arrays.
For part (a) of the original exercise, we compute:
b / a + 1.
Here, the division is performed element-wise:
array([2/1, 4/2, 6/3, 8/4]) = array([2, 2, 2, 2]).
Adding 1 to each element also happens element-wise, resulting in:
array([2+1, 2+1, 2+1, 2+1]) = array([3, 3, 3, 3]).

The same methodology applies to part (b). Adding 1 to elements of array a and then dividing array b by the modified a elements:
array([2/2, 4/3, 6/4, 8/5]) = array([1.0, 1.33333333, 1.5, 1.6]).
Part (c) involves dividing 1 by each element in array a:
array([1/1, 1/2, 1/3, 1/4]) = array([1.0, 0.5, 0.33333333, 0.25]).

These element-wise operations make numpy arrays suitable for complex numerical computations, maintaining efficiency and clarity.
numerical computations
Numerical computations are at the heart of solving scientific and engineering problems. Using numpy arrays, these computations are made simpler and more efficient.

The key benefit of numpy in numerical computations lies in its optimized C code for array operations, which ensures high performance.
For example, in our exercise, the operations:
b / a + 1,
b / (a + 1), and
1 / a
were computed rapidly due to numpy's efficient manipulation of array data.

Additionally, numpy offers a suite of functions for more advanced numerical computations including:
  • Linear algebra operations such as matrix multiplication and inversions.
  • Statistical functions like mean, median, and standard deviation.
  • Integration with other scientific libraries like SciPy for even broader functionality.

This extensive functionality makes numpy an essential tool for any computational physics task, enabling accurate and efficient problem solving.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Quantum potential step A well-known quantum mechanics problem involves a particle of mass \(m\) that encounters a one-dimensional potential step, like this: The particle with initial kinetic energy \(E\) and wavevector \(k_{1}=\sqrt{2 m E} / \hbar\) enters from the left and encounters a sudden jump in potential energy of height \(V\) at position \(x=0\). By solving the Schrödinger equation, one can show that when \(E>V\) the particle may either (a) pass the step, in which case it has a lower kinetic energy of \(E-V\) on the other side and a correspondingly smaller wavevector of \(k_{2}=\sqrt{2 m(E-V)} / \hbar\), or \((b)\) it may be reflected, keeping all of its kinetic energy and an unchanged wavevector but moving in the opposite direction. The probabilities \(T\) and \(R\) for transmission and reflection are given by $$ T=\frac{4 k_{1} k_{2}}{\left(k_{1}+k_{2}\right)^{2}}, \quad R=\left(\frac{k_{1}-k_{2}}{k_{1}+k_{2}}\right)^{2} . $$ Suppose we have a particle with mass equal to the electron mass \(m=9.11 \times\) \(10^{-31} \mathrm{~kg}\) and energy \(10 \mathrm{eV}\) encountering a potential step of height \(9 \mathrm{eV}\). Write a Python program to compute and print out the transmission and reflection probabilities using the formulas above.

Binomial coefficients The binomial coefficient \(\left(\begin{array}{l}n \\ k\end{array}\right)\) is an integer equal to $$ \left(\begin{array}{l} n \\ k \end{array}\right)=\frac{n !}{k !(n-k) !}=\frac{n \times(n-1) \times(n-2) \times \ldots \times(n-k+1)}{1 \times 2 \times \ldots \times k} $$ when \(k \geq 1\), or \(\left(\begin{array}{l}n \\ 0\end{array}\right)=1\) when \(k=0\). a) Using this form for the binomial coefficient, write a Python user-defined function binomial \((\mathrm{n}, \mathrm{k})\) that calculates the binomial coefficient for given \(n\) and \(k\). Make sure your function returns the answer in the form of an integer (not a float) and gives the correct value of 1 for the case where \(k=0\). b) Using your function write a program to print out the first 20 lines of "Pascal's triangle." The \(n\)th line of Pascal's triangle contains \(n+1\) numbers, which are the coefficients \(\left(\begin{array}{l}n \\\ 0\end{array}\right),\left(\begin{array}{l}n \\ 1\end{array}\right)\), and so on up to \(\left(\begin{array}{l}n \\ n\end{array}\right)\). Thus the first few lines are $$ \begin{aligned} &11 \\ &121 \\ &1331 \\ &14641 \end{aligned} $$ c) The probability that an unbiased coin, tossed \(n\) times, will come up heads \(k\) times is \(\left(\begin{array}{l}n \\ k\end{array}\right) / 2^{n}\). Write a program to calculate (a) the total probability that a coin tossed 100 times comes up heads exactly 60 times, and (b) the probability that it comes up heads 60 or more times.

The Madelung constant In condensed matter physics the Madelung constant gives the total electric potential felt by an atom in a solid. It depends on the charges on the other atoms nearby and their locations. Consider for instance solid sodium chloride- table salt. The sodium chloride crystal has atoms arranged on a cubic lattice, but with alternating sodium and chlorine atoms, the sodium ones having a single positive charge \(+e\) and the chlorine ones a single negative charge \(-e\), where \(e\) is the charge on the electron. If we label each position on the lattice by three integer coordinates \((i, j, k)\), then the sodium atoms fall at positions where \(i+j+k\) is even, and the chlorine atoms at positions where \(i+j+k\) is odd. Consider a sodium atom at the origin, \(i=j=k=0\), and let us calculate the Madelung constant. If the spacing of atoms on the lattice is \(a\), then the distance from the origin to the atom at position \((i, j, k)\) is $$ \sqrt{(i a)^{2}+(j a)^{2}+(k a)^{2}}=a \sqrt{i^{2}+j^{2}+k^{2}} $$ and the potential at the origin created by such an atom is $$ V(i, j, k)=\pm \frac{e}{4 \pi e_{0} a \sqrt{i^{2}+j^{2}+k^{2}}}, $$ with \(e_{0}\) being the permittivity of the vacuum and the sign of the expression depending on whether \(i+j+k\) is even or odd. The total potential felt by the sodium atom is then the sum of this quantity over all other atoms. Let us assume a cubic box around the sodium at the origin, with \(L\) atoms in all directions. Then $$ V_{\text {beal }}=\sum_{n, j, i=-t \atop n=0}^{L} V(i, j, k)=\frac{e}{4 \pi e_{0} a} M \text {, } $$ where \(M\) is the Madelung constant, at least approximately-technically the Madelung constant is the value of \(M\) when \(L \rightarrow \infty\), but one can get a good approximation just by using a large value of \(L\). Write a program to calculate and print the Madelung constant for sodium chloride. Use as large a value of \(L\) as you can, while still having your program run in reasonable time-say in a minute or less.

Altitude of a satellite A satellite is to be launched into a circular orbit around the Earth so that it orbits the planet once every \(T\) seconds. a) Show that the altitude \(h\) above the Earth's surface that the satellite must have is $$ h=\left(\frac{G M T^{2}}{4 \pi^{2}}\right)^{1 / 3}-R $$ where \(G=6.67 \times 10^{-11} \mathrm{~m}^{3} \mathrm{~kg}^{-1} \mathrm{~s}^{-2}\) is Newton's gravitational constant, \(M=\) \(5.97 \times 10^{24} \mathrm{~kg}\) is the mass of the Earth, and \(R=6371 \mathrm{~km}\) is its radius. b) Write a program that asks the user to enter the desired value of \(T\) and then calculates and prints out the correct altitude in meters. c) Use your program to calculate the altitudes of satellites that orbit the Earth once a day (so-called "geosynchronous" orbit), once every 90 minutes, and once every 45 minutes. What do you conclude from the last of these calculations? d) Technically a geosynchronous satellite is one that orbits the Earth once per sidereal day, which is \(23.93\) hours, not 24 hours. Why is this? And how much difference will it make to the altitude of the satellite?

Catalan numbers The Catalan numbers \(C_{n}\) are a sequence of integers \(1,1,2,5,14,42,132 \ldots\) that play an important role in quantum mechanics and the theory of disordered systems. (They were central to Eugene Wigner's proof of the so- called semicircle law.) They are given by $$ C_{0}=1, \quad C_{n+1}=\frac{4 n+2}{n+2} C_{n} . $$ Write a program that prints in increasing order all Catalan numbers less than or equal to one billion.

See all solutions

Recommended explanations on Physics Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.