how to have sympy output latex on my local Env? - python

this command output latex in online Env
solve(x**2 - 33*x + 20*13-14**2, x)
same command outputs a plaintext on my local Env.
a simpler one could be rendered successfully.
did I missing something?

You need to initiate session first by calling init_session()
If you plan to work in an interactive calculator-type session, the init_session() function will automatically import everything in SymPy, create some common Symbols, setup plotting, and run init_printing()
from sympy import init_session
init_session()
x = 5
solve(x**2 - 33*x + 20*13 - 14**2, x)

Related

Scipy solve_ivp crashes script and debugger without error message

I am trying to solve a differential equation using scipy.integrate.solve_ivp using the "rk45" method. However, directly after the first actual call to my system function f, the script crashes without any exception message or similar, it just stops. The same thing happens when I try to run it using the debugger as python3 -m pdb ./solve.py. I also tried using the trace module as described here. However, that gives me too much information and I don't really see where exactly the error appears. The error strictly appears directly after the system function is called, somewhere in the scipy module.
I currently have not constructed a minimal example to reproduce this, I might add that later. For now, I am wondering if there are further ways I could try to debug this problem. The error might occur somewhere outside of the actual python code.
When I try running it in Juypter, the same error message as shown in this question appears.
Here is the example:
import numpy
import scipy.integrate as integrate
N = 300
def f(t, x):
return numpy.ravel(numpy.ones((2, N, N, N), dtype=complex))
ival = numpy.ravel(numpy.ones((2, N, N, N), dtype=complex))
integrate.solve_ivp(f, (0, 100), ival)

Python & Inkscape: SVG-to-EMF conversion breaks down?

I am trying to automatically convert an SVG graphic to EMF with Python, using Inkscape on the command line. My code is
from subprocess import call
import matplotlib.pyplot as plt
from numpy import linspace, sin
x = linspace(0,10,10)
y = sin(x)
plt.plot(x,y)
plt.savefig("source.svg")
for k in range(0,5):
call(["C:\Program Files\Inkscape\inkscape.exe", "--file", "source.svg", "--export-emf", "result" + str(k) + ".emf" ])
# this usually breaks down at k = 1 or 2
The "call" command works fine when called once. If I call it multiple times, e.g. on consecutive lines as shown above, Inkscape breaks down and I have to restart my python kernel and kill Inkscape via the Windows Task-Manager.
Any ideas why that might be?
Thanks!

how to disable init_printing in sympy + IPython

ok, I know how to use init_printing to get sympy to automatically render IPython output using MathJax.
from sympy import init_printing
init_printing()
How do I get it to stop? (Yeah, I could reset my notebook but I'd like to turn it on just for a few cells and turn it off again.)
found it (had to UTSL):
sympy.init_printing(pretty_print=False)

Python Spyder Display Symbolic Math

In Spyder 2 (Anaconda distribution) and in the IPython QT Console I'm able to print results of symbolic calculations (from an answer I got for a previous post) but I can't get equations in strings to print with the a IPython's Rich Display System:
from sympy import *
from IPython.display import display, Math
init_printing(use_unicode=False, wrap_line=False, no_global=True)
x, y, z = symbols('x y z')
#----- prints correctly
ii = integrate(x**2 + x + 1, x)
display(ii)
#----- does not print
Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')
The above prints the results of the integrate correctly but the Math() does not print (no error -- just skips it). Note it all works in SciPy web notebook.
Thank you!
The Math class doesn't generate the rendered image from your Latex, that's why it doesn't work directly.
To get what you want you need to use this code
from IPython.display import Image, display
from IPython.lib.latextools import latex_to_png
eq = r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx'
data = latex_to_png(eq, wrap=True)
display(Image(data=data))
Then you will see the right image
Hello guys im still having the problem, whe i use
from IPython.display import display, Math, Latex
display((Math(r'P(Purchase|Male)= \frac{Numero\ total\ de\ compras\ hechas\ por\ hombres\}{Total\ de\ hombres\ en\ el\ grupo\} = \frac{Purchase\cap Male}{Male}')))
on jupyter it works fine, but when i do the exact same code on spyder it doesnt work
im using python 3.6 , spyder 3.3.3
also i tried the marked asnwer but latex_to_png makes a NoneType Object on spyder

Python SymPy solving linear system

I have Python 3.3.3 and SymPy 0.7.4.1, both in x64 version and installed locally on my computer. I am using PSPad as a configured editor for Python scripting.
When using imports from library sympy in a module which should solve a set of three linear equations:
from sympy import Matrix, solve_linear_system
from sympy.abc import x, y, z
def main():
system = Matrix (((3,2,-1,1) ,(2,-2,4,-2),(-1,0.5,-1,0)))
print(solve_linear_system(system, x, y,z))
if __name__ == "__main__":
main()
The editor PSPad console output returns the following:
traceback (most recent call last): File "C:\Users\GOODLU~1\AppData\Local\Temp\PSpad\securesafety_DISK_5GB\Programmation\linear system solve SYMPY.py", line 1, in <module>
from sympy import Matrix,solve_linear_system File "C:\Users\GOODLU~1\AppData\Local\Temp\PSpad\securesafety_DISK_5GB\Programmation\sympy.py", line 2, in <module>
from sympy import var,Eq,solve ImportError: cannot import name var
Process completed, Exit Code 1.
Execution time: 00:00.134
Actually, I am wondering myself heavily about those issues:
Why, when typing the same thing, without an object def main(), and entered line by line in IDLE, everything is solved correctly, as: {x: 1.00000000000000, y: -2.00000000000000, z: -2.00000000000000}
Why, my PSPad file with object, having the same computation lines, doesn't work and returns errors?
In fact, I would like to use SymPy in normal python code and get computed results in a list or printed in console( .. as in IDLE). Just in order to avoid some annoying line-to-line IDLE manipulations, what should my code file look like?
The problem seems to be that you have created a file named sympy.py, which has the same name as the sympy module.
Hence, in the from sympy import ... statement, your sympy.py is acting as the sympy module.
Try renaming the file to something else, like sympy_programming_test.py and let know if it works.

Categories