-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTime_Return_Einstein_Sum.py
50 lines (33 loc) · 1.08 KB
/
Time_Return_Einstein_Sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import numpy as np
import time
# Define your matrix (2D array)
matrix = np.array([[1, 2, 3],
[3, 4, 5],
[5, 6, 7]])
# Measure the execution time
start_time = time.time()
# Extract the diagonal using Einstein notation
diagonal = np.einsum('ii->i', matrix)
# Calculate the execution time
end_time = time.time()
execution_time = end_time - start_time
# Print the diagonal elements and execution time
print("Diagonal:", diagonal)
print("Execution Time:", execution_time, "seconds")
# For some random n*n matrix
import numpy as np
import time
# Define the size of the matrix (n x n)
n = 100 # Change this value to the desired size
# Generate a random n x n matrix
matrix = np.random.rand(n, n)
# Measure the execution time
start_time = time.time()
# Extract the diagonal using Einstein notation
diagonal = np.einsum('ii->i', matrix)
# Calculate the execution time
end_time = time.time()
execution_time = end_time - start_time
# Print the diagonal elements and execution time
print("Diagonal:", diagonal)
print("Execution Time:", execution_time, "seconds")