nd2py package

Contents

nd2py package#

nd2py.no_copy_value()[source]#
class nd2py.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.aggr#

alias of Aggr

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

Bases: Symbol

n_operands = 1#
nd2py.pow2#

alias of Pow2

nd2py.cos#

alias of Cos

nd2py.log#

alias of Log

class nd2py.SplitByMul[source]#

Bases: Visitor

generic_visit(node: Symbol, *args, **kwargs) _Type[source]#
visit_Mul(node: Mul, *args, **kwargs) _Type[source]#
visit_Div(node: Div, *args, **kwargs) _Type[source]#
merge_coefficients(items: List[Symbol], *args, **kwargs) List[Symbol][source]#

Merge coefficients from the symbols.

class nd2py.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.

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

Bases: Symbol

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

Bases: Symbol

n_operands = 1#
nd2py.set_fitable()[source]#
nd2py.phi_s#

alias of Sour

nd2py.sinh#

alias of Sinh

nd2py.min#

alias of Min

nd2py.regular#

alias of Regular

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

Bases: Symbol

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

Bases: Symbol

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

Bases: Aggr

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

Bases: Symbol

n_operands = 1#
nd2py.cosh#

alias of Cosh

nd2py.sigmoid#

alias of Sigmoid

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

Bases: Symbol

n_operands = 1#
nd2py.div#

alias of Div

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

Bases: Symbol

n_operands = 1#
nd2py.reg#

alias of Regular

nd2py.pow#

alias of Pow

class nd2py.FoldConstant(fold_fitable: bool = True, fold_constant: bool = True)[source]#

Bases: Visitor

访问器,用于将表达式中不含 Number 的子表达式折叠为 Constant。

__init__(fold_fitable: bool = True, fold_constant: bool = True)[source]#
generic_visit(node, *args, **kwargs)[source]#
visit_Empty(node: Symbol, *args, **kwargs)[source]#
visit_Number(node: Number, *args, **kwargs)[source]#
visit_Variable(node: Variable, *args, **kwargs)[source]#
class nd2py.TreePrinter[source]#

Bases: Visitor

generic_visit(node: Symbol, *args, **kwargs) _Type[source]#
visit_Empty(node: Symbol, *args, **kwargs) _Type[source]#
visit_Number(node: Number, *args, **kwargs) _Type[source]#
visit_Variable(node: Variable, *args, **kwargs) _Type[source]#
visit_Add(node: Add, *args, **kwargs) _Type[source]#
visit_Mul(node: Mul, *args, **kwargs) _Type[source]#
class nd2py.Min(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 2#
nd2py.Constant(value, nettype: Literal['node', 'edge', 'scalar'] = 'scalar') Number[source]#

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

nd2py.arcsin#

alias of Arcsin

nd2py.tanh#

alias of Tanh

nd2py.parse(expression: str, variables: Dict[str, Symbol] = None, callables: Dict[str, callable] = None) Symbol[source]#
nd2py.rgga#

alias of Rgga

nd2py.max#

alias of Max

nd2py.pow3#

alias of Pow3

nd2py.add#

alias of Add

nd2py.mul#

alias of Mul

nd2py.neg#

alias of Neg

nd2py.warn_once(warn_name, maxsize=None)[source]#

This function is used to limit the number of times a warning is issued

nd2py.logabs#

alias of LogAbs

nd2py.phi_t#

alias of Targ

nd2py.setup_lazy_imports(module_name: str, import_mapping: Dict[str, Tuple[str, str]])[source]#

Set up lazy imports for a module’s __init__.py.

Returns (__getattr__, __dir__, __all__) which should be assigned at the module level so that from package import OptionalClass works without importing the optional dependency until it is actually needed.

Parameters:
  • module_name – The __name__ of the calling module.

  • import_mapping – A dict mapping attribute names to (module_path, requires) tuples. module_path is a relative import path (e.g. ".torch_calc") and requires is the optional-dependency group name (e.g. "nn") shown in the error message when the dependency is missing.

Usage:

# __init__.py
from .core import CoreClass
from ..utils.lazy_loader import setup_lazy_imports

if TYPE_CHECKING:
    from .optional import OptionalClass

__getattr__, __dir__, __all__ = setup_lazy_imports(__name__, {
    "OptionalClass": (".optional", "nn"),
})
nd2py.sub#

alias of Sub

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

Bases: Symbol

n_operands = 1#
class nd2py.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.Cos(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

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

Bases: Symbol

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

Bases: Symbol

n_operands = 2#
nd2py.set_copy_value(value: bool)[source]#
nd2py.variables(vars, *args, **kwargs)[source]#

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

nd2py.csc#

alias of Csc

class nd2py.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.targ#

alias of Targ

class nd2py.NumpyCalc[source]#

Bases: Visitor

generic_visit(node: Symbol, *args, **kwargs)[source]#
visit_Empty(node: Empty, *args, **kwargs)[source]#
visit_Number(node: Number, *args, **kwargs)[source]#
visit_Variable(node: Variable, *args, **kwargs)[source]#
visit_Add(node: Add, x1, x2, *args, **kwargs)[source]#
visit_Sub(node: Sub, x1, x2, *args, **kwargs)[source]#
visit_Mul(node: Mul, x1, x2, *args, **kwargs)[source]#
visit_Div(node: Div, x1, x2, *args, **kwargs)[source]#
visit_Pow(node: Pow, x1, x2, *args, **kwargs)[source]#
visit_Max(node: Max, x1, x2, *args, **kwargs)[source]#
visit_Min(node: Min, x1, x2, *args, **kwargs)[source]#
visit_Identity(node: Identity, x, *args, **kwargs)[source]#
visit_Sin(node: Sin, x, *args, **kwargs)[source]#
visit_Cos(node: Cos, x, *args, **kwargs)[source]#
visit_Tan(node: Tan, x, *args, **kwargs)[source]#
visit_Sec(node: Sec, x, *args, **kwargs)[source]#
visit_Csc(node: Csc, x, *args, **kwargs)[source]#
visit_Cot(node: Cot, x, *args, **kwargs)[source]#
visit_Log(node: Log, x, *args, **kwargs)[source]#
visit_LogAbs(node: LogAbs, x, *args, **kwargs)[source]#
visit_Exp(node: Exp, x, *args, **kwargs)[source]#
visit_Abs(node: Abs, x, *args, **kwargs)[source]#
visit_Neg(node: Neg, x, *args, **kwargs)[source]#
visit_Inv(node: Inv, x, *args, **kwargs)[source]#
visit_Sqrt(node: Sqrt, x, *args, **kwargs)[source]#
visit_SqrtAbs(node: SqrtAbs, x, *args, **kwargs)[source]#
visit_Pow2(node: Pow2, x, *args, **kwargs)[source]#
visit_Pow3(node: Pow3, x, *args, **kwargs)[source]#
visit_Arcsin(node: Arcsin, x, *args, **kwargs)[source]#
visit_Arccos(node: Arccos, x, *args, **kwargs)[source]#
visit_Arctan(node: Arctan, x, *args, **kwargs)[source]#
visit_Sinh(node: Sinh, x, *args, **kwargs)[source]#
visit_Cosh(node: Cosh, x, *args, **kwargs)[source]#
visit_Tanh(node: Tanh, x, *args, **kwargs)[source]#
visit_Sech(node: Sech, x, *args, **kwargs)[source]#
visit_Csch(node: Csch, x, *args, **kwargs)[source]#
visit_Coth(node: Coth, x, *args, **kwargs)[source]#
visit_Sigmoid(node: Sigmoid, x, *args, **kwargs)[source]#
visit_Regular(node: Regular, x1, x2, *args, **kwargs)[source]#
visit_Sour(node: Sour, x, *args, **kwargs)[source]#

(, n_nodes or 1) -> (, n_edges or 1)

visit_Targ(node: Targ, x, *args, **kwargs)[source]#

(, n_nodes or 1) -> (, n_edges or 1)

visit_Aggr(node: Aggr, x, *args, **kwargs)[source]#

(, n_edges or 1) -> (, n_nodes)

visit_Rgga(node: Rgga, x, *args, **kwargs)[source]#

(, n_edges or 1) -> (, n_nodes)

visit_Readout(node: Readout, x, *args, **kwargs)[source]#

(, n_nodes or n_edges or 1) -> (, 1)

nd2py.readout#

alias of Readout

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

Bases: Symbol

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

Bases: Symbol

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

Bases: Symbol

n_operands = 2#
class nd2py.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.

class nd2py.BFGSFit(*args: Any, **kwargs: Any)[source]#

Bases: BaseEstimator, RegressorMixin

__init__(expression: Symbol, edge_list=None, num_nodes=None, use_eps=1e-08, method='BFGS', tol=1e-06, options=None, fold_constant=False)[source]#
fit(X, y=None)[source]#
predict(X)[source]#

用拟合好的 expression 去计算新的 X 上的输出。

class nd2py.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 值域,并在首次调用时缓存到类属性中。

nd2py.arccos#

alias of Arccos

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

Bases: Symbol

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

Bases: Symbol

n_operands = 1#
nd2py.from_postorder(nodes: List[Symbol | Type[Symbol]], **kwargs) Symbol[source]#

Construct a Symbol tree from a list of Symbols in postorder traversal order.

class nd2py.Simplify[source]#

Bases: Visitor

generic_visit(node: Symbol, *args, **kwargs) _Type[source]#
remove_nested_unary(node: Symbol, *args, **kwargs) _Type[source]#
visit_Sin(node: Symbol, *args, **kwargs) _Type#
visit_Cos(node: Symbol, *args, **kwargs) _Type#
visit_Tanh(node: Symbol, *args, **kwargs) _Type#
visit_Sigmoid(node: Symbol, *args, **kwargs) _Type#
visit_Sqrt(node: Symbol, *args, **kwargs) _Type#
visit_SqrtAbs(node: Symbol, *args, **kwargs) _Type#
visit_Exp(node: Symbol, *args, **kwargs) _Type#
visit_Log(node: Symbol, *args, **kwargs) _Type#
visit_LogAbs(node: Symbol, *args, **kwargs) _Type#
visit_Readout(node: Readout, *args, **kwargs) _Type[source]#
visit_Number(node: Number, *args, **kwargs) _Type[source]#
visit_Variable(node: Variable, *args, **kwargs) _Type[source]#
visit_Add(node: Add, *args, **kwargs) _Type[source]#
visit_Sub(node: Add, *args, **kwargs) _Type#
visit_Mul(node: Mul, *args, **kwargs) _Type[source]#
visit_Div(node: Mul, *args, **kwargs) _Type#
visit_Neg(node: Neg, *args, **kwargs) _Type[source]#
visit_Inv(node: Inv, *args, **kwargs) _Type[source]#
visit_Aggr(node: Aggr, *args, **kwargs) _Type[source]#
nd2py.prod(*operands)[source]#
class nd2py.Sqrt(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

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

Bases: Symbol

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

Bases: Symbol

n_operands = 1#
nd2py.no_warn()[source]#

Context manager to suppress warnings temporarily

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

Bases: Symbol

n_operands = 1#
class nd2py.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.

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

Bases: Symbol

n_operands = 1#
nd2py.cot#

alias of Cot

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

Bases: Symbol

n_operands = 1#
class nd2py.StringPrinter[source]#

Bases: Visitor

generic_visit(node: Symbol, *args, **kwargs) _Type[source]#
visit_Empty(node: Empty, *args, **kwargs) _Type[source]#
visit_Number(node: Number, *args, **kwargs) _Type[source]#
visit_Variable(node: Variable, *args, **kwargs) _Type[source]#
visit_Add(node: Add, *args, **kwargs) _Type[source]#
visit_Sub(node: Sub, *args, **kwargs) _Type[source]#
visit_Mul(node: Mul, *args, **kwargs) _Type[source]#
visit_Div(node: Div, *args, **kwargs) _Type[source]#
visit_Pow(node: Pow, *args, **kwargs) _Type[source]#
visit_Neg(node: Neg, *args, **kwargs) _Type[source]#
visit_Inv(node: Inv, *args, **kwargs) _Type[source]#
visit_Pow2(node: Pow2, *args, **kwargs) _Type[source]#
visit_Pow3(node: Pow3, *args, **kwargs) _Type[source]#
visit_Sour(node: Sour, *args, **kwargs) _Type[source]#
visit_Targ(node: Targ, *args, **kwargs) _Type[source]#
visit_Aggr(node: Aggr, *args, **kwargs) _Type[source]#
visit_Rgga(node: Rgga, *args, **kwargs) _Type[source]#
nd2py.nettype_inference()[source]#
class nd2py.Max(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#

Bases: Symbol

n_operands = 2#
nd2py.sum(*operands)[source]#
nd2py.sqrtabs#

alias of SqrtAbs

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

Bases: Symbol

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

Bases: Symbol

n_operands = 1#
nd2py.tan#

alias of Tan

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

Bases: Symbol

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

Bases: Symbol

n_operands = 1#
class nd2py.FixNetType[source]#

Bases: Visitor

generic_visit(node, *args, **kwargs) _Type[source]#

direction = ‘top-down’: 每个 node 的 nettype 由 kwargs[‘nettype’] 决定。 direction = ‘bottom-up’: 每个 node 的 nettype 由其 operands 决定。只保证每个 node 运算不会出错即可,不需要对 kwargs[‘nettype’] 负责

visit_Number(node: Number, *args, **kwargs) _Type[source]#
visit_Variable(node: Variable, *args, **kwargs) _Type[source]#
visit_BinaryOp(node, *args, **kwargs) _Type[source]#
visit_Add(node, *args, **kwargs) _Type#
visit_Sub(node, *args, **kwargs) _Type#
visit_Mul(node, *args, **kwargs) _Type#
visit_Div(node, *args, **kwargs) _Type#
visit_Pow(node, *args, **kwargs) _Type#
visit_Max(node, *args, **kwargs) _Type#
visit_Min(node, *args, **kwargs) _Type#
visit_Aggr(node, *args, **kwargs) _Type[source]#
visit_Rgga(node, *args, **kwargs) _Type#
visit_Sour(node, *args, **kwargs) _Type[source]#
visit_Targ(node, *args, **kwargs) _Type#
visit_Readout(node, *args, **kwargs) _Type[source]#
fix_nettype(node: Symbol, *args, **kwargs) Symbol[source]#
edge_to_node(node: Symbol, *args, **kwargs) Symbol[source]#
node_to_edge(node: Symbol, *args, **kwargs) Symbol[source]#
edge_to_scalar(node: Symbol, *args, **kwargs) Symbol[source]#
node_to_scalar(node: Symbol, *args, **kwargs) Symbol[source]#
scalar_to_node(node: Symbol, *args, **kwargs) Symbol[source]#
scalar_to_edge(node: Symbol, *args, **kwargs) Symbol[source]#
nd2py.sin#

alias of Sin

nd2py.sec#

alias of Sec

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

Bases: Symbol

n_operands = 1#
nd2py.exp#

alias of Exp

nd2py.rho#

alias of Aggr

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

Bases: Symbol

n_operands = 2#
nd2py.sour#

alias of Sour

nd2py.sech#

alias of Sech

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

Bases: Symbol

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

Bases: Symbol

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

Bases: Sour

nd2py.inv#

alias of Inv

nd2py.from_preorder(nodes: List[Symbol | Type[Symbol]], **kwargs) Symbol[source]#

Construct a Symbol tree from a list of Symbols in preorder traversal order.

nd2py.abs#

alias of Abs

nd2py.sqrt#

alias of Sqrt

class nd2py.SplitByAdd[source]#

Bases: Visitor

generic_visit(node: Symbol, *args, **kwargs) _Type[source]#
visit_Add(node: Add, *args, **kwargs) _Type[source]#
visit_Sub(node: Sub, *args, **kwargs) _Type[source]#
visit_Mul(node: Mul, *args, **kwargs) _Type[source]#
visit_Div(node: Div, *args, **kwargs) _Type[source]#
visit_Neg(node: Neg, *args, **kwargs) _Type[source]#
visit_Sour(node: Sour, *args, **kwargs) _Type[source]#
visit_Targ(node: Targ, *args, **kwargs) _Type[source]#
visit_Aggr(node: Aggr, *args, **kwargs) _Type[source]#
visit_Rgga(node: Rgga, *args, **kwargs) _Type[source]#
visit_Readout(node: Readout, *args, **kwargs) _Type[source]#
merge_bias(items: List[Symbol], *args, **kwargs) List[Symbol][source]#

Merge bias terms in the node.

nd2py.csch#

alias of Csch

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

Bases: Symbol

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

Bases: Symbol

n_operands = 1#
nd2py.arctan#

alias of Arctan

Subpackages#