3

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]])
m_goldberg
  • 107,779
  • 16
  • 103
  • 257

2 Answers2

2

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])
Jason B.
  • 68,381
  • 3
  • 139
  • 286
2

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.    ]])