Opened 12 years ago

# Add inverse method to symbolic expressions

Reported by: Owned by: ltw tba minor symbolics InverseFunction, inverse function kcrisman Eviatar Bach, Ralf Stephan N/A u/rws/add_inverse_method_to_symbolic_expressions f9bfdf4426602a74006806c7561a4e13466520fa

Mathematica has an `InverseFunction` function:

Sage can accomplish this via the `roots` method, as described on AskSage: http://ask.sagemath.org/question/502/can-sage-compute-the-inverse-of-a-function

However, there is no "convenience" method to find inverses of symbolic expressions. Since `roots` itself just calls Maxima's `solve`, it should be relatively straightforward to implement an `inverse` method in the same fashion. This should also be able to find the inverse of vector functions (`n`-dimensional input, `n`-dimensional output).

### comment:2 Changed 12 years ago by ltw

Description: modified (diff) Add inverse method to callable symbolic functions → Add inverse method to symbolic expressions

### comment:3 Changed 12 years ago by ltw

Description: modified (diff)

### comment:4 Changed 11 years ago by eviatarbach

Status: new → needs_review

### comment:5 Changed 11 years ago by eviatarbach

Note that this method is faster than using roots, as was suggested by Kelvin Li:

```sage: timeit('inverse(log(x), y)')
125 loops, best of 3: 2.88 ms per loop
sage: timeit('(log(x) - y).roots(x)')
125 loops, best of 3: 3.39 ms per loop
```
Last edited 11 years ago by eviatarbach (previous) (diff)

### comment:6 Changed 11 years ago by kcrisman

Interesting. I hope someone looks at this more carefully, because it would be great to have this more easily accessible. Two comments:

• The documentation that would come up in this is in the functional.py file. Since some people might think this is pretty fully featured, but it probably isn't, that documentation should be bigger, and point to the full doc in the expression file.
• I'd like to see, from whomever tests it, a pretty sizable number of test cases that indicate we won't get "weird" output. Namely, what happens when `solve` is baffled? Multivariate things? Things that have inverses (like functions defined by integrals or something) but which won't have them shown here?

In the past we've had a lot of complaints from adding "early stage" things. I'm still in favor of doing so, as that's the only way this project improves, but we want to make sure that we make it really clear to even the most obtuse reader what the real status and abilities of something like this is.

### comment:7 Changed 11 years ago by eviatarbach

Okay, will do.

I don't know what you mean by it not being "fully featured". It should be able to handle any invertible functions, assuming solve supports them. Those are really the only limitations here. If it can't solve it, it just leaves it unevaluated, the same way that solve does. For example,

```(abs(x) == y).solve(x)
[abs(x) == y]
```

Don't think there are many places for this to go wrong, since all it does is switch around two variables and solves for one of them. But I will add some test cases (or did you want someone else to do it, since you said "from whomever tests it"?). I did include a multivariate example, by the way. I don't know what you mean by functions defined by integrals; could you please give an example?

Thank you.

### comment:9 Changed 11 years ago by eviatarbach

Authors: → Eviatar Bach

### comment:10 follow-up:  11 Changed 11 years ago by tkluck

I've taken a look at your patch (haven't tried it yet). It looks like good work! Here's some feedback:

```if not with_respect_to:
raise TypeError('You must specify with respect to which \
variable to take the inverse.')
```

I would test for

```if with_respect_to == None or not with_respect_to.is_symbol():
```

I also think that a `ValueError` is more appropriate than a `TypeError`.

Also, it would be a nice enhancement if something like this would work:

```sage: f(x) = log(x)
sage: f
x |--> log(x)
sage: inverse(f)
x |--> exp(x)
```

(or does it?)

### comment:11 in reply to:  10 ; follow-up:  12 Changed 11 years ago by eviatarbach

I've taken a look at your patch (haven't tried it yet). It looks like good work! Here's some feedback:

```if not with_respect_to:
raise TypeError('You must specify with respect to which \
variable to take the inverse.')
```

I would test for

```if with_respect_to == None or not with_respect_to.is_symbol():
```

I also think that a `ValueError` is more appropriate than a `TypeError`.

Also, it would be a nice enhancement if something like this would work:

```sage: f(x) = log(x)
sage: f
x |--> log(x)
sage: inverse(f)
x |--> exp(x)
```

(or does it?)

Thanks for the feedback! I'll see if I can work on this soon.

### comment:12 in reply to:  11 ; follow-up:  13 Changed 10 years ago by kcrisman

```if not with_respect_to:
raise TypeError('You must specify with respect to which \
variable to take the inverse.')
```

I would test for

```if with_respect_to == None or not with_respect_to.is_symbol():
```

Or `if with_respect_to is None`?

### comment:13 in reply to:  12 Changed 10 years ago by tkluck

` if with_respect_to == None or not with_respect_to.is_symbol(): `

Or `if with_respect_to is None` ?

This guy agrees with you.

### comment:14 Changed 9 years ago by jdemeyer

Milestone: sage-5.11 → sage-5.12

### comment:15 Changed 9 years ago by vbraun_spam

Milestone: sage-6.1 → sage-6.2

### comment:16 Changed 9 years ago by vbraun_spam

Milestone: sage-6.2 → sage-6.3

### comment:17 Changed 9 years ago by rws

Status: needs_review → needs_work → address review, rebase

### comment:18 Changed 8 years ago by vbraun_spam

Milestone: sage-6.3 → sage-6.4

### comment:20 Changed 7 years ago by rws

Authors: Eviatar Bach → Eviatar Bach, Ralf Stephan → f9bfdf4426602a74006806c7561a4e13466520fa sage-6.4 → sage-7.4 needs_work → needs_review address review, rebase

New commits:

 ​c5fe908 `11202: Add inverse method to symbolic expressions` ​f9bfdf4 `11202: address reviewers' comments`

### comment:21 Changed 7 years ago by rws

Cannot reproduce the patchbot fails in `modular/modform/`, apparently a once-only problem of the librae patchbot.

### comment:22 Changed 5 years ago by gh-bryangingechen

Milestone: sage-7.4 → sage-8.4

Is there a good reason that:

```sage: y = var('y')
sage: log(x).inverse(y)
```

should return `[y == exp(x)]` rather than `[x == exp(y)]`? I find the latter much more intuitive.

The tests should also cover some cases where errors are raised.

More trivially, there are some violated conventions in the docstring:

• extra indentation and `-` instead of `--` in the `INPUT:` section
• formatting of `OUTPUT:` section

### comment:23 Changed 5 years ago by gh-bryangingechen

Status: needs_review → needs_work

### comment:24 Changed 6 weeks ago by mkoeppe

Milestone: sage-8.4
Note: See TracTickets for help on using tickets.