Course 1.1Lesson 2 of 5
An array works with a number or with another array of the same length place by place, so one formula serves all the values at once. Writing v equal to g times t for an array of times gives an array of speeds, with no loop. Functions of NumPy such as np.sqrt, np.sin, np.exp, np.abs and np.round work on every number of an array, and np.deg2rad changes degrees to radians because the trigonometric functions need radians. The math module takes one number only, so math.sqrt of an array is an error. Arrays of different lengths do not fit, and give a ValueError. A comparison such as temps greater than 32 gives an array of True and False, and sum counts the True values.
Try the code and the simulations beside the lesson.
After this lesson you can
np.sqrt, np.sin, np.exp, np.abs and np.round on arrays,In science, you often have a formula and many values. A ball falls from rest. After a time , its speed is and the distance it has fallen is . For the times 0, 1, 2, 3 and 4 seconds, you would calculate five speeds and five distances. With a list, that is a loop. With an array, you write the formula once.
Predict first. What is the distance fallen after 2 seconds? Then run it.
A number with an array works on every place. A scalar (one number) is stretched to fit. This is a first example of broadcasting, which you meet again in lesson 4.
NumPy has its own versions of the maths functions, and they work on a whole array in one call.
np.sqrt, np.exp (the power of ), np.log, np.abs, and np.round(x, 3) which rounds to 3 decimals.np.sin, np.cos and np.tan need the angle in radians, as in the maths class. To change degrees to radians, use np.deg2rad. Then np.sin(np.deg2rad(30)) is 0.5. (And np.rad2deg goes back.) Forgetting this conversion is the most common mistake in physics programs.math module works on one number only. math.sqrt(16) is fine, and math.sqrt(x) with an array of four numbers is a TypeError. When you work with arrays, use np.sqrt, not math.sqrt. (For one single number, either one is fine.)With a list, this would need a loop or a comprehension. With an array, the formula is written once, like the mathematics.
Use == to compare for equality (two equals signs), as in temps == 28.
math.sqrt or math.sin on an array. Use np.sqrt and np.sin.np.sin(30) is not 0.5. Write np.sin(np.deg2rad(30)).^ for a power. In Python, the power is **. The symbol ^ is something else.and and or between arrays. Use & and |, with brackets. This is explained in the next lesson.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
np.sqrt, np.sin, np.exp) for arrays, and np.deg2rad for angles.True and False, and .sum() counts the True values.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
np.array([1, 2, 3]) ** 2 give?np.sin(np.deg2rad(90))?(np.array([3, 8, 5]) > 4).sum() give?math.sqrt(np.array([1, 4])) an error?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.