Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Plot C arrays with matplotlib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michaël El Kharroubi
Plot C arrays with matplotlib
Commits
a6f9ae3f
Verified
Commit
a6f9ae3f
authored
4 years ago
by
Michaël El Kharroubi
Browse files
Options
Downloads
Patches
Plain Diff
Add 3d example.
parent
36576604
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
C/main.c
+53
-6
53 additions, 6 deletions
C/main.c
C/vector.c
+37
-7
37 additions, 7 deletions
C/vector.c
C/vector.h
+17
-4
17 additions, 4 deletions
C/vector.h
Python/example.py
+44
-12
44 additions, 12 deletions
Python/example.py
with
151 additions
and
29 deletions
C/main.c
+
53
−
6
View file @
a6f9ae3f
...
@@ -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
This diff is collapsed.
Click to expand it.
C/vector.c
+
37
−
7
View file @
a6f9ae3f
...
@@ -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
();
...
...
This diff is collapsed.
Click to expand it.
C/vector.h
+
17
−
4
View file @
a6f9ae3f
...
@@ -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.
*
*
...
...
This diff is collapsed.
Click to expand it.
Python/example.py
+
44
−
12
View file @
a6f9ae3f
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
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment