Function

nerdamer.uSub

src/index.ts:1024
nerdamer.uSub(x: ExpressionInput, value: ExpressionInput, map?: SubstitutionMap): USubstitutionResult

Replaces one repeated sub-expression with a generated uN symbol. The generated symbol is chosen so it does not collide with variables already present in the expression or substitution map. Pass the returned map to later calls when several substitutions should share one namespace, then use uUnSub to restore the original expressions.

Parameters

NameTypeDescription
xExpressionInputExpression in which the substitution will occur.
valueExpressionInputSub-expression to replace.
mapSubstitutionMapExisting substitution map to extend.

Returns

USubstitutionResult — The substituted expression and the updated substitution map.

Examples

const [substituted, map] = uSub('cos(x)^2+cos(x)+1', 'cos(x)');
substituted.text({ sort: true }); // "u0^2+u0+1"
uUnSub(substituted, map).eq('cos(x)^2+cos(x)+1'); // true