# Microstructure Bases¶

## DiscreteIndicatorBasis¶

pymks.bases.DiscreteIndicatorBasis

alias of PrimitiveBasis

## ContinuousIndicatorBasis¶

pymks.bases.ContinuousIndicatorBasis

alias of PrimitiveBasis

## LegendreBasis¶

class pymks.bases.LegendreBasis(n_states=2, domain=None)

Discretize a continuous field into deg local states using a Legendre polynomial basis such that,

$\frac{1}{\Delta} \int_s m(h, x) dx = \sum_0^{L-1} m[l, s] P_l(h)$

where the $$P_l$$ are Legendre polynomials and the local state space $$H$$ is mapped into the orthogonal domain of the Legendre polynomials

$-1 \le H \le 1$

The mapping of $$H$$ into the domain is done automatically in PyMKS by using the domain key work argument.

>>> n_states = 3
>>> X = np.array([[0.25, 0.1],
...               [0.5, 0.25]])
>>> def P(x):
...    x = 4 * x - 1
...    polys = np.array((np.ones_like(x), x, (3.*x**2 - 1.) / 2.))
...    tmp = (2. * np.arange(3)[:, None, None] + 1.) / 2. * polys
...    return np.rollaxis(tmp, 0, 3)
>>> basis = LegendreBasis(n_states, [0., 0.5])
>>> assert(np.allclose(basis.discretize(X), P(X)))


If the microstructure local state values fall outside of the specified domain they will no longer be mapped into the orthogonal domain of the legendre polynomais.

>>> n_states = 2
>>> X = np.array([-1, 1])
>>> leg_basis = LegendreBasis(n_states, domain=[0, 1])
>>> leg_basis.discretize(X)
Traceback (most recent call last):
...
RuntimeError: X must be within the specified domain


Instantiate a Basis

Parameters: n_states (int) – The number of local states domain (list, optional) – indicate the range of expected values for the microstructure, default is [0, n_states - 1].
discretize(X)

Discretize X.

Parameters: X (ND array) – The microstructure, an (n_samples, n_x, ...) shaped array where n_samples is the number of samples and n_x is the spatial discretization. Float valued field of of Legendre polynomial coefficients.
>>> X = np.array([[-1, 1],
...               [0, -1]])
>>> leg_basis = LegendreBasis(3, [-1, 1])
>>> def p(x):
...    polys = np.array((np.ones_like(x), x, (3.*x**2 - 1.) / 2.))
...    tmp = (2. * np.arange(3)[:, None, None] + 1.) / 2. * polys
...    return np.rollaxis(tmp, 0, 3)
>>> assert(np.allclose(leg_basis.discretize(X), p(X)))