예제 #1
0
def test_est_2dkernel():
    """example from Harsha."""
    x, xi, xj = symbols('x xi xj')
    y, yi, yj = symbols('y yi yj')

    X = Tuple(x, y)
    Xi = Tuple(xi, yi)
    Xj = Tuple(xj, yj)

    u = Unknown('u')

    phi = Constant('phi')
    theta = Constant('theta')

    expr = phi * u + dx(u) + dy(dy(u))

    print('> generic_kernel := ', expand(generic_kernel(expr, u, (Xi, Xj))))
    print('')

    kuu = theta * exp(-0.5 * ((xi - xj)**2 + (yi - yj)**2))

    kuf = compute_kernel(expr, kuu, Xi)
    kfu = compute_kernel(expr, kuu, Xj)
    kff = compute_kernel(expr, kuu, (Xi, Xj))

    print('> kuf := ', kuf)
    print('> kfu := ', kfu)
    print('> kff := ', kff)
예제 #2
0
def test_3d():
    x, xi, xj = symbols('x xi xj')
    y, yi, yj = symbols('y yi yj')
    z, zi, zj = symbols('z zi zj')

    X = Tuple(x, y, z)
    Xi = Tuple(xi, yi, zi)
    Xj = Tuple(xj, yj, zj)

    u = Unknown('u')

    alpha = Constant('alpha')
    beta = Constant('beta')
    mu = Constant('mu')
    nu = Constant('nu')
    theta = Constant('theta')

    #    expr = alpha * u
    #    expr = alpha * dx(u)
    #    expr = alpha * dy(u)
    #    expr = alpha * dz(u)
    #    expr = alpha * u + beta * dx(u)
    #    expr = alpha * u + beta * dy(u)
    #    expr = alpha * u + beta * dz(u)
    #    expr = mu * u + alpha * dx(u) + beta * dx(dx(u))
    #    expr = mu * u + alpha * dx(u) + beta * dy(dy(u))
    #    expr = mu * u + alpha * dx(u) + beta * dz(dz(u))
    expr = mu * u + alpha * dx(u) + beta * dy(dz(u)) + nu * dx(dz(u))

    #    print('> generic_kernel := ', expand(generic_kernel(expr, u, Xi)))
    #    print('> generic_kernel := ', expand(generic_kernel(expr, u, Xj)))
    print('> generic_kernel := ', expand(generic_kernel(expr, u, (Xi, Xj))))
예제 #3
0
def test_generic_kernel_1d():
    x, xi, xj = symbols('x xi xj')

    u = Unknown('u')

    # ... testing u
    assert (generic_kernel(u, u, xi) == Function('u')(xi))
    assert (generic_kernel(u, u, xj) == Function('u')(xj))
    assert (generic_kernel(u, u, (xi, xj)) == Function('u')(xi, xj))
    # ...

    # ... testing dx(u)
    assert (generic_kernel(dx(u), u, xi) == Derivative(Function('u')(xi), xi))
    assert (generic_kernel(dx(u), u, xj) == Derivative(Function('u')(xj), xj))
    assert (generic_kernel(dx(u), u, (xi, xj)) == Derivative(
        Function('u')(xi, xj), xi, xj))
    # ...

    # ... testing dx(dx(u))
    assert (generic_kernel(dx(dx(u)), u,
                           xi) == Derivative(Function('u')(xi), xi, xi))
    assert (generic_kernel(dx(dx(u)), u,
                           xj) == Derivative(Function('u')(xj), xj, xj))
    assert (generic_kernel(dx(dx(u)), u, (xi, xj)) == Derivative(
        Function('u')(xi, xj), xi, xi, xj, xj))
예제 #4
0
def test_generic_kernel_3d():
    x, xi, xj = symbols('x xi xj')
    y, yi, yj = symbols('y yi yj')
    z, zi, zj = symbols('z zi zj')

    X = Tuple(x, y, z)
    Xi = Tuple(xi, yi, zi)
    Xj = Tuple(xj, yj, zj)

    u = Unknown('u')

    # ... testing u
    assert (generic_kernel(u, u, xi) == Function('u')(xi))
    assert (generic_kernel(u, u, xj) == Function('u')(xj))
    assert (generic_kernel(u, u, (xi, xj)) == Function('u')(xi, xj))
    # ...

    # ... testing dx(u)
    assert (generic_kernel(dx(u), u, Xi) == Derivative(Function('u')(*Xi), xi))

    assert (generic_kernel(dx(u), u, Xj) == Derivative(Function('u')(*Xj), xj))

    assert (generic_kernel(dx(u), u, (Xi, Xj)) == Derivative(
        Function('u')(*Xi, *Xj), xi, xj))
    # ...

    # ... testing dy(u)
    assert (generic_kernel(dy(u), u, Xi) == Derivative(Function('u')(*Xi), yi))

    assert (generic_kernel(dy(u), u, Xj) == Derivative(Function('u')(*Xj), yj))

    assert (generic_kernel(dy(u), u, (Xi, Xj)) == Derivative(
        Function('u')(*Xi, *Xj), yi, yj))
    # ...

    # ... testing dz(u)
    assert (generic_kernel(dz(u), u, Xi) == Derivative(Function('u')(*Xi), zi))

    assert (generic_kernel(dz(u), u, Xj) == Derivative(Function('u')(*Xj), zj))

    assert (generic_kernel(dz(u), u, (Xi, Xj)) == Derivative(
        Function('u')(*Xi, *Xj), zi, zj))
    # ...

    # ... testing dx(dx(u))
    assert (generic_kernel(dx(dx(u)), u,
                           Xi) == Derivative(Function('u')(*Xi), xi, xi))

    assert (generic_kernel(dx(dx(u)), u,
                           Xj) == Derivative(Function('u')(*Xj), xj, xj))

    assert (generic_kernel(dx(dx(u)), u, (Xi, Xj)) == Derivative(
        Function('u')(*Xi, *Xj), xi, xi, xj, xj))
예제 #5
0
def test_1d():
    x, xi, xj = symbols('x xi xj')

    u = Unknown('u')

    alpha = Constant('alpha')
    beta = Constant('beta')
    mu = Constant('mu')
    theta = Constant('theta')

    #    expr = alpha * u
    #    expr = alpha * dx(u)
    #    expr = alpha * u + beta * dx(u)
    #    expr = mu * u + dx(u)
    #    expr = mu * u + dx(dx(u))
    #    expr = mu * u + alpha * dx(u) + beta * dx(dx(u))

    expr = mu * u + dx(u) + dx(dx(u))

    #    print('> generic_kernel := ', expand(generic_kernel(expr, u, xi)))
    #    print('> generic_kernel := ', expand(generic_kernel(expr, u, xj)))
    print('> generic_kernel := ', expand(generic_kernel(expr, u, (xi, xj))))
예제 #6
0
def generic_kernel(expr, func, y, args=None):
    if isinstance(y, Symbol):
        _derivatives = tuple([dx])
    elif isinstance(y, Tuple):
        _derivatives = tuple(_partial_derivatives[:len(y)])
    elif not isinstance(y, (list, tuple)):
        raise TypeError('expecting a Symbol or Tuple')

    _args = []
    if args:
        for a in args:
            if isinstance(a, Symbol):
                _args += [a]
            elif isinstance(a, Tuple):
                _args += [*a]
            else:
                raise TypeError('expecting a Symbol or Tuple')
        args = _args

    if isinstance(y, (list, tuple)):
        args = y
        ei = func
        for xi in y:
            ej = generic_kernel(expr, ei, xi, args=args)
            ei = ej
        expr = ei

        # check that there are no partial derivatives in the final expression
        ops = find_partial_derivatives(expr)
        if not (len(ops) == 0):
            raise ValueError(
                'Found unexpected partial differential operators in \n{}'.
                format(expr))

        return expr
    else:
        if isinstance(func, Unknown):
            func = [func]

        elif isinstance(func, (Add, Mul)):
            fn = [
                i for i in preorder_traversal(expr) if isinstance(i, Unknown)
            ]
            # make it unique
            fn = set(fn)
            fn = list(fn)
            if not (len(fn) == 1):
                raise ValueError('expecting only one unknown')

            expr = expr.subs({fn[0]: func})
            func = fn

        elif isinstance(func, Derivative):
            fn = [i for i in expr.free_symbols if isinstance(i, Unknown)]
            if not (len(fn) == 1):
                raise ValueError('expecting only one unknown')

            u = fn[0]
            expr = expr.subs({u: func})
            func = [u]

        # TODO improve
        elif isinstance(func, Function):
            fname = type(func).__name__
            func = [Unknown(fname)]

        else:
            raise NotImplementedError('type = ', type(func), func)

        if not args:
            args = [y]
            if isinstance(y, Tuple):
                args = [*y]

        # ... TODO is there a way to remove this part?
        for f in func:
            fnew = Function(f.name)

            if isinstance(y, Tuple):
                for d in _derivatives:
                    for D in _derivatives:
                        i_d = d.grad_index
                        i_D = D.grad_index
                        dD_f = fnew(*args).diff(y[i_d]).diff(y[i_D])
                        expr = expr.subs({d(D(f)): dD_f})

                for d in _derivatives:
                    i_d = d.grad_index
                    d_f = fnew(*args).diff(y[i_d])
                    expr = expr.subs({d(f): d_f})

            elif isinstance(y, Symbol):
                # 1D case, we only use dx
                expr = expr.subs({dx(dx(f)): fnew(*args).diff(y).diff(y)})
                expr = expr.subs({dx(f): fnew(*args).diff(y)})

            else:
                raise TypeError('expecting Tuple or Symbol')
        # ...

        # partial derivatives must be sorted from high to low
        ops = sort_partial_derivatives(expr)
        for i in ops:

            if not (len(i.args) == 1):
                raise ValueError(
                    'expecting only one argument for partial derivatives')

            # if i = dx(u) then type(i) is dx
            d = type(i)

            a = i.args[0]

            # terms like dx(Derivative(..))
            if isinstance(a, Derivative):
                if isinstance(y, Tuple):
                    i_d = d.grad_index
                    expr = expr.subs({i: a.diff(y[i_d])})

                elif isinstance(y, Symbol):
                    expr = expr.subs({i: a.diff(y)})

            # terms like dx(u(..))
            # TODO this is not good, since we don't know if the function is an
            # unkown => store unknown names in a list and pass it recursively?
            elif isinstance(a, Function) and not (isinstance(a, _derivatives)):
                if isinstance(y, Tuple):
                    i_d = d.grad_index
                    expr = expr.subs({i: a.diff(y[i_d])})

                elif isinstance(y, Symbol):
                    expr = expr.subs({i: a.diff(y)})

            # terms like dx(dx(Derivative(..)))
            elif isinstance(a, _derivatives):
                if not (len(a.args) == 1):
                    raise ValueError(
                        'expecting only one argument for partial derivatives')

                D = type(a)
                b = a.args[0]

                if isinstance(y, Tuple):
                    i_d = d.grad_index
                    i_D = D.grad_index
                    expr = expr.subs({i: b.diff(y[i_d]).diff(y[i_D])})

                elif isinstance(y, Symbol):
                    expr = expr.subs({i: b.diff(y).diff(y)})

            else:
                raise TypeError('expecting a Derivative or partial derivative,'
                                ' given {} :: {}'.format(a, type(a)))

        # finally, we replace u by u(xi)
        fn = [i for i in expr.free_symbols if isinstance(i, Unknown)]
        for u in fn:
            fnew = Function(u.name)
            expr = expr.subs({u: fnew(*args)})

        return expr
예제 #7
0
from mlhiphy.calculus import Constant
from mlhiphy.calculus import Unknown
from mlhiphy.kernels import compute_kernel, generic_kernel
from mlhiphy.templates import template_scalar, template_header_scalar

from sympy import expand
from sympy import Lambda
from sympy import symbols
from sympy import exp

x, xi, xj = symbols('x xi xj')

alpha = Constant('alpha')
beta = Constant('beta')

u = Unknown('u')
expr = alpha * u + dx(u)

print('> generic_kernel := ', expand(generic_kernel(expr, u, (xi, xj))))

xi, xj, theta = symbols('xi xj theta')
kuu = theta * exp(-0.5 * ((xi - xj)**2))

import os


def mkdir_p(dir):
    if os.path.isdir(dir):
        return
    os.makedirs(dir)