Skip to content
Snippets Groups Projects
Commit b1c0a419 authored by Nando Farchmin's avatar Nando Farchmin
Browse files

Add basic package setup

parent 2badfa8d
No related branches found
No related tags found
No related merge requests found
import numpy as np
import matplotlib.pyplot as plt
import torch
from src.misc import time_stamp, timeit
from src import target_function
def main() -> None:
print(time_stamp(), "Initialize main file")
with timeit("create x_train data ({:4.2f} s)"):
x_train = np.random.uniform(0, 1, (100000, 2))
with timeit("create y_train data ({:4.2f} s)"):
y_train = target_function.sin2d(x_train)
plt.figure()
plt.hexbin(x_train[:, 0], x_train[:, 1], y_train, gridsize=50)
plt.show()
if __name__ == "__main__":
main()
from typing import List, Union
import time
from contextlib import contextmanager
import numpy as np
def time_stamp():
"""Time stamp for logging."""
return time.strftime("[%Y-%m-%d %H:%M:%S]", time.localtime())
@contextmanager
def timeit(title: str, verbose: bool = True) -> None:
"""Time functions and print time."""
start = time.perf_counter()
try:
yield
finally:
if verbose:
print(time_stamp(), title.format(time.perf_counter()-start))
def cart_prod(array_list: Union[List[np.ndarray], np.ndarray]) -> np.ndarray:
"""Compute the outer product of two or more arrays.
Assemble an array containing all possible combinations of the elements
of the input vectors :math:`v_1,\\dots,v_n`.
Parameters
----------
array_list : list of array_like
List of vectors :math:`v_1,\\dots,v_n`.
Returns
-------
:
Cartesian product array.
"""
dim = len(array_list)
if dim == 1:
return np.array(array_list).T
x = np.hstack((np.meshgrid(*array_list))).swapaxes(0, 1).reshape(dim, -1).T
return x
import numpy as np
def sin2d(xs: np.ndarray) -> np.ndarray:
"""Benchmark function for approximation.
The benchmark function is given by
.. math::
f(x_1, x_2) = \\sin(4\\pi x_1) \\sin(3\\pi x_2)
Parameters
----------
xs : np.ndarray
Evaluation points. Shape is (n, 2).
Returns
-------
:
Function evaluations.
"""
assert xs.ndim == 2 and xs.shape[1] == 2
return np.sin(4*np.pi*xs[:, 0]) * np.sin(3*np.pi*xs[:, 1])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment