Skip to content

Commit

Permalink
Update operators.md
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Dec 13, 2023
1 parent 80d7197 commit d39c0a6
Showing 1 changed file with 60 additions and 36 deletions.
96 changes: 60 additions & 36 deletions docs/operators.md
Expand Up @@ -2,46 +2,60 @@

## Pre-defined

All Base julia operators that take 1 or 2 scalars as input,
and output a scalar as output, are available. A selection
of these and other valid operators are stated below.
First, note that pretty much any valid Julia function which
takes one or two scalars as input, and returns on scalar as output,
is likely to be a valid operator[^1].
A selection of these and other valid operators are stated below.

**Binary**

`+`, `-`, `*`, `/`, `^`, `greater`, `mod`, `logical_or`,
`logical_and`
- `+`
- `-`
- `*`
- `/`
- `^`
- `cond`
- Equal to `(x, y) -> x > 0 ? y : 0`
- `greater`
- Equal to `(x, y) -> x > y ? 1 : 0`
- `logical_or`
- Equal to `(x, y) -> (x > 0 || y > 0) ? 1 : 0`
- `logical_and`
- Equal to `(x, y) -> (x > 0 && y > 0) ? 1 : 0`
- `mod`

**Unary**

`neg`,
`square`,
`cube`,
`exp`,
`abs`,
`log`,
`log10`,
`log2`,
`log1p`,
`sqrt`,
`sin`,
`cos`,
`tan`,
`sinh`,
`cosh`,
`tanh`,
`atan`,
`asinh`,
`acosh`,
`atanh_clip` (=atanh((x+1)%2 - 1)),
`erf`,
`erfc`,
`gamma`,
`relu`,
`round`,
`floor`,
`ceil`,
`round`,
`sign`.
- `neg`
- `square`
- `cube`
- `exp`
- `abs`
- `log`
- `log10`
- `log2`
- `log1p`
- `sqrt`
- `sin`
- `cos`
- `tan`
- `sinh`
- `cosh`
- `tanh`
- `atan`
- `asinh`
- `acosh`
- `atanh_clip`
- Equal to `atanh(mod(x + 1, 2) - 1)`
- `erf`
- `erfc`
- `gamma`
- `relu`
- `round`
- `floor`
- `ceil`
- `round`
- `sign`

## Custom

Expand All @@ -52,7 +66,11 @@ you can define with by passing it to the `pysr` function, with, e.g.,
PySRRegressor(
...,
unary_operators=["myfunction(x) = x^2"],
binary_operators=["myotherfunction(x, y) = x^2*y"]
binary_operators=["myotherfunction(x, y) = x^2*y"],
extra_sympy_mappings={
"myfunction": lambda x: x**2,
"myotherfunction": lambda x, y: x**2 * y,
},
)
```

Expand All @@ -62,7 +80,7 @@ Make sure that it works with
instead of `1.5e3`, if you write any constant numbers, or simply convert a result to `Float64(...)`.

PySR expects that operators not throw an error for any input value over the entire real line from `-3.4e38` to `+3.4e38`.
Thus, for "invalid" inputs, such as negative numbers to a `sqrt` function, you may simply return a `NaN` of the same type as the input. For example,
Thus, for invalid inputs, such as negative numbers to a `sqrt` function, you may simply return a `NaN` of the same type as the input. For example,

```julia
my_sqrt(x) = x >= 0 ? sqrt(x) : convert(typeof(x), NaN)
Expand All @@ -71,3 +89,9 @@ my_sqrt(x) = x >= 0 ? sqrt(x) : convert(typeof(x), NaN)
would be a valid operator. The genetic algorithm
will preferentially selection expressions which avoid
any invalid values over the training dataset.


<!-- Footnote for 1: -->
<!-- (Will say "However, you may need to define a `extra_sympy_mapping`":) -->

[^1]: However, you will need to define a sympy equivalent in `extra_sympy_mapping` if you want to use a function not in the above list.

0 comments on commit d39c0a6

Please sign in to comment.