Ticket #1773 (new defect)
piecewise functions and integration / arithmetic do not play well together
| Reported by: | was | Owned by: | gfurnish |
|---|---|---|---|
| Priority: | major | Milestone: | sage-5.11 |
| Component: | calculus | Keywords: | |
| Cc: | kcrisman | Work issues: | |
| Report Upstream: | N/A | Reviewers: | |
| Authors: | Merged in: | ||
| Dependencies: | Stopgaps: |
Description
On 1/13/08, Hector Villafuerte <> wrote: > > I defined a piecewise function (specifically, a triangular wave) like this: > > sage: f1(x) = -abs(x) + 1 > sage: f2(x) = abs(x - 2) - 1 > sage: tri_wave = piecewise([ [(-1,1), f1], [(1,3), f2]]) > > One can plot it and it looks very nice: > > sage: tri_wave.plot() > > But while calculating this integral I get "ValueError: Value not > defined outside of domain." > > sage: integrate(tri_wave(x)^2, x, -1, 3) > > Is there a way to integrate piecewise-defined functions? > As always, thanks for your help, This is clearly broken. As a band-aide, you can at least numerically integrate as follows: sage: integral_numerical(lambda x: tri_wave(x)^2, -1, 3) (1.3333333333333333, 1.4765966227514582e-14) The first output (1.3333...) is the answer, and the second is an error bound. -- William
Change History
comment:1 Changed 5 years ago by gfurnish
- Owner changed from was to gfurnish
- Status changed from new to assigned
comment:2 follow-up: ↓ 6 Changed 4 years ago by rlm
This isn't specific to integrate. The problem is that piecewise functions don't play well with *symbolics*
sage: f1(x) = -abs(x) + 1 sage: f2(x) = abs(x - 2) - 1 sage: tri_wave = piecewise([ [(-1,1), f1], [(1,3), f2]]) sage: tri_wave(x) SAME ERROR
comment:3 Changed 4 years ago by rlm
What should maybe happen is in piecewise.py, in the __call__ method, if a SymbolicVariable? is passed in as x0, it should return a CallableSymbolicExpression? which just in turn calls back to the __call__ method again. Does this sound reasonable? If so, how would one implement this?
comment:5 Changed 22 months ago by kcrisman
See also #11225, which is about plotting - but sometimes this stuff causes plots to not work, of course.
comment:6 in reply to: ↑ 2 Changed 22 months ago by kcrisman
Replying to rlm:
This isn't specific to integrate. The problem is that piecewise functions don't play well with *symbolics*
sage: f1(x) = -abs(x) + 1 sage: f2(x) = abs(x - 2) - 1 sage: tri_wave = piecewise([ [(-1,1), f1], [(1,3), f2]]) sage: tri_wave(x) SAME ERROR
Or see what happens when we try to multiply piecewise and symbolic.
sage: f = Piecewise([[(0,pi/2),-1],[(pi/2,pi),2]]) sage: f*sin(x) --------------------------------------------------------------------------- AttributeError
