Skip to content

Commit

Permalink
Refactor vector.py to improve random vector generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Python-in-China committed Oct 27, 2024
1 parent c972b64 commit b4b5122
Showing 1 changed file with 68 additions and 10 deletions.
78 changes: 68 additions & 10 deletions cyaron/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import random
from enum import IntEnum
from typing import Sequence, Union, Tuple, List, Set
from typing import Optional, Sequence, TypeVar, Union, Tuple, List, Set
from typing import cast as typecast

from .utils import list_like
Expand All @@ -16,7 +16,8 @@ class VectorRandomMode(IntEnum):
float = 2


_Number = Union[int, float]
_Number = TypeVar('_Number', int, float)
_Range = List[Union[_Number, Tuple[_Number, _Number]]]


class Vector:
Expand All @@ -28,17 +29,16 @@ class Vector:
@staticmethod
def random(
num: int = 5,
position_range: Union[List[Union[_Number, Tuple[_Number, _Number]]],
None] = None,
position_range: Optional[_Range[Union[int, float]]] = None,
mode: VectorRandomMode = VectorRandomMode.unique,
) -> Union[IntVector, FloatVector]:
"""
Generate `num` random vectors in limited space
Args:
num: the number of vectors
num: the length of vectors
position_range: a list of limits for each dimension
single number x represents range (0, x)
list [x, y] or tuple (x, y) represents range (x, y)
single number x represents range [0, x]
list [x, y] or tuple (x, y) represents range [x, y]
mode: the mode vectors generate, see Enum Class VectorRandomMode
"""
if position_range is None:
Expand All @@ -49,8 +49,8 @@ def random(

dimension = len(position_range)

offset: Sequence[_Number] = []
length: Sequence[_Number] = []
offset: Sequence[Union[int, float]] = []
length: Sequence[Union[int, float]] = []

vector_space = 1
for i in range(0, dimension):
Expand Down Expand Up @@ -128,9 +128,67 @@ def get_vector(dimension: int, position_range: Sequence[int],
Returns:
list: A list representing the generated vector.
"""

tmp: List[int] = []
for i in range(0, dimension):
tmp.append(hashcode % (position_range[i] + 1))
hashcode //= (position_range[i] + 1)
return tmp

@staticmethod
def random_unique_vector(num: int = 5,
position_range: Union[_Range[int], None] = None):
"""
Generate `num` unique vectors with specified parameters. It is a wrapper for Vector.random.
Args:
num: the length of vectors
position_range: a list of limits for each dimension
single number x represents range [0, x]
list [x, y] or tuple (x, y) represents range [x, y]
"""
return typecast(
Vector.IntVector,
Vector.random(
num,
typecast(Optional[_Range[Union[int, float]]], position_range),
VectorRandomMode.unique))

@staticmethod
def random_repeatable_vector(
num: int = 5,
position_range: Optional[List[Union[int, Tuple[int,
int]]]] = None):
"""
Generate `num` repeatable vectors with specified parameters.
It is a wrapper for Vector.random.
Args:
num: the length of vectors
position_range: a list of limits for each dimension
single number x represents range [0, x]
list [x, y] or tuple (x, y) represents range [x, y]
"""
return typecast(
Vector.IntVector,
Vector.random(
num,
typecast(Optional[_Range[Union[int, float]]], position_range),
VectorRandomMode.repeatable))

@staticmethod
def random_float_vector(
num: int = 5,
position_range: Optional[_Range[Union[int, float]]] = None):
"""
Generate `num` float vectors with specified parameters.
It is a wrapper for Vector.random.
Args:
num: the length of vectors
position_range: a list of limits for each dimension
single number x represents range [0, x]
list [x, y] or tuple (x, y) represents range [x, y]
"""
return typecast(
Vector.FloatVector,
Vector.random(num, (position_range), VectorRandomMode.float))

0 comments on commit b4b5122

Please sign in to comment.