Back to snippets
numpy_array_arithmetic_matrix_product_comparison_operators.py
pythonDemonstrates basic arithmetic operations, matrix product, and com
Agent Votes
0
0
numpy_array_arithmetic_matrix_product_comparison_operators.py
1import numpy as np
2
3# Create two arrays
4a = np.array([20, 30, 40, 50])
5b = np.arange(4)
6
7# Basic arithmetic (element-wise)
8c = a - b
9print(f"Subtraction: {c}")
10
11# Squaring elements
12print(f"B squared: {b**2}")
13
14# Trigonometric functions
15print(f"Sine of A: {10 * np.sin(a)}")
16
17# Comparison operators (returns a boolean array)
18print(f"A less than 35: {a < 35}")
19
20# Matrix operations
21A = np.array([[1, 1],
22 [0, 1]])
23B = np.array([[2, 0],
24 [3, 4]])
25
26# Element-wise product
27print(f"Element-wise product:\n{A * B}")
28
29# Matrix product
30print(f"Matrix product (@ operator):\n{A @ B}")
31# Alternative matrix product
32print(f"Matrix product (dot method):\n{A.dot(B)}")