Note that there are some explanatory texts on larger screens.

plurals
  1. POPython (sympy) TypeError: cannot concatenate 'str' and 'Add' objects
    primarykey
    data
    text
    <p>I'm trying to use an equation that I've generated using <code>sympy.Eq(func, var)</code> in a lambda. It seems to be returning a list of 'Add' objects, which I'm not sure how to use. I tried typecasting to a<code>str</code> before using <code>eval</code>, and i tried <code>from sympy import I</code>, which i think has something to do with the problem.</p> <p>Here's the code, and the error message:</p> <blockquote> <p>py solid.py -v y = (x-1.0)<em>(x-3.0)</em>*2 1.0 3.0 -axis y --method disk</p> </blockquote> <hr> <p><strong>Here is a running example:</strong></p> <pre><code>import sys import argparse import math from scipy import integrate from sympy import Symbol, Eq, solve, I import numpy import pylab parser = argparse.ArgumentParser(description='Find the volume of the solid of rotation defined. Use * for multiplication and ** to raise a power. Bounds are set equal to the implicit variable. Trailing .0\'s must be used for constants, and parentheses must be escaped ( e.g. \(x-2\) ). Answers are accurate to 6 decimals') parser.add_argument('var', choices='xy', help='x|y') parser.add_argument('=', metavar='=', choices='=') parser.add_argument('equation', help='continous function') parser.add_argument('a', type=float, help='bound \'a\'') parser.add_argument('b', type=float, help='bound \'b\'') parser.add_argument('-axis', choices='xy', help='axis of revolution (default == x)') parser.add_argument('-m', choices='ds', help='method (disk/shell)') parser.add_argument('-g', action='store_true', help='show graph') parser.add_argument('-v', action='store_true', help='verbose (for debugging)') args = parser.parse_args() y = Symbol('y') x = Symbol('x') def solve_for_implicit(func, var): if var == 'x': equation = Eq(eval(func), x) func = solve(equation, y) else: equation = Eq(eval(func), y) func = solve(equation, x) return func def volume(var, func, a, b, axis=None, method=None): if axis == None: axis = 'x' if method == 's': method = 'shell' if method == 'd': method = 'disk' if var == axis and axis == 'x': if args.v: print 'x = y about x' if not method == 'disk': pass else: pass elif var == axis and axis == 'y': # SHELL METHOD if args.v: print 'y = x about y' if not method == 'disk': # this is what should be used for y = x about y if args.v: print 'using SHELL method' func = eval('lambda x: ' + 'x*(' + func + ')') integral = integrate.quad(func, a, b) if args.v: print 'integral from', a, 'to', b, '=', integral[0] answer = 2 * math.pi * integral[0] if args.v: print '2*pi*', integral[0], '=', answer else: func = solve_for_implicit(func, var) print func[0] func = eval('lambda y: ' + '(' + str(func[0]) + ')**2') integral = integrate.quad(func, a, b) if args.v: print 'integral=', integral[0] answer = math.pi * integral[0] elif not var == axis and axis == 'y': # DISK METHOD if args.v: print 'x = y about y -- ' if not method == 'shell': pass else: pass elif not var == axis and axis == 'x': # DISK if args.v: print 'y = x about x --', if not method == 'shell': pass else: pass return answer print volume(args.var, args.equation, args.a, args.b, args.axis, args.m) if args.g: graph(args.equation, args.a, args.b) </code></pre> <blockquote> <p>Traceback (most recent call last):<br> File "solid.py", line 136, in print volume(args.var, args.equation, args.a, args.b, args.axis, args.m) File "solid.py", line 75, in volume func = eval('lambda y: ' + '(' + func[0] + ')**2') TypeError: cannot concatenate 'str' and 'Add' objects</p> </blockquote> <p>and when I try typecasting it to a str() first i get this:</p> <blockquote> <p>File "solid.py", line 136, in print volume(args.var, args.equation, args.a, args.b, args.axis, args.m) File "solid.py", line 76, in volume integral = integrate.quad(func, a, b) File "/usr/lib/python2.6/dist-packages/scipy/integrate/quadpack.py", line 185, in quad retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points) File "/usr/lib/python2.6/dist-packages/scipy/integrate/quadpack.py", line 249, in _quad return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit) quadpack.error: Supplied function does not return a valid float.</p> </blockquote> <p>also, <code>print func[0]</code> gives:</p> <blockquote> <p>2.33333333333333 + (0.296296296296298 - y/2 + (-0.0877914951989024 + (0.592592592592595 - y)<strong>2/4)</strong>(1/2))<strong>(1/3)*(1/2 - I*3</strong>(1/2)/2) + 0.444444444444444/((1/2 - I*3*<em>(1/2)/2)</em>(0.296296296296298 - y/2 + (-0.0877914951989024 + (0.592592592592595 - y)<strong>2/4)</strong>(1/2))**(1/3))</p> </blockquote> <p>thanks for your help everyone, sorry for not providing a complete script before.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload