# modulus.sympy_utils

## sympy_utils.functions

simple Sympy helper functions

modulus.sympy_utils.functions.line(x, point_x_1, point_y_1, point_x_2, point_y_2)

line function from point intercepts

xSympy Symbol/Exp

the x in equation y=a*x+b

point_x_1Sympy Symbol/Exp, float, int

first intercept x position

point_y_1Sympy Symbol/Exp, float, int

first intercept y position

point_x_2Sympy Symbol/Exp, float, int

second intercept x position

point_y_2Sympy Symbol/Exp, float, int

second intercept y position

ySympy Expr

y=slope*x+intercept

modulus.sympy_utils.functions.parabola(x, inter_1, inter_2, height)

parabola from point intercepts

xSympy Symbol/Exp

the x in equation y=a*x*2+b*x+c

inter_1Sympy Symbol/Exp, float, int

first intercept such that y=0 when x=inter_1

inter_2Sympy Symbol/Exp, float, int

second intercept such that y=0 when x=inter_1

heightSympy Symbol/Exp, float, int

max height of parabola

ySympy Expr

y=factor*(x-inter_1)*(x-+inter_2)

modulus.sympy_utils.functions.parabola2D(x, y, inter_1_x, inter_2_x, inter_1_y, inter_2_y, height)

square parabola from point intercepts

xSympy Symbol/Exp

the x in equation z=parabola(x)*parabola(y)

ySympy Symbol/Exp

the y in equation z=a*x**2+b*y**2+c*xy+d*y+e*x+f

inter_1_xSympy Symbol/Exp, float, int

first intercept such that z=0 when x=inter_1_x

inter_2_xSympy Symbol/Exp, float, int

second intercept such that z=0 when x=inter_2_x

inter_1_ySympy Symbol/Exp, float, int

first intercept such that z=0 when y=inter_1_y

inter_2_ySympy Symbol/Exp, float, int

second intercept such that z=0 when y=inter_2_y

heightSympy Symbol/Exp, float, int

max height of parabola

ySympy Expr

y=factor*(x-inter_1)*(x-+inter_2)

## sympy_utils.numpy_printer

Helper functions for converting sympy equations to numpy

modulus.sympy_utils.numpy_printer.np_lambdify(f, r)

generates a numpy function from a sympy equation

fSympy Exp, float, int, bool or list of the previous

the equation to convert to a numpy function. If float, int, or bool this gets converted to a constant function of value f. If f is a list then output for each element in list is is concatenated on axis -1.

rlist, dict

A list of the arguments for f. If dict then the keys of the dict are used.

np_f : numpy function

## sympy_utils.torch_printer

Helper functions for converting sympy equations to pytorch

class modulus.sympy_utils.torch_printer.CustomDerivativePrinter(settings=None)

Bases: sympy.printing.str.StrPrinter

class modulus.sympy_utils.torch_printer.SympyToTorch(sympy_expr, name: str, detach_names: List[str] = [])

Bases: torch.nn.modules.module.Module

forward(var: Dict[str, torch.Tensor]) → Dict[str, torch.Tensor]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

modulus.sympy_utils.torch_printer.torch_lambdify(f, r, separable=False)

generates a PyTorch function from a sympy equation

fSympy Exp, float, int, bool

the equation to convert to torch. If float, int, or bool this gets converted to a constant function of value f.

rlist, dict

A list of the arguments for f. If dict then the keys of the dict are used.

torch_f : PyTorch function

© Copyright 2021-2022, NVIDIA. Last updated on Feb 3, 2023.