nd2py.core.symbols package

Contents

nd2py.core.symbols package#

Submodules#

nd2py.core.symbols.empty module#

class nd2py.core.symbols.empty.Empty(nettype: Literal['node', 'edge', 'scalar'] | None = None)[source]#

Bases: Symbol

n_operands = 0#
__init__(nettype: Literal['node', 'edge', 'scalar'] | None = None)[source]#

Initialize a Symbol node.

This constructor sets the nettype, sanitizes and attaches child operands, and then triggers a nettype inference pass on the whole expression tree.

Parameters:
  • *operands – Child operands of this symbol. The number of operands must match n_operands of the concrete subclass. Non-symbol scalar values are automatically wrapped as Number symbols.

  • nettype (Optional[NetType | Set[NetType]]) – Nettype constraint for this symbol, such as "node", "edge", or "scalar", or a set of allowed nettypes. If provided, it is propagated through the tree by infer_nettype().

map_nettype() Literal['node', 'edge', 'scalar'] | None[source]#

Default nettype mapping rule for symbol subclasses.

The default behavior enforces that "node" and "edge" nettypes cannot be mixed. If only scalars are present, the result is "scalar"; otherwise it follows the presence of "node" or "edge".

Parameters:

*children_nettypes (NetType) – Nettypes of the child operands.

Returns:

Inferred nettype for the parent symbol, or None if the combination is invalid or cannot be determined.

Return type:

Optional[NetType]

Raises:

ValueError – If the number of child nettypes does not match cls.n_operands.

nd2py.core.symbols.functions module#

nd2py.core.symbols.functions.add#

alias of Add

nd2py.core.symbols.functions.sub#

alias of Sub

nd2py.core.symbols.functions.mul#

alias of Mul

nd2py.core.symbols.functions.div#

alias of Div

nd2py.core.symbols.functions.pow#

alias of Pow

nd2py.core.symbols.functions.max#

alias of Max

nd2py.core.symbols.functions.min#

alias of Min

nd2py.core.symbols.functions.sin#

alias of Sin

nd2py.core.symbols.functions.cos#

alias of Cos

nd2py.core.symbols.functions.tan#

alias of Tan

nd2py.core.symbols.functions.sec#

alias of Sec

nd2py.core.symbols.functions.csc#

alias of Csc

nd2py.core.symbols.functions.cot#

alias of Cot

nd2py.core.symbols.functions.arcsin#

alias of Arcsin

nd2py.core.symbols.functions.arccos#

alias of Arccos

nd2py.core.symbols.functions.arctan#

alias of Arctan

nd2py.core.symbols.functions.log#

alias of Log

nd2py.core.symbols.functions.logabs#

alias of LogAbs

nd2py.core.symbols.functions.exp#

alias of Exp

nd2py.core.symbols.functions.abs#

alias of Abs

nd2py.core.symbols.functions.neg#

alias of Neg

nd2py.core.symbols.functions.inv#

alias of Inv

nd2py.core.symbols.functions.sqrt#

alias of Sqrt

nd2py.core.symbols.functions.sqrtabs#

alias of SqrtAbs

nd2py.core.symbols.functions.pow2#

alias of Pow2

nd2py.core.symbols.functions.pow3#

alias of Pow3

nd2py.core.symbols.functions.sinh#

alias of Sinh

nd2py.core.symbols.functions.cosh#

alias of Cosh

nd2py.core.symbols.functions.tanh#

alias of Tanh

nd2py.core.symbols.functions.sech#

alias of Sech

nd2py.core.symbols.functions.csch#

alias of Csch

nd2py.core.symbols.functions.sigmoid#

alias of Sigmoid

nd2py.core.symbols.functions.reg#

alias of Regular

nd2py.core.symbols.functions.regular#

alias of Regular

nd2py.core.symbols.functions.sour#

alias of Sour

nd2py.core.symbols.functions.phi_s#

alias of Sour

nd2py.core.symbols.functions.targ#

alias of Targ

nd2py.core.symbols.functions.phi_t#

alias of Targ

nd2py.core.symbols.functions.aggr#

alias of Aggr

nd2py.core.symbols.functions.rho#

alias of Aggr

nd2py.core.symbols.functions.rgga#

alias of Rgga

nd2py.core.symbols.functions.readout#

alias of Readout

nd2py.core.symbols.functions.sum(*operands)[source]#
nd2py.core.symbols.functions.prod(*operands)[source]#
nd2py.core.symbols.functions.maximum(*operands)[source]#
nd2py.core.symbols.functions.minimum(*operands)[source]#
nd2py.core.symbols.functions.Constant(value, nettype: Literal['node', 'edge', 'scalar'] = 'scalar') Number[source]#

一个工厂函数,返回一个 fitable 为 False 的 Number 对象。

nd2py.core.symbols.functions.variables(vars, *args, **kwargs)[source]#

一个工厂函数,返回一个或多个 Variable 对象。 如果 vars 中包含空格,则认为是多个变量的名字,并返回一个列表;否则认为是单个变量的名字,并返回一个 Variable 对象。

nd2py.core.symbols.number module#

class nd2py.core.symbols.number.Number(value, nettype: Literal['node', 'edge', 'scalar'] = 'scalar', fitable=None)[source]#

Bases: Symbol

n_operands = 0#
__init__(value, nettype: Literal['node', 'edge', 'scalar'] = 'scalar', fitable=None)[source]#

Initialize a Symbol node.

This constructor sets the nettype, sanitizes and attaches child operands, and then triggers a nettype inference pass on the whole expression tree.

Parameters:
  • *operands – Child operands of this symbol. The number of operands must match n_operands of the concrete subclass. Non-symbol scalar values are automatically wrapped as Number symbols.

  • nettype (Optional[NetType | Set[NetType]]) – Nettype constraint for this symbol, such as "node", "edge", or "scalar", or a set of allowed nettypes. If provided, it is propagated through the tree by infer_nettype().

map_nettype() Literal['node', 'edge', 'scalar'] | None[source]#

Default nettype mapping rule for symbol subclasses.

The default behavior enforces that "node" and "edge" nettypes cannot be mixed. If only scalars are present, the result is "scalar"; otherwise it follows the presence of "node" or "edge".

Parameters:

*children_nettypes (NetType) – Nettypes of the child operands.

Returns:

Inferred nettype for the parent symbol, or None if the combination is invalid or cannot be determined.

Return type:

Optional[NetType]

Raises:

ValueError – If the number of child nettypes does not match cls.n_operands.

get_nettype_range() Set[Literal['node', 'edge', 'scalar']][source]#

获取此节点可能产生的所有 nettype 值域,并在首次调用时缓存到类属性中。

property nettype_range: Set[Literal['node', 'edge', 'scalar']]#

获取此节点可能产生的所有 nettype 值域,并在首次调用时缓存到类属性中。

nd2py.core.symbols.operands module#

class nd2py.core.symbols.operands.Add(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 2#
class nd2py.core.symbols.operands.Sub(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 2#
class nd2py.core.symbols.operands.Mul(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 2#
class nd2py.core.symbols.operands.Div(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 2#
class nd2py.core.symbols.operands.Pow(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 2#
class nd2py.core.symbols.operands.Max(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 2#
class nd2py.core.symbols.operands.Min(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 2#
class nd2py.core.symbols.operands.Identity(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Sin(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Cos(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Tan(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Sec(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Csc(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Cot(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Arcsin(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Arccos(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Arctan(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Log(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.LogAbs(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Exp(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Abs(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Neg(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Inv(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Sqrt(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.SqrtAbs(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Pow2(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Pow3(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Sinh(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Cosh(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Tanh(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Coth(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Sech(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Csch(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Sigmoid(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
class nd2py.core.symbols.operands.Regular(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 2#
class nd2py.core.symbols.operands.Sour(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
classmethod map_nettype(*children_nettypes: Literal['node', 'edge', 'scalar']) Literal['node', 'edge', 'scalar'] | None[source]#

Default nettype mapping rule for symbol subclasses.

The default behavior enforces that "node" and "edge" nettypes cannot be mixed. If only scalars are present, the result is "scalar"; otherwise it follows the presence of "node" or "edge".

Parameters:

*children_nettypes (NetType) – Nettypes of the child operands.

Returns:

Inferred nettype for the parent symbol, or None if the combination is invalid or cannot be determined.

Return type:

Optional[NetType]

Raises:

ValueError – If the number of child nettypes does not match cls.n_operands.

class nd2py.core.symbols.operands.Targ(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Sour

class nd2py.core.symbols.operands.Aggr(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
classmethod map_nettype(*children_nettypes: Literal['node', 'edge', 'scalar']) Literal['node', 'edge', 'scalar'] | None[source]#

Default nettype mapping rule for symbol subclasses.

The default behavior enforces that "node" and "edge" nettypes cannot be mixed. If only scalars are present, the result is "scalar"; otherwise it follows the presence of "node" or "edge".

Parameters:

*children_nettypes (NetType) – Nettypes of the child operands.

Returns:

Inferred nettype for the parent symbol, or None if the combination is invalid or cannot be determined.

Return type:

Optional[NetType]

Raises:

ValueError – If the number of child nettypes does not match cls.n_operands.

class nd2py.core.symbols.operands.Rgga(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Aggr

class nd2py.core.symbols.operands.Readout(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 1#
classmethod map_nettype(*children_nettypes: Literal['node', 'edge', 'scalar']) Literal['node', 'edge', 'scalar'] | None[source]#

Default nettype mapping rule for symbol subclasses.

The default behavior enforces that "node" and "edge" nettypes cannot be mixed. If only scalars are present, the result is "scalar"; otherwise it follows the presence of "node" or "edge".

Parameters:

*children_nettypes (NetType) – Nettypes of the child operands.

Returns:

Inferred nettype for the parent symbol, or None if the combination is invalid or cannot be determined.

Return type:

Optional[NetType]

Raises:

ValueError – If the number of child nettypes does not match cls.n_operands.

nd2py.core.symbols.symbol module#

class nd2py.core.symbols.symbol.Symbol(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: NetTypeMixin, TreeMixin, SymbolAPIMixin

n_operands = None#
__init__(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Initialize a Symbol node.

This constructor sets the nettype, sanitizes and attaches child operands, and then triggers a nettype inference pass on the whole expression tree.

Parameters:
  • *operands – Child operands of this symbol. The number of operands must match n_operands of the concrete subclass. Non-symbol scalar values are automatically wrapped as Number symbols.

  • nettype (Optional[NetType | Set[NetType]]) – Nettype constraint for this symbol, such as "node", "edge", or "scalar", or a set of allowed nettypes. If provided, it is propagated through the tree by infer_nettype().

copy()[source]#

Return a deep copy of this symbol.

The copied symbol has the same tree structure and values as the original but does not share parent links, so it can be safely inserted into a different expression tree.

Returns:

A deep copy of the current symbol.

Return type:

Symbol

get_numbers(fitable_only: bool = False, float_only: bool = False, scalar_only: bool = False) List[Number][source]#

Collect all Number nodes contained in this symbol.

Traverses the expression tree in preorder and returns all numeric nodes that satisfy the given filters.

Parameters:
  • fitable_only (bool, optional) – If True, return only numbers marked as fitable (trainable) parameters. Defaults to False.

  • float_only (bool, optional) – If True, exclude integer-like values (for example exponents that should remain fixed). Defaults to False.

  • scalar_only (bool, optional) – If True, only consider scalar numbers (nettype "scalar"). Defaults to False.

Returns:

List of numeric symbol nodes that match the filters.

Return type:

List[Number]

get_parameters(fitable_only: bool = False, float_only: bool = False) List[float][source]#

Return numeric parameter values contained in this symbol.

This is a convenience wrapper over get_numbers() that extracts the underlying scalar values from Number nodes.

Parameters:
  • fitable_only (bool, optional) – If True, return only parameters associated with fitable numbers. Defaults to False.

  • float_only (bool, optional) – If True, exclude integer-like parameters. Defaults to False.

Returns:

Flat list of parameter values in traversal order.

Return type:

List[float]

set_parameters(params: List[float], fitable_only: bool = False, float_only: bool = False)[source]#

Assign new numeric parameter values to this symbol.

The values in params are consumed in the same order as produced by get_parameters() with the same filter options.

Parameters:
  • params (List[float]) – New parameter values to assign.

  • fitable_only (bool, optional) – If True, only update fitable parameters and leave others unchanged. Defaults to False.

  • float_only (bool, optional) – If True, only update non-integer parameters. Defaults to False.

Raises:

ValueError – If the length of params does not match the number of parameters selected by the filters.

classmethod map_nettype(*children_nettypes: Literal['node', 'edge', 'scalar']) Literal['node', 'edge', 'scalar'] | None[source]#

Default nettype mapping rule for symbol subclasses.

The default behavior enforces that "node" and "edge" nettypes cannot be mixed. If only scalars are present, the result is "scalar"; otherwise it follows the presence of "node" or "edge".

Parameters:

*children_nettypes (NetType) – Nettypes of the child operands.

Returns:

Inferred nettype for the parent symbol, or None if the combination is invalid or cannot be determined.

Return type:

Optional[NetType]

Raises:

ValueError – If the number of child nettypes does not match cls.n_operands.

nd2py.core.symbols.variable module#

class nd2py.core.symbols.variable.Variable(name, nettype: Literal['node', 'edge', 'scalar'] = 'scalar')[source]#

Bases: Symbol

n_operands = 0#
__init__(name, nettype: Literal['node', 'edge', 'scalar'] = 'scalar')[source]#

Initialize a Symbol node.

This constructor sets the nettype, sanitizes and attaches child operands, and then triggers a nettype inference pass on the whole expression tree.

Parameters:
  • *operands – Child operands of this symbol. The number of operands must match n_operands of the concrete subclass. Non-symbol scalar values are automatically wrapped as Number symbols.

  • nettype (Optional[NetType | Set[NetType]]) – Nettype constraint for this symbol, such as "node", "edge", or "scalar", or a set of allowed nettypes. If provided, it is propagated through the tree by infer_nettype().

map_nettype() Literal['node', 'edge', 'scalar'] | None[source]#

Default nettype mapping rule for symbol subclasses.

The default behavior enforces that "node" and "edge" nettypes cannot be mixed. If only scalars are present, the result is "scalar"; otherwise it follows the presence of "node" or "edge".

Parameters:

*children_nettypes (NetType) – Nettypes of the child operands.

Returns:

Inferred nettype for the parent symbol, or None if the combination is invalid or cannot be determined.

Return type:

Optional[NetType]

Raises:

ValueError – If the number of child nettypes does not match cls.n_operands.

get_nettype_range() Set[Literal['node', 'edge', 'scalar']][source]#

获取此节点可能产生的所有 nettype 值域,并在首次调用时缓存到类属性中。

property nettype_range: Set[Literal['node', 'edge', 'scalar']]#

获取此节点可能产生的所有 nettype 值域,并在首次调用时缓存到类属性中。