|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | + |
| 6 | +function to_std_str(arg::ir.Mat) |
| 7 | + if arg.id isa ir.Var |
| 8 | + return to_std_str(arg.id) |
| 9 | + end |
| 10 | + |
| 11 | + return "mat(" * to_std_str(arg.id) * ")" |
| 12 | +end |
| 13 | + |
| 14 | +function to_std_str(arg::ir.Vec) |
| 15 | + if arg.id isa ir.Var |
| 16 | + return to_std_str(arg.id) |
| 17 | + end |
| 18 | + |
| 19 | + return "vec(" * to_std_str(arg.id) * ")" |
| 20 | +end |
| 21 | + |
| 22 | +function to_std_str(arg::ir.Scal) |
| 23 | + return to_std_str(arg.id) |
| 24 | +end |
| 25 | + |
| 26 | +function to_std_str(arg::ir.Var) |
| 27 | + return arg.id |
| 28 | +end |
| 29 | + |
| 30 | +function to_std_str(arg::ir.Const) |
| 31 | + return to_std_str(arg.value) |
| 32 | +end |
| 33 | + |
| 34 | +function to_std_str(arg::Real) |
| 35 | + out = string(arg) |
| 36 | + |
| 37 | + if arg < 0 |
| 38 | + out = "(" * out * ")" |
| 39 | + end |
| 40 | + |
| 41 | + return out |
| 42 | +end |
| 43 | + |
| 44 | +function to_std_str(arg::Rational) |
| 45 | + out = string(arg) |
| 46 | + |
| 47 | + return "(" * out * ")" |
| 48 | +end |
| 49 | + |
| 50 | +function to_std_str(arg::ir.Identity) |
| 51 | + return "I" |
| 52 | +end |
| 53 | + |
| 54 | +function to_std_str(arg::ir.Abs) |
| 55 | + return "abs(" * to_std_str(arg.arg) * ")" |
| 56 | +end |
| 57 | + |
| 58 | +function to_std_str(arg::ir.Sgn) |
| 59 | + return "sgn(" * to_std_str(arg.arg) * ")" |
| 60 | +end |
| 61 | + |
| 62 | +function to_std_str(arg::ir.Sin) |
| 63 | + return "sin(" * to_std_str(arg.arg) * ")" |
| 64 | +end |
| 65 | + |
| 66 | +function to_std_str(arg::ir.Cos) |
| 67 | + return "cos(" * to_std_str(arg.arg) * ")" |
| 68 | +end |
| 69 | + |
| 70 | +function parenthesize(f, arg::ir.Add) |
| 71 | + return "(" * f(arg) * ")" |
| 72 | +end |
| 73 | + |
| 74 | +function parenthesize(f, arg::ir.Sub) |
| 75 | + return "(" * f(arg) * ")" |
| 76 | +end |
| 77 | + |
| 78 | +function parenthesize(f, arg::ir.HadamardProduct) |
| 79 | + return "(" * f(arg.l) * " ⊙ " * f(arg.r) * ")" |
| 80 | +end |
| 81 | + |
| 82 | +function parenthesize(f, arg) |
| 83 | + return f(arg) |
| 84 | +end |
| 85 | + |
| 86 | +function to_std_str(arg::ir.Add) |
| 87 | + return parenthesize(to_std_str, arg.l) * " + " * parenthesize(to_std_str, arg.r) |
| 88 | +end |
| 89 | + |
| 90 | +function to_std_str(arg::ir.Sub) |
| 91 | + return parenthesize(to_std_str, arg.l) * " - " * parenthesize(to_std_str, arg.r) |
| 92 | +end |
| 93 | + |
| 94 | +function to_std_str(arg::ir.Product) |
| 95 | + return parenthesize(to_std_str, arg.l) * parenthesize(to_std_str, arg.r) |
| 96 | +end |
| 97 | + |
| 98 | +function to_std_str(arg::ir.HadamardProduct) |
| 99 | + return to_std_str(arg.l) * " ⊙ " * to_std_str(arg.r) |
| 100 | +end |
| 101 | + |
| 102 | +function to_std_str(arg::ir.Power) |
| 103 | + out = to_std_str(arg.base) |
| 104 | + |
| 105 | + if arg.base isa ir.Product || |
| 106 | + arg.base isa ir.HadamardProduct || |
| 107 | + arg.base isa ir.Add || |
| 108 | + arg.base isa ir.Sub |
| 109 | + out = "(" * out * ")" |
| 110 | + end |
| 111 | + |
| 112 | + return out * "^" * to_std_str(arg.exponent) |
| 113 | +end |
| 114 | + |
| 115 | +function to_std_str(arg::ir.Trace) |
| 116 | + return "tr(" * to_std_str(arg.arg) * ")" |
| 117 | +end |
| 118 | + |
| 119 | +function to_std_str(arg::ir.Diag) |
| 120 | + return "diag(" * to_std_str(arg.arg) * ")" |
| 121 | +end |
| 122 | + |
| 123 | +function to_std_str(arg::ir.Transpose) |
| 124 | + return parenthesize(to_std_str, arg.arg) * "ᵀ" |
| 125 | +end |
| 126 | + |
| 127 | +function to_std_str(arg::ir.Sum) |
| 128 | + return "sum(" * to_std_str(arg.arg) * ")" |
| 129 | +end |
| 130 | + |
| 131 | +function to_std_str(arg::ir.PartialSum) |
| 132 | + if arg.dim == 1 |
| 133 | + return "vec(1)ᵀ" * to_std_str(arg.arg) |
| 134 | + elseif arg.dim == 2 |
| 135 | + return to_std_str(arg.arg) * "vec(1)" |
| 136 | + end |
| 137 | + |
| 138 | + throw(RuntimeError("Encountered a sum over an unsupported index")) |
| 139 | +end |
0 commit comments