2010-09-06 22:15:53 +08:00
|
|
|
"""
|
2023-10-04 02:25:29 +08:00
|
|
|
The :mod:`sklearn.utils.arrayfuncs` module includes a small collection of auxiliary
|
|
|
|
|
functions that operate on arrays.
|
2010-09-06 22:15:53 +08:00
|
|
|
"""
|
2019-03-01 01:45:15 +08:00
|
|
|
|
2019-02-21 02:23:34 +08:00
|
|
|
from cython cimport floating
|
|
|
|
|
from libc.math cimport fabs
|
2013-01-03 21:58:16 +08:00
|
|
|
from libc.float cimport DBL_MAX, FLT_MAX
|
|
|
|
|
|
2019-02-21 02:23:34 +08:00
|
|
|
from ._cython_blas cimport _copy, _rotg, _rot
|
2010-11-05 21:54:09 +08:00
|
|
|
|
2010-09-06 22:15:53 +08:00
|
|
|
|
2023-02-28 03:32:35 +08:00
|
|
|
def min_pos(const floating[:] X):
|
2021-02-04 05:52:18 +08:00
|
|
|
"""Find the minimum value of an array over positive values
|
|
|
|
|
|
|
|
|
|
Returns the maximum representable value of the input dtype if none of the
|
|
|
|
|
values are positive.
|
|
|
|
|
"""
|
|
|
|
|
cdef Py_ssize_t i
|
|
|
|
|
cdef floating min_val = FLT_MAX if floating is float else DBL_MAX
|
2023-02-28 03:32:35 +08:00
|
|
|
for i in range(X.size):
|
2021-02-04 05:52:18 +08:00
|
|
|
if 0. < X[i] < min_val:
|
|
|
|
|
min_val = X[i]
|
|
|
|
|
return min_val
|
2010-09-15 04:02:44 +08:00
|
|
|
|
2012-10-31 16:05:35 +08:00
|
|
|
|
2019-02-21 02:23:34 +08:00
|
|
|
# General Cholesky Delete.
|
|
|
|
|
# Remove an element from the cholesky factorization
|
|
|
|
|
# m = columns
|
|
|
|
|
# n = rows
|
|
|
|
|
#
|
|
|
|
|
# TODO: put transpose as an option
|
2023-11-13 21:48:46 +08:00
|
|
|
def cholesky_delete(floating[:, :] L, int go_out):
|
2023-04-19 22:32:53 +08:00
|
|
|
cdef:
|
|
|
|
|
int n = L.shape[0]
|
|
|
|
|
int m = L.strides[0]
|
|
|
|
|
floating c, s
|
|
|
|
|
floating *L1
|
|
|
|
|
int i
|
2022-05-14 21:58:31 +08:00
|
|
|
|
2023-04-19 22:32:53 +08:00
|
|
|
if floating is float:
|
|
|
|
|
m /= sizeof(float)
|
|
|
|
|
else:
|
|
|
|
|
m /= sizeof(double)
|
2019-02-21 02:23:34 +08:00
|
|
|
|
2023-04-19 22:32:53 +08:00
|
|
|
# delete row go_out
|
|
|
|
|
L1 = &L[0, 0] + (go_out * m)
|
|
|
|
|
for i in range(go_out, n-1):
|
|
|
|
|
_copy(i + 2, L1 + m, 1, L1, 1)
|
|
|
|
|
L1 += m
|
2019-02-21 02:23:34 +08:00
|
|
|
|
2023-04-19 22:32:53 +08:00
|
|
|
L1 = &L[0, 0] + (go_out * m)
|
|
|
|
|
for i in range(go_out, n-1):
|
|
|
|
|
_rotg(L1 + i, L1 + i + 1, &c, &s)
|
|
|
|
|
if L1[i] < 0:
|
|
|
|
|
# Diagonals cannot be negative
|
|
|
|
|
L1[i] = fabs(L1[i])
|
|
|
|
|
c = -c
|
|
|
|
|
s = -s
|
2019-02-21 02:23:34 +08:00
|
|
|
|
2023-04-19 22:32:53 +08:00
|
|
|
L1[i + 1] = 0. # just for cleanup
|
|
|
|
|
L1 += m
|
2019-02-21 02:23:34 +08:00
|
|
|
|
2023-04-19 22:32:53 +08:00
|
|
|
_rot(n - i - 2, L1 + i, m, L1 + i + 1, m, c, s)
|