Skip to content
Snippets Groups Projects
Commit 2ed0814e authored by Wenzel, Tizian's avatar Wenzel, Tizian
Browse files

Initial commit.

parent 4e906c04
No related branches found
No related tags found
No related merge requests found
# Added Tiz
tb_logs/
*.npy
# Created by .ignore support plugin (hsz.mobi)
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# IPython Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# dotenv
.env
# virtualenv
venv/
ENV/
# Spyder project settings
.spyderproject
# Rope project settings
.ropeproject
### VirtualEnv template
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
.Python
[Bb]in
[Ii]nclude
[Ll]ib
[Ll]ib64
[Ll]ocal
[Ss]cripts
pyvenv.cfg
.venv
pip-selfcheck.json
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff:
.idea/workspace.xml
.idea/tasks.xml
.idea/dictionaries
.idea/vcs.xml
.idea/jsLibraryMappings.xml
# Sensitive or high-churn files:
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml
# Gradle:
.idea/gradle.xml
.idea/libraries
# Mongo Explorer plugin:
.idea/mongoSettings.xml
.idea/
## File-based project format:
*.iws
## Plugin-specific files:
# IntelliJ
/out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Example file to demonstrate the use of the VKOGA-PDE code:
# We use \Omega = [0, 1]^2, L = -LaPlace and the Gaussian kernel.
# Some imports
import numpy as np
from matplotlib import pyplot as plt
from vkoga_pde.kernels_PDE import Gaussian_laplace
from vkoga_pde.vkoga_PDE import VKOGA_PDE
np.random.seed(1)
# Create data set: Square domain
dim = 2
scale_domain = 1
X1 = scale_domain * np.random.rand(int(1e4), dim)
X2 = scale_domain * np.random.rand(400, dim)
X2[:100, 0] = 0
X2[100:200, 0] = scale_domain
X2[200:300, 1] = 0
X2[300:400, 1] = scale_domain
# Define some functions
u = lambda x: x[:, [1]] * x[:, [0]]**3
f = lambda x: -6 * x[:, [0]] * x[:, [1]]
# Define right hand side values
y1 = f(X1)
y2 = u(X2)
# Initialize and run models
kernel = Gaussian_laplace(dim=2)
maxIter = 200
model_f = VKOGA_PDE(kernel=kernel, greedy_type='f_greedy')
model_p = VKOGA_PDE(kernel=kernel, greedy_type='p_greedy')
_ = model_f.fit(X1, y1, X2, y2, maxIter=maxIter)
_ = model_p.fit(X1, y1, X2, y2, maxIter=maxIter)
# Plot some results
plt.figure(1)
plt.clf()
plt.plot(np.array(model_f.train_hist['f'])**.5, 'r')
plt.plot(np.array(model_p.train_hist['f'])**.5, 'b')
plt.yscale('log')
plt.xscale('log')
plt.legend(['f-greedy', 'p-greedy'])
plt.title('residuals')
plt.draw()
plt.figure(2)
plt.clf()
plt.plot(model_f.ctrs_[model_f.ind_ctrs1, 0], model_f.ctrs_[model_f.ind_ctrs1, 1], 'rx')
plt.plot(model_f.ctrs_[model_f.ind_ctrs2, 0], model_f.ctrs_[model_f.ind_ctrs2, 1], 'ro')
plt.title('Selected centers of f-greedy')
plt.draw()
plt.figure(3)
plt.clf()
plt.plot(model_p.ctrs_[model_p.ind_ctrs1, 0], model_p.ctrs_[model_p.ind_ctrs1, 1], 'bx')
plt.plot(model_p.ctrs_[model_p.ind_ctrs2, 0], model_p.ctrs_[model_p.ind_ctrs2, 1], 'bo')
plt.title('Selected centers of P-greedy')
plt.draw()
plt.figure(4)
plt.clf()
plt.plot(X1[:, 0], X1[:, 1], 'm.')
plt.plot(X2[:, 0], X2[:, 1], 'r.')
#!/usr/bin/env python3
from abc import ABC, abstractmethod
from scipy.spatial import distance_matrix
import numpy as np
# Abstract kernel
class Kernel(ABC):
@abstractmethod
def __init__(self):
super().__init__()
@abstractmethod
def eval(self, x, y):
pass
def eval_prod(self, x, y, v, batch_size=100):
N = x.shape[0]
num_batches = int(np.ceil(N / batch_size))
mat_vec_prod = np.zeros((N, 1))
for idx in range(num_batches):
idx_begin = idx * batch_size
idx_end = (idx + 1) * batch_size
A = self.eval(x[idx_begin:idx_end, :], y)
mat_vec_prod[idx_begin:idx_end] = A @ v
return mat_vec_prod
@abstractmethod
def diagonal(self, X):
pass
@abstractmethod
def __str__(self):
pass
@abstractmethod
def set_params(self, params):
pass
# Abstract RBF
class L_RBF(Kernel):
@abstractmethod
def __init__(self):
super(L_RBF, self).__init__()
def eval(self, x, y):
return self.rbf(self.ep, distance_matrix(np.atleast_2d(x), np.atleast_2d(y)))
def diagonal(self, X):
return np.ones(X.shape[0]) * self.rbf(self.ep, 0)
def __str__(self):
return self.name + ' [gamma = %2.2e]' % self.ep
def set_params(self, par):
self.ep = par
def dd_diagonal(self, X):
# Evaluation of (L^x - shift) (L^y - shift) k(x,y) for x=y: Likely some constant
return np.ones(X.shape[0]) * self.dd_eval(np.zeros((1, 1)), np.zeros((1, 1))).item()
def dd_eval(self, x, y):
# Evaluation of (L^x - shift) (L^y - shift) k(x,y)
array_dists = distance_matrix(np.atleast_2d(x), np.atleast_2d(y))
return self.rbf_utility(self.ep, array_dists) \
- 2 * self.shift * self.d2_eval(x, y) - self.shift ** 2 * self.rbf(self.ep, array_dists)
def d2_eval(self, x, y):
# Evaluation of (L^z - shift * Id) k(*,z)|_{x,y}
# Required exactly like this for "compute the nth basis", see also 14.12.21\A1
array_dists = distance_matrix(np.atleast_2d(x), np.atleast_2d(y))
array_result = - ((self.dim - 1) * self.rbf1_divided_by_r(self.ep, array_dists)
+ self.rbf2(self.ep, array_dists)) \
- self.shift * self.rbf(self.ep, array_dists)
return array_result
def d1_eval(self, x, y):
# Evaluation of (L - shift * Id) k(*,y)|_{x}
# Required exactly like this for "compute the nth basis", see also 14.12.21\A1
# Since the LaPlace is radial, the formula is the same as d2_eval
return self.d2_eval(x, y)
def mixed_eval_L(self, x, ctrs, list_ind_interior):
# Kernel evaluation L^x k(x, y), whereby y1 := y[ind_interior, :] are centers in the interior,
# y2 := y[~ind_interior, :] are centers on the boundary.
# Therefore ddeval needs to be used with respect to y1, but deval with respect to y2.
list_not_ind_interior = [idx for idx in range(x.shape[0]) if idx not in list_ind_interior]
array_result = np.zeros((x.shape[0], ctrs.shape[0]))
array_result[list_ind_interior, :] = self.dd_eval(x[list_ind_interior, :], ctrs)
array_result[list_not_ind_interior, :] = self.d1_eval(x[list_not_ind_interior, :], ctrs)
return array_result
def mixed_eval(self, x, ctrs, list_ind_interior):
# Kernel evaluation k(x, y), whereby y1 := y[ind_interior, :] are centers in the interior,
# y2 := y[~ind_interior, :] are centers on the boundary.
# Therefore deval needs to be used with respect to y1, but eval with respect to y2.
list_not_ind_interior = [idx for idx in range(x.shape[0]) if idx not in list_ind_interior]
array_result = np.zeros((x.shape[0], ctrs.shape[0]))
array_result[list_ind_interior, :] = self.d2_eval(x[list_ind_interior, :], ctrs)
array_result[list_not_ind_interior, :] = self.eval(x[list_not_ind_interior, :], ctrs)
return array_result
# Implementation of concrete RBFs
class Gaussian_laplace(L_RBF):
# # Gaussian kernel phi(r) = exp(-r^2),
# used in conjunction with L = -LaPlace - shift * Id, whereby shift is some constant
# (e.g. an eigenvalue of -LaPlace).
def __init__(self, dim, shift=0, ep=1):
super().__init__()
self.dim = dim
self.shift = shift
self.ep = 1 # ToDo: Generalize this one!!!
self.name = 'gauss'
# Implement not only the rbf, but also its derivatives
self.rbf = lambda ep, r: np.exp(-r**2)
self.rbf1 = lambda ep, r: -2 * r * np.exp(-r**2)
self.rbf1_divided_by_r = lambda ep, r: -2 * np.exp(-r**2)
self.rbf2 = lambda ep, r: (4 * r ** 2 - 2) * np.exp(-r**2)
# Utility RBF for dd_eval: Double LaPlacian
self.rbf_utility = lambda ep, r: \
np.exp(-r ** 2) * (16 * r ** 4 - (16 * self.dim + 32) * r ** 2 + (4 * self.dim ** 2 + 8 * self.dim))
class wendland32_laplace(L_RBF):
# Wendland kernel 3, 2: phi(r) = (1-r)_+^6 * (35r**2 + 18r + 3)
# used in conjunction with L = -LaPlace - shift * Id, whereby shift is some constant
# (e.g. an eigenvalue of -LaPlace).
# Only for dim <= 3!
def __init__(self, dim, shift=0, ep=1):
super().__init__()
self.dim = dim
self.shift = shift
self.ep = 1 # ep ToDo: Implement general case!
self.name = 'Wendland 3, 2'
# Implement not only the rbf, but also its derivatives
self.rbf = lambda ep, r: 1/3 * np.maximum(1-r, 0)**6 * (35 * r**2 + 18 * r + 3)
self.rbf1 = lambda ep, r: -1/3 * np.maximum(1 - r, 0) ** 5 * 56 * r * (5 * r + 1)
self.rbf1_divided_by_r = lambda ep, r: -1/3 * np.maximum(1 - r, 0) ** 5 * 56 * (5 * r + 1)
self.rbf2 = lambda ep, r: 1/3 * np.maximum(1 - r, 0) ** 4 * 56 * (35 * r ** 2 - 4 * r - 1)
# Utility RBF for dd_eval: Double LaPlacian
self.rbf_utility = lambda ep, r: \
1/3 * (1680*self.dim**2 + 3360*self.dim
+ r**4*(1680*self.dim**2 + 16800*self.dim + 40320)
+ r**3*(-6720*self.dim**2 - 53760*self.dim - 100800)
+ r**2*(10080*self.dim**2 + 60480*self.dim + 80640)
+ r*(-6720*self.dim**2 - 26880*self.dim - 20160)) * (r < 1)
class wendland33_laplace(L_RBF):
# Wendland kernel 3, 3: phi(r) = (1-r)_+^8 * (32r**3+ 25r^2 + 8r + 1)
# used in conjunction with L = -LaPlace - shift * Id, whereby shift is some constant
# (e.g. an eigenvalue of -LaPlace).
# Only for dim <= 3!
def __init__(self, dim, shift=0, ep=1):
super().__init__()
self.dim = dim
self.shift = shift
self.ep = 1 # ep ToDo: Implement general case!
self.name = 'wendland 3, 3'
# Implement not only the rbf, but also its derivatives
self.rbf = lambda ep, r: np.maximum(1 - r, 0) ** 8 * (32 * r ** 3 + 25 * r ** 2 + 8 * r + 1)
self.rbf1 = lambda ep, r: - np.maximum(1 - r, 0) ** 7 * 22 * r * (16 * r ** 2 + 7 * r + 1)
self.rbf1_divided_by_r = lambda ep, r: - np.maximum(1 - r, 0) ** 7 * 22 * (16 * r ** 2 + 7 * r + 1)
self.rbf2 = lambda ep, r: np.maximum(1 - r, 0) ** 6 * 22 * (160 * r ** 3 + 15 * r ** 2 - 6 * r - 1)
# Utility RBF for dd_eval: Double LaPlacian
self.rbf_utility = lambda ep, r: \
(528 * self.dim ** 2 + 1056 * self.dim + r ** 7 * (3168 * self.dim ** 2 + 50688 * self.dim + 199584)
+ r ** 6 * (-18480 * self.dim ** 2 - 258720 * self.dim - 887040)
+ r ** 5 * (44352 * self.dim ** 2 + 532224 * self.dim + 1552320)
+ r ** 4 * (-55440 * self.dim ** 2 - 554400 * self.dim - 1330560)
+ r ** 3 * (36960 * self.dim ** 2 + 295680 * self.dim + 554400)
+ r ** 2 * (-11088 * self.dim ** 2 - 66528 * self.dim - 88704)) * (r < 1)
class quadrMatern_laplace(L_RBF):
# Quadratic matern kernel phi(r) = (3 + 3r + r^2)*exp(-r),
# used in conjunction with L = -LaPlace
def __init__(self, dim, shift=0, ep=1):
super().__init__()
self.dim = dim
self.shift = shift
self.ep = 1 # ep ToDo: Implement general case!
self.name = 'quadratic Matern'
# Implement not only the rbf, but also its derivatives
self.rbf = lambda ep, r: np.exp(-r) * (3 + 3 *r + r ** 2)
self.rbf1 = lambda ep, r: np.exp(-r) * (-r) * (r+1)
self.rbf1_divided_by_r = lambda ep, r: np.exp(-r) * (-1) * (r+1)
self.rbf2 = lambda ep, r: np.exp(-r) * (r ** 2 - r - 1)
# Utility RBF for dd_eval: Double LaPlacian
self.rbf_utility = lambda ep, r: \
np.exp(-r) * (r**2 - (2 * self.dim + 3) * r + self.dim ** 2 + 2 * self.dim)
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment