예제 #1
0
def curve_normal(xy1, xy2, xy3=None):
    if xy3 == None:
        xy3 = xy2
        xy2 = vec.scale(vec.add(xy1, xy2), 0.5)

    if vec.colinear(vec.sub(xy2, xy1), vec.sub(xy3, xy2)):
        xy2 = (xy2[0] + gauss(0, 0.001), xy2[1] + gauss(0, 0.001))

    vec12 = vec.norm((xy2[0] - xy1[0], xy2[1] - xy1[1]))
    vec32 = vec.norm((xy2[0] - xy3[0], xy2[1] - xy3[1]))

    return vec.norm(vec.add(vec12, vec32))
def curve_normal(xy1, xy2, xy3=None):
    if xy3 == None:
        xy3 = xy2
        xy2 = vec.scale(vec.add(xy1, xy2), 0.5)

    if vec.colinear(vec.sub(xy2, xy1), vec.sub(xy3, xy2)):
        xy2 = (xy2[0] + gauss(0, 0.001), xy2[1] + gauss(0, 0.001))

    vec12 = vec.norm((xy2[0] - xy1[0], xy2[1] - xy1[1]))
    vec32 = vec.norm((xy2[0] - xy3[0], xy2[1] - xy3[1]))

    return vec.norm(vec.add(vec12, vec32))
def leapfrog(gradient_fx, x, u, epsilon):
    dU = gradient_fx(x)
    u_new = dict(map(lambda (k, v): (k, vec.sub(v, vec.scale(dU[k], epsilon*0.5))), u.items()))
    x = dict(map(lambda (k, v): (k, vec.add(x[k], vec.scale(v, epsilon))), u_new.items()))
    dU = gradient_fx(x)
    u_new = dict(map(lambda (k, v): (k, vec.sub(v, vec.scale(dU[k], epsilon*0.5))), u.items()))
    return x, u_new
def gradient_fromfactors(factors, x):
    dUdx = {}
    def setdU(k, v):
        dUdx[k] = v
    bound = map(lambda f: (f, map(lambda v: x[v], f.variables)), factors)
    gradient_results = map(lambda (f, a): (f, f.grad(*a)), bound) #(f, {f.variables[0]:(...)})
    map(lambda (f, grad): (f, map(lambda (var, g): setdU(var, vec.add(dUdx.get(var, (0, 0)), g)), grad.items())), gradient_results) #FIXME
    return dUdx
예제 #5
0
def signed_straightness(xy1, xy2, xy3):
    vec12 = (xy2[0] - xy1[0], xy2[1] - xy1[1], 0)
    vec32 = (xy2[0] - xy3[0], xy2[1] - xy3[1], 0)
    if vec.length(vec12) > 0 and vec.length(vec32) > 0:
        vec12 = vec.norm(vec12)
        vec32 = vec.norm(vec32)
        sign = 1 if vec.cross(vec12, vec32)[2] > 0 else -1
        return sign * vec.length(vec.add(vec12, vec32))
    else:
        return 0
def signed_straightness(xy1, xy2, xy3):
    vec12 = (xy2[0] - xy1[0], xy2[1] - xy1[1], 0)
    vec32 = (xy2[0] - xy3[0], xy2[1] - xy3[1], 0)
    if vec.length(vec12) > 0 and vec.length(vec32) > 0:
        vec12 = vec.norm(vec12)
        vec32 = vec.norm(vec32)
        sign = 1 if vec.cross(vec12, vec32)[2] > 0 else -1
        return sign * vec.length(vec.add(vec12, vec32))
    else:
        return 0
예제 #7
0
def leapfrog(gradient_fx, x, u, epsilon):
    dU = gradient_fx(x)
    u_new = dict(
        map(lambda (k, v): (k, vec.sub(v, vec.scale(dU[k], epsilon * 0.5))),
            u.items()))
    x = dict(
        map(lambda (k, v): (k, vec.add(x[k], vec.scale(v, epsilon))),
            u_new.items()))
    dU = gradient_fx(x)
    u_new = dict(
        map(lambda (k, v): (k, vec.sub(v, vec.scale(dU[k], epsilon * 0.5))),
            u.items()))
    return x, u_new
예제 #8
0
def gradient_fromfactors(factors, x):
    dUdx = {}

    def setdU(k, v):
        dUdx[k] = v

    bound = map(lambda f: (f, map(lambda v: x[v], f.variables)), factors)
    gradient_results = map(lambda (f, a): (f, f.grad(*a)),
                           bound)  #(f, {f.variables[0]:(...)})
    map(lambda (f, grad):
        (f,
         map(lambda (var, g): setdU(var, vec.add(dUdx.get(var, (0, 0)), g)),
             grad.items())), gradient_results)  #FIXME
    return dUdx
예제 #9
0
def straightness(xy1, xy2, xy3):
    vec12 = vec.norm((xy2[0] - xy1[0], xy2[1] - xy1[1]))
    vec32 = vec.norm((xy2[0] - xy3[0], xy2[1] - xy3[1]))
    return vec.length(vec.add(vec12, vec32))
def straightness(xy1, xy2, xy3):
    vec12 = vec.norm((xy2[0] - xy1[0], xy2[1] - xy1[1]))
    vec32 = vec.norm((xy2[0] - xy3[0], xy2[1] - xy3[1]))
    return vec.length(vec.add(vec12, vec32))