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: 
Description (last modified by )
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/cansagecomputetheinverseofafunction
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).
sagedevel discussion: http://groups.google.com/group/sagedevel/browse_thread/thread/d7d9af95a67d4481
Attachments (1)
Change History (25)
comment:1 Changed 12 years ago by
Cc:  kcrisman added 

comment:2 Changed 12 years ago by
Description:  modified (diff) 

Summary:  Add inverse method to callable symbolic functions → Add inverse method to symbolic expressions 
comment:3 Changed 12 years ago by
Description:  modified (diff) 

Changed 11 years ago by
Attachment:  11202.patch added 

comment:4 Changed 11 years ago by
Status:  new → needs_review 

comment:5 Changed 11 years ago by
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
comment:6 Changed 11 years ago by
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
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
Authors:  → Eviatar Bach 

comment:10 followup: 11 Changed 11 years ago by
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 followup: 12 Changed 11 years ago by
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 aTypeError
.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 followup: 13 Changed 10 years ago by
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 Changed 10 years ago by
comment:14 Changed 9 years ago by
Milestone:  sage5.11 → sage5.12 

comment:15 Changed 9 years ago by
Milestone:  sage6.1 → sage6.2 

comment:16 Changed 9 years ago by
Milestone:  sage6.2 → sage6.3 

comment:17 Changed 9 years ago by
Status:  needs_review → needs_work 

Work issues:  → address review, rebase 
comment:18 Changed 8 years ago by
Milestone:  sage6.3 → sage6.4 

comment:19 Changed 7 years ago by
Branch:  → u/rws/add_inverse_method_to_symbolic_expressions 

comment:20 Changed 7 years ago by
Authors:  Eviatar Bach → Eviatar Bach, Ralf Stephan 

Commit:  → f9bfdf4426602a74006806c7561a4e13466520fa 
Milestone:  sage6.4 → sage7.4 
Status:  needs_work → needs_review 
Work issues:  address review, rebase 
comment:21 Changed 7 years ago by
Cannot reproduce the patchbot fails in modular/modform/
, apparently a onceonly problem of the librae patchbot.
comment:22 Changed 5 years ago by
Milestone:  sage7.4 → sage8.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 theINPUT:
section  formatting of
OUTPUT:
section
comment:23 Changed 5 years ago by
Status:  needs_review → needs_work 

comment:24 Changed 6 weeks ago by
Milestone:  sage8.4 

Patch added!