The problem is surely that the pieces f1a etc are just Python lambda-functions, yet the integral() method is being called on each of these in the line return sum([funcs[i].integral(x,invs[i][0],invs[i][1]) for i in range(n)]) so the problem arises since you are asking to integrate each funcs[i] but they do not know how to integrate themselves.
The solution would be to define your f1a (etc) not as lambda-functions but as functions which Sage knows how to integrate, and then form the piecewise function out of those "integrable" components. John On 14/01/2008, David Joyner <[EMAIL PROTECTED]> wrote: > > At the moment, > (a) "piecewise" is only set up for piecewise polynomials, > (b) the "integrate" command is "integral". > So, for your function (which is piecewise polynomial), this should > work: > > sage: f1a = lambda x: -x+1; f1b = lambda x: x+1 > sage: f2a = lambda x : -(x - 2) - 1; f2b = lambda x : (x - 2) - 1 > sage: tri_wave = piecewise([ [(-1,0), f1a], [(0,1),f1b], [(1,2), f2a], > [(2,3),f2b]]) > sage: tri_wave.integral() > --------------------------------------------------------------------------- > <type 'exceptions.AttributeError'> Traceback (most recent call last) > > /mnt/drive_hda1/sagefiles/sage-2.9.alpha5/<ipython console> in <module>() > > /home/wdj/wdj/sagefiles/sage-2.9.alpha5/local/lib/python2.5/site-packages/sage/functions/piecewise.py > in integral(self, x) > 636 invs = self.intervals() > 637 n = len(funcs) > --> 638 return sum([funcs[i].integral(x,invs[i][0],invs[i][1]) > for i in range(n)]) > 639 > 640 def convolution(self,other): > > <type 'exceptions.AttributeError'>: 'function' object has no attribute > 'integral' > > > I have no idea what that error means. It is (if you look at the piecewise.py > code) clear that integral is a method for the class PiecewisePolynomial and > that > tri_wave is an instance of that class. Indeed, tri_wave.[tab] gives > you the methods > allowed: > > sage: tri_wave. > tri_wave.base_ring tri_wave.intervals > tri_wave.convolution tri_wave.laplace > tri_wave.cosine_series_coefficient tri_wave.length > tri_wave.critical_points tri_wave.list > tri_wave.derivative tri_wave.plot > tri_wave.domain > tri_wave.plot_fourier_series_partial_sum > tri_wave.end_points > tri_wave.plot_fourier_series_partial_sum_cesaro > tri_wave.extend_by_zero_to > tri_wave.plot_fourier_series_partial_sum_filtered > tri_wave.fourier_series_cosine_coefficient > tri_wave.plot_fourier_series_partial_sum_hann > tri_wave.fourier_series_partial_sum tri_wave.riemann_sum > tri_wave.fourier_series_partial_sum_cesaro > tri_wave.riemann_sum_integral_approximation > tri_wave.fourier_series_partial_sum_filtered > tri_wave.sine_series_coefficient > tri_wave.fourier_series_partial_sum_hann tri_wave.tangent_line > tri_wave.fourier_series_sine_coefficient tri_wave.trapezoid > tri_wave.fourier_series_value > tri_wave.trapezoid_integral_approximation > tri_wave.functions tri_wave.unextend > tri_wave.integral tri_wave.which_function > > > I have no idea if this is another bug or what. Once I understand this, > hopefully I can fix the > first bug. > > > On Jan 14, 2008 1:00 AM, William Stein <[EMAIL PROTECTED]> wrote: > > > > On 1/13/08, Hector Villafuerte <[EMAIL PROTECTED]> 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. > > > > I've made the bug you point out above trac #1773: > > http://trac.sagemath.org/sage_trac/ticket/1773 > > > > -- William > > > > > > > > > > > > > -- John Cremona --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/ -~----------~----~----~----~------~----~------~--~---