-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
69 lines (54 loc) · 1.67 KB
/
utils.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
import numpy as np
def strictly_greater(v1, v2):
"""
Check if each corresponding element in v1 is greater than or equal to v2,
with at least one element being strictly greater.
Parameters
----------
v1 : iterable
The first vector (e.g., list or tuple of numbers).
v2 : iterable)
The second vector, of the same length as v1.
Returns
-------
bool
True if v1 is strictly greater than v2 component-wise,
meaning all elements of v1 are greater than or equal to
the corresponding elements of v2, and at least one element
is strictly greater. Otherwise, returns False.
"""
return all(x >= y for x, y in zip(v1, v2)) and v1 != v2
def scalar_product(v1, v2):
"""
Compute the scalar (dot) product of two vectors.
Parameters
----------
v1 : iterable or np.ndarray
The first vector, either a list, tuple, or NumPy array.
v2 : iterable or np.ndarray
The second vector, of the same length as v1.
Returns
-------
float or int:
The scalar product of v1 and v2.
"""
if isinstance(v1, np.ndarray) and isinstance(v2, np.ndarray):
return np.dot(v1, v2)
return sum(x * y for x, y in zip(v1, v2))
def is_integer_matrix(A):
"""
Check if a matrix consists only of integer values.
Parameters
----------
A : np.ndarray
A two-dimensional NumPy array of shape (m, n).
Returns
-------
bool
True if all elements of A are integers, False otherwise.
"""
for i in range(A.shape[0]):
for j in range(A.shape[1]):
if A[i, j] % 1:
return False
return True