Commit f3277d98 authored by Thibaut.Lunet's avatar Thibaut.Lunet

TL: work on numpy example dir

parent d548d421
\ No newline at end of file
# Numpy examples
This directory contains several script that show how numpy can be used for particular scientific applications.
## [](
Read and write data (matrix, vector, etc ...) with simple, efficient and beautiful one-line command.
In particular, this script uses the [loadtxt](
and [savetxt]( numpy functions, to read the data stored
in the [data.txt](data.txt) file, modify those and write the modified data into [data_double.txt](data_double.txt).
## [](
Read noisy data that are stored in a .csv file (comma separated values, like Excel files, ...), and build a polynomial that can fit the data.
In particular, it uses the [genfromtxt]( function to read
the .csv file, and the [polyfit]( to fit a polynomial to the noisy data.
\ No newline at end of file
......@@ -11,7 +11,7 @@ import numpy as np
import matplotlib.pyplot as plt
# Get data from the csv file
data = np.genfromtxt('data.csv',dtype=float,delimiter=',',names=True)
data = np.genfromtxt('data.csv', dtype=float, delimiter=',', names=True)
# Getting the fields
times = data['Time']
......@@ -21,12 +21,12 @@ velocities = data['Velocity']
# Interpoling data to suppress measurement error
polyDeg = 25
# getting the coefficient of the best polynomial fit
pressuresPolyCoeff = np.polyfit(times,pressures,polyDeg)
velocitiesPolyCoeff = np.polyfit(times,velocities,polyDeg)
pressuresPolyCoeff = np.polyfit(times, pressures, polyDeg)
velocitiesPolyCoeff = np.polyfit(times, velocities, polyDeg)
# Constructing the polynomial with the coefficients
pressuresPoly = np.poly1d(pressuresPolyCoeff)
velocitiesPoly = np.poly1d(velocitiesPolyCoeff)
# Evaluate the polynomial at each time
pressuresInter = pressuresPoly(times)
......@@ -35,16 +35,16 @@ velocitiesInter = velocitiesPoly(times)
# Plotting all data
plt.plot(times, pressures, 'b', label="Measurements")
plt.plot(times, pressuresInter, 'r', label="Interpolation")
plt.title('Comparison between measured and interpolated fields')
plt.plot(times, velocities, 'b', label="Measurements")
plt.plot(times, velocitiesInter, 'r', label="Interpolation")
\ No newline at end of file
# File containing some different vectors
# X f(X) g(X)
0.5 6 1.1
1.0 5 1.2
1.5 4 1.3
2.0 3 1.4
2.5 2 1.5
\ No newline at end of file
# -*- coding: utf-8 -*-
Function that uses numpy to read and write file into text files
import numpy as np
# Read data
data = np.loadtxt('data.txt', dtype=float)
# Extract specific data
x = data[:, 0] # x is first column
f = data[:, 1] # f is second column
g = data[:, 2] # g is third column
# Print data
print('x = {}'.format(x))
print('f(x) = {}'.format(f))
print('g(x) = {}'.format(g))
# Multiply f and g by two
f2 = 2*f
g2 = 2*g
# Save modified data
dataOut = np.block([[x],
[g2]]).T # Concatenate data column wise
np.savetxt('data_double.txt', dataOut, fmt=('%.2f', '%2d', '%.2e'),
header='X 2f(X) 2g(X)')
......@@ -6,7 +6,7 @@ Also, you can look at an [extended list of Scipy Tutorials](https://docs.scipy.o
## [](
This script performs a regression using non-linear least-square optimization extract a behavior law from noisy data.
In particular, it uses the [minimize]( function of the scipy packages.
In particular, it uses the [minimize]( function of the scipy package.
## [](
......@@ -16,4 +16,5 @@ This script focus on the Jacobi matrix of the advection operator:
f(u) = c_x \frac{\partial u}{\partial x}
It compute it in 1D, 2D, 3D, and extract the eigenvalues for each cases.
\ No newline at end of file
It compute it in 1D, 2D, 3D, and extract the eigenvalues for each cases.
in particular, it uses the [eigvals]( of the scipy package.
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment