NumPy - Mathematics #23
Replies: 22 comments
-
Write a NumPy program to add, subtract, multiply, divide arguments element-wise. import numpy as np
print("Add:")
print(np.add(4.0, 1.0))
print("Subtract:")
print(np.subtract(4.0, 1.0))
print("Multiply:")
print(np.multiply(4.0, 1.0))
print("Divide:")
print(np.divide(4.0, 1.0))`` |
Beta Was this translation helpful? Give feedback.
-
Write a NumPy program to compute logarithm of the sum of exponentiations of the inputs, sum of exponentiations of the inputs in base-2. import numpy as np
l1 = np.log(1e-50)
l2 = np.log(2.5e-50)
print("Logarithm of the sum of exponentiations:")
print(np.logaddexp(l1, l2))
print("Logarithm of the sum of exponentiations of the inputs in base-2:")
print(np.logaddexp2(l1, l2))
|
Beta Was this translation helpful? Give feedback.
-
Write a NumPy program to get the largest integer smaller or equal to the division of the inputs import numpy as np
x = [1., 2., 3., 4.]
print("Original array:")
print(x)
print("Largest integer smaller or equal to the division of the inputs:")
print(np.floor_divide(x, 1.5)) |
Beta Was this translation helpful? Give feedback.
-
Write a NumPy program to get the powers of an array values element-wise. import numpy as np
x = np.arange(7)
print("Original array")
print(x)
print("First array elements raised to powers from second array, element-wise:")
print(np.power(x, 3)) |
Beta Was this translation helpful? Give feedback.
-
Write a NumPy program to get the element-wise remainder of an array of division. import numpy as np
x = np.arange(7)
print("Original array:")
print(x)
print("Element-wise remainder of division:")
print(np.remainder(x, 5)) |
Beta Was this translation helpful? Give feedback.
-
#TASK6
import numpy as np
x = np.array([-10.2, 122.2, .20])
print("Original array:")
print(x)
print("Element-wise absolute value:")
print(np.absolute(x))
#TASK7
import numpy as np
x = np.array([-.7, -1.5, -1.7, 0.3, 1.5, 1.8, 2.0])
print("Original array:")
print(x)
x = np.rint(x)
print("Round elements of the array to the nearest integer:")
print(x) |
Beta Was this translation helpful? Give feedback.
-
Write a NumPy program to get the floor, ceiling and truncated values of the elements of a numpy array. import numpy as np
x = np.array([-1.6, -1.5, -0.3, 0.1, 1.4, 1.8, 2.0])
print("Original array:")
print(x)
print("Floor values of the above array elements:")
print(np.floor(x))
print("Ceil values of the above array elements:")
print(np.ceil(x))
print("Truncated values of the above array elements:")
print(np.trunc(x))
#TASK9
import numpy as np
x = np.random.random((5,3))
print("First array:")
print(x)
y = np.random.random((3,2))
print("Second array:")
print(y)
z = np.dot(x, y)
print("Dot product of two arrays:")
print(z)
|
Beta Was this translation helpful? Give feedback.
-
import numpy as np
a = int(input("Number 1 \n"))
b = int(input("Number 2 \n"))
print(f"Add: \n {np.add(a,b)}")
print(f"Subtract: \n {np.subtract(a,b)}")
print(f"Multiply: \n {np.multiply(a,b)}")
print(f"Divide: \n {np.divide(a,b)}") import numpy as np
a = np.log(1e-50)
b = np.log(2.5e-50)
print(np.logaddexp(a,b))
print(np.logaddexp2(a,b)) import numpy as np
a = [1, 2, 3, 4]
print(f"{np.floor_divide(a, 1.5)}") import numpy as np
a = [1, 2, 3, 4]
print(f"Original \n{a}")
print(f"Array to the 3rd power \n{np.power(a,3)}") import numpy as np
a = [1, 2, 3, 4]
print(f"Original \n{a}")
print(f"Array to the remainder \n{np.remainder(a,3)}") import numpy as np
a = [-1, -2, 3, 4]
print(f"Original \n{a}")
print(f"Array to the remainder \n{np.absolute(a)}") import numpy as np
a = [1.23, -2, 3, 4]
print(np.round(a)) import numpy as np
a = [-1, -2, 3, 4]
print(f"Original \n{a}")
print(f"Array floor \n{np.floor(a)}")
print(f"Array ceiling \n{np.ceil(a)}")
print(f"Array truncated \n{np.trunc(a)}") import numpy as np
a = np.random.random((5, 3))
print(a)
b = np.random.random((3, 5))
print(b)
print(f"multiplication of two\n {np.dot(a,b)}") import numpy as np
a = np.array([1+1j, 3+4j])
print(f"{a}\n")
b = np.array([5+6j, 7+8j])
print(f"{b}\n")
print(np.dot(a,b)) import numpy as np
a = np.arange(24).reshape(2,3,4)
print(f"{a}\n")
b = np.arange(4)
print(f"{b}\n ")
print(f"{np.inner(a,b)}") import numpy as np
a = np.array([1,4,0])
print(f"{a}\n")
b = np.array([2,2,1])
print(f"{b}\n ")
print(f"Inner is \n{np.inner(a,b)}")
print(f"Outer is \n {np.outer(a,b)}")
print(f"Cross product \n {np.cross(a,b)}") import numpy as np
a = np.array([[1,4],[1,0]])
print(f"{a}\n")
b = np.array([[1,2],[4,1]])
print(f"{b}\n ")
print(f"Matrix product is \n{np.matmul(a,b)}") import numpy as np
print("Roots of the first polynomial:")
print(np.roots([1, -2, 1]))
print("Roots of the second polynomial:")
print(np.roots([1, -12, 10, 7, -10])) import numpy as np
a = (10, 20, 30)
b = (30, 40, 50)
print(f"Add one\n {np.polyadd(a,b)}")
print(f"Subtract one\n {np.polysub(a,b)}")
print(f"Multiply\n {np.polymul(a,b)}")
print(f"Divide\n {np.polydiv(a,b)}") import numpy as np
a = np.array([[10, 30], [20, 60]])
print(f"Mean \n{a.mean(axis=0)}")
print(f"Mean accross each row \n {a.mean(axis=1}") import numpy as np
a = np.random.random(1000)
print(f"Average {np.average(a)}")
print(f"Standard deviation {np.std(a)}")
print(f"Variance {np.var(a)}") import numpy as np
print(f"Sine array of angles {np.sin(np.array((0, 30, 45, 60, 90))*np.pi /180)}")
print(f"COS array of angles {np.cos(np.array((0, 30, 45, 60, 90))*np.pi /180)}")
print(f"TAN array of angles {np.tan(np.array((0, 30, 45, 60, 90))*np.pi /180)}") import numpy as np
a = np.array([-np.pi, -np.pi/2, np.pi/2, np.pi])
degrees = np.degrees(a)
radians = np.rad2deg(a)
print(degrees) 20 import numpy as np
a = np.array([-180,-90,90,180])
degrees = np.radians(a)
radians = np.rad2deg(a)
print(degrees) import numpy as np
a = np.array([-1, 0, 1])
print(f"HyperSin {np.sinh(a)}")
print(f"HyperCos {np.cosh(a)}")
print(f"HyperTan {np.tanh(a)}") import numpy as np
a = np.array([3.1, 3.5, 4.5, 2.9, -3.1, -3.5, -5.9])
print(f"Round {np.around(a)}")
print(f"floor {np.floor(a)}")
print(f"ceil {np.ceil(a)}")
print(f"truc {np.trunc(a)}")
for i in a:
print(np.round(i)) import numpy as np
a = np.array([[1,2,3],[4,5,7]])
print(np.cumsum(a))
print(np.cumsum(a,axis=0))
print(np.cumsum(a,axis=1)) import numpy as np
a = np.array([1,2,3,4,5,7])
print(np.diff(a)) import numpy as np
a = np.array([1,2,3,4,5,7])
print(np.exp(a)) import numpy as np
a = np.array([1,2,3,4,5,7])
print(f"Natural log\n {np.log(a)}")
print(f"Common log\n {np.log10(a)}")
print(f"base 2 log\n {np.log2(a)}") |
Beta Was this translation helpful? Give feedback.
-
1. Add, substract, multiply and divide element-wiseimport numpy as np
def main():
A = np.array([1,2,3,4])
B = np.array([6,5,9,8])
print(f"Original Arrays: \n{A}{B}")
print(f"Add: \n{np.add(A,B)}")
print(f"Subtraction: \n{np.subtract(A,B)}")
print(f"Multiply: \n{np.multiply(A,B)}")
print(f"Divide: \n{np.divide(A,B)}")
main() 2. Logarithm of the sum of exponentiationimport numpy as np
def main():
A = np.array([np.log(1e-50),np.log(2.5e-50)])
print(f"Original Array: \n{A}")
print(f"Logarithm of the sum of exponentiations: {np.log(np.sum(np.exp(A)))}")
print(f"Logarithm of the sum of exponentiations: {np.logaddexp(A[0],A[1])}")
print(f"Logarithm of the sum of exponentiations of the inputs in base-2: {np.log2(np.sum(2**(A)))}")
print(f"Logarithm of the sum of exponentiations of the inputs in base-2: {np.logaddexp2(A[0],A[1])}")
main() 3. Largest int smaller or equal to the division of the inputsimport numpy as np
def main():
A = np.array([1.0,2.0,3.0,4.0])
print(f"Original Array: \n{A}")
i = 1.5
print(f"Largest int smaller or equal than division of {i}: \n{np.floor_divide(A,i)}")
main() 4. Power element-wiseimport numpy as np
def main():
A = np.array([10,2,3,4])
B = np.array([0,2,4,1])
print(f"Original Arrays: \n{A}{B}")
print(f"A^B element-wise: \n{np.power(A,B)}")
main() 5. Remainderimport numpy as np
def main():
A = np.array([10,4,123,4])
B = np.array([23,2,4,1])
print(f"Original Arrays: \n{A}{B}")
print(f"A%B element-wise: \n{np.mod(A,B)}")
main() 6. Absolute Valueimport numpy as np
def main():
A = np.array([10,-2,3,-4])
# B = np.array([0,2,4,1])
print(f"Original Arrays: \n{A}")
print(f"|A| element-wise: \n{np.abs(A)}")
main() 7. Roundimport numpy as np
def main():
A = np.array([10.2,-2.9,3.5,-4.4])
# B = np.array([0,2,4,1])
print(f"Original Arrays: \n{A}")
print(f"Round element-wise: \n{np.round(A)}")
main() 8. Floor, Ceiling, Truncateimport numpy as np
def main():
A = np.array([10.2,-2.9,3.5,-4.4])
# B = np.array([0,2,4,1])
print(f"Original Arrays: \n{A}")
print(f"Floor element-wise: \n{np.floor(A)}")
print(f"Ceiling element-wise: \n{np.ceil(A)}")
print(f"Truncate element-wise: \n{np.trunc(A)}")
main() 9. Matrix Multiplicationimport numpy as np
def main():
A = np.array([[ 0.44349753, 0.81043761, 0.00771825],
[ 0.64004088 ,0.86774612 ,0.19944667],
[ 0.61520091 ,0.24796788 ,0.93798297],
[ 0.22156999 ,0.61318856 ,0.82348994],
[ 0.91324026 ,0.13411297, 0.00622696]])
B = np.array([[ 0.73873542 ,0.06448186],
[ 0.90974982 ,0.06409165],
[ 0.22321268 ,0.39147412]])
print(f"Original Arrays: \n{A}\t{B}")
print(f"Dot product: \n{np.dot(A,B)}")
main() 10. Product of complex matriximport numpy as np
def main():
A = np.array([1+2j,3+4j])
B = np.array([5+6j,7+8j])
print(f"Original Arrays: \n{A}\t{B}")
# complex dot product vdot(a,b)
print(f"Product: \n{np.vdot(A,B)}")
main() 11. Inner productimport numpy as np
def main():
A = np.array([[[ 0, 1, 2, 3],
[ 4 ,5, 6, 7],
[ 8 ,9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
B = np.array([0 ,1 ,2 ,3])
print(f"Original Arrays: \n{A}\n{B}")
print(f"Inner product: \n{np.inner(A,B)}")
main() 12, Inner, Outer and Cross productsimport numpy as np
def main():
A = np.array([1,4,0])
B = np.array([2,2,1])
print(f"Original Arrays: \n{A}\n{B}")
print(f"Inner product: \n{np.inner(A,B)}")
print(f"Outer product: \n{np.outer(A,B)}")
print(f"Cross product: \n{np.cross(A,B)}")
main() 13. Matrix productimport numpy as np
def main():
A = np.array([[1,0],[1,1]])
B = np.array([[3,1],[2,2]])
print(f"Original Arrays: \n{A}\n{B}")
print(f"Matrix product: \n{np.dot(A,B)}")
main() 14. Roots of polynomialsimport numpy as np
def main():
print("a) x^2 - 2x + 1")
print("b) x^4 - 12x^3 + 10x^2 + 7^x - 10")
A = np.array([1,-2,1])
B = np.array([1,-12,10,7,-10])
print(f"Original Arrays: \n{A}\n{B}")
print(f"Roots of a: \n{np.roots(A)}")
print(f"Roots of b: \n{np.roots(B)}")
main() 15. Polynomial operationsimport numpy as np
def main():
A = np.array([10,20,30])
B = np.array([30,40,50])
print(f"Original polinomials: \n{A}\n{B}")
print(f"Add of Polynomials: \n{np.polyadd(A,B)}")
print(f"Subtract of Polynomials: \n{np.polysub(A,B)}")
print(f"Multiply of Polynomials: \n{np.polymul(A,B)}")
print(f"Divide of Polynomials: \n{np.polydiv(A,B)}")
main() 16. Mean across axisimport numpy as np
def main():
A = np.array([[10,30],[20,60]])
# B = np.array([30,40,50])
print(f"Original array: \n{A}")
print(f"Mean of each column: \n{np.mean(A,axis=0)}")
print(f"Mean of each row: \n{np.mean(A,axis=1)}")
main() 17. Average, variance, standard deviationimport numpy as np
def main():
A = np.random.rand(1000)
# B = np.array([30,40,50])
# print(f"Original array: \n{A}")
print(f"Average: \n{np.average(A)}")
print(f"Standard Deviation: \n{np.std(A)}")
print(f"Variance: \n{np.nanvar(A)}")
main() 18. sin, cos, tan
19. Convert rad to degimport numpy as np
def main():
A = np.array([-np.pi,-np.pi/2,np.pi/2,np.pi])
# B = np.array([30,40,50])
print(f"Original array: \n{A}")
print(f"Radians to Degrees: \n{np.rad2deg(A)}")
main() 20. Convert deg to radimport numpy as np
def main():
A = np.array([-180,-90,0,90,180,])
# B = np.array([30,40,50])
print(f"Original array: \n{A}")
print(f"Degrees to Radians: \n{np.deg2rad(A)}")
main() 21. sinh, cosh, tanhimport numpy as np
def main():
A = np.array([-1,0,1])
# B = np.array([30,40,50])
print(f"Original array: \n{A}")
print(f"sinh(A): \n{np.sinh(A)}")
print(f"cosh(A): \n{np.cosh(A)}")
print(f"tanh(A): \n{np.tanh(A)}")
main() 22. Round, floor, ceiling, truncate and round with decimalsimport numpy as np
def main():
A = np.array([ 3.1123 ,3.5234, 4.5123, 2.9123 ,-3.1123 ,-3.5123 ,-5.9213])
# B = np.array([30,40,50])
print(f"Original array: \n{A}")
print(f"Round: \n{np.round(A)}")
print(f"Floor: \n{np.floor(A)}")
print(f"Ceiling: \n{np.ceil(A)}")
print(f"Truncate: \n{np.trunc(A)}")
print(f"Round with 2 decimals: \n{np.round(A,2)}")
main() 23. Cumulative sum along axisimport numpy as np
def main():
A = np.array([[1,2,3],[4,5,6]])
# B = np.array([30,40,50])
print(f"Original array: \n{A}")
print(f"Cumulative sum: \n{np.cumsum(A)}")
print(f"Sum over columns: \n{np.cumsum(A,axis=0)}")
print(f"Sum over rows: \n{np.cumsum(A,axis=1)}")
main() 24. Difference between neighboring elementsimport numpy as np
def main():
A = np.array([1,3,5,7,0])
# B = np.array([30,40,50])
print(f"Original array: \n{A}")
print(f"Difference between neighboring elements: \n{np.diff(A)}")
main() 25. e^ximport numpy as np
def main():
A = np.array([1,3,5,7,0])
# B = np.array([30,40,50])
print(f"Original array: \n{A}")
print(f"e^x element-wise: \n{np.exp(A)}")
main() 26. Log - natural, base10, base 2import numpy as np
def main():
A = np.array([0,1,np.e,np.exp(2)])
# B = np.array([30,40,50])
print(f"Original array: \n{A}")
print(f"Natural log: \n{np.log(A)}")
print(f"Common log (base 10): \n{np.log10(A)}")
print(f"Base 2 log: \n{np.log2(A)}")
main() |
Beta Was this translation helpful? Give feedback.
-
TASK10 import numpy as np
x = np.array([1+2j,3+4j])
print("First array:")
print(x)
y = np.array([5+6j,7+8j])
print("Second array:")
print(y)
z = np.vdot(x, y)
print("Product of above two arrays:")
print(z)
TASK11
import numpy as np
x = np.arange(24).reshape((2,3,4))
print("Array x:")
print(x)
print("Array y:")
y = np.arange(4)
print(y)
print("Inner of x and y arrays:")
print(np.inner(x, y)) |
Beta Was this translation helpful? Give feedback.
-
1.- import numpy as np
a = np.array(np.random.randint(10, size=(3,3)))
print("Original array")
print(a)
b = np.random.randint(10)
print(f"Adding {b} to array")
print(a + b)
print(f"Substracting {b} to array")
print(a - b)
print(f"Multiply {b} to array")
print(a * b)
print(f"Diving {b} to array")
print(a / b) 2.- import numpy as np
num = int(input("Enter a number:"))
print("Logarithm of the sum of exponentiations base 2")
print(np.log2(num)) 3.- import numpy as np
a = np.random.randint(0, 10, size = 4)
print(a)
divider = int(input("Digit a number:"))
print(f"Largest integer smaller or equal to the division of {divider}")
print(np.floor_divide(a, 2)) 4.- import numpy as np
a = np.random.randint(0, 10, size = 7)
pow = int(input("Enter power"))
pow_array = np.full(7, pow)
print("Original array:")
print(a)
print(f"Array to the power of {pow}")
print(a**pow_array) 5.- import numpy as np
a = np.random.randint(0, 10, size = 7)
divider = int(input("Enter divider"))
pow_array = np.full(7, divider)
print("Original array:")
print(a)
print(f"Remainder of Array / {divider}")
print(a%pow_array) 6.- import numpy as np
a = np.random.randint(-10,0, size = 10)
print("Original array:")
print(a)
print("Element-wise absolute value:")
print(np.absolute(a)) 7.- import numpy as np
a = np.random.uniform(0, 10, size =10)
print("Original array")
print(a)
print("Round element of the array to the nearest integer")
print(np.round(a)) 8.- a = np.random.uniform(low = 0, high = 10, size = 10)
print("Original array")
print(a)
print("Floor element of the array to the floor integer")
print(np.floor(a))
print("Ceil element of the array to the ceiling integer")
print(np.ceil(a))
print("Truncate element of the array to the truncated integer")
print(np.trunc(a)) 9.- a = np.random.randint(0, 10, size=(5,3))
b = np.random.randint(0, 10, size=(3,2))
print("First array")
print(a)
print("Second array")
print(b)
print("Dot product of two arrays")
print(np.dot(a, b)) 10.- a = np.array([1+2j, 3+4j])
b = np.array([5+6j, 7+8j])
print("First array")
print(a)
print("Second array")
print(b)
print("Product of above matrix")
print(np.vdot(a, b)) 11.- a = np.random.randint(0, 10, size=(3,3))
b = np.random.randint(0, 10, size=(3,3))
print("First array")
print(a)
print("Second array")
print(b)
print("inner product of two arrays")
print(np.inner(a, b)) 12.- a = np.random.randint(0, 10, size=3)
b = np.random.randint(0, 10, size=3)
print("First array")
print(a)
print("Second array")
print(b)
print("inner product of two arrays")
print(np.inner(a, b))
print("Outer product of two arrays")
print(np.outer(a, b))
print("Cross product of two arrays")
print(np.cross(a, b)) 13.- a = np.random.randint(0, 10, size=(2,2))
b = np.random.randint(0, 10, size=(2,2))
print("First array")
print(a)
print("Second array")
print(b)
print("Matrix product of first and second array")
print(np.matmul(a, b)) 14.- a = np.array([1, -4, 7])
b = np.array([1, -11, 9, 11, 10])
print("First polynomio ",a)
print("Second polynomio", b)
print()
print("Root of the first polynomial")
print(np.roots(a))
print("Root of the second polynomial")
print(np.roots(b)) 15.- a = np.array([0, 1, -4, 7])
b = np.array([1, 0, 9, 11])
print("First polynomial ",a)
print("Second polynomial", b)
print()
print("Add one polynomial to another:")
print(np.polyadd(a, b))
print("Sub one polynomial to another:")
print(np.polysub(a, b))
print("Multiply one polynomial to another:")
print(np.polymul(a, b))
print("divide one polynomial to another:")
print(np.polydiv(a, b)) 16.- a = np.random.randint(0, 10, size=(2,2))
print("Original array")
print(a)
print("Mean of each column")
print(np.mean(a,0))
print("Mean of each row")
print(np.mean(a,1)) 17.- a = np.random.randint(0, 10, size=1000)
print("Average:")
print(np.average(a))
print("Variance:")
print(np.var(a))
print("Standar deviation")
print(np.std(a)) 18.- a = np.random.randint(0, 91, size =5)
a = a * np.pi / 180
print("Sine of the elements of the array")
print(np.sin(a))
print("Cosine of the elements of the array")
print(np.cos(a))
print("Tangent of the elements of the array")
print(np.tan(a)) 19.- a = np.random.uniform(low=0, high=6.28320, size = 5)
print("Radians")
print(a)
a = a * 180 / np.pi
print("Radians to degree")
print(a) 20.- a = np.random.uniform(low=0, high=361, size = 5)
print("Degrees")
print(a)
a = a * np.pi / 180
print("Degrees to radians")
print(a) 21.- a = np.array([-1, 0, 1])
print("Original array: ", a)
print("Hyperbolic sine of the elements of the array")
print(np.sinh(a))
print("Hyperbolic cosine of the elements of the array")
print(np.cosh(a))
print("Hyperbolic tangent of the elements of the array")
print(np.tanh(a)) 22.- a = np.random.uniform(low = 0, high = 10, size = 10)
print("Original array")
print(a)
print()
print("Around element of the array to the floor integer")
print(np.around(a))
print("Floor element of the array to the floor integer")
print(np.floor(a))
print("Ceil element of the array to the ceiling integer")
print(np.ceil(a))
print("Truncate element of the array to the truncated integer")
print(np.trunc(a))
print("Round element of the array to the floor integer")
print(np.round(a)) 23.- a = np.random.randint(low = 0, high = 10, size = (2,2))
print("Original array")
print(a)
print()
print("Acumulative sum in a given axis:")
print(np.cumsum(a))
print("Sum of the rows for each 3 column:")
print(np.cumsum(a, axis=0))
print("Sum of the columns for each 2 rows:")
print(np.cumsum(a, axis=1)) 24.- a = np.random.randint(low = 0, high = 10, size = 5)
print("Original array")
print(a)
print()
print("Diferrences between neighboring elements")
print(np.diff(a)) 25.- a = np.random.randint(low = 0, high = 10, size = 5)
print("Original array")
print(a)
print()
print("e^x, element-wise")
print(np.exp(a)) 26.- a = np.random.randint(low = 0, high = 10, size = 5)
print("Original array")
print(a)
print()
print("Natural log")
print(np.log(a))
print("Base 10 log")
print(np.log10(a))
print("Base 2 log")
print(np.log2(a)) |
Beta Was this translation helpful? Give feedback.
-
Task 1import numpy as np
def calculate(a, b):
print("Addition:\n", np.add(a, b))
print("Subtraction:\n", np.subtract(a, b))
print("Multiplication:\n", np.multiply(a, b))
print("Division:\n", np.divide(a, b))
def main():
x = float(input("Enter first number: "))
y = float(input("Enter second number: "))
calculate(y, x)
if __name__ == '__main__': main() Task 2import numpy as np
def main():
l1 = np.log(1e-50)
l2 = np.log(2.5e-50)
print("Logarithm of the sum of exponents:\n", np.logaddexp(l1, l2))
print("Logarithm of the sum of exponents in base-2:\n", np.logaddexp2(l1, l2))
if __name__ == '__main__': main() Task 3import numpy as np
def main():
x = [1., 2., 3., 4.]
print("Starter Array:\n", x)
print("Largest smaller or equal:\n", np.floor_divide(x, 1.5))
if __name__ == '__main__': main() Task 4import numpy as np
def main():
z = np.arange(7)
print("Starter Array:\n", z)
print("Array power of 3:\n", np.power(z, 3))
if __name__ == '__main__': main() Task 5import numpy as np
def main():
z = np.arange(7)
print("Starter Array:\n", z)
print("Array power of 3:\n", np.remainder(z, 5))
if __name__ == '__main__': main() Task 6import numpy as np
def main():
z = np.array([-10.2, 122.2, .20])
print("Starter Array:\n", z)
print("Absolute Array:\n", np.absolute(z))
if __name__ == '__main__': main() Task 7import numpy as np
def main():
z = np.array([-.7, -1.5, -1.7, 0.3, 1.5, 1.8, 2.0])
print("Starter Array:\n", z)
z = np.rint(z)
print("Absolute Array:\n", z)
if __name__ == '__main__': main() Task 8import numpy as np
def main():
z = np.array([-1.6, -1.5, -0.3, 0.1, 1.4, 1.8, 2.0])
print("Starter Array:\n", z)
print("Floor Array:\n", np.floor(z))
print("Ceiling Array:\n", np.ceil(z))
print("Truncated Array:\n", np.trunc(z))
if __name__ == '__main__': main() Task 9import numpy as np
def main():
x = np.random.random((5, 3))
print("First Array:\n", x)
y = np.random.random((3, 2))
print("Second Array:\n", y)
z = np.dot(x, y)
print("Product Array:\n", z)
if __name__ == '__main__': main() Task 10import numpy as np
def main():
x = np.array([1 + 2j, 3 + 4j])
print("First Array:\n", x)
y = np.array([5 + 6j, 7 + 8j])
print("Second Array:\n", y)
z = np.vdot(x, y)
print("Product Array:\n", z)
if __name__ == '__main__': main() Task 11import numpy as np
def main():
x = np.arange(24).reshape((2, 3, 4))
print("First Array:\n", x)
y = y = np.arange(4)
print("Second Array:\n", y)
z = np.inner(x, y)
print("Product Array:\n", z)
if __name__ == '__main__': main() Task 12import numpy as np
def main():
a = np.array([1, 4, 0], float)
b = np.array([2, 2, 1], float)
print("Matrix and vectors.")
print("X:\n", a)
print("Y:\n", b)
print("Inner P:\n", np.inner(a, b))
print("Outer P:\n", np.outer(a, b))
print("Cross P:\n", np.cross(a, b))
if __name__ == '__main__': main() Task 13import numpy as np
def main():
a = [[1, 0], [1, 1]]
b = [[3, 1], [2, 2]]
print("Matrix and vectors.")
print("X:\n", a)
print("Y:\n", b)
print("Matrix P:\n", np.matmul(a, b))
if __name__ == '__main__': main() Task 14import numpy as np
def main():
print("Root First Plynomial:\n", np.roots([1, -2, 1]))
print("Root Second Plynomial:\n", np.roots([1, -12, 10, 7, -10]))
if __name__ == '__main__': main() Task 15from numpy.polynomial import polynomial as P
def main():
x = (10, 20, 30)
y = (30, 40, 50)
print("Add one poly to another:\n", P.polyadd(x, y))
print("Substract one poly from another:\n", P.polysub(x, y))
print("Multiply one poly by another:\n", P.polymul(x, y))
print("Divide one ply by another:\n", P.polydiv(x, y))
if __name__ == '__main__': main() Task 16import numpy as np
def main():
z = np.array([[10, 30], [20, 60]])
print("First Array:\n", z)
print("Mean of each column:\n", z.mean(axis=0))
print("Mean of each row:\n", z.mean(axis=1))
if __name__ == '__main__': main() Task 17import numpy as np
def main():
x = np.random.randn(1000)
print("Average of the array:\n", x.mean())
print("Standard deviation of the array:\n", x.std())
print("Variance of the array:\n", x.var())
if __name__ == '__main__': main() Task 18import numpy as np
def main():
print("Sine:\n", np.sin(np.array((0., 30., 45., 60., 90.,)) * np.pi / 180.))
print("Cosine:\n", np.cos(np.array((0., 30., 45., 60., 90.,)) * np.pi / 180.))
print("Tangent:\n", np.tan(np.array((0., 30., 45., 60., 90.,)) * np.pi / 180.))
if __name__ == '__main__': main() Task 19import numpy as np
def main():
z = np.array([-np.pi, -np.pi/2, np.pi/2, np.pi])
r1 = np.degrees(z)
r2 = np.rad2deg(z)
assert np.array_equiv(r1, r2)
print("Radians to Degrees:\n", r1)
if __name__ == '__main__': main() Task 20import numpy as np
def main():
z = np.array([-180., -90., 90., 180.])
r1 = np.radians(z)
r2 = np.deg2rad(z)
assert np.array_equiv(r1, r2)
print("Degrees to radians:\n", r1)
if __name__ == '__main__': main() Task 21import numpy as np
def main():
y = np.array([-1., 0, 1.])
print("Hyper Sine:\n", np.sinh(y))
print("Hyper Cosine:\n", np.cosh(y))
print("Hyper Tangent:\n", np.tanh(y))
if __name__ == '__main__': main() Task 22import numpy as np
def main():
a = np.array([3.1, 3.5, 4.5, 2.9, -3.1, -3.5, -5.9])
print("Starter Array:\n", a)
r1 = [round(elem) for elem in a]
print("\naround:", np.around(a))
print("floor: ", np.floor(a))
print("ceil: ", np.ceil(a))
print("trunc: ", np.trunc(a))
print("round: ", r1)
if __name__ == '__main__': main() Task 23import numpy as np
def main():
z = np.array([[1, 2, 3], [4, 5, 6]])
print("Starter Array:\n", z)
print("Cummulative Array:\n", np.cumsum(z))
print("Cummulative Sum Rows:\n", np.cumsum(z, axis=0))
print("Cummulative Sum Columns:\n", np.cumsum(z, axis=1))
if __name__ == '__main__': main() Task 24import numpy as np
def main():
z = np.array([1, 3, 5, 7, 0])
print("Starter Array:\n", z)
print("Different Neighbors:\n", np.diff(z))
if __name__ == '__main__': main() Task 25import numpy as np
def main():
z = np.array([1., 2., 3., 4.], np.float32)
print("Starter Array:\n", z)
print("Element-Wise Array:\n", np.exp(z))
if __name__ == '__main__': main() Task 26import numpy as np
def main():
z = np.array([1, np.e, np.e**2])
print("Starter Array:\n", z)
print("Natural log array:", np.log(z))
print("Common log array:", np.log10(z))
print("Base 2 Log Array:", np.log2(z))
if __name__ == '__main__': main() |
Beta Was this translation helpful? Give feedback.
-
#1. import numpy as np
array1 = np.array([1,2])
array2 = np.array([2,3])
print(np.add(array1,array2))
print("-" * 40)
print(np.subtract(array1,array2))
print("-" * 40)
print(np.multiply(array1,array2))
print("-" * 40)
print(np.divide(array1,array2))
print("-" * 40) #2. import numpy as np
in_num1 = 2
in_num2 = 3
print ("Input number1 : ", in_num1)
print ("Input number2 : ", in_num2)
out_num = np.logaddexp2(in_num1, in_num2)
print ("Output number : ", out_num) #3. import numpy as np
arr = np.array([14,28,56,84])
print("array...\n",arr)
print("\nOur array type...\n",arr.dtype)
print("\nOur array dimension...\n",arr.ndim)
print("\nOur array shape...\n",arr.shape)
print("\nResult...\n",np.floor_divide(arr,3)) #4. import numpy as np
sample_array1 = np.arange(5)
sample_array2 = np.arange(0, 10, 2)
print("Original array ")
print("array1 ", sample_array1)
print("array2 ", sample_array2)
power_array = np.power(sample_array1, sample_array2)
print("power to the array1 and array 2 : ", power_array) #5. import numpy as np
x = np.arange(10)
print("Original array:",
x)
rslt = np.true_divide(x, 3)
print("After the element-wise division:",
rslt) #6. import numpy as np
array = np.array([1, -2, 3])
print("Given array:\n", array)
rslt = np.absolute(array)
print("Absolute array:\n", rslt) #7. import numpy as np
y = np.array([0.2, 0.3, 0.4, 0.5, 0.6, 0.7])
print("Original array:", end=" ")
print(y)
y = np.rint(y)
print("After rounding off:", end=" ")
print(y) #8. import numpy as np
input_arr = np.array([-1.8, -1.6, -0.5, 0.5,
1.6, 1.8, 3.0])
print(input_arr)
floor_values = np.floor(input_arr)
print("\nFloor values : \n", floor_values)
ceil_values = np.ceil(input_arr)
print("\nCeil values : \n", ceil_values)
trunc_values = np.trunc(input_arr)
print("\nTruncated values : \n", trunc_values) #9. import numpy as np
x = np.random.random((5,3))
print("First array:")
print(x)
y = np.random.random((3,2))
print("Second array:")
print(y)
z = np.dot(x, y)
print("Dot product of two arrays:")
print(z) #10. import numpy as np
x = np.array([2+3j, 4+5j])
print("Printing First matrix:")
print(x)
y = np.array([8+7j, 5+6j])
print("Printing Second matrix:")
print(y)
# vector dot product of two matrices
z = np.vdot(x, y)
print("Product of first and second matrices are:")
print(z) #11. import numpy as np
x = np.arange(24).reshape((2,3,4))
print("Array x:")
print(x)
print("Array y:")
y = np.arange(4)
print(y)
print("Inner of x and y arrays:")
print(np.inner(x, y)) #12. import numpy as np
x = np.array([1, 4, 0], float)
y = np.array([2, 2, 1], float)
print("Matrices and vectors.")
print("x:")
print(x)
print("y:")
print(y)
print("Inner product of x and y:")
print(np.inner(x, y))
print("Outer product of x and y:")
print(np.outer(x, y))
print("Cross product of x and y:")
print(np.cross(x, y)) #13. import numpy as np
arr1 = np.array([[1,2],[3,4]])
arr2 = np.array([[3,4],[5,6]])
matrix_product = np.matmul(arr1,arr2)
print("matrix product is")
print(matrix_product)
print() #14. import numpy as np
a = [1,4,7]
print( np.roots(a))
b = [1,11,9,11,10]
print(np.roots(b)) #15. |
Beta Was this translation helpful? Give feedback.
-
#1. import numpy as np
print("Add:")
print(np.add(1.0, 4.0))
print("Subtract:")
print(np.subtract(1.0, 4.0))
print("Multiply:")
print(np.multiply(1.0, 4.0))
print("Divide:")
print(np.divide(1.0, 4.0)) import numpy as np
l1 = np.log(1e-50)
l2 = np.log(2.5e-50)
print("Logarithm of the sum of exponentiations:")
print(np.logaddexp(l1, l2))
print("Logarithm of the sum of exponentiations of the inputs in base-2:")
print(np.logaddexp2(l1, l2)) import numpy as np
x = [1., 2., 3., 4.]
print("Original array:")
print(x)
print("Largest integer smaller or equal to the division of the inputs:")
print(np.floor_divide(x, 1.5)) import numpy as np
x = np.arange(7)
print("Original array")
print(x)
print("First array elements raised to powers from second array, element-wise:")
print(np.power(x, 3)) import numpy as np
x = np.arange(7)
print("Original array:")
print(x)
print("Element-wise remainder of division:")
print(np.remainder(x, 5)) import numpy as np
x = np.array([-10.2, 122.2, .20])
print("Original array:")
print(x)
print("Element-wise absolute value:")
print(np.absolute(x)) import numpy as np
x = np.array([-.7, -1.5, -1.7, 0.3, 1.5, 1.8, 2.0])
print("Original array:")
print(x)
x = np.rint(x)
print("Round elements of the array to the nearest integer:")
print(x) import numpy as np
x = np.array([-1.6, -1.5, -0.3, 0.1, 1.4, 1.8, 2.0])
print("Original array:")
print(x)
print("Floor values of the above array elements:")
print(np.floor(x))
print("Ceil values of the above array elements:")
print(np.ceil(x))
print("Truncated values of the above array elements:")
print(np.trunc(x)) import numpy as np
x = np.random.random((5,3))
print("First array:")
print(x)
y = np.random.random((3,2))
print("Second array:")
print(y)
z = np.dot(x, y)
print("Dot product of two arrays:")
print(z) import numpy as np
def main():
x = np.array([1 + 2j, 3 + 4j])
print("First Array:\n", x)
y = np.array([5 + 6j, 7 + 8j])
print("Second Array:\n", y)
z = np.vdot(x, y)
print("Product Array:\n", z)
if __name__ == '__main__': main() import numpy as np
x = np.arange(24).reshape((2,3,4))
print("Array x:")
print(x)
print("Array y:")
y = np.arange(4)
print(y)
print("Inner of x and y arrays:")
print(np.inner(x, y)) import numpy as np
x = np.array([1, 4, 0], float)
y = np.array([2, 2, 1], float)
print("Matrices and vectors.")
print("x:")
print(x)
print("y:")
print(y)
print("Inner product of x and y:")
print(np.inner(x, y))
print("Outer product of x and y:")
print(np.outer(x, y))
print("Cross product of x and y:")
print(np.cross(x, y)) import numpy as np
x = [[1, 0], [1, 1]]
y = [[3, 1], [2, 2]]
print("Matrices and vectors.")
print("x:")
print(x)
print("y:")
print(y)
print("Matrix product of above two arrays:")
print(np.matmul(x, y)) import numpy as np
print("Roots of the first polynomial:")
print(np.roots([1, -2, 1]))
print("Roots of the second polynomial:")
print(np.roots([1, -12, 10, 7, -10]))
import numpy as np
x = np.array([[10, 30], [20, 60]])
print("Original array:")
print(x)
print("Mean of each column:")
print(x.mean(axis=0))
print("Mean of each row:")
print(x.mean(axis=1)) import numpy as np
x = np.array([[10, 30], [20, 60]])
print("Original array:")
print(x)
print("Mean of each column:")
print(x.mean(axis=0))
print("Mean of each row:")
print(x.mean(axis=1)) import numpy as np
x = np.random.randn(1000)
print("Average of the array elements:")
mean = x.mean()
print(mean)
print("Standard deviation of the array elements:")
std = x.std()
print(std)
print("Variance of the array elements:")
var = x.var()
print(var) import numpy as np
print("sine: array of angles given in degrees")
print(np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))
print("cosine: array of angles given in degrees")
print(np.cos(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))
print("tangent: array of angles given in degrees")
print(np.tan(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.)) import numpy as np
def main():
z = np.array([-np.pi, -np.pi/2, np.pi/2, np.pi])
r1 = np.degrees(z)
r2 = np.rad2deg(z)
assert np.array_equiv(r1, r2)
print("Radians to Degrees:\n", r1)
if __name__ == '__main__': main()
import numpy as np
x = np.array([-180., -90., 90., 180.])
r1 = np.radians(x)
r2 = np.deg2rad(x)
assert np.array_equiv(r1, r2)
print(r1) import numpy as np
x = np.array([-1., 0, 1.])
print(np.sinh(x))
print(np.cosh(x))
print(np.tanh(x)) import numpy as np
x = np.array([3.1, 3.5, 4.5, 2.9, -3.1, -3.5, -5.9])
print("Original array: ")
print(x)
r1 = np.around(x)
r2 = np.floor(x)
r3 = np.ceil(x)
r4 = np.trunc(x)
r5 = [round(elem) for elem in x]
print("\naround: ", r1)
print("floor: ",r2)
print("ceil: ",r3)
print("trunc: ",r4)
print("round: ",r5) import numpy as np
x = np.array([[1,2,3], [4,5,6]])
print("Original array: ")
print(x)
print("Cumulative sum of the elements along a given axis:")
r = np.cumsum(x)
print(r)
print("\nSum over rows for each of the 3 columns:")
r = np.cumsum(x,axis=0)
print(r)
print("\nSum over columns for each of the 2 rows:")
r = np.cumsum(x,axis=1)
print(r) import numpy as np
x = np.array([1, 3, 5, 7, 0])
print("Original array: ")
print(x)
print("Difference between neighboring elements, element-wise of the said array.")
print(np.diff(x)) import numpy as np
x = np.array([1., 2., 3., 4.], np.float32)
print("Original array: ")
print(x)
print("\ne^x, element-wise of the said:")
r = np.exp(x)
print(r) import numpy as np
x = np.array([1, np.e, np.e**2])
print("Original array: ")
print(x)
print("\nNatural log =", np.log(x))
print("Common log =", np.log10(x))
print("Base 2 log =", np.log2(x))
|
Beta Was this translation helpful? Give feedback.
-
#TASK12 import numpy as np
x = np.array([1, 4, 0], float)
y = np.array([2, 2, 1], float)
print("Matrices and vectors.")
print("x:")
print(x)
print("y:")
print(y)
print("Inner product of x and y:")
print(np.inner(x, y))
print("Outer product of x and y:")
print(np.outer(x, y))
print("Cross product of x and y:")
print(np.cross(x, y))
#TASK13
import numpy as np
x = [[1, 0], [1, 1]]
y = [[3, 1], [2, 2]]
print("Matrices and vectors.")
print("x:")
print(x)
print("y:")
print(y)
print("Matrix product of above two arrays:")
print(np.matmul(x, y))
#TASK14
import numpy as np
print("Roots of the first polynomial:")
print(np.roots([1, -2, 1]))
print("Roots of the second polynomial:")
print(np.roots([1, -12, 10, 7, -10])) |
Beta Was this translation helpful? Give feedback.
-
#TASK15 import polynomial as P
x = (10,20,30)
y = (30,40,50)
print("Add one polynomial to another:")
print(P.polyadd(x,y))
print("Subtract one polynomial from another:")
print(P.polysub(x,y))
print("Multiply one polynomial by another:")
print(P.polymul(x,y))
print("Divide one polynomial by another:")
print(P.polydiv(x,y))
#tTASK16
import numpy as np
x = np.array([[10, 30], [20, 60]])
print("Original array:")
print(x)
print("Mean of each column:")
print(x.mean(axis=0))
print("Mean of each row:")
print(x.mean(axis=1))
|
Beta Was this translation helpful? Give feedback.
-
TASK17 import numpy as np
x = np.random.randn(1000)
print("Average of the array elements:")
mean = x.mean()
print(mean)
print("Standard deviation of the array elements:")
std = x.std()
print(std)
print("Variance of the array elements:")
var = x.var()
print(var)
TASK18
import numpy as np
print("sine: array of angles given in degrees")
print(np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))
print("cosine: array of angles given in degrees")
print(np.cos(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))
print("tangent: array of angles given in degrees")
print(np.tan(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.)) |
Beta Was this translation helpful? Give feedback.
-
TASK19
import numpy as np
def main():
z = np.array([-np.pi, -np.pi/2, np.pi/2, np.pi])
r1 = np.degrees(z)
r2 = np.rad2deg(z)
assert np.array_equiv(r1, r2)
print("Radians to Degrees:\n", r1)
if __name__ == '__main__': main()
Task 20
import numpy as np
def main():
z = np.array([-180., -90., 90., 180.])
r1 = np.radians(z)
r2 = np.deg2rad(z)
assert np.array_equiv(r1, r2)
print("Degrees to radians:\n", r1)
if __name__ == '__main__': main()
Task 21
import numpy as np
def main():
y = np.array([-1., 0, 1.])
print("Hyper Sine:\n", np.sinh(y))
print("Hyper Cosine:\n", np.cosh(y))
print("Hyper Tangent:\n", np.tanh(y))
if __name__ == '__main__': main()
Task 22
import numpy as np
def main():
a = np.array([3.1, 3.5, 4.5, 2.9, -3.1, -3.5, -5.9])
print("Starter Array:\n", a)
r1 = [round(elem) for elem in a]
print("\naround:", np.around(a))
print("floor: ", np.floor(a))
print("ceil: ", np.ceil(a))
print("trunc: ", np.trunc(a))
print("round: ", r1)
if __name__ == '__main__': main() |
Beta Was this translation helpful? Give feedback.
-
#23
import numpy as np
x = np.array([[1,2,3], [4,5,6]])
print("Original array: ")
print(x)
print("Cumulative sum of the elements along a given axis:")
r = np.cumsum(x)
print(r)
print("\nSum over rows for each of the 3 columns:")
r = np.cumsum(x,axis=0)
print(r)
print("\nSum over columns for each of the 2 rows:")
r = np.cumsum(x,axis=1)
print(r)
#24
import numpy as np
x = np.array([1, 3, 5, 7, 0])
print("Original array: ")
print(x)
print("Difference between neighboring elements, element-wise of the said array.")
print(np.diff(x))
#25
import numpy as np
x = np.array([1., 2., 3., 4.], np.float32)
print("Original array: ")
print(x)
print("\ne^x, element-wise of the said:")
r = np.exp(x)
print(r)
#26
import numpy as np
x = np.array([1, np.e, np.e**2])
print("Original array: ")
print(x)
print("\nNatural log =", np.log(x))
print("Common log =", np.log10(x))
print("Base 2 log =", np.log2(x)) |
Beta Was this translation helpful? Give feedback.
-
1. Write a NumPy program to add, subtract, multiply, divide arguments element-wise.import numpy as np
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Add = ",np.add(a, b))
print("Subs = ",np.subtract(a, b))
print("Mult = ",np.multiply(a, b))
print("Div = ",np.divide(a, b)) 2. Write a NumPy program to compute logarithm of the sum of exponentiations of the inputs, sum of exponentiations of the inputs in base-2.import numpy as np
print("Logarithm of the sum of exponentiations:", np.logaddexp(np.log(1e-50), np.log(2.5e-50)))
print("Logarithm of the sum of exponentiations of the inputs in base-2:",np.logaddexp2(np.log(1e-50), np.log(2.5e-50))) 3. Write a NumPy program to get the largest integer smaller or equal to the division of the inputs.import numpy as np
x = [1., 2., 3., 4.]
print("Results:")
print(np.floor_divide(x, 1.5)) 4. Write a NumPy program to get the powers of an array values element-wise.import numpy as np
x = np.arange(7)
print("Powers from 0-6:")
print(np.power(x, 3)) 5. Write a NumPy program to get the element-wise remainder of an array of division.import numpy as np
x = np.arange(7)
print("Element-wise remainder of division:")
print(np.remainder(x, 5)) 6. Write a NumPy program to calculate the absolute value element-wise.import numpy as np
x = np.array([-10.2, 122.2, .20])
print("Element-wise absolute value:")
print(np.absolute(x)) 7. Write a NumPy program to round elements of the array to the nearest integer.import numpy as np
x = np.array([-.7, -1.5, -1.7, 0.3, 1.5, 1.8, 2.0])
x = np.rint(x)
print("Round elements of the array to the nearest integer:")
print(x) 8. Write a NumPy program to get the floor, ceiling and truncated values of the elements of a numpy array.import numpy as np
x = np.array([-1.6, -1.5, -0.3, 0.1, 1.4, 1.8, 2.0])
print("Floor values of the above array elements:")
print(np.floor(x))
print("Ceil values of the above array elements:")
print(np.ceil(x))
print("Truncated values of the above array elements:")
print(np.trunc(x)) 9.Write a NumPy program to multiply a 5x3 matrix by a 3x2 matrix and create a real matrix product.import numpy as np
x = np.random.random((5,3))
print("A:",x)
y = np.random.random((3,2))
print("B:",y)
print("Dot product of A and B:", np.dot(x, y)) 10. Write a NumPy program to multiply a matrix by another matrix of complex numbers and create a new matrix of complex numbers.import numpy as np
x = np.array([1+2j,3+4j])
print("First array:",x)
y = np.array([5+6j,7+8j])
print("Second array:",y)
print("Product of above two arrays:", np.vdot(x, y)) 11. Write a NumPy program to create an inner product of two arrays.import numpy as np
x = np.arange(24).reshape((2,3,4))
print("Array x:",x)
y = np.arange(4)
print("Array y:",y)
print("Inner:", np.inner(x, y)) 12. Write a NumPy program to generate inner, outer, and cross products of matrices and vectors.import numpy as np
x = np.array([1, 4, 0], float)
y = np.array([2, 2, 1], float)
print("x:",x)
print("y:",y)
print("Inner product: ",np.inner(x, y))
print("Outer product:", np.outer(x, y))
print("Cross product:",np.cross(x, y)) 13. Write a NumPy program to generate a matrix product of two arrays.import numpy as np
x = [[1, 0], [1, 1]]
y = [[3, 1], [2, 2]]
print("x:",x)
print("y:",y)
print("Matrix product:",np.matmul(x, y)) 14. Write a NumPy program to find the roots of the following polynomials.import numpy as np
print("Roots of the first polynomial:",np.roots([1, -2, 1]))
print("Roots of the second polynomial:",np.roots([1, -12, 10, 7, -10])) 15. Write a NumPy program to add one polynomial to another, subtract one polynomial from another, multiply one polynomial by another and divide one polynomial by another.from numpy.polynomial import polynomial
x = (10,20,30)
y = (30,40,50)
print("Add one polynomial to another:",polynomial.polyadd(x,y))
print("Subtract one polynomial from another:",polynomial.polysub(x,y))
print("Multiply one polynomial by another:",polynomial.polymul(x,y))
print("Divide one polynomial by another:",polynomial.polydiv(x,y)) 16. Write a NumPy program to calculate mean across dimension, in a 2D numpy array.import numpy as np
x = np.array([[10, 30], [20, 60]])
print("Mean of each column:",x.mean(axis=0))
print("Mean of each row:",x.mean(axis=1)) 17. Write a NumPy program to create a random array with 1000 elements and compute the average, variance, standard deviation of the array elements.import numpy as np
x = np.random.randn(1000)
print("Average of the array elements:",x.mean())
print("Standard deviation of the array elements:",x.std())
print("Variance of the array elements:",x.var()) 18. Write a NumPy program to compute the trigonometric sine, cosine and tangent array of angles given in degrees.import numpy as np
print("SIN:",np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))
print("COS: ",np.cos(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))
print("TAN:".np.tan(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.)) 19. Write a NumPy program to convert angles from radians to degrees for all elements in a given array.import numpy as np
x = np.array([-np.pi, -np.pi/2, np.pi/2, np.pi])
r1 = np.degrees(x)
r2 = np.rad2deg(x)
assert np.array_equiv(r1, r2)
print(r1) 20. Write a NumPy program to convert angles from degrees to radians for all elements in a given array.import numpy as np
x = np.array([-180., -90., 90., 180.])
r1 = np.radians(x)
r2 = np.deg2rad(x)
assert np.array_equiv(r1, r2)
print(r1) 21. Write a NumPy program to calculate hyperbolic sine, hyperbolic cosine, and hyperbolic tangent for all elements in a given array.import numpy as np
x = np.array([-1., 0, 1.])
print(np.sinh(x))
print(np.cosh(x))
print(np.tanh(x)) 22. Write a NumPy program to calculate round, floor, ceiling, truncated and round (to the given number of decimals) of the input, element-wise of a given array.import numpy as np
x = np.array([3.1, 3.5, 4.5, 2.9, -3.1, -3.5, -5.9])
print("Around: ", np.around(x))
print("Floor: ",np.floor(x))
print("Ceil: ",np.ceil(x))
print("Trunc: ",np.trunc(x))
print("Round: ",[round(elem) for elem in x]) 23. Write a NumPy program to calculate cumulative sum of the elements along a given axis, sum over rows for each of the 3 columns and sum over columns for each of the 2 rows of a given 3x3 array.import numpy as np
x = np.array([[1,2,3], [4,5,6]])
print("Cumulative sum of elements: \n",np.cumsum(x))
print("Sum over rows: \n",np.cumsum(x,axis=0))
print("Sum over columns: \n",np.cumsum(x,axis=1) ) 24. Write a NumPy program to calculate the difference between neighboring elements, element-wise of a given array.import numpy as np
x = np.array([1, 3, 5, 7, 0])
print("Difference: ",np.diff(x)) 25.Write a NumPy program to compute ex, element-wise of a given array.import numpy as np
x = np.array([1., 2., 3., 4.], np.float32)
print("\ne^x: ", np.exp(x)) 26. Write a NumPy program to compute natural, base 10, and base 2 logarithms for all elements in a given array.import numpy as np
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)) |
Beta Was this translation helpful? Give feedback.
-
x=input("Enter array")
y=input("Enter array")
np.add(x,y)
np.substract(x,y)
np.multiply(x,y)
np.divide(x,y) x = np.log(1e-50)
y= np.log(2.5e-50)
print(np.logaddexp(x,y))
print(np.logaddexp2(x,y)) arr = [1.0,2.0, 3.0,4.0]
print(np.floor_divide(arr,1.5)) arr = [1.0,2.0, 3.0,4.0]
print(np.power(arr,3)) arr = [0,1,2, 3,4,5,6]
print(np.remainder(arr,5)) arr = np.array([-10.2, 122.2, .20])
print(np.absolute(arr)) arr=x = np.array([-.7, -1.5, -1.7, 0.3, 1.5, 1.8, 2.0])
print(np.rint(arr)) arr= np.array([-.7, -1.5, -1.7, 0.3, 1.5, 1.8, 2.0])
print(np.trunc(arr))
print(np.ceil(arr))
print(np.floor(arr)) a = np.random.random((5,3))
b = np.random.random((3,2))
print(np.dot(a,b)) a = np.array([1.+2.j, 3.+4.j])
b = np.array([5.+6.j, 7.+8.j])
print(np.vdot(a,b))
```` |
Beta Was this translation helpful? Give feedback.
-
1.- import numpy as np
print("Add:")
print(np.add(7, 3))
print("Subtract:")
print(np.subtract(10, 2))
print("Multiply:")
print(np.multiply(5, 4))
print("Divide:")
print(np.divide(25, 5)) 2.- import numpy as np
num1 = np.log(1e-50)
num2 = np.log(2.5e-50)
print("Logarithm of the sum of exponentiations:")
print(np.logaddexp(num1, num2))
print("Logarithm of the sum of exponentiations of the inputs in base-2:")
print(np.logaddexp2(num1, num2)) 3.- import numpy as np
x = [1, 2, 3, 4]
print("Original array:")
print(x)
print("Largest integer smaller or equal to the division of the inputs:")
print(np.floor_divide(x, 1.5)) 4.- import numpy as np
array = np.arange(7)
print("Original array")
print(array)
print("First array elements raised to powers from second array, element-wise:")
print(np.power(array, 3)) 5.- import numpy as np
array = np.arange(7)
print("Original array:")
print(array)
print("Element-wise remainder of division:")
print(np.remainder(array, 5)) 6.- import numpy as np
array = np.array([-10.2, 122.2, .20])
print("Original array:")
print(array)
print("Element-wise absolute value:")
print(np.absolute(array)) 7.- import numpy as np
array = np.array([-.7, -1.5, -1.7, 0.3, 1.5, 1.8, 2.0])
print("Original array:")
print(array)
array = np.rint(array)
print("Round elements of the array to the nearest integer:")
print(array) 8.- import numpy as np
array = np.array([-1.6, -1.5, -0.3, 0.1, 1.4, 1.8, 2.0])
print("Original array:")
print(array)
print("Floor values of the above array elements:")
print(np.floor(array))
print("Ceil values of the above array elements:")
print(np.ceil(array))
print("Truncated values of the above array elements:")
print(np.trunc(array)) 9.- import numpy as np
array = np.random.random((5,3))
print("First array:")
print(array)
array2 = np.random.random((3,2))
print("Second array:")
print(array2)
array3 = np.dot(array, array2)
print("Dot product of two arrays:")
print(array3) 10.- import numpy as np
array = np.array([1 + 2j, 3 + 4j])
print("First array:")
print(array)
array2 = np.array([5 + 6j, 7 + 8j])
print("Second array:")
print(array2)
array3 = np.vdot(array, array2)
print("Product of above two arrays:")
print(array3) 11.- import numpy as np
array = np.arange(24).reshape((2, 3, 4))
print("Array x:")
print(array)
print("Array y:")
array2 = np.arange(4)
print(array2)
print("Inner of x and y arrays:")
print(np.inner(array, array2)) 12.- import numpy as np
array = np.array([1, 4, 0], float)
array1 = np.array([2, 2, 1], float)
print("Matrices and vectors.")
print("x:")
print(array)
print("y:")
print(array1)
print("Inner product of x and y:")
print(np.inner(array, array1))
print("Outer product of x and y:")
print(np.outer(array, array1))
print("Cross product of x and y:")
print(np.cross(array, array1)) 13.- import numpy as np
x = [[1, 0], [1, 1]]
y = [[3, 1], [2, 2]]
print("Matrices and vectors.")
print("x:")
print(x)
print("y:")
print(y)
print("Matrix product of above two arrays:")
print(np.matmul(x, y)) 14.- import numpy as np
print("Roots of the first polynomial:")
print(np.roots([1, -2, 1]))
print("Roots of the second polynomial:")
print(np.roots([1, -12, 10, 7, -10])) 15.- from numpy.polynomial import polynomial as P
x = (10,20,30)
y = (30,40,50)
print("Add one polynomial to another:")
print(P.polyadd(x,y))
print("Subtract one polynomial from another:")
print(P.polysub(x,y))
print("Multiply one polynomial by another:")
print(P.polymul(x,y))
print("Divide one polynomial by another:")
print(P.polydiv(x,y)) 16.- import numpy as np
x = np.array([[10, 30], [20, 60]])
print("Original array:")
print(x)
print("Mean of each column:")
print(x.mean(axis=0))
print("Mean of each row:")
print(x.mean(axis=1)) 17.- import numpy as np
x = np.random.randn(1000)
print("Average of the array elements:")
mean = x.mean()
print(mean)
print("Standard deviation of the array elements:")
std = x.std()
print(std)
print("Variance of the array elements:")
var = x.var()
print(var) 18.- import numpy as np
print("sine: array of angles given in degrees")
print(np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))
print("cosine: array of angles given in degrees")
print(np.cos(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.))
print("tangent: array of angles given in degrees")
print(np.tan(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.)) 19.- import numpy as np
x = np.array([-np.pi, -np.pi/2, np.pi/2, np.pi])
r1 = np.degrees(x)
r2 = np.rad2deg(x)
assert np.array_equiv(r1, r2)
print(r1) 20.- import numpy as np
x = np.array([-180, -90, 90, 180])
r1 = np.radians(x)
r2 = np.deg2rad(x)
assert np.array_equiv(r1, r2)
print(r1) 21.- import numpy as np
x = np.array([-1, 0, 1])
print(np.sinh(x))
print(np.cosh(x))
print(np.tanh(x)) 22.- import numpy as np
x = np.array([3.1, 3.5, 4.5, 2.9, -3.1, -3.5, -5.9])
print("Original array: ")
print(x)
r1 = np.around(x)
r2 = np.floor(x)
r3 = np.ceil(x)
r4 = np.trunc(x)
r5 = [round(elem) for elem in x]
print("\naround: ", r1)
print("floor: ", r2)
print("ceil: ", r3)
print("trunc: ", r4)
print("round: ", r5) 23.- import numpy as np
x = np.array([[1,2,3], [4,5,6]])
print("Original array: ")
print(x)
print("Cumulative sum of the elements along a given axis:")
r = np.cumsum(x)
print(r)
print("\nSum over rows for each of the 3 columns:")
r = np.cumsum(x,axis=0)
print(r)
print("\nSum over columns for each of the 2 rows:")
r = np.cumsum(x,axis=1)
print(r) 24.- import numpy as np
x = np.array([1, 3, 5, 7, 0])
print("Original array: ")
print(x)
print("Difference between neighboring elements, element-wise of the said array.")
print(np.diff(x)) 25.- import numpy as np
x = np.array([1., 2., 3., 4.], np.float32)
print("Original array: ")
print(x)
print("\ne^x, element-wise of the said:")
r = np.exp(x)
print(r) 26.- import numpy as np
x = np.array([1., 2., 3., 4.], np.float32)
print("Original array: ")
print(x)
print("\ne^x, element-wise of the said:")
r = np.exp(x)
print(r) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Write a NumPy program to add, subtract, multiply, divide arguments element-wise.
Expected Output:
Add:
5.0
Subtract:
-3.0
Multiply:
4.0
Divide:
0.25
Write a NumPy program to compute logarithm of the sum of exponentiations of the inputs, sum of exponentiations of the inputs in base-2.
Expected Output:
Logarithm of the sum of exponentiations:
-113.876491681
Logarithm of the sum of exponentiations of the inputs in base-2:
-113.599555228
Write a NumPy program to get the largest integer smaller or equal to the division of the inputs.
Expected Output:
Original array:
[1.0, 2.0, 3.0, 4.0]
Largest integer smaller or equal to the division of the inputs:
[ 0. 1. 2. 2.]
Write a NumPy program to get the powers of an array values element-wise.
Note: First array elements raised to powers from second array
Expected Output:
Original array
[0 1 2 3 4 5 6]
First array elements raised to powers from second array, element-wise:
[ 0 1 8 27 64 125 216]
Write a NumPy program to get the element-wise remainder of an array of division.
Sample Output:
Original array:
[0 1 2 3 4 5 6]
Element-wise remainder of division:
[0 1 2 3 4 0 1]
Write a NumPy program to calculate the absolute value element-wise.
Original array:
[ -10.2 122.2 0.2]
Element-wise absolute value:
[ 10.2 122.2 0.2]
Write a NumPy program to round elements of the array to the nearest integer.
Sample Output:
Original array:
[-0.7 -1.5 -1.7 0.3 1.5 1.8 2. ]
Round elements of the array to the nearest integer:
[-1. -2. -2. 0. 2. 2. 2.]
Write a NumPy program to get the floor, ceiling and truncated values of the elements of a numpy array.
Sample Output:
Original array:
[-1.6 -1.5 -0.3 0.1 1.4 1.8 2. ]
Floor values of the above array elements:
[-2. -2. -1. 0. 1. 1. 2.]
Ceil values of the above array elements:
[-1. -1. -0. 1. 2. 2. 2.]
Truncated values of the above array elements:
[-1. -1. -0. 0. 1. 1. 2.]
Write a NumPy program to multiply a 5x3 matrix by a 3x2 matrix and create a real matrix product.
Sample output:
First array:
[[ 0.44349753 0.81043761 0.00771825]
[ 0.64004088 0.86774612 0.19944667]
[ 0.61520091 0.24796788 0.93798297]
[ 0.22156999 0.61318856 0.82348994]
[ 0.91324026 0.13411297 0.00622696]]
Second array:
[[ 0.73873542 0.06448186]
[ 0.90974982 0.06409165]
[ 0.22321268 0.39147412]]
Dot product of two arrays:
[[ 1.06664562 0.08356133]
[ 1.30677176 0.17496452]
[ 0.88942914 0.42275803]
[ 0.90534318 0.37596252]
[ 0.79804212 0.06992065]]
Write a NumPy program to multiply a matrix by another matrix of complex numbers and create a new matrix of complex numbers.
Sample output:
First array:
[ 1.+2.j 3.+4.j]
Second array:
[ 5.+6.j 7.+8.j]
Product of above two arrays:
(70-8j)
Write a NumPy program to create an inner product of two arrays.
Sample Output:
Array x:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
Array y:
[0 1 2 3]
Inner of x and y arrays:
[[ 14 38 62]
[ 86 110 134]]
Write a NumPy program to generate inner, outer, and cross products of matrices and vectors.
Expected Output:
Matrices and vectors.
x:
[ 1. 4. 0.]
y:
[ 2. 2. 1.]
Inner product of x and y:
10.0
Outer product of x and y:
[[ 2. 2. 1.]
[ 8. 8. 4.]
[ 0. 0. 0.]]
Cross product of x and y:
[ 4. -1. -6.]
Write a NumPy program to generate a matrix product of two arrays.
Sample Output:
Matrices and vectors.
x:
[[1, 0], [1, 1]]
y:
[[3, 1], [2, 2]]
Matrix product of above two arrays:
[[3 1]
[5 3]]
Write a NumPy program to find the roots of the following polynomials.
a) x2 - 4x + 7.
b) x4 - 11x3 + 9x2 + 11x ? 10
Sample output:
Roots of the first polynomial:
[ 1. 1.]
Roots of the second polynomial:
[ 11.04461946+0.j -0.87114210+0.j 0.91326132+0.4531004j
0.91326132-0.4531004j]
Write a NumPy program to add one polynomial to another, subtract one polynomial from another, multiply one polynomial by another and divide one polynomial by another.
Sample output:
Add one polynomial to another:
[ 40. 60. 80.]
Subtract one polynomial from another:
[-20. -20. -20.]
Multiply one polynomial by another:
[ 300. 1000. 2200. 2200. 1500.]
Divide one polynomial by another:
(array([ 0.6]), array([-8., -4.]))
Write a NumPy program to calculate mean across dimension, in a 2D numpy array.
Sample output:
Original array:
[[10 30]
[20 60]]
Mean of each column:
[ 15. 45.]
Mean of each row:
[ 20. 40.]
Write a NumPy program to create a random array with 1000 elements and compute the average, variance, standard deviation of the array elements.
Sample output:
Average of the array elements:
-0.0255137240796
Standard deviation of the array elements:
0.984398282476
Variance of the array elements:
0.969039978542
Write a NumPy program to compute the trigonometric sine, cosine and tangent array of angles given in degrees.
Sample output:
sine: array of angles given in degrees
[ 0. 0.5 0.70710678 0.8660254 1. ]
cosine: array of angles given in degrees
[ 1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
6.12323400e-17]
tangent: array of angles given in degrees
[ 0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00
1.63312394e+16]
Write a NumPy program to convert angles from radians to degrees for all elements in a given array.
Input: [-np.pi, -np.pi/2, np.pi/2, np.pi]
Sample output:
[-180. -90. 90. 180.]
Write a NumPy program to convert angles from degrees to radians for all elements in a given array.
Input: Input: [-180., -90., 90., 180.]
Sample output:
[-3.14159265 -1.57079633 1.57079633 3.14159265]
Write a NumPy program to calculate hyperbolic sine, hyperbolic cosine, and hyperbolic tangent for all elements in a given array.
Input: Input: Input: [-1., 0, 1.]
Sample output:
[-1.17520119 0. 1.17520119]
[1.54308063 1. 1.54308063]
[-0.76159416 0. 0.76159416]
Write a NumPy program to calculate round, floor, ceiling, truncated and round (to the given number of decimals) of the input, element-wise of a given array.
Sample output:
Original array:
[ 3.1 3.5 4.5 2.9 -3.1 -3.5 -5.9]
around: [ 3. 4. 4. 3. -3. -4. -6.]
floor: [ 3. 3. 4. 2. -4. -4. -6.]
ceil: [ 4. 4. 5. 3. -3. -3. -5.]
trunc: [ 3. 3. 4. 2. -3. -3. -5.]
round: [3.0, 4.0, 4.0, 3.0, -3.0, -4.0, -6.0]
Write a NumPy program to calculate cumulative sum of the elements along a given axis, sum over rows for each of the 3 columns and sum over columns for each of the 2 rows of a given 3x3 array.
Sample output:
Original array:
[[1 2 3]
[4 5 6]]
Cumulative sum of the elements along a given axis:
[ 1 3 6 10 15 21]
Sum over rows for each of the 3 columns:
[[1 2 3]
[5 7 9]]
Sum over columns for each of the 2 rows:
[[ 1 3 6]
[ 4 9 15]]
Write a NumPy program to calculate the difference between neighboring elements, element-wise of a given array.
Sample output:
Original array:
[1 3 5 7 0]
Difference between neighboring elements, element-wise of the said array.
[ 2 2 2 -7]
Write a NumPy program to compute ex, element-wise of a given array.
Sample output:
Original array:
[1. 2. 3. 4.]
e^x, element-wise of the said:
[ 2.7182817 7.389056 20.085537 54.59815 ]
Write a NumPy program to compute natural, base 10, and base 2 logarithms for all elements in a given array.
Sample output:
Original array:
Original array:
[1. 2.71828183 7.3890561 ]
Natural log = [0. 1. 2.]
Common log = [0. 0.43429448 0.86858896]
Base 2 log = [0. 1.44269504 2.88539008]
Beta Was this translation helpful? Give feedback.
All reactions