-1
私はmathematicaによって生成されたテキストファイルを持っています。主な問題は、ファイルが中かっこ( "{...}")で配列を格納していることです。もう1つの問題は、mathematicaはpythonがjを使うとき、Iを虚数として使うことです。解決策は何ですか?複素数交換のMathematicaで生成されたテキストファイルを読む
私はmathematicaによって生成されたテキストファイルを持っています。主な問題は、ファイルが中かっこ( "{...}")で配列を格納していることです。もう1つの問題は、mathematicaはpythonがjを使うとき、Iを虚数として使うことです。解決策は何ですか?複素数交換のMathematicaで生成されたテキストファイルを読む
例:
のMathematica:
Export["cmplx.csv",RandomComplex[{-1-I,1+I} , {3,3}]
のpython:
import csv
reader=csv.reader(open('cmplx.csv','r'))
complexarray=[complex(item.replace('*I','j'))
for row in reader for item in row]
これは、少量のデータのために本当に適しています。大規模な配列の場合、実数部と虚数部を分割して配列を分割し、ここに示すバイナリ交換を使用してhttps://stackoverflow.com/a/44184067/1004168を生成し、numpyとの複合に再構成します。
のMathematica:
m = Table[RandomComplex[], {3}, {3}]
f = OpenWrite["test.bin", BinaryFormat -> True];
BinaryWrite[f, ArrayDepth[m], "Integer32"];
BinaryWrite[f, Dimensions[m], "Integer32"];
BinaryWrite[f, Re[m], "Real64"];
BinaryWrite[f, Im[m], "Real64"];
Close[f]
のpython:もう一度同じ質問をして
import numpy as np
with open('test.bin','rb') as f:
depth=np.fromfile(f,dtype=np.dtype('int32'),count=1)
dims=np.fromfile(f,dtype=np.dtype('int32'),count=depth)
count=reduce(lambda x,y:x*y,dims)
complexarray=np.reshape(np.fromfile(f,dtype=np.dtype('float64'),
count=count),dims)
complexarray=complexarray+
1j*np.reshape(np.fromfile(f,dtype=np.dtype('float64'),
count=count),dims)
。 ? https://stackoverflow.com/q/44173958/1004168。なぜあなたは扱いがはるかに簡単な12種類の文字の代わりにその秘密の形式を使用するのかを明確に説明する必要があります。 – agentp
@エージェントが言ったこと。しかし、おそらくあなたは 'CForm'といくつかのサポート定義、例えば' def List(* x):return list(x) 'であなたが望むものを得ることができます。 – Alan
[Mathematica import from Python]のデータが複製される可能性があります(https://stackoverflow.com/questions/44173958/data-from-mathematica-import-to-python) –