-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoddeddot.py
73 lines (54 loc) · 1.88 KB
/
moddeddot.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import numpy as np
def modified_dot_product_and_sum_corrected(A, B):
A = np.array(A)
B = np.array(B)
# Validate that A and B are 2D matrices
if A.ndim != 2 or B.ndim != 2:
raise ValueError("Both A and B must be 2D matrices.")
# Get the shape of matrix A
m, n = A.shape
# Get the shape of matrix B
p, q = B.shape
# Create a new array to hold the result
result = np.zeros((m, n, p, q))
# Compute the modified dot product
for i in range(m):
for j in range(n):
result[i, j] = A[i, j] * B
# Sum across the first two axes to collapse into the shape of B
final_result = result.sum(axis=0).sum(axis=0)
return final_result
# Example matrices
A = [[3, 5], [7, 9]]
B = [[2, 8], [22, 19]]
# Calculate the modified dot product and then sum appropriately
result = modified_dot_product_and_sum_corrected(A, B)
print("Final Summed Result:")
print(result)
import numpy as np
def modified_dot_product_and_sum_corrected(A, B):
A = np.array(A)
B = np.array(B)
# Validate that A and B are 2D matrices
if A.ndim != 2 or B.ndim != 2:
raise ValueError("Both A and B must be 2D matrices.")
# Get the shape of matrix A
m, n = A.shape
# Get the shape of matrix B
p, q = B.shape
# Create a new array to hold the result
result = np.zeros((m, n, p, q))
# Compute the modified dot product
for i in range(m):
for j in range(n):
result[i, j] = A[i, j] * B
# Sum across the first two axes to collapse into the shape of B
final_result = result.sum(axis=0).sum(axis=0)
return final_result
# Example matrices
A = [[5, 6], [11, 12]]
B = [[1, 2], [3, 4]]
# Calculate the modified dot product and then sum appropriately
result = modified_dot_product_and_sum_corrected(A, B)
print("Final Summed Result:")
print(result)