Skip to content

Commit 821867a

Browse files
authored
Create vector.py
1 parent e325c0e commit 821867a

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

cyaron/vector.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from .consts import ALPHABET_SMALL
2+
from .utils import *
3+
import random
4+
5+
6+
class Vector:
7+
@staticmethod
8+
def random(num=5, position_range=[10], mode=0, **kwargs):
9+
# mode 0=unique 1=repeatable 2=float
10+
if(num > 1000000):
11+
raise Exception("num no more than 1e6")
12+
if(not list_like(position_range)):
13+
raise Exception("the 2nd param must be a list, whose length = 1st param")
14+
dimension = len(position_range)
15+
offset = []
16+
vector_space = 1
17+
for i in range(0, dimension):
18+
if(list_like(position_range[i])):
19+
if(position_range[i][1] < position_range[i][0]):
20+
raise Exception("max should larger than min")
21+
offset.insert(i, position_range[i][0])
22+
position_range[i] = position_range[i][1] - offset[i]
23+
else:
24+
offset.insert(i, 0)
25+
if(position_range[i] <= 0):
26+
raise Exception("the difference must more than 0")
27+
vector_space *= (position_range[i] + 1)
28+
result = []
29+
30+
if(mode == 2 or mode == 1):
31+
for i in range(0, num):
32+
tmp = []
33+
for j in range(0, dimension):
34+
one_num = random.randint(0,position_range[j]) if mode == 1 else random.uniform(0,position_range[j])
35+
tmp.insert(j, one_num + offset[j])
36+
result.insert(i, tmp)
37+
38+
elif((mode == 0 and vector_space > 5 * num)):
39+
num_set = set([])
40+
rand = 0;
41+
for i in range(0, num):
42+
while True:
43+
rand = random.randint(0, vector_space - 1);
44+
if(not rand in num_set):
45+
break
46+
# Todo: 这边效率如何?我认为是logn级别的检查
47+
num_set.add(rand)
48+
tmp = Vector.get_vector(dimension, position_range, rand)
49+
for j in range(0, dimension):
50+
tmp[j] += offset[j]
51+
result.insert(i, tmp)
52+
53+
54+
else:
55+
# 生成0~vector_space的所有向量空间
56+
rand_arr = [i for i in range(0, vector_space)]
57+
random.shuffle(rand_arr)
58+
for i in range(0, num):
59+
tmp = Vector.get_vector(dimension, position_range, rand_arr[i])
60+
for j in range(0, dimension):
61+
tmp[j] += offset[j]
62+
result.insert(i, tmp)
63+
return result
64+
65+
@staticmethod
66+
def get_vector(dimension, position_range, hashnum):
67+
tmp = []
68+
for i in range(0, dimension):
69+
tmp.insert(i, hashnum % (position_range[i] + 1))
70+
hashnum //= (position_range[i] + 1)
71+
return tmp

0 commit comments

Comments
 (0)