Datachain

Numeric Functions

Functions for numeric operations, bit manipulation, and mathematical computations.

numeric

bit_and

bit_and(*args: str | Column | Func | int) -> Func

Returns a function that computes the bitwise AND operation between two values.

Parameters:

  • args (str | Column | Func | int, default: () ) –

    Two values to compute the bitwise AND operation between. If a string is provided, it is assumed to be the name of the column. If a Column is provided, it is assumed to be a column. If a Func is provided, it is assumed to be a function returning an int. If an integer is provided, it is assumed to be a constant value.

Returns:

  • Func ( Func ) –

    A Func object that represents the bitwise AND function.

Example
dc.mutate(
    and1=func.bit_and("signal.value", 0x0F),
    and2=func.bit_and(dc.C("signal.value1"), "signal.value2"),
)
Notes
  • The result column will always be of type int.
Source code in datachain/func/numeric.py
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
def bit_and(*args: str | Column | Func | int) -> Func:
    """
    Returns a function that computes the bitwise AND operation between two values.

    Args:
        args (str | Column | Func | int): Two values to compute
            the bitwise AND operation between.
            If a string is provided, it is assumed to be the name of the column.
            If a Column is provided, it is assumed to be a column.
            If a Func is provided, it is assumed to be a function returning an int.
            If an integer is provided, it is assumed to be a constant value.

    Returns:
        Func: A `Func` object that represents the bitwise AND function.

    Example:
        ```py
        dc.mutate(
            and1=func.bit_and("signal.value", 0x0F),
            and2=func.bit_and(dc.C("signal.value1"), "signal.value2"),
        )
        ```

    Notes:
        - The result column will always be of type int.
    """
    cols, func_args = [], []
    for arg in args:
        if isinstance(arg, int):
            func_args.append(arg)
        else:
            cols.append(arg)

    if len(cols) + len(func_args) != 2:
        raise ValueError("bit_and() requires exactly two arguments")

    return Func(
        "bit_and",
        inner=numeric.bit_and,
        cols=cols,
        args=func_args,
        result_type=int,
    )

bit_hamming_distance

bit_hamming_distance(*args: str | Column | Func | int) -> Func

Returns a function that computes the Hamming distance between two integers.

The Hamming distance is the number of positions at which the corresponding bits are different. This function returns the dissimilarity between the integers, where 0 indicates identical integers and values closer to the number of bits in the integer indicate higher dissimilarity.

Parameters:

  • args (str | Column | Func | int, default: () ) –

    Two integers to compute the Hamming distance between. If a string is provided, it is assumed to be the name of the column. If a Column is provided, it is assumed to be a column. If a Func is provided, it is assumed to be a function returning an int. If an int is provided, it is assumed to be an integer literal.

Returns:

  • Func ( Func ) –

    A Func object that represents the Hamming distance function.

Example
dc.mutate(
    hd1=func.bit_hamming_distance("signal.value1", "signal.value2"),
    hd2=func.bit_hamming_distance(dc.C("signal.value1"), 0x0F),
)
Notes
  • The result column will always be of type int.
Source code in datachain/func/numeric.py
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
def bit_hamming_distance(*args: str | Column | Func | int) -> Func:
    """
    Returns a function that computes the Hamming distance between two integers.

    The Hamming distance is the number of positions at which the corresponding bits
    are different. This function returns the dissimilarity between the integers,
    where 0 indicates identical integers and values closer to the number of bits
    in the integer indicate higher dissimilarity.

    Args:
        args (str | Column | Func | int): Two integers to compute
            the Hamming distance between.
            If a string is provided, it is assumed to be the name of the column.
            If a Column is provided, it is assumed to be a column.
            If a Func is provided, it is assumed to be a function returning an int.
            If an int is provided, it is assumed to be an integer literal.

    Returns:
        Func: A `Func` object that represents the Hamming distance function.

    Example:
        ```py
        dc.mutate(
            hd1=func.bit_hamming_distance("signal.value1", "signal.value2"),
            hd2=func.bit_hamming_distance(dc.C("signal.value1"), 0x0F),
        )
        ```

    Notes:
        - The result column will always be of type int.
    """
    cols, func_args = [], []
    for arg in args:
        if isinstance(arg, int):
            func_args.append(arg)
        else:
            cols.append(arg)

    if len(cols) + len(func_args) != 2:
        raise ValueError("bit_hamming_distance() requires exactly two arguments")

    return Func(
        "bit_hamming_distance",
        inner=numeric.bit_hamming_distance,
        cols=cols,
        args=func_args,
        result_type=int,
    )

bit_or

bit_or(*args: str | Column | Func | int) -> Func

Returns a function that computes the bitwise OR operation between two values.

Parameters:

  • args (str | Column | Func | int, default: () ) –

    Two values to compute the bitwise OR operation between. If a string is provided, it is assumed to be the name of the column. If a Column is provided, it is assumed to be a column. If a Func is provided, it is assumed to be a function returning an int. If an integer is provided, it is assumed to be a constant value.

Returns:

  • Func ( Func ) –

    A Func object that represents the bitwise OR function.

Example
dc.mutate(
    or1=func.bit_or("signal.value", 0x0F),
    or2=func.bit_or(dc.C("signal.value1"), "signal.value2"),
)
Notes
  • The result column will always be of type int.
Source code in datachain/func/numeric.py
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
def bit_or(*args: str | Column | Func | int) -> Func:
    """
    Returns a function that computes the bitwise OR operation between two values.

    Args:
        args (str | Column | Func | int): Two values to compute
            the bitwise OR operation between.
            If a string is provided, it is assumed to be the name of the column.
            If a Column is provided, it is assumed to be a column.
            If a Func is provided, it is assumed to be a function returning an int.
            If an integer is provided, it is assumed to be a constant value.

    Returns:
        Func: A `Func` object that represents the bitwise OR function.

    Example:
        ```py
        dc.mutate(
            or1=func.bit_or("signal.value", 0x0F),
            or2=func.bit_or(dc.C("signal.value1"), "signal.value2"),
        )
        ```

    Notes:
        - The result column will always be of type int.
    """
    cols, func_args = [], []
    for arg in args:
        if isinstance(arg, int):
            func_args.append(arg)
        else:
            cols.append(arg)

    if len(cols) + len(func_args) != 2:
        raise ValueError("bit_or() requires exactly two arguments")

    return Func(
        "bit_or",
        inner=numeric.bit_or,
        cols=cols,
        args=func_args,
        result_type=int,
    )

bit_xor

bit_xor(*args: str | Column | Func | int) -> Func

Returns a function that computes the bitwise XOR operation between two values.

Parameters:

  • args (str | Column | Func | int, default: () ) –

    Two values to compute the bitwise XOR operation between. If a string is provided, it is assumed to be the name of the column. If a Column is provided, it is assumed to be a column. If a Func is provided, it is assumed to be a function returning an int. If an integer is provided, it is assumed to be a constant value.

Returns:

  • Func ( Func ) –

    A Func object that represents the bitwise XOR function.

Example
dc.mutate(
    xor1=func.bit_xor("signal.value", 0x0F),
    xor2=func.bit_xor(dc.C("signal.value1"), "signal.value2"),
)
Notes
  • The result column will always be of type int.
Source code in datachain/func/numeric.py
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
def bit_xor(*args: str | Column | Func | int) -> Func:
    """
    Returns a function that computes the bitwise XOR operation between two values.

    Args:
        args (str | Column | Func | int): Two values to compute
            the bitwise XOR operation between.
            If a string is provided, it is assumed to be the name of the column.
            If a Column is provided, it is assumed to be a column.
            If a Func is provided, it is assumed to be a function returning an int.
            If an integer is provided, it is assumed to be a constant value.

    Returns:
        Func: A `Func` object that represents the bitwise XOR function.

    Example:
        ```py
        dc.mutate(
            xor1=func.bit_xor("signal.value", 0x0F),
            xor2=func.bit_xor(dc.C("signal.value1"), "signal.value2"),
        )
        ```

    Notes:
        - The result column will always be of type int.
    """
    cols, func_args = [], []
    for arg in args:
        if isinstance(arg, int):
            func_args.append(arg)
        else:
            cols.append(arg)

    if len(cols) + len(func_args) != 2:
        raise ValueError("bit_xor() requires exactly two arguments")

    return Func(
        "bit_xor",
        inner=numeric.bit_xor,
        cols=cols,
        args=func_args,
        result_type=int,
    )

int_hash_64

int_hash_64(col: str | Column | Func | int) -> Func

Returns a function that computes the 64-bit hash of an integer.

Parameters:

  • col (str | Column | Func | int) –

    Integer to compute the hash of. If a string is provided, it is assumed to be the name of the column. If a Column is provided, it is assumed to be a column. If a Func is provided, it is assumed to be a function returning an int. If an int is provided, it is assumed to be an int literal.

Returns:

  • Func ( Func ) –

    A Func object that represents the 64-bit hash function.

Example
dc.mutate(
    val_hash=func.int_hash_64("val"),
    val_hash2=func.int_hash_64(dc.C("val2")),
)
Notes
  • The result column will always be of type int.
Source code in datachain/func/numeric.py
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
def int_hash_64(col: str | Column | Func | int) -> Func:
    """
    Returns a function that computes the 64-bit hash of an integer.

    Args:
        col (str | Column | Func | int): Integer to compute the hash of.
            If a string is provided, it is assumed to be the name of the column.
            If a Column is provided, it is assumed to be a column.
            If a Func is provided, it is assumed to be a function returning an int.
            If an int is provided, it is assumed to be an int literal.

    Returns:
        Func: A `Func` object that represents the 64-bit hash function.

    Example:
        ```py
        dc.mutate(
            val_hash=func.int_hash_64("val"),
            val_hash2=func.int_hash_64(dc.C("val2")),
        )
        ```

    Notes:
        - The result column will always be of type int.
    """
    cols, args = [], []
    if isinstance(col, int):
        args.append(col)
    else:
        cols.append(col)

    return Func(
        "int_hash_64", inner=numeric.int_hash_64, cols=cols, args=args, result_type=int
    )