Example #1
0
def test_reuse_of_expr():
    x = cg.Symbol('x')
    y = cg.Symbol('y')

    xy = x * y
    f = (xy + 1) * xy
    checkf(f, {x: 2, y: 3}, value=42, ngrad={x: 39, y: 26})
Example #2
0
def test_exp():
    x = cg.Symbol('x')

    f = cg.sym_exp(x)
    checkf(f, {x: 2}, value=math.exp(2), ngrad={x: math.exp(2)})
    checkf(f, {x: [1, 0]},
           value=[math.exp(1), math.exp(0)],
           ngrad={x: [math.exp(1), math.exp(0)]})
Example #3
0
def test_pow():
    x = cg.Symbol('x')
    y = cg.Symbol('y')

    f = x**y
    checkf(f, {x: 2, y: 3}, value=8, ngrad={x: 12, y: math.log(256)})

    d = cg.symbolic_gradient(f)
    checkf(d[x], {
        x: 2,
        y: 3
    }, value=12, ngrad={
        x: 12,
        y: 4 + math.log(4096)
    })  # ddf/dxdx and ddf/dxdy
    checkf(d[y], {
        x: 2,
        y: 3
    },
           value=math.log(256),
           ngrad={
               x: 4 + math.log(4096),
               y: 8 * math.log(2) * math.log(2)
           })  # ddf/dydx and ddf/dydy

    f = (x * 2 + y)**2
    checkf(f, {x: 2, y: 3}, value=7**2, ngrad={x: 2 * 7 * 2, y: 2 * 7 * 1})
Example #4
0
def test_add():
    x = cg.Symbol('x')
    y = cg.Symbol('y')

    f = x + y
    checkf(f, {x: 2, y: 3}, value=5, ngrad={x: 1, y: 1})
    checkf(f, {
        x: [2, 3],
        y: [4, 5]
    },
           value=[6, 8],
           ngrad={
               x: [1, 1],
               y: [1, 1]
           })
Example #5
0
def test_sub():
    x = cg.Symbol('x')
    y = cg.Symbol('y')

    f = x - y
    checkf(f, {x: 2, y: 3}, value=-1, ngrad={x: 1, y: -1})
    checkf(f, {
        x: [2, 3],
        y: [4, 5]
    },
           value=[-2, -2],
           ngrad={
               x: [1, 1],
               y: [-1, -1]
           })
Example #6
0
def test_complex_expr():

    x = cg.Symbol('x')
    y = cg.Symbol('y')
    z = cg.Symbol('z')

    f = (x * y + 3) / (z - 2)
    checkf(f, {x: 3, y: 4, z: 4}, value=7.5, ngrad={x: 2., y: 1.5, z: -3.75})

    k = x * 3 - math.pi
    m = f / k
    checkf(m, {
        x: 3,
        y: 4,
        z: 4
    },
           value=1.28021142206776,
           ngrad={
               x: -0.314186801525697,
               y: 0.2560422844135512,
               z: -0.64010571103387
           })
Example #7
0
def test_circle():
    x = cg.Symbol('x')
    y = cg.Symbol('y')

    c = sdf.Circle(center=[1, 1], radius=1.)
    checkf(c.sdf, {x: 2, y: 1}, value=0., ngrad={x: 1, y: 0})
    checkf(c.sdf, {
        x: 0.9,
        y: 0.7
    },
           value=-0.683772,
           ngrad={
               x: -0.316228,
               y: -0.948683
           })
    checkf(c.sdf, {x: 5, y: 4}, value=4, ngrad={x: 0.8, y: 0.6})
Example #8
0
def test_halfspace():
    x = cg.Symbol('x')
    y = cg.Symbol('y')

    c = sdf.Halfspace(normal=[0, 1], d=1)
    checkf(c.sdf, {x: 0, y: 0}, value=-1., ngrad={x: 0, y: 1})
Example #9
0
def test_sqrt():
    x = cg.Symbol('x')

    f = cg.sym_sqrt(x)
    checkf(f, {x: 4}, value=2, ngrad={x: 0.25})
Example #10
0
def test_neg():
    x = cg.Symbol('x')

    f = -x
    checkf(f, {x: 2}, value=-2, ngrad={x: -1})
Example #11
0
def test_log():
    x = cg.Symbol('x')

    f = cg.sym_log(x)
    checkf(f, {x: 2}, value=math.log(2), ngrad={x: 1 / 2})
Example #12
0
def test_div():
    x = cg.Symbol('x')
    y = cg.Symbol('y')

    f = x / y
    checkf(f, {x: 2, y: 3}, value=2 / 3, ngrad={x: 1 / 3, y: -2 / 9})
Example #13
0
def test_mul():
    x = cg.Symbol('x')
    y = cg.Symbol('y')

    f = x * y
    checkf(f, {x: 2, y: 3}, value=6, ngrad={x: 3, y: 2})
Example #14
0
def test_sum():
    x = cg.Symbol('x')
    y = cg.Symbol('y')

    f = cg.sym_sum([x, y, x, y])
    checkf(f, {x: 2, y: 3}, value=10, ngrad={x: 2, y: 2})
Example #15
0
def test_min():
    x = cg.Symbol('x')
    y = cg.Symbol('y')

    f = cg.sym_min(x, y)
    checkf(f, {x: 2, y: 1}, value=1, ngrad={x: 0, y: 1}, with_sgrad=False)
Example #16
0
def test_cos():
    x = cg.Symbol('x')

    f = cg.sym_cos(x)
    checkf(f, {x: 2}, value=math.cos(2), ngrad={x: -math.sin(2)})
Example #17
0
def test_max():
    x = cg.Symbol('x')
    y = cg.Symbol('y')

    f = cg.sym_max(x, y)
    checkf(f, {x: 2, y: 1}, value=2, ngrad={x: 1, y: 0}, with_sgrad=False)