|
1 | | -#coding=utf8 |
| 1 | +# coding=utf8 |
2 | 2 |
|
3 | | -from .consts import ALPHABET_SMALL |
4 | 3 | from .utils import * |
5 | 4 | import random |
| 5 | +from enum import Enum |
| 6 | + |
| 7 | + |
| 8 | +class VectorRandomMode(Enum): |
| 9 | + unique = 0 |
| 10 | + repeatable = 1 |
| 11 | + float = 2 |
6 | 12 |
|
7 | 13 |
|
8 | 14 | class Vector: |
| 15 | + |
9 | 16 | @staticmethod |
10 | | - def random(num=5, position_range=[10], mode=0, **kwargs): |
11 | | - # mode 0=unique 1=repeatable 2=float |
12 | | - if(num > 1000000): |
| 17 | + def random(num: int = 5, position_range: list = None, mode: VectorRandomMode = 0, **kwargs): |
| 18 | + """ |
| 19 | + brief : generating n random vectors in limited space |
| 20 | + param : |
| 21 | + # num : the number of vectors |
| 22 | + # position_range : a list of limits for each dimension |
| 23 | + # single number x represents range (0, x) |
| 24 | + # list [x, y] or tuple (x, y) represents range (x, y) |
| 25 | + # mode : the mode vectors generate, see Enum Class VectorRandomMode |
| 26 | + """ |
| 27 | + if position_range is None: |
| 28 | + position_range = [10] |
| 29 | + |
| 30 | + if num > 1000000: |
13 | 31 | raise Exception("num no more than 1e6") |
14 | | - if(not list_like(position_range)): |
15 | | - raise Exception("the 2nd param must be a list") |
| 32 | + if not list_like(position_range): |
| 33 | + raise Exception("the 2nd param must be a list or tuple") |
16 | 34 |
|
17 | 35 | dimension = len(position_range) |
| 36 | + |
18 | 37 | offset = [] |
| 38 | + length = [] |
| 39 | + |
19 | 40 | vector_space = 1 |
20 | 41 | for i in range(0, dimension): |
21 | | - if(list_like(position_range[i])): |
22 | | - if(position_range[i][1] < position_range[i][0]): |
23 | | - raise Exception("max should larger than min") |
24 | | - offset.insert(i, position_range[i][0]) |
25 | | - position_range[i] = position_range[i][1] - offset[i] |
| 42 | + if list_like(position_range[i]): |
| 43 | + if position_range[i][1] < position_range[i][0]: |
| 44 | + raise Exception("upper-bound should larger than lower-bound") |
| 45 | + offset.append(position_range[i][0]) |
| 46 | + length.append(position_range[i][1] - position_range[i][0]) |
26 | 47 | else: |
27 | | - offset.insert(i, 0) |
28 | | - if(position_range[i] <= 0): |
29 | | - raise Exception("the difference must more than 0") |
30 | | - vector_space *= (position_range[i] + 1) |
31 | | - if(mode == 0 and num > vector_space): |
32 | | - raise Exception("1st param is too large that CYaRon can not generate unique vectors") |
| 48 | + offset.append(0) |
| 49 | + length.append(position_range[i]) |
| 50 | + vector_space *= (length[i] + 1) |
| 51 | + |
| 52 | + if mode == VectorRandomMode.unique and num > vector_space: |
| 53 | + raise Exception("1st param is so large that CYaRon can not generate unique vectors") |
| 54 | + |
33 | 55 | result = [] |
34 | | - |
35 | | - if(mode == 2 or mode == 1): |
36 | | - for i in range(0, num): |
37 | | - tmp = [] |
38 | | - for j in range(0, dimension): |
39 | | - one_num = random.randint(0,position_range[j]) if mode == 1 else random.uniform(0,position_range[j]) |
40 | | - tmp.insert(j, one_num + offset[j]) |
41 | | - result.insert(i, tmp) |
42 | | - |
43 | | - elif((mode == 0 and vector_space > 5 * num)): |
44 | | - num_set = set([]) |
45 | | - rand = 0; |
| 56 | + if mode == VectorRandomMode.repeatable: |
| 57 | + result = [[random.randint(x, y) for x, y in zip(offset, length)] for _ in range(num)] |
| 58 | + elif mode == VectorRandomMode.float: |
| 59 | + result = [[random.uniform(x, y) for x, y in zip(offset, length)] for _ in range(num)] |
| 60 | + elif mode == VectorRandomMode.unique and vector_space > 5 * num: |
| 61 | + # O(NlogN) |
| 62 | + num_set = set() |
46 | 63 | for i in range(0, num): |
47 | 64 | while True: |
48 | | - rand = random.randint(0, vector_space - 1); |
49 | | - if(not rand in num_set): |
| 65 | + rand = random.randint(0, vector_space - 1) |
| 66 | + if rand not in num_set: |
50 | 67 | break |
51 | | - # Todo: So how to analyse then complexity? I think it is logn |
52 | 68 | num_set.add(rand) |
53 | | - tmp = Vector.get_vector(dimension, position_range, rand) |
| 69 | + tmp = Vector.get_vector(dimension, length, rand) |
54 | 70 | for j in range(0, dimension): |
55 | 71 | tmp[j] += offset[j] |
56 | | - result.insert(i, tmp) |
57 | | - |
58 | | - |
| 72 | + result.append(tmp) |
59 | 73 | else: |
60 | 74 | # generate 0~vector_space and shuffle |
61 | | - rand_arr = [i for i in range(0, vector_space)] |
| 75 | + rand_arr = list(range(0, vector_space)) |
62 | 76 | random.shuffle(rand_arr) |
63 | | - for i in range(0, num): |
64 | | - tmp = Vector.get_vector(dimension, position_range, rand_arr[i]) |
65 | | - for j in range(0, dimension): |
66 | | - tmp[j] += offset[j] |
67 | | - result.insert(i, tmp) |
| 77 | + result = [Vector.get_vector(dimension, length, x) for x in rand_arr[:num]] |
| 78 | + |
| 79 | + for x in result: |
| 80 | + for i in range(dimension): |
| 81 | + x[i] += offset[i] |
| 82 | + |
68 | 83 | return result |
69 | 84 |
|
70 | | - @staticmethod |
71 | | - def get_vector(dimension, position_range, hashnum): |
| 85 | + @staticmethod |
| 86 | + def get_vector(dimension: int, position_range: list, hashcode: int): |
72 | 87 | tmp = [] |
73 | 88 | for i in range(0, dimension): |
74 | | - tmp.insert(i, hashnum % (position_range[i] + 1)) |
75 | | - hashnum //= (position_range[i] + 1) |
| 89 | + tmp.append(hashcode % (position_range[i] + 1)) |
| 90 | + hashcode //= (position_range[i] + 1) |
76 | 91 | return tmp |
77 | | - |
78 | | - |
|
0 commit comments