utils
module¶
This module contains some utility programs used by the package.
ScalarFunctionAndGradient: TypeAlias = Callable[[np.ndarray, list, Optional[bool]], float | tuple[float, np.ndarray]]
module-attribute
¶
Type of f(v, args, gr) that returns a scalar value and also a gradient if gr is True
bs_error_abort(msg='error, aborting')
¶
Report error and exits with code 1
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg |
str
|
specifies the error message |
'error, aborting'
|
Returns:
Type | Description |
---|---|
None
|
nothing |
Source code in cupid_matching/utils.py
64 65 66 67 68 69 70 71 72 73 74 |
|
bs_name_func(back=2)
¶
Get the name of the current function, or further back in the stack
Parameters:
Name | Type | Description | Default |
---|---|---|---|
back |
int
|
2 for the current function, 3 for the function that called it, etc |
2
|
Returns:
Type | Description |
---|---|
str
|
the name of the function requested |
Source code in cupid_matching/utils.py
31 32 33 34 35 36 37 38 39 40 41 42 |
|
check_gradient_scalar_function(fg, p, args, mode='central', EPS=1e-06)
¶
Checks the gradient of a scalar function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fg |
Any
|
should return the scalar value, and the gradient if its |
required |
p |
np.ndarray
|
where we are checking the gradient |
required |
args |
list[Any]
|
other arguments passed to |
required |
mode |
str
|
"central" or "forward" derivatives |
'central'
|
EPS |
float
|
the step for forward or central derivatives |
1e-06
|
Returns:
Type | Description |
---|---|
tuple[np.ndarray, np.ndarray]
|
the analytic and numeric gradients |
Source code in cupid_matching/utils.py
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
|
der_nppow(a, b)
¶
evaluates the derivatives in a and b of element-by-element a^b
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a |
np.ndarray
|
input Numpy arrays |
required |
b |
int | float | np.ndarray
|
a Numpy array of the same shape, or a scalar |
required |
Returns:
Type | Description |
---|---|
TwoArrays
|
a pair of two arrays of the same shape as |
Source code in cupid_matching/utils.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
|
describe_array(v, name='The array')
¶
Descriptive statistics on an array interpreted as a vector
Parameters:
Name | Type | Description | Default |
---|---|---|---|
v |
np.ndarray
|
the array |
required |
name |
str
|
its name |
'The array'
|
Returns:
Type | Description |
---|---|
NamedTuple
|
a |
Source code in cupid_matching/utils.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
npexp(a, deriv=False, bigx=30.0, verbose=False)
¶
C^2 extension of \exp(a) above bigx
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a |
np.ndarray
|
a Numpy array |
required |
deriv |
bool
|
if |
False
|
bigx |
float
|
an upper bound |
30.0
|
verbose |
bool
|
whether diagnoses are printed |
False
|
Returns:
Name | Type | Description |
---|---|---|
bigx |
np.ndarray | TwoArrays
|
upper bound \exp(a) C^2-extended above |
np.ndarray | TwoArrays
|
with its derivative if |
Source code in cupid_matching/utils.py
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 |
|
nplog(a, deriv=False, eps=1e-30, verbose=False)
¶
C^2 extension of \ln(a) below eps
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a |
np.ndarray
|
a Numpy array |
required |
deriv |
bool
|
if |
False
|
eps |
float
|
a lower bound |
1e-30
|
verbose |
bool
|
whether diagnoses are printed |
False
|
Returns:
Type | Description |
---|---|
np.ndarray | tuple[np.ndarray, np.ndarray]
|
\ln(a) C^2-extended below |
np.ndarray | tuple[np.ndarray, np.ndarray]
|
with its derivative if |
Source code in cupid_matching/utils.py
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 |
|
npmaxabs(a)
¶
The maximum absolute value in an array
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a |
np.ndarray
|
the array |
required |
Returns:
Type | Description |
---|---|
float
|
\max{\vert a \vert} |
Source code in cupid_matching/utils.py
166 167 168 169 170 171 172 173 174 175 176 |
|
nppow(a, b, deriv=False)
¶
Evaluates a^b element-by-element
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a |
np.ndarray
|
a Numpy array |
required |
b |
int | float | np.ndarray
|
if an array, it should have the same shape as |
required |
deriv |
bool
|
if |
False
|
Returns:
Type | Description |
---|---|
np.ndarray | ThreeArrays
|
an array of the same shape as |
np.ndarray | ThreeArrays
|
the derivatives wrt |
Source code in cupid_matching/utils.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
|
nprepeat_col(v, n)
¶
Creates a matrix with n
columns, all equal to v
Parameters:
Name | Type | Description | Default |
---|---|---|---|
v |
np.ndarray
|
a vector of size |
required |
n |
int
|
the number of columns requested |
required |
:return: a matrix of shape (m, n)
Source code in cupid_matching/utils.py
137 138 139 140 141 142 143 144 145 146 147 148 |
|
nprepeat_row(v, m)
¶
Creates a matrix with m
rows, all equal to v
Parameters:
Name | Type | Description | Default |
---|---|---|---|
v |
np.ndarray
|
a vector of size |
required |
m |
int
|
the number of rows requested |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
a matrix of shape |
Source code in cupid_matching/utils.py
151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
print_stars(title=None, n=70)
¶
Prints a starred line, or two around the title
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
Optional[str]
|
an optional title |
None
|
n |
int
|
the number of stars on the line |
70
|
Returns:
Type | Description |
---|---|
None
|
nothing |
Source code in cupid_matching/utils.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
test_matrix(x, fun_name=None)
¶
Tests that x
is a matrix; aborts otherwise
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Any
|
a potential matrix |
required |
fun_name |
Optional[str]
|
the name of the calling function |
None
|
Returns:
Type | Description |
---|---|
tuple[int, int]
|
the shape of |
Source code in cupid_matching/utils.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
test_vector(x, fun_name=None)
¶
Tests that x
is a vector; aborts otherwise
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Any
|
a potential vector |
required |
fun_name |
Optional[str]
|
the name of the calling function |
None
|
Returns:
Type | Description |
---|---|
int
|
the size of |
Source code in cupid_matching/utils.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|