CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
arrayfuncs.pyx119 linesDownload Raw Back to utils
1"""A small collection of auxiliary functions that operate on arrays."""
2
3from cython cimport floating
4from libc.math cimport fabs
5from libc.float cimport DBL_MAX, FLT_MAX
6
7from ._cython_blas cimport _copy, _rotg, _rot
8
9
10ctypedef fused real_numeric:
11    short
12    int
13    long
14    long long
15    float
16    double
17
18
19def min_pos(const floating[:] X):
20    """Find the minimum value of an array over positive values.
21
22    Returns the maximum representable value of the input dtype if none of the
23    values are positive.
24
25    Parameters
26    ----------
27    X : ndarray of shape (n,)
28        Input array.
29
30    Returns
31    -------
32    min_val : float
33        The smallest positive value in the array, or the maximum representable value
34         of the input dtype if no positive values are found.
35
36    Examples
37    --------
38    >>> import numpy as np
39    >>> from sklearn.utils.arrayfuncs import min_pos
40    >>> X = np.array([0, -1, 2, 3, -4, 5])
41    >>> min_pos(X)
42    2.0
43    """
44    cdef Py_ssize_t i
45    cdef floating min_val = FLT_MAX if floating is float else DBL_MAX
46    for i in range(X.size):
47        if 0. < X[i] < min_val:
48            min_val = X[i]
49    return min_val
50
51
52def _all_with_any_reduction_axis_1(real_numeric[:, :] array, real_numeric value):
53    """Check whether any row contains all values equal to `value`.
54
55    It is equivalent to `np.any(np.all(X == value, axis=1))`, but it avoids to
56    materialize the temporary boolean matrices in memory.
57
58    Parameters
59    ----------
60    array: array-like
61        The array to be checked.
62    value: short, int, long, float, or double
63        The value to use for the comparison.
64
65    Returns
66    -------
67    any_all_equal: bool
68        Whether or not any rows contains all values equal to `value`.
69    """
70    cdef Py_ssize_t i, j
71
72    for i in range(array.shape[0]):
73        for j in range(array.shape[1]):
74            if array[i, j] != value:
75                break
76        else:  # no break
77            return True
78    return False
79
80
81# General Cholesky Delete.
82# Remove an element from the cholesky factorization
83# m = columns
84# n = rows
85#
86# TODO: put transpose as an option
87def cholesky_delete(floating[:, :] L, int go_out):
88    cdef:
89        int n = L.shape[0]
90        int m = L.strides[0]
91        floating c, s
92        floating *L1
93        int i
94
95    if floating is float:
96        m /= sizeof(float)
97    else:
98        m /= sizeof(double)
99
100    # delete row go_out
101    L1 = &L[0, 0] + (go_out * m)
102    for i in range(go_out, n-1):
103        _copy(i + 2, L1 + m, 1, L1, 1)
104        L1 += m
105
106    L1 = &L[0, 0] + (go_out * m)
107    for i in range(go_out, n-1):
108        _rotg(L1 + i, L1 + i + 1, &c, &s)
109        if L1[i] < 0:
110            # Diagonals cannot be negative
111            L1[i] = fabs(L1[i])
112            c = -c
113            s = -s
114
115        L1[i + 1] = 0.  # just for cleanup
116        L1 += m
117
118        _rot(n - i - 2, L1 + i, m, L1 + i + 1, m, c, s)
119 
Aluode/PerceptionLabPortable · CoolFace