Sympy: How to plot a particular integral function - sympy

I am a newbie in Sympy programming (on python language).
I need to plot on Jupyter this integral-function to check its qualitative study:
F(x) = Integral((ln(1+t**2))/(t*(1+t**2)),(t,0,1/cos(x)))
What is the best way to follow?
I thank you to all for precious help.
A.

The following works well for me:
>>> from sympy.abc import x, t
>>> plot(Integral((ln(1+t**2))/(t*(1+t**2)),(t,0,1/cos(x))))

Related

Does the SymPy function integration_steps reveal the result if the integration?

I'm using the SymPy function integral_steps to build a tool that, just like SymPy Gamma, reveals the integration steps when you ask it to integrate a function. My work-in-progress is available at https://lem.ma/1YH.
What I can't quite figure out is how to obtain the result of applying a particular rule. For example, consider the substitution rule
URule(u_var=_u, u_func=sin(x), constant=1, substep=ExpRule(base=E, exp=_u, context=exp(_u), symbol=_u), context=exp(sin(x))*cos(x), symbol=x)
The context field tells that the function being integrated is exp(sin(x))*cos(x) and that the rule uses a particular substitution - but what's the result of the integration so I can report to the user the same way SymPy Gamma does it. What I currently do is call integrate at every step, but that seems quite inefficient.
Perhaps there's an option that one can pass to integral_steps to make that information available?
SymPy Gamma is open source as SymPy itself. Looking at its module intsteps I see lines like
self.append("So, the result is: {}".format(
self.format_math(_manualintegrate(rule))))
So, the way to obtain the outcome of a rule from the rule is to call _manualintegrate(rule), which needs to be imported as
from sympy.integrals.manualintegrate import _manualintegrate
I imagine reading the rest of intsteps.py will be useful as well.

Using GAMS/CPLEX from Python PYOMO

I noticed that Pyomo 5.3 offers a GAMS solver plugin.
https://github.com/Pyomo/pyomo/blob/master/pyomo/solvers/plugins/solvers/GAMS.py
This is very exciting, as we have a GAMS/CPLEX license where we can use CPLEX as solver, but only via GAMS. With the new Pyomo-Gams interface, it should from my understanding be possible to formulate a problem in Pyomo, and have it translated to GAMS and solved by CPLEX.
However, when I test this with the shell integration, it is very slow (40s for 30 solves of a small MIP versus 6s with glpk/ipopt/cbc). Also, the documentation of the plugin is effectively non-existent.
But maybe someone of you has some experience using that interface and can help me with it
does pyomo actually translate the pyomo model into gams code? If yes, where can I find the gams-file?
how efficient is the translation, and how should I proceed if I want to solve a small model repeatedly?
what is the difference between using the shell or the GAMS Python API?
is there any place to find documentation about this?
Also, it seems that conda provides Pyomo 5.3 only for Linux/Python 3.6 OR for Windows/Python 2.7
https://anaconda.org/conda-forge/pyomo/files?version=5.3, so I had to use pip to install Pyomo 5.3 on my machine.
Thanks in advance, Theo
import pyomo.environ as pe
# set up the model
model = pe.ConcreteModel()
model.MaxWeight = pe.Param(initialize=0,mutable=True)
model.Item = ['hammer','wrench','screwdriver','towel']
Weight = {'hammer':5,'wrench':7,'screwdriver':4,'towel':3}
Value = {'hammer':8,'wrench':3,'screwdriver':6,'towel':11}
model.x = pe.Var(model.Item,within=pe.Binary)
model.z = pe.Objective(expr=sum(Value[i] * model.x[i] for i in model.Item),sense=pe.maximize)
model.constraint = pe.Constraint(expr=sum(Weight[i]*model.x[i] for i in model.Item) <= model.MaxWeight)
# time execution
solver_list = ['cbc', 'ipopt', 'gams', 'glpk']
for i, solver_name in enumerate(solver_list):
solver = pe.SolverFactory(solver_name)
print(solver_name)
tic = time.time()
for MaxWeight_i in range(0,30):
model.MaxWeight = MaxWeight_i
result = solver.solve(model)
soln_items = list()
for i in model.x:
if pe.value(model.x[i]) > 0.5:
soln_items.append(i)
# print("Maximum Weight =", MaxWeight_i, soln_items)
print("{:7.2f} s".format(time.time()-tic))
print(" ")
This is rather delayed, but I can answer a few of your questions.
First, a basic documentation page was just created for the GAMS interface on readthedocs which you can find at: http://pyomo.readthedocs.io/en/latest/library_reference/solvers/gams.html. Note that this location may change as I believe we are restructuring the documentation tree some time soon, but you should be able to search for "gams" to find it again in the future. If there's more documentation that you believe you or others would like to see, please let me know as I'd be happy to provide anything that would be helpful.
As for the difference between the shell interface and the Python API interface, there really isn't any. I thought there would have been a performance increase by using the API but that didn't seem to be the case in the past (and in fact the one model I tried it on saw the shell interface be faster anyway). If you try both and experience otherwise, again I'd be happy to know that too.

Can't get scipy.io.wavfile.read() to work

I am trying to read a .wav file into an array so that I can then plot the array and do a FFT. I got the file open with the wave module and now I am struggling. I was advised to use scipy.io.wavfile.read(filename, mmap=False) but am not having any luck. This function should do exactly what I want it to do but it isn't. I am running Python 2.7 and maybe that is it. Please help me figure out how to make this work. The code I have written is below.
import scipy
import wave
harp=wave.open('/Users/williamweiss2/Desktop/Test 2/harp.wav','r')
frames_harp=harp.getnframes()
harp_rate,harp_data=scipy.io.wavfile.read(harp,mmap=False)
This is the error I am getting when I try to run the program.
---> harp_rate,harp_data=scipy.io.wavfile.read(harp,mmap=False)
AttributeError: 'module' object has no attribute 'io'
Any help would be greatly appreciated. Thanks in advance.
You have confused SciPy's WAV module with Python's. Remove import wave, use import scipy.io.wavfile, and call scipy.io.wavfile.read.
Example:
>>> import scipy.io.wavfile
>>> FSample, samples = scipy.io.wavfile.read('myfile.wav')
SciPy's module does the job of converting from a byte string to numbers for you, unlike Python's module. See the linked docs for more details.

Incompatibility of numpy.sum() with Python 3

In Python 2.7 one could write:
sum(map(int, "123"))
In Python 3 it became:
sum(x for x in map(int, "123"))
Is there a way to make it more concise?
EDIT:
I was running pylab, which imports sum() from numpy changing semantics of the first snippet. IDLE produces the same results for both Python 2.7 and Python 3.3 as DSM noticed.
NOTE:
I would appreciate anyone down voting this question to leave a comment with a short explanation. I edited the title (originally: Is there a way to get back a concise notation for map() in Python 3?) to reflect my better understanding of underlying issue. What else bothers you?
map still exists in Python 3; in fact, your first code snippet should still work.
The more Python3-ish way of doing it would be
sum(int(d) for d in "123")
though as was pointed out, map() still exists.
Oooops, I found an answer in related questions links:
sum(list(map(int, "123")))

Embedding IPython-shell in C/C++-program

I have a C++-program that allows me to run Python-scripts that C++ passes data to. If the Python-script fails the C++-program calls PyRun_InteractiveLoop to allow investigating the problem on an interactive shell. This works alright but I thought "Would be cool if IPython is installed I could use it in that case".
Now my problem is: all I find when looking for "ipython embedding" is instructions how to embed IPython into Python-programs (amongst others http://ipython.org/ipython-doc/dev/interactive/reference.html#embedding-ipython). I tried to reproduce these on the embedded regular python-shell I have but most of them fail in some way (usually because of a missing sys.argv .... that one I can solve).
Any suggestions how to do this? My first plan would have been to first import IPython via the C-API (that one I got covered). If that fails use the "regular" shell. Otherwise call IPython.embed() (or similar) via PyRun_InteractiveOneFlags
Have you considered using python debugger
>>> import pdb
>>> import yourmodule
>>> pdb.run('yourmodule.test()')