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

Add 3d example.

parent 36576604
Branches
No related tags found
No related merge requests found
...@@ -10,23 +10,70 @@ ...@@ -10,23 +10,70 @@
* @param x A double variable. * @param x A double variable.
* @return f(x) = 2.0*sin(x) - 3.0*cos(x) * @return f(x) = 2.0*sin(x) - 3.0*cos(x)
*/ */
double my_function(double x) double my_1d_function(double x)
{ {
return 2.0 * sin(x) - 3.0 * cos(x); return 2.0 * sin(x) - 3.0 * cos(x);
} }
int main() /** @brief
* An exemple of mathematical function
* in 2 dimension.
*
* @param x The first argument.
* @param y The second argument.
* @return f(x,y) = x^2 + y^2
*/
double my_2d_function(double x, double y)
{
return x * x + y * y;
}
/** @brief
* Generate an example of 1D data,
* wich is stored into two vec files.
*/
void generate_1D_data()
{ {
// Create a vector X = [0,1,2...99] // Create a vector X = [0,1,2...99]
double_vector_t *X = iota(100); double_vector_t *X = iota(100);
// Create a vector Y = my_function(x) // Create a vector Y = my_1d_function(x)
double_vector_t *Y = apply_function(X, my_function); double_vector_t *Y = apply_1d_function(X, my_1d_function);
// Export our vectors into files // Export our vectors into files
export_vector("../X.vec", X); export_vector("../X_1D.vec", X);
export_vector("../Y.vec", Y); export_vector("../Y_1D.vec", Y);
// Free our vectors // Free our vectors
destroy_vector(&Y); destroy_vector(&Y);
destroy_vector(&X); destroy_vector(&X);
} }
/** @brief
* Generate an example of 2D data,
* wich is stored into three vec files.
*/
void generate_2D_data()
{
// Create a vector X = [0,1,2...99]
double_vector_t *X = iota(100);
// Create a vector Y = [0,1,2...99]
double_vector_t *Y = iota(100);
// Create a vector Z = my_2d_function(X, Y)
double_vector_t *Z = apply_2d_function(X, Y, my_2d_function);
// Export our vectors into files
export_vector("../X_2D.vec", X);
export_vector("../Y_2D.vec", Y);
export_vector("../Z_2D.vec", Z);
// Free our vectors
destroy_vector(&Z);
destroy_vector(&Y);
destroy_vector(&X);
}
int main()
{
generate_1D_data();
generate_2D_data();
}
\ No newline at end of file
...@@ -56,18 +56,43 @@ double_vector_t *iota(uint32_t N) ...@@ -56,18 +56,43 @@ double_vector_t *iota(uint32_t N)
* to a given vector, and return the * to a given vector, and return the
* result in a new vector. * result in a new vector.
* *
* @param vec The argument vector * @param X The argument vector
* @param f The 1d function to apply * @param f The 1d function to apply
* @return A dynamically allocated vector : f(X) * @return A dynamically allocated vector : f(X)
*/ */
double_vector_t *apply_function(double_vector_t *vec, double_function_t f) double_vector_t *apply_1d_function(double_vector_t *X, double_1d_function_t f)
{ {
double_vector_t *res = init_vector(vec->N); double_vector_t *Y = init_vector(X->N);
for (uint32_t i = 0; i < vec->N; i++) for (uint32_t i = 0; i < X->N; i++)
{ {
res->components[i] = f(vec->components[i]); Y->components[i] = f(X->components[i]);
} }
return res; return Y;
}
/** @brief
* Apply a 2d function element-wise
* to given vectors, and return the
* result in a new vector.
*
* @param X The first argument vector
* @param Y The second argument vector
* @param f The 2d function to apply
* @return A dynamically allocated vector : f(X,Y)
*/
double_vector_t *apply_2d_function(double_vector_t *X, double_vector_t *Y, double_2d_function_t f)
{
if (X->N != Y->N)
{
fprintf(stderr, "Bad shape len(X) = %d and len(Y) = %d\n", X->N, Y->N);
return NULL;
}
double_vector_t *Z = init_vector(X->N);
for (uint32_t i = 0; i < X->N; i++)
{
Z->components[i] = f(X->components[i], Y->components[i]);
}
return Z;
} }
/** @brief /** @brief
* Export a vector into a file. * Export a vector into a file.
...@@ -78,6 +103,11 @@ double_vector_t *apply_function(double_vector_t *vec, double_function_t f) ...@@ -78,6 +103,11 @@ double_vector_t *apply_function(double_vector_t *vec, double_function_t f)
void export_vector(const char *filename, double_vector_t *vec) void export_vector(const char *filename, double_vector_t *vec)
{ {
FILE *output = fopen(filename, "w"); FILE *output = fopen(filename, "w");
if (output == NULL)
{
fprintf(stderr, "Can't open output file");
exit(EXIT_FAILURE);
}
vector_metadata_t metadata; vector_metadata_t metadata;
metadata.endianness = get_endianness(); metadata.endianness = get_endianness();
......
...@@ -6,7 +6,9 @@ typedef struct double_vector ...@@ -6,7 +6,9 @@ typedef struct double_vector
double *components; double *components;
} double_vector_t; } double_vector_t;
// Function pointer, example : double f(double x); // Function pointer, example : double f(double x);
typedef double (*double_function_t)(double); typedef double (*double_1d_function_t)(double);
// Function pointer, example : double f(double x, double y);
typedef double (*double_2d_function_t)(double, double);
/* /*
* The attribute "packed" tells the compiler, * The attribute "packed" tells the compiler,
...@@ -42,11 +44,22 @@ double_vector_t *iota(uint32_t N); ...@@ -42,11 +44,22 @@ double_vector_t *iota(uint32_t N);
* to a given vector, and return the * to a given vector, and return the
* result in a new vector. * result in a new vector.
* *
* @param vec The argument vector * @param X The argument vector
* @param f The 1d function to apply * @param f The 1d function to apply
* @return A dynamically allocated vector : f(X) * @return A dynamically allocated vector : f(X)
*/ */
double_vector_t *apply_function(double_vector_t *vec, double_function_t f); double_vector_t *apply_1d_function(double_vector_t *X, double_1d_function_t f);
/** @brief
* Apply a 2d function element-wise
* to given vectors, and return the
* result in a new vector.
*
* @param X The first argument vector
* @param Y The second argument vector
* @param f The 2d function to apply
* @return A dynamically allocated vector : f(X,Y)
*/
double_vector_t *apply_2d_function(double_vector_t *X, double_vector_t *Y, double_2d_function_t f);
/** @brief /** @brief
* Export a vector into a file. * Export a vector into a file.
* *
......
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
from load_vec import load_vector from load_vec import load_vector
import numpy as np
X = load_vector("../X.vec")
Y = load_vector("../Y.vec")
type_of_data = 'curve' TYPE_OF_DATA = 'curve'
if type_of_data == 'curve':
plt.plot(X, Y, label="my curve") def show_1d():
X = load_vector("../X_1D.vec")
Y = load_vector("../Y_1D.vec")
if TYPE_OF_DATA == 'curve':
plt.plot(X, Y, c='g', label="my curve")
else: else:
plt.scatter(X, Y, marker='x', label="my points") plt.scatter(X, Y, c='r', marker='x', label="my points")
plt.title("My data") plt.title("My data")
plt.xlabel("X") plt.xlabel("X")
...@@ -17,3 +23,29 @@ plt.ylabel("Y") ...@@ -17,3 +23,29 @@ plt.ylabel("Y")
plt.legend(loc="upper right") plt.legend(loc="upper right")
plt.show() plt.show()
def show_2d():
fig = plt.figure()
ax = fig.gca(projection='3d')
X = load_vector("../X_2D.vec")
Y = load_vector("../Y_2D.vec")
Z = load_vector("../Z_2D.vec")
if TYPE_OF_DATA == 'curve':
ax.plot(X, Y, Z, c='r', label="my curve")
else:
ax.scatter(X, Y, Z, c='r', marker='x', label="my points")
ax.set_title("My data")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
ax.legend(loc="upper right")
plt.show()
show_1d()
show_2d()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment