Expand math to polynomials
expand_math_to_polynomials
ExpandExp
dataclass
Bases: RewritePattern
Replace math.exp operations with a polynomial expansion.
Only expands when the number of terms is specified, either via an attribute on the operation or via the pass-level default.
Source code in xdsl/transforms/expand_math_to_polynomials.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
default_terms: int | None = None
class-attribute
instance-attribute
Pass-level default for number of terms. None means don't expand unless the operation has an explicit terms attribute.
__init__(default_terms: int | None = None) -> None
match_and_rewrite(op: math.ExpOp, rewriter: PatternRewriter) -> None
Source code in xdsl/transforms/expand_math_to_polynomials.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
ExpandMathToPolynomialsPass
dataclass
Bases: ModulePass
This pass expands math operations to a polynomial expansion using the Taylor series.
Currently only expands math.exp operations.
Operations are only expanded when the number of terms is specified,
either via a terms attribute on the operation itself or via the
pass-level terms parameter.
Source code in xdsl/transforms/expand_math_to_polynomials.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
name = 'expand-math-to-polynomials'
class-attribute
instance-attribute
terms: int | None = None
class-attribute
instance-attribute
Number of terms in the resulting polynomial expansion. If not set, only operations with an explicit terms attribute are expanded.
__init__(terms: int | None = None) -> None
apply(ctx: Context, op: ModuleOp) -> None
Source code in xdsl/transforms/expand_math_to_polynomials.py
123 124 125 126 127 | |
expand_exp(op: math.ExpOp, rewriter: PatternRewriter, terms: int) -> Operation
Expand exp(x) using a Taylor-series polynomial expansion.
Pseudo-code::
result = 1.0
term = 1.0
for i in range(1, terms): # loop will be unrolled by the rewriter
term *= x / i
result += term
return result
Source code in xdsl/transforms/expand_math_to_polynomials.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |