Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if random == "pseudo":
x = np.random.rand(n, self.dim)
elif random == "sobol":
x = sobol_sequence.sample(n + 1, self.dim)[1:]
return (self.xmax - self.xmin) * x + self.xmin
def periodic_point(self, x, component):
y = np.copy(x)
if np.isclose(y[component], self.xmin[component]):
y[component] += self.xmax[component] - self.xmin[component]
elif np.isclose(y[component], self.xmax[component]):
y[component] -= self.xmax[component] - self.xmin[component]
return y
class Hypersphere(Geometry):
def __init__(self, center, radius):
self.center, self.radius = np.array(center), radius
super(Hypersphere, self).__init__(
len(center), (self.center - radius, self.center + radius), 2 * radius
)
self._r2 = radius ** 2
def inside(self, x):
return np.linalg.norm(x - self.center) <= self.radius
def on_boundary(self, x):
return np.isclose(np.linalg.norm(x - self.center), self.radius)
def distance2boundary_unitdirn(self, x, dirn):
"""https://en.wikipedia.org/wiki/Line%E2%80%93sphere_intersection
)
return np.array(x)
def periodic_point(self, x, component):
if self.geom1.on_boundary(x) and not self.geom2.inside(x):
y = self.geom1.periodic_point(x, component)
if self.on_boundary(y):
return y
if self.geom2.on_boundary(x) and not self.geom1.inside(x):
y = self.geom2.periodic_point(x, component)
if self.on_boundary(y):
return y
return x
class CSGDifference(geometry.Geometry):
"""Construct an object by CSG Difference."""
def __init__(self, geom1, geom2):
if geom1.dim != geom2.dim:
raise ValueError(
"{} - {} failed (dimensions do not match).".format(
geom1.idstr, geom2.idstr
)
)
super(CSGDifference, self).__init__(geom1.dim, geom1.bbox, geom1.diam)
self.geom1 = geom1
self.geom2 = geom2
def inside(self, x):
return self.geom1.inside(x) and not self.geom2.inside(x)
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
from SALib.sample import sobol_sequence
from scipy import spatial
from .geometry import Geometry
from .geometry_nd import Hypercube
class Disk(Geometry):
def __init__(self, center, radius):
self.center, self.radius = np.array(center), radius
super(Disk, self).__init__(
2, (self.center - radius, self.center + radius), 2 * radius
)
self._r2 = radius ** 2
def inside(self, x):
return np.linalg.norm(x - self.center) <= self.radius
def on_boundary(self, x):
return np.isclose(np.linalg.norm(x - self.center), self.radius)
def distance2boundary_unitdirn(self, x, dirn):
"""https://en.wikipedia.org/wiki/Line%E2%80%93sphere_intersection
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
from SALib.sample import sobol_sequence
from .geometry import Geometry
from .. import config
class Interval(Geometry):
def __init__(self, l, r):
super(Interval, self).__init__(1, (np.array([l]), np.array([r])), r - l)
self.l, self.r = l, r
def inside(self, x):
return self.l <= x[0] <= self.r
def on_boundary(self, x):
return np.any(np.isclose(x, [self.l, self.r]))
def distance2boundary(self, x, dirn):
return x[0] - self.l if dirn < 0 else self.r - x[0]
def mindist2boundary(self, x):
return min(np.amin(x - self.l), np.amin(self.r - x))
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
from . import geometry
class CSGUnion(geometry.Geometry):
"""Construct an object by CSG Union."""
def __init__(self, geom1, geom2):
if geom1.dim != geom2.dim:
raise ValueError(
"{} | {} failed (dimensions do not match).".format(
geom1.idstr, geom2.idstr
)
)
super(CSGUnion, self).__init__(
geom1.dim,
(
np.minimum(geom1.bbox[0], geom2.bbox[0]),
np.maximum(geom1.bbox[1], geom2.bbox[1]),
),
geom1.diam + geom2.diam,
u = u[:n]
else:
u = np.random.rand(n)
u *= self.perimeter
x = []
for l in u:
if l < self.l12:
x.append(l * self.n12 + self.x1)
elif l < self.l12 + self.l23:
x.append((l - self.l12) * self.n23 + self.x2)
else:
x.append((l - self.l12 - self.l23) * self.n31 + self.x3)
return np.vstack((x_corner, x))
class Polygon(Geometry):
"""Simple polygon.
Args:
vertices: Clockwise or counterclockwise.
"""
def __init__(self, vertices):
self.vertices = np.array(vertices)
self.diagonals = spatial.distance.squareform(
spatial.distance.pdist(self.vertices)
)
super(Polygon, self).__init__(
2,
(np.amin(self.vertices, axis=0), np.amax(self.vertices, axis=0)),
np.max(self.diagonals),
)
print(
"Warning: {} points required, but {} points sampled. Uniform random is not guaranteed.".format(
n, len(x)
)
)
return np.array(x)
def periodic_point(self, x, component):
if self.geom1.on_boundary(x) and not self.geom2.inside(x):
y = self.geom1.periodic_point(x, component)
if self.on_boundary(y):
return y
return x
class CSGIntersection(geometry.Geometry):
"""Construct an object by CSG Intersection."""
def __init__(self, geom1, geom2):
if geom1.dim != geom2.dim:
raise ValueError(
"{} & {} failed (dimensions do not match).".format(
geom1.idstr, geom2.idstr
)
)
super(CSGIntersection, self).__init__(
geom1.dim,
(
np.maximum(geom1.bbox[0], geom2.bbox[0]),
np.minimum(geom1.bbox[1], geom2.bbox[1]),
),
min(geom1.diam, geom2.diam),
u = np.random.rand(n)
u *= self.perimeter
x = []
for l in u:
if l < l1:
x.append([self.xmin[0] + l, self.xmin[1]])
elif l < l2:
x.append([self.xmax[0], self.xmin[1] + l - l1])
elif l < l3:
x.append([self.xmax[0] - l + l2, self.xmax[1]])
else:
x.append([self.xmin[0], self.xmax[1] - l + l3])
return np.vstack((x_corner, x))
class Triangle(Geometry):
def __init__(self, x1, x2, x3):
self.x1 = np.array(x1)
self.x2 = np.array(x2)
self.x3 = np.array(x3)
self.v12 = self.x2 - self.x1
self.v23 = self.x3 - self.x2
self.v31 = self.x1 - self.x3
self.l12 = np.linalg.norm(self.v12)
self.l23 = np.linalg.norm(self.v23)
self.l31 = np.linalg.norm(self.v31)
self.n12 = self.v12 / self.l12
self.n23 = self.v23 / self.l23
self.n31 = self.v31 / self.l31
self.perimeter = self.l12 + self.l23 + self.l31
super(Triangle, self).__init__(
2,
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import itertools
import numpy as np
from SALib.sample import sobol_sequence
from scipy import stats
from sklearn import preprocessing
from .geometry import Geometry
class Hypercube(Geometry):
def __init__(self, xmin, xmax):
if len(xmin) != len(xmax):
raise ValueError("Dimensions of xmin and xmax do not match.")
if np.any(np.array(xmin) >= np.array(xmax)):
raise ValueError("xmin >= xmax")
self.xmin, self.xmax = np.array(xmin), np.array(xmax)
super(Hypercube, self).__init__(
len(xmin), (self.xmin, self.xmax), np.linalg.norm(self.xmax - self.xmin)
)
def inside(self, x):
return np.all(x >= self.xmin) and np.all(x <= self.xmax)
def on_boundary(self, x):
return self.inside(x) and (