Integration of piecewise function

143 views
Skip to first unread message

Sand Wraith

unread,
Dec 8, 2009, 4:28:40 PM12/8/09
to sage-support
Hello,

there is signal function:
signal = Piecewise([
[(0,5), cos(2*pi*1*x) ],
[(5,10), cos(2*pi*2.5*x) ],
])

And window function:
window = Piecewise([[(-1,1),1]])

And the result function:
r=window*signal

How can I integrate it now? I'am trying:
r.integral(definite=True)

but it leads to error: "TypeError: integral() takes no arguments (3
given)"

David Joyner

unread,
Dec 8, 2009, 4:47:11 PM12/8/09
to sage-s...@googlegroups.com
Unfortunately, the piecewise class was written before the symbolic
expressions class and has not kept pace.

The obvious solution produced this:


sage: signal = Piecewise([[(0,1), cos(2*pi*1*x)], [(-1, 0), 0*x]])
sage: signal.integral(definite=True)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)

/Users/wdj/.sage/temp/jeeves.home/2806/_Users_wdj__sage_init_sage_0.py
in <module>()

/Users/wdj/sagefiles/sage-4.2.1/local/lib/python2.6/site-packages/sage/functions/piecewise.pyc
in integral(self, x, a, b, definite)
787 fun_integrated = fun.integral(x, end, x)
788 else:
--> 789 fun_integrated = fun.integral(x, start, x) + area
790 if definite or end != infinity:
791 area += fun.integral(x, start, end)

/Users/wdj/sagefiles/sage-4.2.1/local/lib/python2.6/site-packages/sage/symbolic/expression.so
in sage.symbolic.expression.Expression.integral
(sage/symbolic/expression.cpp:25344)()

/Users/wdj/sagefiles/sage-4.2.1/local/lib/python2.6/site-packages/sage/calculus/calculus.pyc
in integral(expression, v, a, b, algorithm)
635 else:
636 try:
--> 637 result = expression._maxima_().integrate(v, a, b)
638 except TypeError, error:
639 s = str(error)

/Users/wdj/sagefiles/sage-4.2.1/local/lib/python2.6/site-packages/sage/interfaces/maxima.pyc
in integral(self, var, min, max)
2023 if max is None:
2024 raise ValueError, "neither or both of min/max
must be specified."
-> 2025 return I(var, min, max)
2026
2027 integrate = integral

/Users/wdj/sagefiles/sage-4.2.1/local/lib/python2.6/site-packages/sage/interfaces/expect.pyc
in __call__(self, *args, **kwds)
1402
1403 def __call__(self, *args, **kwds):
-> 1404 return self._obj.parent().function_call(self._name,
[self._obj] + list(args), kwds)
1405
1406 def help(self):

/Users/wdj/sagefiles/sage-4.2.1/local/lib/python2.6/site-packages/sage/interfaces/expect.pyc
in function_call(self, function, args, kwds)
1310 [s.name() for s in args],
1311
['%s=%s'%(key,value.name()) for key, value in kwds.items()])
-> 1312 return self.new(s)
1313
1314 def _function_call_string(self, function, args, kwds):

/Users/wdj/sagefiles/sage-4.2.1/local/lib/python2.6/site-packages/sage/interfaces/expect.pyc
in new(self, code)
1094
1095 def new(self, code):
-> 1096 return self(code)
1097
1098 ###################################################################

/Users/wdj/sagefiles/sage-4.2.1/local/lib/python2.6/site-packages/sage/interfaces/expect.pyc
in __call__(self, x, name)
1029
1030 if isinstance(x, basestring):
-> 1031 return cls(self, x, name=name)
1032 try:
1033 return self._coerce_from_special_method(x)

/Users/wdj/sagefiles/sage-4.2.1/local/lib/python2.6/site-packages/sage/interfaces/expect.pyc
in __init__(self, parent, value, is_name, name)
1445 except (TypeError, KeyboardInterrupt,
RuntimeError, ValueError), x:
1446 self._session_number = -1
-> 1447 raise TypeError, x
1448 self._session_number = parent._session_number
1449

TypeError: Computation failed since Maxima requested additional
constraints (try the command 'assume(x>0)' before integral or limit
evaluation, for example):
Is x positive, negative, or zero?
> --
> To post to this group, send email to sage-s...@googlegroups.com
> To unsubscribe from this group, send email to sage-support...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-support
> URL: http://www.sagemath.org
>

Sand Wraith

unread,
Dec 9, 2009, 1:03:59 PM12/9/09
to sage-support
Does anyone know is this issue only for newest version? (may be I
should use older version of sage)

Eugene Goldberg

unread,
Dec 13, 2009, 3:32:38 PM12/13/09
to sage-support
So... There is no solution?

David Joyner

unread,
Dec 13, 2009, 3:39:56 PM12/13/09
to sage-s...@googlegroups.com, Paul Butler
I'm cc'ing Paul Butler who wrote that method.

Paul, are you following this thread?

Paul Butler

unread,
Dec 13, 2009, 4:52:41 PM12/13/09
to sage-support
David, thanks for making me aware of this, I wasn't following the
thread.

It looks like two things are happening.

First, the multiplication of piecewise functions results in some of
the elements (the 0's) of the piecewise function becoming
Polynomial_rational_dense instances. It looks like integration is
defined for Polynomial_rational_dense, but only indefinite integration
(the piecewise integration function relies on its pieces having
integral(variable, start, end) defined). There are a number of ways to
solve this, but I'm not sure which is the best until I have a better
chance to look at the polynomial code and piecewise multiplication.

Second, the issue of maxima not knowing the range of x. Adding assume
(start < x) to the piecewise integration function may be enough to
resolve this.

I have some time over the next few days to have a better look and put
together a patch.

-- Paul
Reply all
Reply to author
Forward
0 new messages