def main(): v = np.r_[np.deg2rad(10.), 0., 0.] w = np.r_[np.deg2rad(30.), 0., 0.] z = axMakeInterpolated(0.3, v, 0.6, w) print 'v:', v print 'w:', w print 'z:', z print 'z[0] (deg):', np.rad2deg(z[0]) print 'axScale(0.3, v): ', axScale(0.3, v) print 'axScale(0.6, w): ', axScale(0.6, w) print 'axAdd(axScale(0.3, v), axScale(0.6, w)): ', print axAdd(axScale(0.3, v), axScale(0.6, w)) approx_Dv = approx_jac(lambda x: axAdd(x, w), v, epsilon=1e-3) print 'approx_Dv:' print np.around(approx_Dv, decimals=3) Dv = axAdd_da(v, w) print 'Dv:' print np.around(Dv, decimals=3) print 'allclose? ', np.allclose(Dv, approx_Dv, atol=1e-4) approx_Dw = approx_jac(lambda x: axAdd(v, x), w, epsilon=1e-6) print 'approx_Dw:' print np.around(approx_Dw, decimals=3) Dw = axAdd_db(v, w) print 'Dw:' print np.around(Dw, decimals=3) print 'allclose? ', np.allclose(Dw, approx_Dw, atol=1e-4)
def approx_jacs(f, indices, epsilon, *args, **kwargs): args = list(args) jacs = [] for i in indices: orig_x = args[i] def wrap_f(x): args[i] = x.reshape(orig_x.shape) return f(*args, **kwargs) jacs.append(approx_jac(wrap_f, np.ravel(orig_x), epsilon)) args[i] = orig_x return jacs
def main_EvaluateSingleARAP2(): T = np.r_[1, 2, 4, 2, 3, 4, 3, 0, 4, 0, 1, 4].reshape(-1, 3).astype(np.int32) V = np.r_[0., 0., 0., 0., 2., 0., 2., 2., 0., 2., 0., 0., 1., 1., 1.].reshape(-1, 3).astype(np.float64) X = np.zeros((5, 3), dtype=np.float64) Xg = np.zeros((1, 3), dtype=np.float64) s = np.abs(np.random.randn(1).reshape(1, 1).astype(np.float64)) V = np.random.randn(V.size).reshape(V.shape) V1 = np.random.randn(V.size).reshape(V.shape) k = 5 print 'V:' print V print 'V1:' print V1 r = EvaluateSingleARAP2(T, V, X, Xg, s, V1, k, verbose=True) e, Jx, Jxg, Js, JV1i, JV1j = r print 'e:', e print '\nJx:' print Jx print 'approx_Jx:' approx_Jx = approx_jac( lambda x: EvaluateSingleARAP2(T, V, x.reshape(V.shape), Xg, s, V1, k)[ 0], np.ravel(X)) print approx_Jx[:, 12:15] print 'allclose? ', np.allclose(Jx, approx_Jx[:, 12:15], atol=1e-3) print '\nJxg:' print Jxg print 'approx_Jxg:' approx_Jxg = approx_jac( lambda x: EvaluateSingleARAP2(T, V, X, x.reshape(1, 3), s, V1, k)[0], np.ravel(Xg)) print approx_Jxg print 'allclose? ', np.allclose(Jxg, approx_Jxg, atol=1e-3) print '\nJs:' print Js print 'approx_Js:' approx_Js = approx_jac( lambda x: EvaluateSingleARAP2(T, V, X, Xg, x.reshape(1, 1), V1, k)[0], np.ravel(s)) print approx_Js print 'allclose? ', np.allclose(Js, approx_Js, atol=1e-3) print '\nJV1i:' print JV1i print 'approx_JV1i:' approx_JV1 = approx_jac( lambda x: EvaluateSingleARAP2(T, V, X, Xg, s, x.reshape(V1.shape), k)[ 0], np.ravel(V1)) print approx_JV1[:, 12:15] print '\nJV1j:' print JV1j print 'approx_JV1j:' print approx_JV1[:, 6:9]
def main_EvaluateDualNonLinearBasisARAP(): T = np.r_[1, 2, 4, 2, 3, 4, 3, 0, 4, 0, 1, 4].reshape(-1, 3).astype(np.int32) V = np.r_[0., 0., 0., 0., 2., 0., 2., 2., 0., 2., 0., 0., 1., 1., 1.].reshape(-1, 3).astype(np.float64) Xg = np.zeros((1, 3), dtype=np.float64) # s = np.r_[1.0].reshape(1, 1).astype(np.float64) s = np.r_[np.random.rand()].reshape(1, 1).astype(np.float64) # n = 1 # Xs = [np.zeros((5, 3), dtype=np.float64) for i in xrange(n)] # Xs[0][4, -1] = np.pi # ys = [np.r_[1.0 / n].reshape(1, 1).astype(np.float64) for i in xrange(n)] n = 5 Xs = [np.random.randn(V.size).reshape(V.shape) for i in xrange(n)] y = np.random.randn(n).reshape(n, 1) V1 = V.copy() k = 5 print 's:' print s print 'V:' print V print 'V1:' print V1 r = EvaluateDualNonLinearBasisARAP(T, V, Xg, s, Xs, y, V1, k, verbose=True) e, Jxg, Js, JVi, JVj, JV1i, JV1j = r[:7] print 'e:', e JXs = r[7:7 + n] Jy = r[7 + n:] print '\nJxg:' print np.around(Jxg, decimals=4) print 'approx_Jxg:' approx_Jxg = approx_jac( lambda x: EvaluateDualNonLinearBasisARAP(T, V, x.reshape(Xg.shape), s, Xs, y, V1, k)[0], np.ravel(Xg)) print np.around(approx_Jxg, decimals=4) print 'allclose? ', np.allclose(Jxg, approx_Jxg, atol=1e-3) print '\nJs:' print np.around(Js, decimals=4) print 'approx_Js:' approx_Js = approx_jac(lambda x: EvaluateDualNonLinearBasisARAP( T, V, Xg, x.reshape(1, 1), Xs, y, V1, k)[0], np.ravel(s), epsilon=1e-6) print np.around(approx_Js, decimals=4) print 'allclose? ', np.allclose(Js, approx_Js, atol=1e-3) print '\nJVi:' print np.around(JVi, decimals=4) print 'approx_JVi:' approx_JV = approx_jac( lambda x: EvaluateDualNonLinearBasisARAP(T, x.reshape(V.shape), Xg, s, Xs, y, V1, k)[0], np.ravel(V)) print np.around(approx_JV[:, 12:15], decimals=4) print 'allclose? ', np.allclose(JVi, approx_JV[:, 12:15], atol=1e-3) print '\nJVj:' print np.around(JVj, decimals=4) print 'approx_JVj:' print np.around(approx_JV[:, 6:9], decimals=4) print 'allclose? ', np.allclose(JVj, approx_JV[:, 6:9], atol=1e-3) print '\nJV1i:' print np.around(JV1i, decimals=4) print 'approx_JV1i:' approx_JV1 = approx_jac( lambda x: EvaluateDualNonLinearBasisARAP(T, V, Xg, s, Xs, y, x.reshape(V1.shape), k)[0], np.ravel(V1)) print np.around(approx_JV1[:, 12:15], decimals=4) print 'allclose? ', np.allclose(JV1i, approx_JV1[:, 12:15], atol=1e-3) print '\nJV1j:' print np.around(JV1j, decimals=4) print 'approx_JV1j:' print np.around(approx_JV1[:, 6:9], decimals=4) print 'allclose? ', np.allclose(JV1j, approx_JV1[:, 6:9], atol=1e-3) for i in xrange(n): print '\nJX[%d]' % i print np.around(JXs[i], decimals=4) def f(x): _Xs = Xs[:] _Xs[i] = x.reshape(_Xs[i].shape) return EvaluateDualNonLinearBasisARAP(T, V, Xg, s, _Xs, y, V1, k)[0] approx_JX = approx_jac(f, np.ravel(Xs[i])) print 'approx_JX[%d]' % i print np.around(approx_JX[:, 12:15], decimals=4) print 'allclose? ', np.allclose(JXs[i], approx_JX[:, 12:15], atol=1e-3) print '\nJy:' Jy = np.hstack(Jy) print np.around(Jy, decimals=4) print 'approx_Jy:' approx_Jy = approx_jac( lambda x: EvaluateDualNonLinearBasisARAP(T, V, Xg, s, Xs, x[:, np.newaxis], V1, k)[0], np.ravel(y)) print np.around(approx_Jy, decimals=4) print 'allclose? ', np.allclose(Jy, approx_Jy, atol=1e-3) return