| 4237 | : ce.function('Multiply', scalars); |
| 4238 | let atoms: TrigAtom[] = [{ coef: ce.One, kind: null, arg: ce.One }]; |
| 4239 | for (const t of trigFactors) atoms = mulAtomsByTrig(ce, atoms, t.head, t.arg); |
| 4240 | // fold the term scalar into every atom's coefficient |
| 4241 | return atoms.map((a) => ({ |
| 4242 | coef: scalar.isSame(1) ? a.coef : ce.function('Multiply', [scalar, a.coef]), |
| 4243 | kind: a.kind, |
| 4244 | arg: a.arg, |
| 4245 | })); |
| 4246 | } |
| 4247 | |
| 4248 | /** Materialize a list of trig atoms as a canonical Add. */ |
| 4249 | function atomsToExpr(ce: ComputeEngine, atoms: TrigAtom[]): Expression { |
| 4250 | const terms = atoms.map((a) => |
| 4251 | a.kind === null |
| 4252 | ? a.coef |
| 4253 | : ce.function('Multiply', [a.coef, ce.function(a.kind, [a.arg])]) |
| 4254 | ); |
| 4255 | if (terms.length === 0) return ce.Zero; |
| 4256 | if (terms.length === 1) return terms[0]; |
| 4257 | return ce.function('Add', terms); |
| 4258 | } |
| 4259 | |
| 4260 | /** ExpandTrigReduce for circular Sin/Cos: product-to-sum into a real-trig |
| 4261 | * linear combination. `deepExpand` first distributes products and expands |