-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmath_nodes.py
More file actions
379 lines (353 loc) · 8.78 KB
/
math_nodes.py
File metadata and controls
379 lines (353 loc) · 8.78 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
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
import random
from .autonode import node_wrapper, get_node_names_mappings, validate, anytype
classes = []
node = node_wrapper(classes)
import math
@node
class MinNode:
"""
Returns the minimum of two values
"""
RETURN_TYPES = (anytype,)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input1": (anytype,),
"input2": (anytype,),
}
}
FUNCTION = "min"
CATEGORY = "Math"
custom_name = "Min"
def min(self, input1, input2):
return (min(input1, input2),)
@node
class MaxNode:
"""
Returns the maximum of two values
"""
RETURN_TYPES = (anytype,)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input1": (anytype,),
"input2": (anytype,),
}
}
FUNCTION = "max"
CATEGORY = "Math"
custom_name = "Max"
def max(self, input1, input2):
return (max(input1, input2),)
@node
class RoundNode:
"""
Rounds a value to the nearest integer
"""
RETURN_TYPES = ("INT",)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input1": (anytype,),
}
}
FUNCTION = "round"
CATEGORY = "Math"
custom_name = "Round"
def round(self, input1):
return (round(input1),)
@node
class AbsNode:
"""
Returns the absolute value of a number
"""
RETURN_TYPES = (anytype,)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input1": (anytype,),
}
}
FUNCTION = "abs"
CATEGORY = "Math"
custom_name = "Abs"
def abs(self, input1):
return (abs(input1),)
@node
class FloorNode:
"""
Returns the floor of a number
"""
RETURN_TYPES = ("INT",)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input1": (anytype,),
}
}
FUNCTION = "floor"
CATEGORY = "Math"
custom_name = "Floor"
def floor(self, input1):
return (math.floor(input1),)
@node
class CeilNode:
"""
Returns the ceiling of a number
"""
RETURN_TYPES = ("INT",)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input1": (anytype,),
}
}
FUNCTION = "ceil"
CATEGORY = "Math"
custom_name = "Ceil"
def ceil(self, input1):
return (math.ceil(input1),)
@node
class PowerNode:
"""
Returns the power of a number
"""
RETURN_TYPES = (anytype,)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input1": (anytype,),
"power": (anytype,),
}
}
FUNCTION = "power"
CATEGORY = "Math"
custom_name = "Power"
def power(self, input1, power):
abs_input = abs(input1)
# fast paths for values that won't overflow digit-wise
if abs_input == 0:
if power < 0:
raise ZeroDivisionError("0 cannot be raised to a negative power")
return (math.pow(input1, power),)
if abs_input == 1:
return (math.pow(input1, power),)
# validate power with log10 scale, prevent huge magnitudes
log10_abs = math.log10(abs_input)
if (log10_abs * power) > 100:
raise OverflowError("Power is too large, exceeds 100 digits")
return (math.pow(input1, power),)
@node
class SigmoidNode:
"""
Returns the sigmoid of a number
"""
RETURN_TYPES = ("FLOAT",)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input1": ("FLOAT",),
}
}
FUNCTION = "sigmoid"
CATEGORY = "Math"
custom_name = "Sigmoid"
def sigmoid(self, input1):
return (1 / (1 + math.exp(-input1)),)
def is_prime_small(n: int) -> bool:
"""
Deterministic check for primality for smaller n.
Skips multiples of 2 and 3, then checks i, i+2, i+4 up to sqrt(n).
"""
if n < 2:
return False
if n in (2, 3):
return True
if n % 2 == 0 or n % 3 == 0:
return n == 2 or n == 3
# 6k ± 1 optimization
limit = int(math.isqrt(n)) # integer sqrt
i = 5
while i <= limit:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def miller_rabin_test(d: int, n: int) -> bool:
""" One round of the Miller-Rabin test with a random base 'a'. """
a = random.randrange(2, n - 1)
x = pow(a, d, n) # a^d % n
if x == 1 or x == n - 1:
return True
# Keep squaring x while d does not reach n-1
while d != n - 1:
x = (x * x) % n
d <<= 1 # d *= 2
if x == 1:
return False
if x == n - 1:
return True
return False
def is_prime_miller_rabin(n: int, k: int = 5) -> bool:
"""
Miller-Rabin primality test with k rounds (probabilistic).
Good enough for big integers in practice.
"""
# Handle small or trivial cases
if n < 2:
return False
# check small primes quickly
for small_prime in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]:
if n == small_prime:
return True
if n % small_prime == 0 and n != small_prime:
return False
# Write n - 1 as d * 2^r
d = n - 1
while d % 2 == 0:
d //= 2
# Witness loop
for _ in range(k):
if not miller_rabin_test(d, n):
return False
return True
@node
class IsPrimeNode:
"""
Checks if an integer is prime.
- If the integer |value| < threshold, uses a deterministic small-check (trial division).
- Otherwise, uses a Miller-Rabin pseudoprime test for a faster check (probabilistic).
Returns a BOOLEAN (True if prime, False if composite).
"""
FUNCTION = "is_prime"
RETURN_TYPES = ("BOOLEAN",)
CATEGORY = "Math"
custom_name = "Is Prime?"
@staticmethod
def is_prime(value: int, threshold: int = 10_000_000, miller_rabin_rounds: int = 5):
# handle negative or zero
if value < 2:
return (False,)
if value < threshold:
# use small prime check
return (is_prime_small(value),)
else:
# use Miller-Rabin
return (is_prime_miller_rabin(value, k=miller_rabin_rounds),)
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"value": ("INT", {"default": 1, "min": -9999999999, "max": 9999999999, "step": 1}),
},
"optional": {
"threshold": ("INT", {"default": 10_000_000, "min": 1, "max": 9999999999, "step": 1}),
"miller_rabin_rounds": ("INT", {"default": 5, "min": 1, "max": 50, "step": 1}),
}
}
@node
class RAMPNode:
"""
Returns the ramp of a number
"""
RETURN_TYPES = ("FLOAT",)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input1": ("FLOAT",),
}
}
FUNCTION = "ramp"
CATEGORY = "Math"
custom_name = "RAMP"
def ramp(self, input1):
return (max(0, input1),)
@node
class ModuloNode:
"""
Returns the modulo of a number
"""
RETURN_TYPES = ("INT",)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input1": ("INT",),
"modulo": ("INT",),
}
}
FUNCTION = "modulo"
CATEGORY = "Math"
custom_name = "Modulo"
def modulo(self, input1, modulo):
return (input1 % modulo,)
@node
class LogNode:
"""
Returns the log of a number
"""
RETURN_TYPES = ("FLOAT",)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input1": ("FLOAT",),
"base": ("FLOAT",),
}
}
FUNCTION = "log"
CATEGORY = "Math"
custom_name = "Log"
def log(self, input1, base):
return (math.log(input1, base),)
@node
class MultiplyNode:
"""
Returns the product of two numbers
"""
RETURN_TYPES = (anytype,)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input1": (anytype,),
"input2": (anytype,),
}
}
FUNCTION = "multiply"
CATEGORY = "Math"
custom_name = "Multiply"
def multiply(self, input1, input2):
return (input1 * input2,)
@node
class DivideNode:
"""
Returns the quotient of two numbers
"""
RETURN_TYPES = ("FLOAT",)
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input1": (anytype,),
"input2": (anytype,),
}
}
FUNCTION = "divide"
CATEGORY = "Math"
custom_name = "Divide"
def divide(self, input1, input2):
if input2 == 0:
raise ZeroDivisionError("Cannot divide by zero")
return (input1 / input2,)
validate(classes)
CLASS_MAPPINGS, CLASS_NAMES = get_node_names_mappings(classes)