I am meant to Rewrite the following functions so that they can also be called with arrays of values. If they are called

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

I am meant to Rewrite the following functions so that they can also be called with arrays of values. If they are called

Post by answerhappygod »

I am meant to Rewrite the following functions so that they
can also be called with arrays of values. If they are called with
an array, then they should return an array containing the relevant
value for each value in the input array, e.g. the sin value for
each value in the input array.
""" a module that contains functions
for plotting the path of the sun over
the course of a day or a year"""
import math
import numpy as np
def cos_d(degrees):
""" To save us converting to radians all the time
"""
return math.cos(math.radians(degrees))
def sin_d(degrees):
""" To save us converting to radians all the time
"""
return math.sin(math.radians(degrees))
def asin_d(n):
""" Returns the degrees such that sin(degrees) ==
n
This is also know as the inverse sin function or
arcsin.
The stanard asin function returns the value in
radians.
This function converts the result to degrees
"""
return math.degrees(math.asin(n))
I Am Meant To Rewrite The Following Functions So That They Can Also Be Called With Arrays Of Values If They Are Called 1
I Am Meant To Rewrite The Following Functions So That They Can Also Be Called With Arrays Of Values If They Are Called 1 (82.48 KiB) Viewed 44 times
I am confused on how to create the array version for the
functions
Rewrite the following functions so that they can also be called with arrays of values. If they are called with an array, then they should return an array containing the relevant value for each value in the input array, e.g. the sin value for each value in the input array. See the examples below. import math def cos_d(degrees): """ To save us converting to radians all the time return math.cos(math.radians(degrees)) def sin_d(degrees): """ To save us converting to radians all the time" return math.sin(math.radians(degrees)) def asin_d(n): " Returns the degrees such that sin(degrees) == n This is also know as the inverse sin function or arcsin. The stanard asin function returns the value in radians. This function converts the result to degrees return math.degrees(math.asin(n)) Notes: You must import numpy in the standard way before defining your functions. That is: import numpy as np Your functions must still all be only 1 line long (excluding the docstrings). o This is probably easier than you think. :) • You're building a module/program, so don't forget your module docstring. For yamnle

For example: Test Result [1. 0.87 0.5 0. ] [0. 0.5 0.87 1. ] values = np.array( [0, 30, 60, 90]) cozes = cos_d(values) np.set_printoptions(precision=2, suppress=True) print(cozes) values = np.array([0, 30, 60, 90]) sines= sin_d(values) np.set_printoptions(precision=2, suppress=True) print(sines) values = np.array([0, 0.5, 0.86603, 1]) asines= asin_d(values) np.set_printoptions(precision=2, suppress=True) print(asines) # calling with single values # should still work coz = cos_d(60) print(f'{coz:.2f}') [ 0. 30. 60. 90.] 0.50 Answer: inenalty recimen 5 10 15 20 25 20 an 021
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply