forked from CURENT/andes
-
Notifications
You must be signed in to change notification settings - Fork 2
/
math.py
175 lines (121 loc) · 3.83 KB
/
math.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import numpy as np
from cvxopt import matrix
from cvxopt import mul, exp
def altb(a, b):
"""Return a matrix of logic comparison of A<B"""
return matrix(np.less(a, b).astype('float'))
def mmax(a, b):
"""Return a matrix of maximum values in a and b element-wise"""
return matrix(np.maximum(a, b))
def mmin(a, b):
"""Return a matrix of minimum values in a and b element-wise"""
return matrix(np.minimum(a, b))
def agtb(a, b):
"""Return a matrix of logic comparision of A>B"""
return matrix(np.greater(a, b).astype('float'))
def aleb(a, b):
"""Return a matrix of logic comparison of A<=B"""
return matrix(np.less_equal(a, b).astype('float'))
def ageb(a, b):
"""Return a matrix of logic comparision of A>=B"""
return matrix(np.greater_equal(a, b).astype('float'))
def aeqb(a, b):
"""Return a matrix of logic comparison of A == B"""
return matrix(np.equal(a, b).astype('float'))
def aneb(a, b):
"""Return a matrix of logic comparison of A != B"""
return matrix(np.not_equal(a, b).astype('float'))
def aorb(a, b):
"""Return a matrix of logic comparison of A or B"""
return matrix(np.logical_or(a, b).astype('float'), a.size)
def aandb(a, b):
"""Return a matrix of logic comparison of A or B"""
return matrix(np.logical_and(a, b).astype('float'), a.size)
def nota(a):
"""Return a matrix of logic negative of A"""
return matrix(np.logical_not(a).astype('float'), a.size)
def polar(m, a):
"""Return complex number from polar form m*exp(1j*a)"""
return mul(m, exp(1j * a))
def conj(a):
"""return the conjugate of a"""
return a.H.T
def neg(u):
"""Return the negative of binary states u"""
return 1 - u
def mfloor(a):
"""Return the element-wise floor value of a"""
return matrix(np.floor(a), a.size)
def mround(a):
"""Return the element-wise round value of a"""
return matrix(np.round(a), a.size)
def not0(a):
"""Return u if u!= 0, return 1 if u == 0"""
return matrix(list(map(lambda x: 1 if x == 0 else x, a)), a.size)
def zeros(m, n):
"""Return a m-by-n zero-value matrix"""
return matrix(0.0, (m, n), 'd')
def ones(m, n):
"""Return a m-by-n one-value matrix"""
return matrix(1.0, (m, n), 'd')
def sign(a):
"""Return the sign of a in (1, -1, 0)"""
return matrix(np.sign(np.array(a)))
def sort(m, reverse=False):
"""Return sorted m (default: ascending order)"""
ty = type(m)
if ty == matrix:
m = list(m)
m = sorted(m, reverse=reverse)
if ty == matrix:
m = matrix(m)
return m
def sort_idx(m, reverse=False):
"""Return the indices of m in sorted order (default: ascending order)"""
return sorted(range(len(m)), key=lambda k: m[k], reverse=reverse)
def index(m, val):
"""
Return the indices of all the ``val`` in ``m``
"""
mm = np.array(m)
idx_tuple = np.where(mm == val)
idx = idx_tuple[0].tolist()
return idx
def to_number(s):
"""
Convert a string to a number.
If not successful, return the string without blanks
"""
ret = s
# try converting to float
try:
ret = float(s)
except ValueError:
ret = ret.strip('\'').strip()
# try converting to uid
try:
ret = int(s)
except ValueError:
pass
# try converting to boolean
if ret == 'True':
ret = True
elif ret == 'False':
ret = False
elif ret == 'None':
ret = None
return ret
def sdiv(a, b):
"""Safe division: if a == b == 0, sdiv(a, b) == 1"""
if len(a) != len(b):
raise ValueError('Argument a and b does not have the same length')
idx = 0
ret = matrix(0, (len(a), 1), 'd')
for m, n in zip(a, b):
try:
ret[idx] = m / n
except ZeroDivisionError:
ret[idx] = 1
finally:
idx += 1
return ret