I rewrote the question using a minimal example. My previous post didn't include the code required to import NumPy and other libraries.
I'm trying to export the python output to a LaTeX export block. The issue is that the output contains colons. Previously suggested fixes cause the output to be encapsulated by an example block (which breaks the latex document).
My code:
#+begin_src jupyter-python :session PY :exports results :wrap export latex
import numpy as np
from sympy import *
init_printing()
np.random.seed(376134299) # set random seedV
#Format C style matrix into numpy array for pretty printing
def format_mat(mat,size):
in_mat = mat.copy()
return np.reshape(in_mat,(-1,size))
def upper_echelon(mat,size):
in_mat = mat.copy()
for i in range(size-1):
for j in range(size):
if j>i:
ratio = in_mat[j*size+i]/in_mat[i*size+i]
for k in range(i,size):
in_mat[j*size+k] -= (ratio*in_mat[i*size+k])
return in_mat
N = 4 # size for NxN matrix
mat = np.random.random((N*N)) # init NxN matrix
upper_mat = upper_echelon(mat.copy(),N) # upper echelon form matrix
# print input matrix
print("$"+latex(Matrix(format_mat(mat.copy(),N)))+"$")
# print upper echelon form matrix
print("$"+latex(Matrix(format_mat(upper_mat.copy(),N)))+"$")
#+end_src
The Results:
#+RESULTS:
#+begin_export latex
: $\left[\begin{matrix}0.435551782541394 & 0.653831083538496 & 0.703284715123126 & 0.779211763281097\\0.745624842540662 & 0.382867491810448 & 0.0640634444192878 & 0.205350088572431\\0.455263877780481 & 0.782880235084324 & 0.190229654979851 & 0.731404270938654\\0.13207468759024 & 0.0418823433240818 & 0.617596076042993 & 0.648004941561776\end{matrix}\right]$
: $\left[\begin{matrix}0.435551782541394 & 0.653831083538496 & 0.703284715123126 & 0.779211763281097\\0 & -0.736431563440805 & -1.13989570807275 & -1.12858923067965\\0 & -1.38777878078145 \cdot 10^{-17} & -0.698831926546978 & -0.235493658356284\\0 & 0 & 0 & 0.433555029735554\end{matrix}\right]$
#+end_export
The desired output:
#+RESULTS:
#+begin_export latex
$\left[\begin{matrix}0.435551782541394 & 0.653831083538496 & 0.703284715123126 & 0.779211763281097\\0.745624842540662 & 0.382867491810448 & 0.0640634444192878 & 0.205350088572431\\0.455263877780481 & 0.782880235084324 & 0.190229654979851 & 0.731404270938654\\0.13207468759024 & 0.0418823433240818 & 0.617596076042993 & 0.648004941561776\end{matrix}\right]$
$\left[\begin{matrix}0.435551782541394 & 0.653831083538496 & 0.703284715123126 & 0.779211763281097\\0 & -0.736431563440805 & -1.13989570807275 & -1.12858923067965\\0 & -1.38777878078145 \cdot 10^{-17} & -0.698831926546978 & -0.235493658356284\\0 & 0 & 0 & 0.433555029735554\end{matrix}\right]$
#+end_export
np
module? You must haveimport numpy as np
somewhere, so that seems to be missing from your question, which makes the code block barf with errors when evaluated. Please make sure that your example is complete. I should be able to cut and paste it into a file and doC-c C-c
on it to evaluate it without errors (possibly after installing some things, but you have to tell us that too).NameError: name 'latex' is not defined
from this line:print("$"+latex(Matrix(format_mat(mat.copy(),N)))+"$")
. If I need to install something else to make this work, you will have to tell me.