nd2py.generator.eq package#

Submodules#

nd2py.generator.eq.enumerator module#

class nd2py.generator.eq.enumerator.Enumerator(leafs: List[Variable | Number], binary: List[Type[Symbol]], unary: List[Type[Symbol]])[source]#

Bases: object

Enumerate all possible expression trees of a given length.

This generator exhaustively constructs all valid expression trees by combining given leaf nodes, binary operators, and unary operators to reach the target tree size (measured by number of nodes).

Example

>>> from nd2py import Variable
>>> from nd2py.generator.eq import Enumerator
>>> x = Variable('x')
>>> enumerator = Enumerator(leafs=[x], binary=[Add, Mul], unary=[])
>>> for eq in enumerator(length=3): print(eq)
x + x
x * x
__init__(leafs: List[Variable | Number], binary: List[Type[Symbol]], unary: List[Type[Symbol]])[source]#

Initialize the enumerator.

Parameters:
  • leafs – List of leaf symbols (Variable, Number instances).

  • binary – List of binary operator classes (e.g., Add, Sub, Mul, Div).

  • unary – List of unary operator classes (e.g., Sin, Cos, Log, Exp).

estimate_total(length: int) int[source]#

估计长度为 length 的公式总数

nd2py.generator.eq.gplearn_generator module#

class nd2py.generator.eq.gplearn_generator.GPLearnGenerator(variables: List[Variable], binary: List[str | Symbol] = [Add, Sub, Mul, Div], unary: List[str | Symbol] = [Sqrt, Log, Abs, Neg, Inv, Sin, Cos, Tan], full_prob: float = 0.5, depth_range: Tuple[int, int] = (2, 6), const_range: Tuple[float, float] = None, rng: RandomState = None, edge_list: Tuple[List[int], List[int]] = None, num_nodes: int = None, scalar_number_only=True)[source]#

Bases: object

__init__(variables: List[Variable], binary: List[str | Symbol] = [Add, Sub, Mul, Div], unary: List[str | Symbol] = [Sqrt, Log, Abs, Neg, Inv, Sin, Cos, Tan], full_prob: float = 0.5, depth_range: Tuple[int, int] = (2, 6), const_range: Tuple[float, float] = None, rng: RandomState = None, edge_list: Tuple[List[int], List[int]] = None, num_nodes: int = None, scalar_number_only=True)[source]#
generate_node(nettypes: Set[NetType]) Symbol[source]#
generate_leaf(nettypes: Set[NetType]) Number | Variable[source]#
sample(nettypes: Set[NetType] = 'scalar', assign_root_nettypes=True) Symbol[source]#

nd2py.generator.eq.metaai_generator module#

class nd2py.generator.eq.metaai_generator.MetaAIGenerator(variables: List[Variable], binary: List[str | Symbol] = [Add, Sub, Mul, Div], unary: List[str | Symbol] = [Abs, Inv, Sqrt, Log, Exp, Sin, Arcsin, Cos, Arccos, Tan, Arctan, Pow2, Pow3], operators_to_downsample='Div:0,Arcsin:0,Arccos:0,Tan:0.2,Arctan:0.2,Sqrt:5,Pow2:3,Inv:3', rng: RandomState = None, edge_list: Tuple[List[int], List[int]] = None, num_nodes: int = None, scalar_number_only=True)[source]#

Bases: object

__init__(variables: List[Variable], binary: List[str | Symbol] = [Add, Sub, Mul, Div], unary: List[str | Symbol] = [Abs, Inv, Sqrt, Log, Exp, Sin, Arcsin, Cos, Arccos, Tan, Arctan, Pow2, Pow3], operators_to_downsample='Div:0,Arcsin:0,Arccos:0,Tan:0.2,Arctan:0.2,Sqrt:5,Pow2:3,Inv:3', rng: RandomState = None, edge_list: Tuple[List[int], List[int]] = None, num_nodes: int = None, scalar_number_only=True)[source]#
sample(nettypes: Set[NetType], n_operators: int = None, n_var: int = None) nd.Symbol[source]#
dist(n_op, n_emp)[source]#

max_ops: maximum number of operators Enumerate the number of possible unary-binary trees that can be generated from empty nodes. D[n][e] represents the number of different binary trees with n nodes that can be generated from e empty nodes, using the following recursion:

D(n, 0) = 0 D(0, e) = 1 D(n, e) = D(n, e - 1) + p_1 * D(n - 1, e) + D(n - 1, e + 1)

p1 = 0 if binary trees, or 1 if unary-binary trees

generate_leaf(n_var: int, n_used_var: int) Tuple[Symbol, int][source]#
generate_ops(n_operands: int) Symbol[source]#
generate_next_pos(n_empty, n_operators)[source]#

Sample the position of the next node (binary case). Sample a position in {0, …, n_empty - 1}.

nd2py.generator.eq.snip_generator module#

class nd2py.generator.eq.snip_generator.SNIPGenerator(max_var=5, min_unary=0, max_unary=4, min_binary_per_var=0, max_binary_per_var=1, max_binary_ops_offset=4, max_unary_depth=6, n_mantissa=4, max_exp=1, min_exp=0, **kwargs)[source]#

Bases: MetaAIGenerator

__init__(max_var=5, min_unary=0, max_unary=4, min_binary_per_var=0, max_binary_per_var=1, max_binary_ops_offset=4, max_unary_depth=6, n_mantissa=4, max_exp=1, min_exp=0, **kwargs)[source]#
generate_eqtree(n_var=None, n_unary=None, n_binary=None) Symbol[source]#
generate_float() Number[source]#
add_unaries(eqtree: Symbol, n_unary: int) Symbol[source]#
add_prefactors(eqtree: Symbol) Symbol[source]#
class nd2py.generator.eq.snip_generator.SNIPGenerator2(**kwargs)[source]#

Bases: SNIPGenerator

__init__(**kwargs)[source]#
add_prefactors(eqtree: Symbol) Symbol[source]#
class nd2py.generator.eq.snip_generator.SNIPGenerator3(**kwargs)[source]#

Bases: SNIPGenerator

__init__(**kwargs)[source]#
generate_eqtree(n_var=None, n_unary=None, n_binary=None) Symbol[source]#
generate_float() Number[source]#