DNDSR 0.1.0.dev1+gcd065ad
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
ProductDecompose.py
Go to the documentation of this file.
1import numpy as np
2import sympy
3
4
5def sum_partition_greedy(arr: np.ndarray, n_part: int = 2):
6 """Partitions numbers into M subsets using Largest-First Decreasing (LFD)."""
7 numbers = sorted(
8 [(arr.flat[i], i) for i in range(arr.size)], reverse=True, key=lambda v: v[0]
9 ) # Sort largest first
10 sums = np.zeros((n_part,), dtype=np.float64) # Track partition sums
11 # print(numbers)
12
13 partitions = np.zeros_like(arr, dtype=np.int64)
14
15 for num, i in numbers:
16 idx = np.argmin(sums) # Find partition with smallest sum
17 sums[idx] += num # Update sum
18 partitions.flat[i] = idx
19 # print(f"{i}, {idx}, {sums}")
20 # print(partitions)
21
22 return partitions
23
24
25def int_factor_divide(N: int, n_part: int = 2):
26 factors = sympy.factorint(N)
27 factor_list = []
28 for f, p in factors.items():
29 factor_list += [f] * p
30 factor_list = np.array(factor_list, dtype=np.int64)
31 assert factor_list.prod() == N
32 partition = sum_partition_greedy(
33 np.log2((factor_list * 2).astype(np.float64)), n_part
34 )
35 prods = np.array(
36 [np.prod(factor_list[partition == i]) for i in range(n_part)], dtype=np.int64
37 )
38 assert prods.prod() == N
39 return prods
40
41
42if __name__ == "__main__":
43
44 for i in np.arange(2, 201) * 64:
46 diffv = prods.max() - prods.min()
47 if diffv > np.sqrt(i) * 0:
48 print(f"{i:6}: {str(prods):32}: {diffv}")
49 print(int_factor_divide(12))
50
51 arr = np.array([1, 3, 5, 7, 9, 11])
52 arr = np.sort(arr)
53 target = 11
54
55 # searchsorted gives the insertion point for target
56 index = np.searchsorted(arr, target, side="right") - 1
57 print(index)
int_factor_divide(int N, int n_part=2)
sum_partition_greedy(np.ndarray arr, int n_part=2)