|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +import numpy as np |
| 6 | +import numpy.typing as npt |
| 7 | + |
| 8 | + |
| 9 | +def generate_input(a: float | np.float32 | np.float64, |
| 10 | + b: float | np.float32 | np.float64, |
| 11 | + n: int | np.int32 | np.int64) -> npt.NDArray[np.float64]: |
| 12 | + '''Generate x-values in given range |
| 13 | +
|
| 14 | + Parameters |
| 15 | + ---------- |
| 16 | + a: float |
| 17 | + lower bound |
| 18 | + b: float |
| 19 | + upper bound |
| 20 | + n: int |
| 21 | + number of points to generate |
| 22 | +
|
| 23 | + Returns |
| 24 | + ------- |
| 25 | + np.NDarray |
| 26 | + generated x-values |
| 27 | + ''' |
| 28 | + return np.linspace(a, b, n) |
| 29 | + |
| 30 | + |
| 31 | +def gaussian(x: npt.NDArray[np.float64], mu: float, sigma: float) -> npt.NDArray[np.float64]: |
| 32 | + y: npt.NDArray[np.float64] = np.exp(-0.5*(x - mu)**2/sigma)/np.sqrt(2.0*np.pi*sigma) |
| 33 | + return y |
| 34 | + |
| 35 | + |
| 36 | +def plot_function(x: npt.NDArray[np.float64], y: npt.NDArray[np.float64]) -> None: |
| 37 | + plt.plot(x, y) |
| 38 | + plt.show() |
| 39 | + return |
| 40 | + |
| 41 | + |
| 42 | +if __name__ == '__main__': |
| 43 | + arg_parser = argparse.ArgumentParser(description='numpy type checking') |
| 44 | + arg_parser.add_argument('--mu', type=float, default=0.0, |
| 45 | + help='mean value') |
| 46 | + arg_parser.add_argument('--sigma', type=float, default=1.0, |
| 47 | + help='standard deviation') |
| 48 | + arg_parser.add_argument('--n', type=int, default=10, |
| 49 | + help='number of points') |
| 50 | + arg_parser.add_argument('--plot', action='set_true', |
| 51 | + help='show plot') |
| 52 | + options = arg_parser.parse_args() |
| 53 | + x = generate_input(-3.0, 3.0, options.n) |
| 54 | + y = gaussian(x, options.mu, options.sigma) |
| 55 | + if options.plot: |
| 56 | + plot_function(x, y) |
| 57 | + else: |
| 58 | + print(y) |
0 commit comments