3
$\begingroup$

Is there a way to convert an array in Mathematica such that its output format resembles that of Python's numpy arrays? I want to go from:

{{0.1875, -0.0625, 0.0625, -0.0625, -0.1875, 0.0625, -0.0625, 0.0625, 
  0, 0, -0.125, 0.0625, 0, 0}, 
 {-0.0625, 0.1875, 0.0625, -0.0625, 0.0625, -0.1875, -0.0625, 0.0625, 
  0, 0, 0, -0.0625, 0, 0}}

to something that looks like this:

np.array([
  [0.1875, -0.0625, 0.0625, -0.0625, -0.1875, 0.0625, -0.0625, 0.0625, 
   0, 0, -0.125, 0.0625, 0, 0], 
  [-0.0625, 0.1875, 0.0625, -0.0625, 0.0625, -0.1875, -0.0625, 0.0625, 
   0, 0, 0, -0.0625, 0, 0]])
$\endgroup$
2
$\begingroup$

Here is a quick and dirty method to do it,

input = {{0.1875, -0.0625, 0.0625, -0.0625, -0.1875, 
   0.0625, -0.0625, 0.0625, 0, 0, -0.125, 0.0625, 0, 0}, {-0.0625, 
   0.1875, 0.0625, -0.0625, 0.0625, -0.1875, -0.0625, 0.0625, 0, 0, 
   0, -0.0625, 0, 0}}
(* {{0.1875, -0.0625, 0.0625, -0.0625, -0.1875, 0.0625, -0.0625,
   0.0625, 0, 0, -0.125, 0.0625, 0, 0}, {-0.0625, 0.1875, 
  0.0625, -0.0625, 0.0625, -0.1875, -0.0625, 0.0625, 0, 0, 0, -0.0625,
   0, 0}} *)

First I convert the list to a string, then I use StringReplace and copy the result to the clipboard to paste wherever I need it,

CopyToClipboard@
 StringReplace[
  ToString@input, {"{{" -> "np.array([[", "}, {" -> "], [", 
   "}}" -> "]])"}]

The output has no line breaks,

np.array([[0.1875, -0.0625, 0.0625, -0.0625, -0.1875, 0.0625, -0.0625, 0.0625, 0, 0, -0.125, 0.0625, 0, 0], [-0.0625, 0.1875, 0.0625, -0.0625, 0.0625, -0.1875, -0.0625, 0.0625, 0, 0, 0, -0.0625, 0, 0]])

A more polished, functional form, which can deal with differently shaped arrays, and with large and small numbers, is

toNumpy = CopyToClipboard[
    "np.array(" <>
     StringReplace[
      ToString[
       Map[FortranForm, #, {-1}]]
      , {"{" -> "[", "}" -> "]"}] <> ")"
    ] &;

Some test cases,

toNumpy@RandomReal[10^13, {2, 2, 2}]

np.array([[[6.515662614124779e12, 5.006958562518258e12], [9.038456654284738e12, 6.155528971184453e12]], [[8.352734900856066e12, 3.183407977590039e11], [9.3300341220833e12, 4.83956443633259e12]]])

and

toNumpy@RandomReal[10^-13, {5}]

np.array([2.5981516628601564e-14, 8.152269536832112e-14, 5.284716124977113e-16, 5.3549719092414376e-14, 2.877505407608681e-14])
$\endgroup$
  • $\begingroup$ I try to run the toNumpy versions and they don't give any output on my computer. $\endgroup$ – Alejandro Marcos Aragon Dec 3 '15 at 14:49
  • $\begingroup$ Sorry, I assumed you wanted to paste it somewhere to compare with python, or paste into your python code directly, so I put CopyToClipboard in the function. Just hit paste and it will come out. Just erase that part if you'd rather have it in the notebook $\endgroup$ – Jason B. Dec 3 '15 at 14:59
  • $\begingroup$ @AlejandroMarcosAragon, there is no output! $\endgroup$ – BetterEnglish Feb 12 '16 at 19:52
  • $\begingroup$ @Developer2000 Open a text editor and paste the result. :-) $\endgroup$ – Jason B. Feb 12 '16 at 19:54
  • $\begingroup$ @JasonB, I did not understand what did you say!? $\endgroup$ – BetterEnglish Feb 12 '16 at 22:10
2
$\begingroup$

I've prepared a Module to handle such conversions (original answer https://mathematica.stackexchange.com/a/144200/48312). It would work like this for Alejandro's question:

ToPython[{{0.1875, -0.0625, 0.0625, -0.0625, -0.1875, 0.0625, -0.0625,
0.0625, 0, 0, -0.125, 0.0625, 0, 0}, {-0.0625, 0.1875, 
0.0625, -0.0625, 0.0625, -0.1875, -0.0625, 0.0625, 0, 0, 
0, -0.0625, 0, 0}}]

The output in Mathematica would be a string (that is directly copied to clipboard)

numpy.array( [numpy.array( [0.1875e0,(-0.625e-1),0.625e-1,(-0.625e-1),         (-0.1875e0),0.625e-1,(-0.625e-1),0.625e-1,0,0,(-0.125e0),0.625e-1,0,0,]     ),numpy.array( [(-0.625e-1),0.1875e0,0.625e-1,(-0.625e-1),0.625e-1,(-0.1875e0),    (-0.625e-1),0.625e-1,0,0,0,(-0.625e-1),0,0,] ),] )

Once copied and interpret by Numpy...

array([[ 0.1875, -0.0625,  0.0625, -0.0625, -0.1875,  0.0625, -0.0625,
     0.0625,  0.    ,  0.    , -0.125 ,  0.0625,  0.    ,  0.    ],
   [-0.0625,  0.1875,  0.0625, -0.0625,  0.0625, -0.1875, -0.0625,
     0.0625,  0.    ,  0.    ,  0.    , -0.0625,  0.    ,  0.    ]])
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.