API: smoothmath
- exception smoothmath.DomainError
Raised when evaluating or differentiating at a point where an
Expressionis undefined.
- class smoothmath.Point(**kwargs)
A point. Can have any number of coordinates.
>>> from smoothmath import Point >>> Point(x=3, y=4.5) Point(x=3, y=4.5)
- Parameters:
**kwargs (float) – a real number for each coordinate name
- coordinate(variable)
Retrieves a coordinate.
Raises
CoordinateMissingif the point has no entry for the coordinate name.>>> from smoothmath import Point >>> point = Point(x=3, y=4.5) >>> point.coordinate("x") 3 >>> point.coordinate("y") 4.5
- Parameters:
variable (Variable | str) – selects which coordinate
- Return type:
float
- class smoothmath.Expression
An abstract base class for all expresssions. See the
smoothmath.expressionmodule for concrete expression classes.
- class smoothmath.Derivative(expression, compute_early=False)
The derivative of an expression.
>>> from smoothmath import Derivative >>> from smoothmath.expression import Variable, NthPower >>> Derivative(NthPower(Variable("x"), n=2)) Derivative(NthPower(Variable("x"), n=2))
NOTE: The expression must have only one variable. For alternatives that support expressions with multiple variables, see the
Differential,Partial, andLocatedDifferentialclasses.- Parameters:
expression (Expression) – an expression with one variable
compute_early (bool) – whether to do extra work on initialization to have faster evaluation afterwards
- at(point)
Evaluates the derivative.
- Parameters:
point (Point | float) – where to evaluate the derivative
- Return type:
float
- as_expression()
Writes the derivative as an expression.
NOTE: Writing the derivative as an expression may enlargen the domain. For example,
Derivative(Logarithm(Variable("x")))is not defined at negative numbers, but as an expression,Reciprocal(Variable("x"))is defined at negative numbers.- Return type:
- class smoothmath.Differential(expression, compute_early=False)
The differential of an expression.
>>> from smoothmath import Differential >>> from smoothmath.expression import Variable, Multiply >>> Differential(Multiply(Variable("x"), Variable("y"))) Differential(Multiply(Variable("x"), Variable("y")))
- Parameters:
expression (Expression) – an expression
compute_early (bool) – whether to do extra work on initialization to have faster evaluation afterwards
- component(variable)
Retrieves a component of the differential.
NOTE: The components of the differential are the partials of the original expression.
- class smoothmath.Partial(expression, variable, compute_early=False)
The partial derivative of an expression.
>>> from smoothmath import Partial >>> from smoothmath.expression import Variable, Multiply >>> Partial(Multiply(Variable("x"), Variable("y")), Variable("x")) Partial(Multiply(Variable("x"), Variable("y")), Variable("x"))
- Parameters:
expression (Expression) – an expression
variable (Variable | str) – the partial is taken with respect to this variable
compute_early (bool) – whether to do extra work on initialization to have faster evaluation afterwards
- at(point)
Evaluates the partial at a point.
- Parameters:
point (Point) – where to evaluate the partial
- Return type:
float
- as_expression()
Writes the partial as an expression.
NOTE: Writing the partial as an expression may enlargen the domain. For example,
Partial(Logarithm(Variable("x")), Variable("x"))is not defined at negative numbers, but as an expression,Reciprocal(Variable("x"))is defined at negative numbers.- Return type:
- class smoothmath.LocatedDifferential(expression, point)
The differential of an expression located at a point.
>>> from smoothmath import Point, LocatedDifferential >>> from smoothmath.expression import Variable, Multiply >>> LocatedDifferential(Multiply(Variable("x"), Variable("y")), Point(x=2, y=4)) LocatedDifferential(Multiply(Variable("x"), Variable("y")), Point(x=2, y=4))
- Parameters:
expression (Expression) – an expression
point (Point) – where to locate the differential