9 Mathematical Functions Solutions


title: "Mathematical functions" date: 2026-05-24T13:52:09Z

Mathematical functions

1import numpy as np
1np.__version__
'1.11.2'
1__author__ = "kyubyong. kbpark.linguist@gmail.com. https://github.com/kyubyong"

Trigonometric functions

Q1. Calculate sine, cosine, and tangent of x, element-wise.

1x = np.array([0., 1., 30, 90])
2print "sine:", np.sin(x)
3print "cosine:", np.cos(x)
4print "tangent:", np.tan(x)
sine: [ 0.          0.84147098 -0.98803162  0.89399666]
cosine: [ 1.          0.54030231  0.15425145 -0.44807362]
tangent: [ 0.          1.55740772 -6.4053312  -1.99520041]

Q2. Calculate inverse sine, inverse cosine, and inverse tangent of x, element-wise.

1x = np.array([-1., 0, 1.])
2print "inverse sine:", np.arcsin(x2)
3print "inverse cosine:", np.arccos(x2)
4print "inverse tangent:", np.arctan(x2)
inverse sine: [ 1.57079633  0.          1.57079633]
inverse cosine: [ 0.          1.57079633  0.        ]
inverse tangent: [ 0.78539816  0.          0.78539816]

Q3. Convert angles from radians to degrees.

1x = np.array([-np.pi, -np.pi/2, np.pi/2, np.pi])
2
3out1 = np.degrees(x)
4out2 = np.rad2deg(x)
5assert np.array_equiv(out1, out2)
6print out1
[-180.  -90.   90.  180.]

Q4. Convert angles from degrees to radians.

1x = np.array([-180.,  -90.,   90.,  180.])
2
3out1 = np.radians(x)
4out2 = np.deg2rad(x)
5assert np.array_equiv(out1, out2)
6print out1
[-3.14159265 -1.57079633  1.57079633  3.14159265]

Hyperbolic functions

Q5. Calculate hyperbolic sine, hyperbolic cosine, and hyperbolic tangent of x, element-wise.

1x = np.array([-1., 0, 1.])
2print np.sinh(x)
3print np.cosh(x)
4print np.tanh(x)
[-1.17520119  0.          1.17520119]
[ 1.54308063  1.          1.54308063]
[-0.76159416  0.          0.76159416]

Rounding

Q6. Predict the results of these, paying attention to the difference among the family functions.

 1x = np.array([2.1, 1.5, 2.5, 2.9, -2.1, -2.5, -2.9])
 2
 3out1 = np.around(x)
 4out2 = np.floor(x)
 5out3 = np.ceil(x)
 6out4 = np.trunc(x)
 7out5 = [round(elem) for elem in x]
 8
 9print out1
10print out2
11print out3
12print out4
13print out5
[ 2.  2.  2.  3. -2. -2. -3.]
[ 2.  1.  2.  2. -3. -3. -3.]
[ 3.  2.  3.  3. -2. -2. -2.]
[ 2.  1.  2.  2. -2. -2. -2.]
[2.0, 2.0, 3.0, 3.0, -2.0, -3.0, -3.0]

Q7. Implement out5 in the above question using numpy.

1print np.floor(np.abs(x) + 0.5) * np.sign(x) 
2# Read http://numpy-discussion.10968.n7.nabble.com/why-numpy-round-get-a-different-result-from-python-round-function-td19098.html
[ 2.  2.  3.  3. -2. -3. -3.]

Sums, products, differences

Q8. Predict the results of these.

 1x = np.array(
 2    [[1, 2, 3, 4],
 3     [5, 6, 7, 8]])
 4
 5outs = [np.sum(x),
 6        np.sum(x, axis=0),
 7        np.sum(x, axis=1, keepdims=True),
 8        "",
 9        np.prod(x),
10        np.prod(x, axis=0),
11        np.prod(x, axis=1, keepdims=True),
12        "",
13        np.cumsum(x),
14        np.cumsum(x, axis=0),
15        np.cumsum(x, axis=1),
16        "",
17        np.cumprod(x),
18        np.cumprod(x, axis=0),
19        np.cumprod(x, axis=1),
20        "",
21        np.min(x),
22        np.min(x, axis=0),
23        np.min(x, axis=1, keepdims=True),
24        "",
25        np.max(x),
26        np.max(x, axis=0),
27        np.max(x, axis=1, keepdims=True),
28        "",
29        np.mean(x),
30        np.mean(x, axis=0),
31        np.mean(x, axis=1, keepdims=True)]
32           
33for out in outs:
34    if out == "":
35        print
36    else:
37        print("->", out)
('->', 36)
('->', array([ 6,  8, 10, 12]))
('->', array([[10],
       [26]]))

('->', 40320)
('->', array([ 5, 12, 21, 32]))
('->', array([[  24],
       [1680]]))

('->', array([ 1,  3,  6, 10, 15, 21, 28, 36]))
('->', array([[ 1,  2,  3,  4],
       [ 6,  8, 10, 12]]))
('->', array([[ 1,  3,  6, 10],
       [ 5, 11, 18, 26]]))

('->', array([    1,     2,     6,    24,   120,   720,  5040, 40320]))
('->', array([[ 1,  2,  3,  4],
       [ 5, 12, 21, 32]]))
('->', array([[   1,    2,    6,   24],
       [   5,   30,  210, 1680]]))

('->', 1)
('->', array([1, 2, 3, 4]))
('->', array([[1],
       [5]]))

('->', 8)
('->', array([5, 6, 7, 8]))
('->', array([[4],
       [8]]))

('->', 4.5)
('->', array([ 3.,  4.,  5.,  6.]))
('->', array([[ 2.5],
       [ 6.5]]))


/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:34: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison

Q9. Calculate the difference between neighboring elements, element-wise.

1x = np.array([1, 2, 4, 7, 0])
2print np.diff(x)
[ 1  2  3 -7]

Q10. Calculate the difference between neighboring elements, element-wise, and prepend [0, 0] and append[100] to it.

1x = np.array([1, 2, 4, 7, 0])
2
3out1 = np.ediff1d(x, to_begin=[0, 0], to_end=[100])
4out2 = np.insert(np.append(np.diff(x), 100), 0, [0, 0])
5assert np.array_equiv(out1, out2)
6print out2
[  0   0   1   2   3  -7 100]

Q11. Return the cross product of x and y.

1x = np.array([1, 2, 3])
2y = np.array([4, 5, 6])
3
4print np.cross(x, y)
[-3  6 -3]

Exponents and logarithms

Q12. Compute $e^x$, element-wise.

1x = np.array([1., 2., 3.], np.float32)
2
3out = np.exp(x)
4print out
[  2.71828175   7.38905621  20.08553696]

Q13. Calculate exp(x) - 1 for all elements in x.

1x = np.array([1., 2., 3.], np.float32)
2
3out1 = np.expm1(x)
4out2 = np.exp(x) - 1.
5assert np.allclose(out1, out2)
6print out1
[  1.71828175   6.38905621  19.08553696]

Q14. Calculate $2^p$ for all p in x.

1x = np.array([1., 2., 3.], np.float32)
2
3out1 = np.exp2(x)
4out2 = 2 ** x
5assert np.allclose(out1, out2)
6print out1
[ 2.  4.  8.]

Q15. Compute natural, base 10, and base 2 logarithms of x element-wise.

1x = np.array([1, np.e, np.e**2])
2
3print "natural log =", np.log(x)
4print "common log =", np.log10(x)
5print "base 2 log =", np.log2(x)
natural log = [ 0.  1.  2.]
common log = [ 0.          0.43429448  0.86858896]
base 2 log = [ 0.          1.44269504  2.88539008]

Q16. Compute the natural logarithm of one plus each element in x in floating-point accuracy.

1x = np.array([1e-99, 1e-100])
2
3print np.log1p(x)
4# Compare it with np.log(1 +x)
[  1.00000000e-099   1.00000000e-100]

Floating point routines

Q17. Return element-wise True where signbit is set.

1x = np.array([-3, -2, -1, 0, 1, 2, 3])
2
3out1 = np.signbit(x)
4out2 = x < 0
5assert np.array_equiv(out1, out2)
6print out1
[ True  True  True False False False False]

Q18. Change the sign of x to that of y, element-wise.

1x = np.array([-1, 0, 1])
2y = -1.1
3
4print np.copysign(x, y)
[-1. -0. -1.]

Arithmetic operations

Q19. Add x and y element-wise.

1x = np.array([1, 2, 3])
2y = np.array([-1, -2, -3])
3
4out1 = np.add(x, y)
5out2 = x + y
6assert np.array_equal(out1, out2)
7print out1
[0 0 0]

Q20. Subtract y from x element-wise.

1x = np.array([3, 4, 5])
2y = np.array(3)
3
4out1 = np.subtract(x, y)
5out2 = x - y 
6assert np.array_equal(out1, out2)
7print out1
[0 1 2]

Q21. Multiply x by y element-wise.

1x = np.array([3, 4, 5])
2y = np.array([1, 0, -1])
3
4out1 = np.multiply(x, y)
5out2 = x * y 
6assert np.array_equal(out1, out2)
7print out1
[ 3  0 -5]

Q22. Divide x by y element-wise in two different ways.

 1x = np.array([3., 4., 5.])
 2y = np.array([1., 2., 3.])
 3
 4out1 = np.true_divide(x, y)
 5out2 = x / y 
 6assert np.array_equal(out1, out2)
 7print out1
 8
 9out3 = np.floor_divide(x, y)
10out4 = x // y
11assert np.array_equal(out3, out4)
12print out3
13
14# Note that in Python 2 and 3, the handling of `divide` differs.
15# See https://docs.scipy.org/doc/numpy/reference/generated/numpy.divide.html#numpy.divide
[ 3.          2.          1.66666667]
[ 3.  2.  1.]

Q23. Compute numerical negative value of x, element-wise.

1x = np.array([1, -1])
2
3out1 = np.negative(x)
4out2 = -x
5assert np.array_equal(out1, out2)
6print out1
[-1  1]

Q24. Compute the reciprocal of x, element-wise.

1x = np.array([1., 2., .2])
2
3out1 = np.reciprocal(x)
4out2 = 1/x
5assert np.array_equal(out1, out2)
6print out1
[ 1.   0.5  5. ]

Q25. Compute $x^y$, element-wise.

1x = np.array([[1, 2], [3, 4]])
2y = np.array([[1, 2], [1, 2]])
3
4out = np.power(x, y)
5print out
[[ 1  4]
 [ 3 16]]

Q26. Compute the remainder of x / y element-wise in two different ways.

 1x = np.array([-3, -2, -1, 1, 2, 3])
 2y = 2
 3
 4out1 = np.mod(x, y)
 5out2 = x % y
 6assert np.array_equal(out1, out2)
 7print out1
 8
 9out3 = np.fmod(x, y)
10print out3
[1 0 1 1 0 1]
[-1  0 -1  1  0  1]

Miscellaneous

Q27. If an element of x is smaller than 3, replace it with 3. And if an element of x is bigger than 7, replace it with 7.

1x = np.arange(10)
2
3out1 = np.clip(x, 3, 7)
4out2 = np.copy(x)
5out2[out2 < 3] = 3
6out2[out2 > 7] = 7
7
8assert np.array_equiv(out1, out2)
9print out1
[3 3 3 3 4 5 6 7 7 7]

Q28. Compute the square of x, element-wise.

1x = np.array([1, 2, -1])
2
3out1 = np.square(x)
4out2 = x * x
5assert np.array_equal(out1, out2)
6print out1
[1 4 1]

Q29. Compute square root of x element-wise.

1x = np.array([1., 4., 9.])
2
3out = np.sqrt(x)
4print out
[ 1.  2.  3.]

Q30. Compute the absolute value of x.

1x = np.array([[1, -1], [3, -3]])
2
3out = np.abs(x)
4print out
[[1 1]
 [3 3]]

Q31. Compute an element-wise indication of the sign of x, element-wise.

1x = np.array([1, 3, 0, -1, -3])
2
3out1 = np.sign(x)
4out2 = np.copy(x)
5out2[out2 > 0] = 1
6out2[out2 < 0] = -1
7assert np.array_equal(out1, out2)
8print out1
[ 1  1  0 -1 -1]