Skip to content
Snippets Groups Projects
Verified Commit dabad862 authored by Michaël El Kharroubi's avatar Michaël El Kharroubi :satellite:
Browse files

First commit example of C export and matplolib plot

parents
No related branches found
No related tags found
No related merge requests found
*.vec
.vscode
__pycache__
example
*.o
\ No newline at end of file
C/main.c 0 → 100644
#include <stdio.h>
#include <math.h>
#include "vector.h"
double my_function(double x)
{
// 2.0*sin(x) - 3.0*cos(x)
return 2.0 * sin(x) - 3.0 * cos(x);
}
int main()
{
double_vector_t *X = iota(100);
double_vector_t *Y = apply_function(X, my_function);
export_vector("../X.vec", X);
export_vector("../Y.vec", Y);
destroy_vector(&Y);
destroy_vector(&X);
}
\ No newline at end of file
CC=gcc -std=gnu11 -Wall -Wextra -g -fsanitize=leak -fsanitize=undefined
LIBS=-lm
example: vector.o vector.h main.c
$(CC) $^ $(LIBS) -o $@
vector.o: vector.c vector.h
$(CC) -c $< -o $@
clean:
rm -f *.vec *.o example
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include "vector.h"
double_vector_t *init_vector(uint_t N)
{
double_vector_t *vec = malloc(sizeof(double_vector_t));
vec->components = malloc(N * sizeof(double));
vec->N = N;
if (vec == NULL)
{
perror("Can't allocate memory");
exit(EXIT_FAILURE);
}
return vec;
}
double_vector_t *iota(uint_t N)
{
double_vector_t *vec = init_vector(N);
for (uint_t i = 0; i < N; i++)
{
vec->components[i] = i;
}
return vec;
}
double_vector_t *apply_function(double_vector_t *vec, double_function_t f)
{
double_vector_t *res = init_vector(vec->N);
for (uint_t i = 0; i < vec->N; i++)
{
res->components[i] = f(vec->components[i]);
}
return res;
}
void fill_vector(double_vector_t *vec, double value)
{
for (uint_t i = 0; i < vec->N; i++)
{
vec->components[i] = value;
}
}
void export_vector(const char *filename, double_vector_t *vec)
{
FILE *output = fopen(filename, "w");
uint_t endianness = 0x01020304;
vector_metadata_t metadata;
// Return the endianness by accessing the first byte in memory
// which should be 1 if big-endian and 4 if little-endian
metadata.endianness = *((byte_t *)(&endianness)) == 4;
metadata.size_of_a_component = sizeof(double);
metadata.number_of_component = vec->N;
fwrite(&metadata, sizeof(vector_metadata_t), 1, output);
fwrite(vec->components,
metadata.size_of_a_component,
metadata.number_of_component,
output);
fclose(output);
}
void destroy_vector(double_vector_t **vec)
{
free((*vec)->components);
free(*vec);
*vec = NULL;
}
\ No newline at end of file
typedef unsigned int uint_t;
typedef unsigned char byte_t;
typedef struct double_vector
{
uint_t N;
double *components;
} double_vector_t;
typedef double (*double_function_t)(double);
typedef struct vector_metadata
{
byte_t endianness; // 1 = little, 0 = big
byte_t size_of_a_component; // in bytes
uint_t number_of_component;
} __attribute__((packed)) vector_metadata_t;
double_vector_t *init_vector(uint_t N);
double_vector_t *iota(uint_t N);
double_vector_t *apply_function(double_vector_t *vec, double_function_t f);
void fill_vector(double_vector_t *vec, double value);
void export_vector(const char *file, double_vector_t *vec);
void destroy_vector(double_vector_t **vec);
\ No newline at end of file
from matplotlib import pyplot as plt
from load_vec import load_vector
X = load_vector("../X.vec")
Y = load_vector("../Y.vec")
type_of_data = 'curve'
if type_of_data == 'curve':
plt.plot(X, Y, label="my curve")
else:
plt.scatter(X, Y, marker='x', label="my points")
plt.title("My data")
plt.xlabel("X")
plt.ylabel("Y")
plt.legend(loc="upper right")
plt.show()
import numpy as np
def load_vector(filename: str) -> np.ndarray:
file = open(filename, 'rb')
header = file.read(6)
little_endian = bool(header[0])
size_of_components = int(header[1])
nb_components = int.from_bytes(
header[2:], 'little' if little_endian else 'big')
datatype = np.float64 if size_of_components == 8 else np.float
array = np.fromfile(file, dtype=datatype, count=nb_components)
file.close()
return array
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment