Environnement d'exploitation
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Je souhaite afficher une image du coefficient généré par le code C ++ avec Jupyter / matplotlib. Pour ce faire, je lirais probablement le résultat C fwrite () en Python.
J'ai essayé de le mettre en œuvre.
C (write)
writeTest.c
#include <stdio.h>
#include <stdlib.h>
int main(void){
FILE *wfp;
float pi = 3.141592;
wfp = fopen("sample.bin","wb");
fwrite(&pi, sizeof(float), 1, wfp);
fclose(wfp);
}
Python (read)
Référence http://stackoverflow.com/questions/2146031/what-is-the-equivalent-of-fread-from-matlab-in-python
readTest.py
import numpy as np
import array
with open("sample.bin") as rfp:
a = array.array("f") # f: typecode for float
a.fromfile(rfp, 1)
print (a)
Le code de type ci-dessus est basé sur ce qui suit. https://docs.python.org/3.1/library/array.html
Génération sample.bin
$ gcc writeTest.c && ./a.out
lire sample.bin
$ python readTest.py
array('f', [3.141592025756836])
Je lis.
C (write)
writeTest.c
#include <stdio.h>
#include <stdlib.h>
int main(void){
FILE *wfp;
float pi = 3.141592;
float napier = 2.718;
float avogadro1 = 6.022;
wfp = fopen("sample.bin","wb");
fwrite(&pi, sizeof(float), 1, wfp);
fwrite(&napier, sizeof(float), 1, wfp);
fwrite(&avogadro1, sizeof(float), 1, wfp);
fclose(wfp);
}
Python (read)
readTest.py
import numpy as np
import array
with open("sample.bin","rb") as rfp:
a = array.array("f") # f: typecode for float
a.fromfile(rfp, 3)
print (a)
Génération sample.bin
$ gcc writeTest.c && ./a.out
lire sample.bin
$ python readTest.py
array('f', [3.141592025756836, 2.7179999351501465, 6.021999835968018])
(Ajout 2016/12/19)
Je trébuchais en lisant le type size_t. v0.1 @ http://qiita.com/7of9/items/68673ac3239532064c28
sizeofPrint.c
#include <stdio.h>
#include <stdlib.h>
int main(void) {
printf("%ld\n", sizeof(size_t));
printf("%ld\n", sizeof(int));
}
Courir
$ gcc sizeofPrint.c && ./a.out
8
4
Dans cet environnement, size_t
vaut 8.
C (write)
writeTest.c
#include <stdio.h>
#include <stdlib.h>
int main(void){
FILE *wfp;
size_t pi = 314;
float napier = 2.718;
float avogadro1 = 6.022;
wfp = fopen("sample.bin","wb");
fwrite(&pi, sizeof(size_t), 1, wfp);
fwrite(&napier, sizeof(float), 1, wfp);
fwrite(&avogadro1, sizeof(float), 1, wfp);
fclose(wfp);
}
Python (read)
Pour lire la valeur de size_t (sizeof () = 8) côté Python, ce serait du type [unsigned long].
https://docs.python.org/3.1/library/array.html Selon «L»
readTest.py
import numpy as np
import array
with open("sample.bin","rb") as rfp:
szv = array.array("L") # L: typecodef for [unsigned long]
szv.fromfile(rfp, 1)
print (szv)
flv = array.array("f") # f: typecode for float
flv.fromfile(rfp, 2)
print (flv)
Courir
$ python readTest.py
array('L', [314L])
array('f', [2.7179999351501465, 6.021999835968018])
(Ajout 2016/12/20)
Comment lire le type double complexe? Je le lis comme un double type pour le moment.
C (write)
Elle est différente de la valeur réelle du nombre Abogadro, mais pour le moment.
writeTest.c
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
int main(void){
FILE *wfp;
size_t pi = 314;
float napier = 2.718;
double complex avogadro = 6.022 + 10.23 * I;
wfp = fopen("sample.bin","wb");
fwrite(&pi, sizeof(size_t), 1, wfp);
fwrite(&napier, sizeof(float), 1, wfp);
fwrite(&avogadro, sizeof(double complex), 1, wfp);
fclose(wfp);
}
Python (read)
readTest.py
import numpy as np
import array
with open("sample.bin","rb") as rfp:
szv = array.array("L") # L: typecodef for [unsigned long]
szv.fromfile(rfp, 1)
print (szv)
napier = array.array("f") # f: typecode for float
napier.fromfile(rfp, 1)
print (napier)
avogadro = array.array("d") # d: typecode for double
avogadro.fromfile(rfp, 2) # real and imaginary part of the complex number
print (avogadro)
Courir
$ gcc writeTest.c && ./a.out
$ python readTest.py
array('L', [314L])
array('f', [2.7179999351501465])
array('d', [6.022, 10.23])
Pour l'instant, j'ai pu lire la partie réelle et la partie imaginaire du double complexe. Est-ce une méthode comme la conversion en un type complexe basé sur les deux valeurs?
Recommended Posts