Skip to content

Commit 0cebc5c

Browse files
committed
doc: add example to override a function def
1 parent e5b8438 commit 0cebc5c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

docs/compute-engine/06-guide-augmenting.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,25 @@ ce.assign("double",ce.parse("x \\mapsto 2x"));
237237
```
238238

239239

240+
## Overloading Functions
241+
242+
**To overload a function**, use the `ce.lookupFunction()` and `ce.define()` methods.
243+
244+
For example, to overload the `Sqrt` function to return `NaN` for
245+
non-real numbers, you can use the following code:
246+
247+
```js
248+
const originalSqrtDefinition = ce.lookupFunction('Sqrt')!;
249+
ce.defineFunction('Sqrt', {
250+
...originalSqrtDefinition,
251+
evaluate: (x, options) => {
252+
const y = originalSqrtDefinition.evaluate!(x, options);
253+
return y?.isReal ? y : ce.NaN;
254+
},
255+
});
256+
```
257+
258+
240259
## Defining Multiple Functions and Symbols
241260
242261
**To define multiple functions and symbols**, use the `ce.declare()` method

0 commit comments

Comments
 (0)