IPython Notebook Sympy Math Rendering - sympy

I have just started with using IPython Notebook and have been fascinated by its power. I have been using a few examples available on the net to get started with. I was following this tutorial: http://nbviewer.ipython.org/url/finiterank.com/cuadernos/suavesylocas.ipynb but the maths output is not getting rendered as expected. Below is the my code and the output:
In [30]:
%load_ext sympyprinting
%pylab inline
from __future__ import division
import sympy as sym
from sympy import *
init_printing()
x,y,z=symbols("x y z")
k,m,n=symbols("k m n", integer=True)
The sympyprinting extension is already loaded. To reload it, use:
%reload_ext sympyprinting
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.kernel.zmq.pylab.backend_inline].
For more information, type 'help(pylab)'.
In [31]:
t = sin(2*pi*x*(k**2))/ (4*(pi**2)*(k**5)) + (x**2) / (2*k)
t
Out[31]:
2 ⎛ 2 ⎞
x sin⎝2⋅π⋅k ⋅x⎠
─── + ─────────────
2⋅k 2 5
4⋅π ⋅k
I have tried other examples also, and they are also not getting rendered properly. Where am I going wrong?

I had the same problem. Try
from sympy.interactive import printing
printing.init_printing(use_latex=True)
instead of
%load_ext sympyprinting
I am using sympy 0.7.2

I recently had the same problem, and I'm using Linux Crunchbang, which is a derivative of Redhat I think. Originally I installed sympy using
pip install sympy
However, this led to the above problem as described. So then I went to the sympy webpage and cloned the git repository to a folder. Then it can be installed (once in the local folder) by using
python setup.py install
After that everything worked fine, so I think it had something to do with the version used. For the record, the commands I used to initialize the printing in python were
import sympy
sympy.init_printing()

Import:
from sympy import *
init_printing()
Example:
x = symbols('x')
a = Integral(cos(x)*exp(x), x)
Eq(a, a.doit())
Output:

Related

SymPy: name 'symbols' is not defined

This question may be obvious but I have trouble figuring out what's going on. As far as I can see there are no similar problems addressed on the internet.
I'm using Python 3.7 and Spyder (hence the Anaconda bundle). The problem I have is that I'm not able to use SymPy. I've written the following test code which seems to be correct according to documentation and examples:
from sympy import *
x, y, z, t = symbols('x y z t')
expr = x**2+2
d = diff(expr,x)
print(d)
When compiling this I get the message
NameError: name 'symbols' is not defined
What's going on here?
try the following:
import sympy as sp
x, y, z, t = sp.symbols('x, y, z, t')
expr = x**2+2
d = diff(expr,x)
print(d)
The issue MIGHT be, that you called your source file 'sympy.py'.
If you then "import sympy", you actually import your own source file instead of the sympy library. The solution is then to just rename your source file to something else... like "test.py" and try to run it.
The following comment actually gave ne the hint. However, since it's not the original poster, he might have had a different issue.
"Interestingly I have this problem when running the script as a file but not when running the code inside the console line by line. – logicbloke Jul 26 '20 at 1:00"
Your code works fine when I try it.
Have you updated your software recently?
If you're on Ubuntu, try running an update via the command line:
sudo apt-get update
For more information on this, check: https://askubuntu.com/questions/222348/what-does-sudo-apt-get-update-do

TensorFlow scatter_nd function is not working with placeholder and complex input

I am using tf.scatter_nd to update complex values at some index.
It seems the real and imaginary parts are somehow getting added together by this function. My question is how to make it work with placeholders. Here is the minimal working example where the variables b and e should have same values.
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
update=np.asarray([1.+2j])
idx=tf.constant( [[0]])
shp=tf.constant([1])
# works with constants
a=tf.constant(update)
b=tf.scatter_nd(idx,a,shp)
with tf.Session() as sess:
print sess.run(b) # correct output: 1.+2j
#Does not work with placeholders
d=tf.placeholder(tf.complex128)
e=tf.scatter_nd(idx,d,shp)
with tf.Session() as sess:
print sess.run(e,feed_dict={d:update}) # WRONG output: 3.+0j
I am using Anaconda python 2.7 + TensorFlow 1.7 GPU version installed using the conda command.
Edit:
The issue occurs when running the code on GPU. CPU version works correctly.
Here is the updated code to reproduce the issue in TensorFlow-GPU 1.8 installed using Anaconda Python 2.7.
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
update=np.asarray([1.+2j])
idx=tf.constant( [[0]])
shp=tf.constant([1])
a=tf.placeholder(tf.complex128)
with tf.device("/cpu:0"):
b=tf.scatter_nd(idx,a,shp)
with tf.device("/gpu:0"):
c=tf.scatter_nd(idx,a,shp)
with tf.Session() as sess:
print 'Correct output on CPU', sess.run(b,feed_dict={a:update})
print 'Wrong output on GPU',sess.run(c,feed_dict={a:update})
I saw this thread and this thread but could not find how to resolve it. Is there any alternative to tf.scatter_nd that will run on GPU?

Tensorflow- bidirectional_dynamic_rnn: Attempt to reuse RNNCell

The following code (taken from - https://github.com/dennybritz/tf-rnn/blob/master/bidirectional_rnn.ipynb)
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
# Create input data
X = np.random.randn(2, 10, 8)
# The second example is of length 6
X[1,6:] = 0
X_lengths = [10, 6]
cell = tf.contrib.rnn.LSTMCell(num_units=64, state_is_tuple=True)
outputs, states = tf.nn.bidirectional_dynamic_rnn(
cell_fw=cell,
cell_bw=cell,
dtype=tf.float64,
sequence_length=X_lengths,
inputs=X)
output_fw, output_bw = outputs
states_fw, states_bw = states
is giving the following error for
tensorflow - 1.1 for both 2.7 and 3.5
ValueError: Attempt to reuse RNNCell <tensorflow.contrib.rnn.python.ops.core_rnn_cell_impl.LSTMCell object at 0x10ce0c2b0>
with a different variable scope than its first use. First use of cell was with scope
'bidirectional_rnn/fw/lstm_cell', this attempt is with scope 'bidirectional_rnn/bw/lstm_cell'.
Please create a new instance of the cell if you would like it to use a different set of weights.
If before you were using: MultiRNNCell([LSTMCell(...)] * num_layers), change to:
MultiRNNCell([LSTMCell(...) for _ in range(num_layers)]). If before you were using the same cell
instance as both the forward and reverse cell of a bidirectional RNN, simply create two instances
(one for forward, one for reverse). In May 2017, we will start transitioning this cell's behavior to use
existing stored weights, if any, when it is called with scope=None (which can lead to silent model degradation,
so this error will remain until then.)
But it is working in
tensorflow - 1.0.1 for python 3.5 (did not test on python - 2.7)
I tried with multiple code examples I found online but
tf.nn.bidirectional_dynamic_rnn
is giving the same error with tensorflow - 1.1
Is there a bug in tensorflow 1.1 or am i just missing something?
Sorry you ran into this. I can confirm that the error appears in 1.1 (docker run -it gcr.io/tensorflow/tensorflow:1.1.0 python) but not in 1.2 RC0 (docker run -it gcr.io/tensorflow/tensorflow:1.2.0-rc0 python).
So it looks like either 1.2-rc0 or 1.0.1 are your options for the moment.

Subprocess "python file.py" – how to ensure runs in python3?

I'm running the following piece of code:
p = subprocess.getoutput("python ./file.py")
How do I ensure the python version used is python3?
Thanks!
Assuming your controlling script is running in python 3, and thus you are simply wanting to run a subprocess of an identical version of python, then try:
import sys
p = subprocess.getoutput("'{}' ./file.py < input.txt".format(sys.executable))
The obvious way to ensure python 3.x is used is:
p = subprocess.getoutput("python3 ./file.py < input.txt")

Error using matplotlib after updating iPython [duplicate]

This question already has answers here:
IPython Notebook locale error [duplicate]
(4 answers)
Closed 6 years ago.
After updating IPython I constantly have problems with matplotlib. At the beginning of my notebook I have
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import scipy
from qutip import *
import time
Which generate a screen full of issues but the final part is
/Users/murray/anaconda/lib/python2.7/locale.pyc in _parse_localename(localename)
473 elif code == 'C':
474 return None, None
--> 475 raise ValueError, 'unknown locale: %s' % localename
476
477 def _build_localename(localetuple):
ValueError: unknown locale: UTF-8
There were other issues before this which I managed to fix. Similar things have been reported here but no solution which works for me. One solution I found online suggested running
export LANG="it_IT.UTF-8"
in the terminal window (plus about 8 other similar commands). This worked but everytime I restart the notebook I have to reenter all of this. As you might guess I am not an expert - I would assume there is a more permanent fix for this problem
As a work around, you can put export LANG="it_IT.UTF-8" and the "8 other similar commands" into your .profile (assuming your are on Mac OS X).
At the end of this file /Users/murray/.profile write:
# Fix for matplotlib imports in IPython
export LANG="it_IT.UTF-8"
# your other 8 lines here without the # in front
You need to start a new terminal window. In there, start a new IPython session.