<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Mathematical functions
Step1:  Trigonometric functions
Q1. Calculate sine, cosine, and tangent of x, element-wise.
Step2:  Q2. Calculate inverse sine, inverse cosine, and inverse tangent of x, element-wise.
Step3:  Q3. Convert angles from radians to degrees.
Step4:  Q4. Convert angles from degrees to radians.
Step5:  Hyperbolic functions
Q5. Calculate hyperbolic sine, hyperbolic cosine, and hyperbolic tangent of x, element-wise.
Step6:  Rounding
Q6. Predict the results of these, paying attention to the difference among the family functions.
Step7:  Q7. Implement out5 in the above question using numpy.
Step8:  Sums, products, differences
Q8. Predict the results of these.
Step9:  Q9. Calculate the difference between neighboring elements, element-wise.
Step10:  Q10. Calculate the difference between neighboring elements, element-wise, and
prepend [0, 0] and append[100] to it.
Step11:  Q11. Return the cross product of x and y.
Step12:  Exponents and logarithms
Q12. Compute $e^x$, element-wise.
Step13:  Q13. Calculate exp(x) - 1 for all elements in x.
Step14:  Q14. Calculate $2^p$ for all p in x.
Step15:  Q15. Compute natural, base 10, and base 2 logarithms of x element-wise.
Step16:  Q16. Compute the natural logarithm of one plus each element in x in floating-point accuracy.
Step17:  Floating point routines
Q17. Return element-wise True where signbit is set.
Step18:  Q18. Change the sign of x to that of y, element-wise.
Step19:  Arithmetic operations
Q19. Add x and y element-wise.
Step20:  Q20. Subtract y from x element-wise.
Step21:  Q21. Multiply x by y element-wise.
Step22:  Q22. Divide x by y element-wise in two different ways.
Step23:  Q23. Compute numerical negative value of x, element-wise.
Step24:  Q24. Compute the reciprocal of x, element-wise.
Step25:  Q25. Compute $x^y$, element-wise.
Step26:  Q26. Compute the remainder of x / y element-wise in two different ways.
Step27:  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.
Step28:  Q28. Compute the square of x, element-wise.
Step29:  Q29. Compute square root of x element-wise.
Step30:  Q30. Compute the absolute value of x.
Step31:  Q31. Compute an element-wise indication of the sign of x, element-wise.
<ASSISTANT_TASK:>
Python Code:
import numpy as np
np.__version__
__author__ = "kyubyong. kbpark.linguist@gmail.com. https://github.com/kyubyong"
Explanation: Mathematical functions
End of explanation
x = np.array([0., 1., 30, 90])
print "sine:", np.sin(x)
print "cosine:", np.cos(x)
print "tangent:", np.tan(x)
Explanation: Trigonometric functions
Q1. Calculate sine, cosine, and tangent of x, element-wise.
End of explanation
x = np.array([-1., 0, 1.])
print "inverse sine:", np.arcsin(x2)
print "inverse cosine:", np.arccos(x2)
print "inverse tangent:", np.arctan(x2)
Explanation: Q2. Calculate inverse sine, inverse cosine, and inverse tangent of x, element-wise.
End of explanation
x = np.array([-np.pi, -np.pi/2, np.pi/2, np.pi])
out1 = np.degrees(x)
out2 = np.rad2deg(x)
assert np.array_equiv(out1, out2)
print out1
Explanation: Q3. Convert angles from radians to degrees.
End of explanation
x = np.array([-180.,  -90.,   90.,  180.])
out1 = np.radians(x)
out2 = np.deg2rad(x)
assert np.array_equiv(out1, out2)
print out1
Explanation: Q4. Convert angles from degrees to radians.
End of explanation
x = np.array([-1., 0, 1.])
print np.sinh(x)
print np.cosh(x)
print np.tanh(x)
Explanation: Hyperbolic functions
Q5. Calculate hyperbolic sine, hyperbolic cosine, and hyperbolic tangent of x, element-wise.
End of explanation
x = np.array([2.1, 1.5, 2.5, 2.9, -2.1, -2.5, -2.9])
out1 = np.around(x)
out2 = np.floor(x)
out3 = np.ceil(x)
out4 = np.trunc(x)
out5 = [round(elem) for elem in x]
print out1
print out2
print out3
print out4
print out5
Explanation: Rounding
Q6. Predict the results of these, paying attention to the difference among the family functions.
End of explanation
print np.floor(np.abs(x) + 0.5) * np.sign(x) 
# Read http://numpy-discussion.10968.n7.nabble.com/why-numpy-round-get-a-different-result-from-python-round-function-td19098.html
Explanation: Q7. Implement out5 in the above question using numpy.
End of explanation
x = np.array(
    [[1, 2, 3, 4],
     [5, 6, 7, 8]])
outs = [np.sum(x),
        np.sum(x, axis=0),
        np.sum(x, axis=1, keepdims=True),
        "",
        np.prod(x),
        np.prod(x, axis=0),
        np.prod(x, axis=1, keepdims=True),
        "",
        np.cumsum(x),
        np.cumsum(x, axis=0),
        np.cumsum(x, axis=1),
        "",
        np.cumprod(x),
        np.cumprod(x, axis=0),
        np.cumprod(x, axis=1),
        "",
        np.min(x),
        np.min(x, axis=0),
        np.min(x, axis=1, keepdims=True),
        "",
        np.max(x),
        np.max(x, axis=0),
        np.max(x, axis=1, keepdims=True),
        "",
        np.mean(x),
        np.mean(x, axis=0),
        np.mean(x, axis=1, keepdims=True)]
           
for out in outs:
    if out == "":
        print
    else:
        print("->", out)
Explanation: Sums, products, differences
Q8. Predict the results of these.
End of explanation
x = np.array([1, 2, 4, 7, 0])
print np.diff(x)
Explanation: Q9. Calculate the difference between neighboring elements, element-wise.
End of explanation
x = np.array([1, 2, 4, 7, 0])
out1 = np.ediff1d(x, to_begin=[0, 0], to_end=[100])
out2 = np.insert(np.append(np.diff(x), 100), 0, [0, 0])
assert np.array_equiv(out1, out2)
print out2
Explanation: Q10. Calculate the difference between neighboring elements, element-wise, and
prepend [0, 0] and append[100] to it.
End of explanation
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
print np.cross(x, y)
Explanation: Q11. Return the cross product of x and y.
End of explanation
x = np.array([1., 2., 3.], np.float32)
out = np.exp(x)
print out
Explanation: Exponents and logarithms
Q12. Compute $e^x$, element-wise.
End of explanation
x = np.array([1., 2., 3.], np.float32)
out1 = np.expm1(x)
out2 = np.exp(x) - 1.
assert np.allclose(out1, out2)
print out1
Explanation: Q13. Calculate exp(x) - 1 for all elements in x.
End of explanation
x = np.array([1., 2., 3.], np.float32)
out1 = np.exp2(x)
out2 = 2 ** x
assert np.allclose(out1, out2)
print out1
Explanation: Q14. Calculate $2^p$ for all p in x.
End of explanation
x = np.array([1, np.e, np.e**2])
print "natural log =", np.log(x)
print "common log =", np.log10(x)
print "base 2 log =", np.log2(x)
Explanation: Q15. Compute natural, base 10, and base 2 logarithms of x element-wise.
End of explanation
x = np.array([1e-99, 1e-100])
print np.log1p(x)
# Compare it with np.log(1 +x)
Explanation: Q16. Compute the natural logarithm of one plus each element in x in floating-point accuracy.
End of explanation
x = np.array([-3, -2, -1, 0, 1, 2, 3])
out1 = np.signbit(x)
out2 = x < 0
assert np.array_equiv(out1, out2)
print out1
Explanation: Floating point routines
Q17. Return element-wise True where signbit is set.
End of explanation
x = np.array([-1, 0, 1])
y = -1.1
print np.copysign(x, y)
Explanation: Q18. Change the sign of x to that of y, element-wise.
End of explanation
x = np.array([1, 2, 3])
y = np.array([-1, -2, -3])
out1 = np.add(x, y)
out2 = x + y
assert np.array_equal(out1, out2)
print out1
Explanation: Arithmetic operations
Q19. Add x and y element-wise.
End of explanation
x = np.array([3, 4, 5])
y = np.array(3)
out1 = np.subtract(x, y)
out2 = x - y 
assert np.array_equal(out1, out2)
print out1
Explanation: Q20. Subtract y from x element-wise.
End of explanation
x = np.array([3, 4, 5])
y = np.array([1, 0, -1])
out1 = np.multiply(x, y)
out2 = x * y 
assert np.array_equal(out1, out2)
print out1
Explanation: Q21. Multiply x by y element-wise.
End of explanation
x = np.array([3., 4., 5.])
y = np.array([1., 2., 3.])
out1 = np.true_divide(x, y)
out2 = x / y 
assert np.array_equal(out1, out2)
print out1
out3 = np.floor_divide(x, y)
out4 = x // y
assert np.array_equal(out3, out4)
print out3
# Note that in Python 2 and 3, the handling of `divide` differs.
# See https://docs.scipy.org/doc/numpy/reference/generated/numpy.divide.html#numpy.divide
Explanation: Q22. Divide x by y element-wise in two different ways.
End of explanation
x = np.array([1, -1])
out1 = np.negative(x)
out2 = -x
assert np.array_equal(out1, out2)
print out1
Explanation: Q23. Compute numerical negative value of x, element-wise.
End of explanation
x = np.array([1., 2., .2])
out1 = np.reciprocal(x)
out2 = 1/x
assert np.array_equal(out1, out2)
print out1
Explanation: Q24. Compute the reciprocal of x, element-wise.
End of explanation
x = np.array([[1, 2], [3, 4]])
y = np.array([[1, 2], [1, 2]])
out = np.power(x, y)
print out
Explanation: Q25. Compute $x^y$, element-wise.
End of explanation
x = np.array([-3, -2, -1, 1, 2, 3])
y = 2
out1 = np.mod(x, y)
out2 = x % y
assert np.array_equal(out1, out2)
print out1
out3 = np.fmod(x, y)
print out3
Explanation: Q26. Compute the remainder of x / y element-wise in two different ways.
End of explanation
x = np.arange(10)
out1 = np.clip(x, 3, 7)
out2 = np.copy(x)
out2[out2 < 3] = 3
out2[out2 > 7] = 7
assert np.array_equiv(out1, out2)
print out1
Explanation: 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.
End of explanation
x = np.array([1, 2, -1])
out1 = np.square(x)
out2 = x * x
assert np.array_equal(out1, out2)
print out1
Explanation: Q28. Compute the square of x, element-wise.
End of explanation
x = np.array([1., 4., 9.])
out = np.sqrt(x)
print out
Explanation: Q29. Compute square root of x element-wise.
End of explanation
x = np.array([[1, -1], [3, -3]])
out = np.abs(x)
print out
Explanation: Q30. Compute the absolute value of x.
End of explanation
x = np.array([1, 3, 0, -1, -3])
out1 = np.sign(x)
out2 = np.copy(x)
out2[out2 > 0] = 1
out2[out2 < 0] = -1
assert np.array_equal(out1, out2)
print out1
Explanation: Q31. Compute an element-wise indication of the sign of x, element-wise.
End of explanation
<END_TASK>