2

I am trying to compute the result of a Fourier integral coefficient. When I use integrate() and print the result I get a Piecewise object with several arguments, one of them being the answer I'm looking for. However, I only need that argument and can't find a way to subtract it from the piecewise function.

I've read that each argument of the piecewise object is a 2-tuple defining the expression and a condition. I've tried using the item number but I get:

TypeError: 'Piecewise' object is not iterable

I've also tried converting the result into a list and it works, but I get a list of one item with the whole piecewise function so I get the same problem.

The code is fairly simple:

from sympy import *
x = Symbol('x')
w = Symbol('w')

func = exp(-abs(x))
a = integrate(func*cos(w*x), (x, -oo, oo))
print (a)

The result of the Fourier coefficient is 2/(w^2 +1), but the output is:

Piecewise((2/(w**2 + 1), Eq(2*Abs(arg(w)), 0)), (Integral(exp(-Abs(x))cos(wx), (x, -oo, oo)), True))

As you can see the function I'm looking for is there, in the first argument, but I can't find a way to take only the first argument from the objet and the take only the expression. I'm not experienced in python so I don't think I've tried all possible solutions, any ideas?

Jose Ayala
  • 21
  • 3
  • Possible duplicate of [Dealing with piecewise equations returned by sympy integrate](https://stackoverflow.com/questions/15420816/dealing-with-piecewise-equations-returned-by-sympy-integrate) – walnut Oct 06 '19 at 00:04
  • Note that the hints in the duplicates answer about ignoring convergence and about limiting the parameter to real values both apply here as well. – walnut Oct 06 '19 at 00:07

1 Answers1

7

Use a.args[index] will give a corresponding tuple (expr, cond), a.args[index][0] will give the expr, and [1] the cond

Zexuan Sun
  • 71
  • 1
  • 2