Domain and range
Domain and range (lesson 1.1.1)
0 of 6 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Transformations and pieces
Transformations and pieces (lesson 1.1.2)
0 of 7 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Composition and inverses
Composition and inverses (lesson 1.1.3)
0 of 7 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Coding exercise
Functions that build functions
Write two functions. Both return a function, as compose did in lesson 1.1.3.
compose(outer, inner)returns the function .inverse_linear(a, b)returns the inverse of . RaiseValueErrorwhena == 0, because a flat line has no inverse.
The tests call your results with numbers and with NumPy arrays.
See the tests
import numpy as np
def _double_plus_one(x):
return 2 * x + 1
def _square(x):
return x**2
def test_compose_returns_function():
"""compose(f, g) returns something you can call"""
h = compose(_double_plus_one, _square)
assert callable(h), f"expected a function, got {h!r}"
def test_compose_inner_first():
"""compose(f, g)(3) applies g first: f(g(3)) = 19"""
expect_close(compose(_double_plus_one, _square)(3), 19, what="compose(f, g)(3)")
def test_compose_order_matters():
"""compose(g, f)(3) is g(f(3)) = 49"""
expect_close(compose(_square, _double_plus_one)(3), 49, what="compose(g, f)(3)")
def test_compose_arrays():
"""Composition works on a NumPy array"""
x = np.array([0.0, 1.0, 2.0])
expect_close(compose(_double_plus_one, _square)(x), 2 * x**2 + 1, what="compose(f, g) on [0, 1, 2]")
def test_inverse_simple():
"""The inverse of 2x + 3 sends 13 back to 5"""
expect_close(inverse_linear(2, 3)(13), 5.0, what="inverse_linear(2, 3)(13)")
def test_inverse_round_trip():
"""Going there and back returns the input, for f(x) = −0.5x + 4"""
x = np.array([-3.0, 0.0, 1.5, 8.0])
expect_close(inverse_linear(-0.5, 4)(-0.5 * x + 4), x, what="f⁻¹(f(x)) for several x")
def test_flat_line_has_no_inverse():
"""inverse_linear(0, 7) raises ValueError"""
try:
inverse_linear(0, 7)
except ValueError:
return
raise AssertionError("expected ValueError for a == 0")