-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
108 lines (94 loc) · 2.88 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""The module runs the whole approximation process."""
from __future__ import annotations
from argparse import ArgumentParser
from pathlib import Path
import numpy as np
from approximations.approximation_method import ApproximationMethod
from approximations.chebyshev import chebyshevApproximation
from approximations.haar import haarApproximation
from approximations.legendre import legendreApproximation
from approximations.trigonometric import trigonometricApproximation
from utils import plot
def parse_args() -> tuple[Path, str, int, ApproximationMethod, int]:
"""Function, which parses command line arguments.
Returns:
Tuple[Path, str, int, ApproximationMethod, int]: Output directory path, output file name without extension, number of probes used \
for plots on [a, b] interval, approximation method, order of approximation.
"""
parser = ArgumentParser()
parser.add_argument(
"-o",
"--output-dir",
type=Path,
required=True,
help="Output directory path.",
)
parser.add_argument(
"-f",
"--output-file-name",
type=str,
default="approximation",
help="Output file name without extension.",
)
parser.add_argument(
"-p",
"--probes",
type=int,
default=1000,
help="Number of probes for plot.",
)
parser.add_argument(
"-m",
"--method",
type=str,
required=True,
choices=[method.value for method in ApproximationMethod],
help="Approximation method.",
)
parser.add_argument(
"-n",
"--order",
type=int,
default=4,
help="Order of approximation.",
)
args = parser.parse_args()
args.output_dir.mkdir(exist_ok=True, parents=True)
return (
args.output_dir,
args.output_file_name,
args.probes,
ApproximationMethod(args.method),
args.order,
)
def function(time: float) -> float:
"""Simple real domain real value function.
Args:
time (float): Input float value.
Returns:
float: Function value on time argument.
"""
return 2 + np.exp(time) * (1 + time) * (1 - time) * time * (time - 1.0 / 3.0) * (
time - 4.0 / 5.0
)
approximation_methods = {
ApproximationMethod.CHEBYSHEV: chebyshevApproximation,
ApproximationMethod.HAAR: haarApproximation,
ApproximationMethod.LEGENDRE: legendreApproximation,
ApproximationMethod.TRIGONOMETRIC: trigonometricApproximation,
}
if __name__ == "__main__":
output_dir, output_file_name, probes, method, order = parse_args()
approximationMethod = approximation_methods[method]
approximation = approximationMethod(function, order)
plot(
output_dir,
output_file_name,
probes,
order,
method.value,
function,
approximation,
0.0,
1.0,
)