Opened 12 years ago

Last modified 6 weeks ago

#11202 needs_work enhancement

Add inverse method to symbolic expressions

Reported by: ltw Owned by: tba
Priority: minor Milestone:
Component: symbolics Keywords: InverseFunction, inverse function
Cc: kcrisman Merged in:
Authors: Eviatar Bach, Ralf Stephan Reviewers:
Report Upstream: N/A Work issues:
Branch: u/rws/add_inverse_method_to_symbolic_expressions (Commits, GitHub, GitLab) Commit: f9bfdf4426602a74006806c7561a4e13466520fa
Dependencies: Stopgaps:

Status badges

Description (last modified by ltw)

Mathematica has an InverseFunction function:

http://reference.wolfram.com/mathematica/ref/InverseFunction.html

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).

sage-devel discussion: http://groups.google.com/group/sage-devel/browse_thread/thread/d7d9af95a67d4481

Attachments (1)

11202.patch (5.0 KB) - added by eviatarbach 11 years ago.

Download all attachments as: .zip

Change History (25)

comment:1 Changed 12 years ago by kcrisman

Cc: kcrisman added

comment:2 Changed 12 years ago by ltw

Description: modified (diff)
Summary: Add inverse method to callable symbolic functionsAdd inverse method to symbolic expressions

comment:3 Changed 12 years ago by ltw

Description: modified (diff)

Changed 11 years ago by eviatarbach

Attachment: 11202.patch added

comment:4 Changed 11 years ago by eviatarbach

Status: newneeds_review

Patch added!

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:8 Changed 11 years ago by jdemeyer

Please fill in your real name as Author.

comment:9 Changed 11 years ago by eviatarbach

Authors: Eviatar Bach

comment:10 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 ; Changed 11 years ago by eviatarbach

Replying to 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?)

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

comment:12 in reply to:  11 ; 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

Replying to kcrisman:

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.11sage-5.12

comment:15 Changed 9 years ago by vbraun_spam

Milestone: sage-6.1sage-6.2

comment:16 Changed 9 years ago by vbraun_spam

Milestone: sage-6.2sage-6.3

comment:17 Changed 9 years ago by rws

Status: needs_reviewneeds_work
Work issues: address review, rebase

comment:18 Changed 8 years ago by vbraun_spam

Milestone: sage-6.3sage-6.4

comment:19 Changed 7 years ago by rws

Branch: u/rws/add_inverse_method_to_symbolic_expressions

comment:20 Changed 7 years ago by rws

Authors: Eviatar BachEviatar Bach, Ralf Stephan
Commit: f9bfdf4426602a74006806c7561a4e13466520fa
Milestone: sage-6.4sage-7.4
Status: needs_workneeds_review
Work issues: address review, rebase

New commits:

c5fe90811202: Add inverse method to symbolic expressions
f9bfdf411202: 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.4sage-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_reviewneeds_work

comment:24 Changed 6 weeks ago by mkoeppe

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