Trying to convert Matlab .m symbolic function file(s) to SymPy symbolic expression
<p>I have computer generated .m files for electrical circuit transfer functions output by Sapwin 4.0
<a href="http://cirlab.dinfo.unifi.it/Sapwin4/" rel="nofollow">http://cirlab.dinfo.unifi.it/Sapwin4/</a></p>

<p>The .m files have fairly simple structure for my present interests:</p>

<pre class="lang-Matlab prettyprint-override"><code>function [out]=my003_v1(s,C1,C2,E1,R1,R2);
num = + ( E1 )+ ( E1*C1*R2 +E1*C2*R2 )*s;
den = + ( E1 +1 )+ ( E1*C1*R2 +C1*R2 +C1*R1 +E1*C2*R2 +C2*R2 )*s+ ( E1*C2*C1*R1*R2 +C2*C1*R1*R2 )*s^2;
out = num/den;
</code></pre>

<p>I want to convert many of these .m files into SymPy symbolic expressions for further symbolic manipulation</p>

<p>with open, infile.read(), indexing lines, slicing to get desired strings for args and num, den all work</p>

<p>So I just show cut down symbolic conversion steps with even the string variables replaced with the actual strings:
</p>

<pre><code>from sympy import symbols, var, sympify

var('s,C1,C2,E1,R1,R2')
'''

#awkward alternative to var:
exp_str='s,C1,C2,E1,R1,R2' + " = symbols('" + 's,C1,C2,E1,R1,R2' + "')"
exec(exp_str)
print(exp_str)
'''

a=sympify(' ( E1 )+ ( E1*C1*R2 +E1*C2*R2 )*s')
'''
# another alternative to sympify:
from sympy.parsing.sympy_parser import (parse_expr,
standard_transformations)

parse_expr(' ( E1 )+ ( E1*C1*R2 +E1*C2*R2 )*s', transformations=(standard_transformations))
'''
</code></pre>

<p>The triple quote blocks show alternatives I've already tried with similar results, same bottom line:</p>

<blockquote>
  <p>TypeError: unsupported operand type(s) for *: 'function' and 'Symbol'</p>
</blockquote>

<p>The error report with code ran on SympyLive:</p>

<pre><code>Traceback (most recent call last):
  File "&lt;string&gt;", line 12, in &lt;module&gt;
  File "/base/data/home/apps/s~sympy-live-hrd/46.393464279709602171/sympy/sympy/core/sympify.py", line 322, in sympify
    expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate)
  File "/base/data/home/apps/s~sympy-live-hrd/46.393464279709602171/sympy/sympy/parsing/sympy_parser.py", line 894, in parse_expr
    return eval_expr(code, local_dict, global_dict)
  File "/base/data/home/apps/s~sympy-live-hrd/46.393464279709602171/sympy/sympy/parsing/sympy_parser.py", line 807, in eval_expr
    code, global_dict, local_dict)  # take local objects in preference
  File "&lt;string&gt;", line 1, in &lt;module&gt;
TypeError: unsupported operand type(s) for *: 'function' and 'Symbol'
</code></pre>

<p>I see similar error on my Anaconda3 recent install, Spyder, on IPython and regular console</p>

<p>sympify seems to work with similar expression structure in SymPy Live:</p>

<pre><code>&gt;&gt;&gt; sympify(x*y+(z**k+x*y*z)*t)
t(xyz+zk)+xy


... sympify('-(x**k+ y*z*t+ m)*z')
z(âmâtyzâxk)
</code></pre>

<p>What about the .m file den string is breaking it?
or am I doing it wrong some other way?</p>

<p>for more fun, if I slice the .m den string earlier after the "=" to be safe, it includes the leading "+", and I get:</p>

<blockquote>
  <p>TypeError: bad operand type for unary +: 'function'</p>
</blockquote>

<p>Which is a problem that can be worked around with the filtering but not allowing this unary use of "+" seems be a poor assumption
The numerator term in a transfer function may easily be positive or negative</p>

<p>Well, let's try to find the minimum failing case:</p>

<pre><code>&gt;&gt;&gt; a=sympify('E1*C2')
Traceback (most recent call last):
[...]
TypeError: unsupported operand type(s) for *: 'function' and 'Symbol'
</code></pre>

<p>which makes it clear it's the E1 which is the problem here, because it's an <a href="http://docs.sympy.org/latest/modules/functions/special.html?highlight=e1#sympy.functions.special.error_functions.E1" rel="nofollow">existing function</a>:</p>

<pre><code>&gt;&gt;&gt; a=sympify('E1')
&gt;&gt;&gt; a
&lt;function E1 at 0x7fcb04c11510&gt;
</code></pre>

<p>And thus the error message.  One way to get around this would be to specify that you don't want E1 to be the function by overriding it in the locals argument:</p>

<pre><code>&gt;&gt;&gt; a = sympify(' ( E1 )+ ( E1*C1*R2 +E1*C2*R2 )*s', locals={'E1': E1})
&gt;&gt;&gt; a
E1 + s*(C1*E1*R2 + C2*E1*R2)
</code></pre>

<p>(after you've already done your <code>var</code> to create <code>E1</code> in the namespace), or more generally if you wanted to protect everything in your <code>vv</code>:</p>

<pre><code>vv = sympy.var('s,C1,C2,E1,R1,R2')
a=sympify(' ( E1 )+ ( E1*C1*R2 +E1*C2*R2 )*s', locals={str(v):v for v in vv})
</code></pre>

python sympy