-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTools_RBF.py
332 lines (284 loc) · 7.88 KB
/
Tools_RBF.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#-------------------------------------------------------------------------------
# Name: RBF Utility Tools
# Purpose: General utilities used in RBF
#
# Author: Mohammad
#
# Created: 04/05/2016
# Copyright: (c) ISI 2016
# Licence: ISI
#-------------------------------------------------------------------------------
import os, sys
import numpy as np
mypath = os.path.dirname(os.path.realpath(sys.argv[0]))
#===============================================================================
# Duplicae Row
#===============================================================================
def dupRow(v, m):
"""
# M = dupCol(v, m)
#
# Duplicates v, a row vector, m times. Returns the
# result as matrix M with m rows, each one a copy of v.
#
# Inputs
#
# v a row vector (1-by-n)
# m a positive integer
#
# Output
#
# M a matrix (m-by-n) matrix
"""
if not isinstance(v, (np.ndarray, list)):
raise AssertionError('dupCol: input must be list or array')
vec = np.array(v)
if vec.ndim==1:
vec = np.atleast_2d(vec) # Row Vector
[r, c] = vec.shape
if r != 1:
raise AssertionError('dupRow: input vector must be row')
M = np.repeat(vec,m,0)
return M
#===============================================================================
# Duplicae Col
#===============================================================================
def dupCol(v, n):
"""
# M = dupCol(v, n)
#
# Duplicates v, a column vector, n times. Returns the
# result as matrix M with n columns, each one a copy of v.
#
# Inputs
#
# v a column vector (m-by-1)
# n a positive integer
#
# Output
#
# M a matrix (m-by-n) matrix
"""
if not isinstance(v, (np.ndarray, list)):
raise AssertionError('dupCol: input must be list or array')
vec = np.array(v)
if vec.ndim==1:
vec = np.atleast_2d(vec).T # Column Vector
[r, c] = vec.shape
if c != 1:
raise AssertionError('dupCol: input vector must be column')
M = np.repeat(vec,n,axis=1)
return M
#===============================================================================
# Sum Row
#===============================================================================
def rowSum(X):
"""
# s = rowSum(X)
#
# Outputs a column vector whose elements are the
# sums of the rows of X.
#
# Inputs
#
# X matrix (m-by-n)
#
# Output
#
# s vector (m-by-1)
"""
##[m,n] = X.shape
d = X.ndim
##if n > 1:
if d > 1:
s = np.sum(X, axis=1)[:,np.newaxis]
else:
s = X[:,np.newaxis]
return s
#===============================================================================
# Sum Col
#===============================================================================
def colSum(X):
"""
# s = colSum(X)
#
# Outputs a row vector whose elements are the
# sums of the columns of X.
# Designed to get round the feature of the standard
# routine (sum) of summimg row vectors to a scalar.
# If colSum is handed a row vector, the same vector
# is given back.
#
# Inputs
#
# X matrix (m-by-n)
#
# Output
#
# s vector (1-by-n)
"""
##[m,n] = X.shape
d = X.ndim
##if m > 1:
if d > 1:
s = np.sum(X, axis=0)[np.newaxis,:]
else:
s = X[np.newaxis,:]
return s
#===============================================================================
# Diagonal of Product (X & Y)
#===============================================================================
def diagProduct(X, Y):
"""
# d = diagProduct(X, Y)
#
# Outputs the diagonal of the product of X and Y.
# Faster than diag(X*Y).
#
# Inputs
#
# X matrix (m-by-n)
# Y matrix (n-by-m)
#
# Output
# d vector (m-by-1)
"""
[m,n] = X.shape
[p,q] = Y.shape
if (m!=q) or (n!= p):
raise AssertionError('diagProduct: bad dimensions')
return None
### P - a column vector of the rows of X [n*m,1]
##P = X.conj().T
##P = np.ravel(P)[:,np.newaxis]
##
### Q - a column vector of the columns of Y [n*m,1]
##Q = np.ravel(Y)[:,np.newaxis]
##
### Z - an [n,m] matrix containing the components of P.*Q
##Z = np.reshape(P * Q, (n,m))
##
### d - the answer is the sum of the columns of Z
##d = colSum(Z).conj().T
d = np.sum(X*Y.conj().T,axis=-1)[:,np.newaxis]
return d
#===============================================================================
# Trace Product (X & Y)
#===============================================================================
def traceProduct(X, Y):
"""
# t = traceProduct(X, Y)
#
# Outputs the trace of the product of X and Y.
# Faster than trace(X*Y).
#
# Inputs:
#
# X matrix (m-by-n)
# Y matrix (n-by-m)
#
# Output:
#
# t scalar
"""
[m,n] = X.shape
[p,q] = Y.shape
if (m!=q) or (n!=p):
raise AssertionError('traceProduct: bad dimensions')
return None
# use the fast diagonal of a product routine
t = np.sum( diagProduct(X, Y) );
return t
#===============================================================================
# overWrite
#===============================================================================
def overWrite(b, n=None):
"""
# b = overWrite(b, n)
#
# Backup b characters (the number of characters in the
# string printed by the last call to {\tt overWrite})
# and print the new string, returning its length in {\tt b}.
#
# Inputs
#
# b number of characters used in last overWrite
# n string or integer
#
# Outputs
#
# b number of characters used in this overWrite
"""
# backup
for i in range(b):
print '\b'
if n is None:
# rub out
s = ' '
print '#s' #s(ones((1,b)))
# backup again
for i in range(b):
print '\b'
else:
# print
if isinstace(n,str):
print '#s' #n
b = len(n)
else:
s = '#d' #n
print '#s' #s
b = len(s)
return b
#===============================================================================
# Get Next Argument in String Option
#===============================================================================
def getNextArg(s, i):
"""
# [arg, i] = getNextArg(s, i)
#
# Used to parse a comma or space separated list of arguments
# in a single string into a sequence of argument strings.
#
# Inputs
#
# s entire string
# i current position in string
#
# Outputs
#
# arg sub-string
# i new position in string
"""
# check if already at end
if i >= len(s):
arg = ''
else:
if (s[i] ==' ') or (s[i] == ','):
# skip over separators
separator = True
while separator:
i += 1
if i >= len(s):
separator = False
else:
separator = (s[i] == ' ') or (s[i] == ',')
# check if at the end (with +1 shift)
if i > len(s):
arg = ''
else:
# scan word
separator = False
arg = ''
while not separator:
arg += s[i]
i += 1
if i >= len(s):
separator = True
else:
separator = (s[i] == ' ') or (s[i] == ',')
return [arg, i]
# ==============================================================================
# MAIN RUN PART
# ==============================================================================
if __name__ == '__main__':
pass