Course 6.1Lesson 3 of 3
An identity is an equation that is true for every angle. A program cannot try every angle, but it can try thousands at once. np.allclose tests whole arrays within a tolerance, np.max of the absolute difference shows the worst miss, and np.argmax finds a counterexample. One failure proves an identity false, and many passes make it very likely true.
Try the code and the simulations beside the lesson.
An identity is an equation that is true for every angle where both sides make sense. The best known is
It comes from the unit circle. The point is on the circle .
A program cannot try every angle, but it can try thousands in a blink. The result is useful, but know what it means.
Here is a check of on random angles:
np.random.default_rng(5) makes a random number generator. The number is the seed: with the same seed, you get the same angles in every run.rng.uniform(0, 2 * np.pi, 10_000) makes random numbers between and .np.allclose(left, right) is True when every pair of numbers agrees within a tiny tolerance.np.max(np.abs(left - right)) is the worst miss. For a true identity it is about to . That is rounding error, not a mistake.These are the identities of the Class 11 chapter. Write each side as a numpy expression, and compare.
A tempting rule is . It is false, and a program shows it at once.
To find a counterexample in code, mark the pairs that disagree and pick the first:
~ turns True into False and back. bad.sum() counts the failures, since True counts as . np.argmax of a boolean array is the position of the first True.
The false rule is true for a few special angles, for example when . So one lucky angle proves nothing. Always test many random angles.
To test every pair from two lists of angles, make one a column and the other a row. numpy then combines each row with each column:
The shape is by : one test for each of the pairs. This is called broadcasting.
For , both sides become huge when is nearly zero. Then rounding errors are huge too, and np.allclose can fail for a true identity. Test only angles where np.abs(np.cos(a)) > 1e-3.
The program tests three true identities on random pairs of angles. It prints the worst miss of each, then tests the false rule and prints a counterexample.
==. Rounding errors make a true identity fail. Use np.allclose.np.allclose(left, right) tests whole arrays. np.max(np.abs(left - right)) is the worst miss, about for a true identity.rng = np.random.default_rng(seed) and rng.uniform(0, 2 * np.pi, n) make reproducible random angles.np.argmax(bad) of a boolean array is the first True. A column and a row, g[:, None] and g[None, :], test every pair.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
np.allclose(left, right) is False for one angle in a thousand. Is the identity false?np.argmax(bad) return when bad is an array of True and False?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.