Skip to content

Commit

Permalink
Doc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bradhowes committed Jul 15, 2023
1 parent 06408c3 commit be9652a
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 124 deletions.
31 changes: 1 addition & 30 deletions Sources/MathParser/Documentation.docc/GettinStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ By default, the expression parser and evaluator handle the following symbols and
* Constants: `pi` (`π`) and `e`
* 1-argument functions: `sin`, `asin`, `cos`, `acos`, `tan`, `atan`, `log10`, `ln` (`loge`), `log2`, `exp`, `ceil`,
`floor`, `round`, `sqrt` (``), `cbrt` (cube root), `abs`, `sgn`
* 2-argument functions: `atan`, `hypot`, `pow` [^1]
* 2-argument functions: `atan`, `hypot`, `pow`
* alternative math operator symbols: `×` for multiplication and `÷` for division (see example above for use of `×`)

You can reference additional symbols or variables and functions by providing your own mapping functions. There are two
Expand All @@ -48,32 +48,3 @@ that can be resolved to constant values during the parse. Otherwise, the symbols

You can get the unresolved symbol names from the ``Evaluator/unresolved`` attribute. It returns three collections for
unresolved variables, unary functions, and binary function names.

## Custom Symbols

Below is an example that provides a custom unary function that returns the twice the value it receives. There is also a
custom variable called `foo` which holds the constant `123.4`.

```swift
let myVariables = ["foo": 123.4]
let myFuncs: [String:(Double)->Double] = ["twice": {$0 + $0}]
let parser = MathParser(variables: myVariables.producer, unaryFunctions: myFuncs.producer)
let evaluator = parser.parse("power(twice(foo))")

# Expression parsed and `twice(foo)` resolved to `246.8` but `power` is still unknown
evaluator?.value // => nan
evaluator?.unresolved.unaryFunctions // => ['power']'
# Give evaluator way to resolve `power(246.8)`
let myEvalFuncs: [String:(Double)->Double] = ["power": {$0 * $0}]
evaluator?.eval(unaryFunctions: myEvalFuncs.producer) // => 60910.240000000005
```

Instead of passing a closure to access the dictionary of symbols, you can pass the dictionary itself:

```
let parser = MathParser(variableDict: myVariables, unaryFunctionDict: myFuncs)
evaluator?.eval(unaryFunctionDict: myEvalFuncs) // => 60910.240000000005
```

[^1]: Redundant since there is already the `^` operator.

94 changes: 0 additions & 94 deletions docs/data/documentation/mathparser/gettinstarted.json
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,6 @@
{
"code" : "pow",
"type" : "codeVoice"
},
{
"text" : " [^1]",
"type" : "text"
}
],
"type" : "paragraph"
Expand Down Expand Up @@ -530,96 +526,6 @@
}
],
"type" : "paragraph"
},
{
"anchor" : "Custom-Symbols",
"level" : 2,
"text" : "Custom Symbols",
"type" : "heading"
},
{
"inlineContent" : [
{
"text" : "Below is an example that provides a custom unary function that returns the twice the value it receives. There is also a",
"type" : "text"
},
{
"text" : " ",
"type" : "text"
},
{
"text" : "custom variable called ",
"type" : "text"
},
{
"code" : "foo",
"type" : "codeVoice"
},
{
"text" : " which holds the constant ",
"type" : "text"
},
{
"code" : "123.4",
"type" : "codeVoice"
},
{
"text" : ".",
"type" : "text"
}
],
"type" : "paragraph"
},
{
"code" : [
"let myVariables = [\"foo\": 123.4]",
"let myFuncs: [String:(Double)->Double] = [\"twice\": {$0 + $0}]",
"let parser = MathParser(variables: myVariables.producer, unaryFunctions: myFuncs.producer)",
"let evaluator = parser.parse(\"power(twice(foo))\")",
"",
"# Expression parsed and `twice(foo)` resolved to `246.8` but `power` is still unknown",
"evaluator?.value \/\/ => nan",
"evaluator?.unresolved.unaryFunctions \/\/ => ['power']'",
"# Give evaluator way to resolve `power(246.8)`",
"let myEvalFuncs: [String:(Double)->Double] = [\"power\": {$0 * $0}]",
"evaluator?.eval(unaryFunctions: myEvalFuncs.producer) \/\/ => 60910.240000000005"
],
"syntax" : "swift",
"type" : "codeListing"
},
{
"inlineContent" : [
{
"text" : "Instead of passing a closure to access the dictionary of symbols, you can pass the dictionary itself:",
"type" : "text"
}
],
"type" : "paragraph"
},
{
"code" : [
"let parser = MathParser(variableDict: myVariables, unaryFunctionDict: myFuncs)",
"evaluator?.eval(unaryFunctionDict: myEvalFuncs) \/\/ => 60910.240000000005"
],
"syntax" : null,
"type" : "codeListing"
},
{
"inlineContent" : [
{
"text" : "[^1]: Redundant since there is already the ",
"type" : "text"
},
{
"code" : "^",
"type" : "codeVoice"
},
{
"text" : " operator.",
"type" : "text"
}
],
"type" : "paragraph"
}
],
"kind" : "content"
Expand Down

0 comments on commit be9652a

Please sign in to comment.