1

Lengthy title, but I thought it might be best to be very informative...

I have very long expressions using symbols such as %i, %e, log, z1 and z2, that is sandwiched in between double quotes, e.g. something like,

"(4*z1*z2*%e^(z2^2+z1^2)*((%e^z1^2-%e^z2^2)^2*(96*%e^(13*(((-(202907687053026635*%i*sqrt(1037*sqrt(23)*%i+1463)*(log(9)*sqrt(2)*sqrt(7)*sqrt(1037*sqrt(23)*%i+....."X

(where when viewing the file in a hex editor, the last X is not an X but a hexadecimal 0A - whatever that is - don't think looking it up on an ascii chart will shed much light on it)

I'd like to try out the python/sympy/numpy/scipy/... amalgamations in lieu of other CAS-capable software, but I am having no luck finding out how to do this, at least from a consistent "package". I see snippets from a tutorial on scipy, or a snippet from numpy, etc.

I would like to take the Laurent series of an expression like above - it is exact expression devoid of floats.

Hope this is easy to understand request,

Best wishes.

edit - update Python to 3.6 - still AttributeError

So I saw some errors in converting the symbols exp, etc. into an acceptable string. This seems to work better but "ex" is still not being treated as a Sympy expression after sympify:

(py3_kernel) sbh@sbh-MacBookPro ~ $ ipython
Python 3.6.3 (default, Oct  6 2017, 08:44:35) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import sympy as s

In [2]: import numpy as np

In [3]: from numpy import *

In [4]: from sympy import *

In [5]: from sympy.utilities.lambdify import lambdify, implemented_function, lambdastr

In [6]: with open('c.txt', 'r') as myfile:
   ...:     d=myfile.read().replace('\n', '').replace('%i','I').replace('%e','exp').replace('^','**
   ...: ').replace('exp**','exp')
   ...:     

In [7]: d
Out[7]: '"(4*z1*z2*exp(z2**2+z1**2)*sqrt(1037*sqrt(23)*I+1463))+log(1/2)+(sqrt(-I)*z1)/(sqrt(3)*23**(1/4))"'

In [8]: z1,z2 = s.symbols('z1,z2', real=True)

In [9]: ex = s.sympify(d)

In [10]: ex
Out[10]: '(4*z1*z2*exp(z2**2+z1**2)*sqrt(1037*sqrt(23)*I+1463))+log(1/2)+(sqrt(-I)*z1)/(sqrt(3)*23**(1/4))'

In [11]: type(ex)
Out[11]: str

In [12]: ex.subs({z1:0, z2:1})
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-63eab202c7b1> in <module>()
----> 1 ex.subs({z1:0, z2:1})

AttributeError: 'str' object has no attribute 'subs'
nate
  • 269
  • 2
  • 11

1 Answers1

1

numpy.loadtxt() is generally used to read tabular data from text files into Numpy arrays. It may be better to use Python to read your file into a string variable and then convert it to an Sympy expression using sympy.sympify(). Suppose I have a file cal.txt that contains a one-line string z1^2*exp(z2)*%i. The following lines would read it and convert it into a Sympy expression.

import sympy as s

with open('cal.txt', 'r') as myfile:
    d=myfile.read().replace('\n', '').replace('%i',\
                 'I').replace('^','**').replace('"','')


z1,z2 = s.symbols('z1,z2')

ex = s.sympify(d)

ex2 = ex.subs([(z1,0),(z2,0)])

For this to work cal.txt should contain a valid Sympy expression.

Update:

  1. I modified the content of my file cal.txt to "z1^2*exp(z2)*%i" and now I also get the same AttributeError. So we need to strip the " before and after your strings for sympify to work properly. I have modified the code above to replace('"','') double quotes to null. Hope this will work.
  2. In the code above, if we replace the line z1,z2 = s.symbols('z1,z2') with z1,z2 = s.symbols('z1,z2', real=True), the ex2 = ex.subs([(z1,0),(z2,0)]) line will not give desired output because of the assumption we have specified on z1 and z2. So we need to be careful when specifying assumptions on our symbols when using sympify. The source of this information is "Turning strings into expressions" section of this link.
  • sorry, %e is just e, as in %e^3 = e^3; %i is the imaginary number; and I do realize a^b should be a**b for python-y things but then again, I would like to avoid doing hex editing on almost a hundred files... Can't i just do another .replace(' ^', ' **') also? – nate Nov 06 '17 at 20:34
  • I guess I believe you but I can't see that it works because when I try to do something simple, e.g. ex.subs({z1:0, z2:4}), I get back that `'str' object has no attribute 'subs'` - I still think ex is not something I can "work" with yet...? – nate Nov 06 '17 at 20:55
  • I ran the code in Spyder3 and it seems to work for me. `ex` is a Sympy expression object. What version of Sympy and Python are you using? –  Nov 06 '17 at 20:58
  • I am using 2.7.12 and sympy 1.1.1 (python2 because a huge software i use requires it) I added some information to my question when I tried to use python3 and 1.1.1 sympy – nate Nov 06 '17 at 21:09
  • Sorry, I had a meeting to attend. I have copied my output in my answer and it works fine. I am unable to pin-point why we have a difference of results. –  Nov 06 '17 at 22:08
  • Seems like you nailed it! Thanks for sticking it out for this question! – nate Nov 07 '17 at 00:25
  • I like Python :-). Thank you for giving feedback. Well, it almost works. There is a small problem still that the final `subs` does not give the correct answer, if you noticed. –  Nov 07 '17 at 00:36
  • You're right - missed that... But https://mattpap.github.io/scipy-2011-tutorial/html/basics.html gave me the idea of adding "from sympy import *" and "var('z1,z2')", and thereafter subs worked fine. Not sure which, or if both did it. – nate Nov 07 '17 at 00:51
  • 1
    Great! I was trying to find a fix too. I will keep this trick in mind when using `sympify` in the future. I will add it to my answer just in case someone else finds it useful. –  Nov 07 '17 at 01:12