Algorithm Search⌘K

Algorithmcore

Jacobian transpose method

Also called Jacobian transpose inverse kinematics.

Steps the joints by a gain times J^T e, a gradient step on the squared pose error that needs no matrix inverse.

Time
Θ(k m n) worst
Space
Θ(m n) extra
Where
k = the number of iterations; m = the number of task coordinates; n = the number of joints
Properties
deterministicofflineiterative
Origin
William A. Wolovich, H. Elliott, A. Balestrino, G. De Maria, Lorenzo Sciavicco, 1984

The Jacobian transpose method solves the inverse kinematics of an arm by repeated small joint steps Δθ=αJTe\Delta\theta = \alpha J^{\mathsf T}e, where JJ is the Jacobian, ee is the task error and α>0\alpha > 0 is a gain. For a position task, JTeJ^{\mathsf T}e is the negative gradient of half the squared error, so the method is gradient descent on the error. It needs no matrix inverse and no linear solve, so each step is cheap, and it stays defined at a kinematic singularity. The price is slow, linear convergence, and a gain that must be chosen with care.

In plain words

Imagine the target pulls on the hand of the arm with a spring. Each joint feels a turning push from that pull, larger when turning that joint moves the hand more toward the target. The method turns each joint a little in the direction of its push, and then looks again. The pushes are cheap to compute, because no system of equations is solved. Small turns always bring the hand closer, but the hand may approach slowly. Turns that are too large overshoot, and the arm can swing back and forth around the target. If the target is out of reach, the arm stretches toward it and stops.

The problem

Input.

  • A task map x=f(θ)x = f(\theta) from nn joint values to mm task coordinates and its Jacobian J(θ)J(\theta), an m×nm \times n matrix. For a full pose, ee is a twist error and JJ the matching geometric Jacobian.
  • A target xdx_d, an initial guess θ0\theta^0, a gain α>0\alpha > 0 or the rule to choose it, a tolerance ε\varepsilon and a step limit kmax⁡k_{\max}.

Output. A θ\theta with ∣xd−f(θ)∣≤ε|x_d - f(\theta)| \le \varepsilon, or the last iterate with the report that the tolerance was not met.

The method does not need m=nm = n or a Jacobian of full rank.

The algorithm

  1. JACOBIAN-TRANSPOSE-IK(f, J, x_d, θ0, ε, k_max)
  2. 1θ ← θ0
  3. 2for k ← 1 to k_max
  4. 3e ← x_d - f(θ)
  5. 4if |e| ≤ ε then return θ
  6. 5J ← JACOBIAN(θ)
  7. 6g ← J^T e ▷ the negative gradient of |e|^2 / 2
  8. 7α ← <e, J g> / |J g|^2 ▷ or a fixed gain α
  9. 8θ ← θ + α g
  10. 9return θ with the report that the tolerance was not met

Line L7 is the gain of Buss and Kim. For the linear model enew=e−JΔθe_{\mathrm{new}} = e - J\Delta\theta, it is the gain that makes ∣e−αJJTe∣|e - \alpha JJ^{\mathsf T}e| as small as possible. When Jg=0Jg = 0, then g=0g = 0 too, since ∣g∣2=⟨e,Jg⟩|g|^2 = \langle e, Jg\rangle, and the iterate is a stationary point. A fixed gain is the other common choice, and it is simpler to analyze.

Example

All examples use the planar 2R arm with L1=L2=1L_1 = L_2 = 1 m, the target (1,1)(1, 1) m and the start θ0=(0.3,1.2)\theta^0 = (0.3, 1.2) rad.

At θ0\theta^0, the hand is at (1.026074,1.293015)(1.026074, 1.293015) m and the error is e=(−0.026074,−0.293015)e = (-0.026074, -0.293015) m. Then JTe=(−0.266942,0.005281)J^{\mathsf T}e = (-0.266942, 0.005281), and the step with the gain of Buss and Kim is (−0.099972,0.001978)(-0.099972, 0.001978) rad.

gain errors (m) at steps 0 to 4 steps to 10−1010^{-10} m final error ratio
α=0.1\alpha = 0.1 0.294, 0.272, 0.257, 0.246, 0.238 569 0.962
α=0.5\alpha = 0.5 0.294, 0.236, 0.200, 0.171, 0.145 105 0.809
α=2/3\alpha = 2/3 0.294, 0.253, 0.216, 0.178, 0.147 77 0.745
Buss and Kim 0.294, 0.237, 0.112, 0.0545, 0.0118 14 0.129
α=0.9\alpha = 0.9 0.294, 0.300, 0.386, 0.506, 0.733 none

All runs that converge end at (0,π/2)(0, \pi/2), and their errors never increase. At the solution, JTJJ^{\mathsf T}J has the eigenvalues σ12=2.618\sigma_1^2 = 2.618 and σ22=0.382\sigma_2^2 = 0.382. With a fixed gain, the error ratio tends to max⁡i∣1−ασi2∣\max_i|1 - \alpha\sigma_i^2|, which gives 0.962, 0.809 and 0.745 for the first three gains, as the table shows. The gain 2/3=2/(σ12+σ22)2/3 = 2/(\sigma_1^2 + \sigma_2^2) makes this ratio as small as possible. With α=0.9>2/σ12=0.764\alpha = 0.9 > 2/\sigma_1^2 = 0.764, the solution repels the iterates, and the error swings up and down for as long as the loop runs. For the target (2.5,0)(2.5, 0) m, out of reach, both the gain 0.10.1 and the gain of Buss and Kim stretch the arm along the x axis, with the hand at (2,0)(2, 0) m, where JTe=0J^{\mathsf T}e = 0 and the error stays at 0.5 m.

Correctness

The proof page proves these facts for a position task, e=xd−f(θ)e = x_d - f(\theta), with E(θ)=12∣e∣2E(\theta) = \tfrac12|e|^2.

  • Gradient. ∇E(θ)=−J(θ)Te\nabla E(\theta) = -J(\theta)^{\mathsf T}e, so L6 is the direction of steepest descent of EE.
  • Descent. If JTe≠0J^{\mathsf T}e \ne 0, then EE decreases along the step for every small enough α>0\alpha > 0.
  • The gain of Buss and Kim is positive and minimizes ∣e−αJJTe∣|e - \alpha JJ^{\mathsf T}e| over α\alpha.
  • Global result for a fixed gain. For an arm with revolute joints, ∇E\nabla E is Lipschitz with some constant Λ\Lambda on all of joint space, and for 0<α<2/Λ0 < \alpha < 2/\Lambda the errors never increase and JTe→0J^{\mathsf T}e \to 0. So every limit point of the iterates is a stationary point of EE.
  • Local rate. At a solution θ∗\theta^* with m=nm = n and J(θ∗)J(\theta^*) invertible, and a fixed gain 0<α<2/σmax⁡20 < \alpha < 2/\sigma_{\max}^2, the iteration converges to θ∗\theta^* from every start close enough, with the error ratio tending to at most max⁡i∣1−ασi2∣<1\max_i|1 - \alpha\sigma_i^2| < 1.

A stationary point need not be a solution. The stretched arm aimed at a target out of reach is one, and an arm with a local minimum of the distance to the target is another. There is no guarantee that the iteration finds a solution when one exists.

Proof: The Jacobian transpose step is a descent step, the iteration reaches a stationary point for a small fixed gain, and it converges linearly near a regular solutionfull

We use the notation of the page, in exact arithmetic. The task is a position task f:Rn→Rmf : \mathbb R^n \to \mathbb R^m, twice continuously differentiable, with e(θ)=xd−f(θ)e(\theta) = x_d - f(\theta), J=DfJ = Df and E(θ)=12∣e(θ)∣2E(\theta) = \tfrac12|e(\theta)|^2.

Claim 1 (gradient). ∇E(θ)=−J(θ)Te(θ)\nabla E(\theta) = -J(\theta)^{\mathsf T}e(\theta).

Proof. DE(θ)h=eTDe(θ)h=−eTJhDE(\theta)h = e^{\mathsf T}De(\theta)h = -e^{\mathsf T}Jh for every hh, so the gradient is −JTe-J^{\mathsf T}e. □\square

Claim 2 (descent). If g=JTe≠0g = J^{\mathsf T}e \ne 0, there is αˉ>0\bar\alpha > 0 with E(θ+αg)<E(θ)E(\theta + \alpha g) < E(\theta) for all 0<α≤αˉ0 < \alpha \le \bar\alpha.

Proof. ddαE(θ+αg)\frac{d}{d\alpha}E(\theta + \alpha g) at α=0\alpha = 0 is ∇ETg=−∣g∣2<0\nabla E^{\mathsf T}g = -|g|^2 < 0, and the derivative is continuous. □\square

Claim 3 (the gain of Buss and Kim). Let g=JTe≠0g = J^{\mathsf T}e \ne 0. Then Jg≠0Jg \ne 0, the gain αB=⟨e,Jg⟩/∣Jg∣2\alpha_B = \langle e, Jg\rangle/|Jg|^2 is positive, and it is the unique minimizer of r(α)=∣e−αJg∣2r(\alpha) = |e - \alpha Jg|^2.

Proof. ⟨e,Jg⟩=⟨JTe,g⟩=∣g∣2>0\langle e, Jg\rangle = \langle J^{\mathsf T}e, g\rangle = |g|^2 > 0, so Jg≠0Jg \ne 0 and αB>0\alpha_B > 0. rr is a quadratic in α\alpha with the positive leading coefficient ∣Jg∣2|Jg|^2, and r′(α)=−2⟨e,Jg⟩+2α∣Jg∣2r'(\alpha) = -2\langle e, Jg\rangle + 2\alpha|Jg|^2 is zero exactly at αB\alpha_B. □\square

So the gain of L7 is defined whenever the step is not zero, and the code returns the zero step when Jg=0Jg = 0, which by the same identity means g=0g = 0.

A global result for a fixed gain

Claim 4. Suppose ∇E\nabla E is Lipschitz with constant Λ\Lambda on Rn\mathbb R^n, and let 0<α<2/Λ0 < \alpha < 2/\Lambda. Then the iterates θk+1=θk+αJTe\theta^{k+1} = \theta^k + \alpha J^{\mathsf T}e satisfy E(θk+1)≤E(θk)−α(1−αΛ/2)∣∇E(θk)∣2E(\theta^{k+1}) \le E(\theta^k) - \alpha(1 - \alpha\Lambda/2)|\nabla E(\theta^k)|^2. So E(θk)E(\theta^k) never increases, ∑k∣∇E(θk)∣2<∞\sum_k|\nabla E(\theta^k)|^2 < \infty, JTe→0J^{\mathsf T}e \to 0, and every limit point of the iterates is a stationary point of EE.

Proof. With d=−α∇E(θk)d = -\alpha\nabla E(\theta^k), the standard bound for a function with a Lipschitz gradient gives E(θk+d)≤E(θk)+∇E(θk)Td+Λ2∣d∣2E(\theta^k + d) \le E(\theta^k) + \nabla E(\theta^k)^{\mathsf T}d + \tfrac{\Lambda}{2}|d|^2. By Claim 1 the step is dd, and substituting gives the inequality. Since 1−αΛ/2>01 - \alpha\Lambda/2 > 0, EE decreases at each step, and summing over kk gives α(1−αΛ/2)∑k∣∇E(θk)∣2≤E(θ0)\alpha(1 - \alpha\Lambda/2)\sum_k|\nabla E(\theta^k)|^2 \le E(\theta^0). So ∇E(θk)→0\nabla E(\theta^k) \to 0, and by continuity ∇E=0\nabla E = 0 at every limit point. □\square

The hypothesis holds for revolute arms. If all joints are revolute, ff is 2π2\pi-periodic in each θi\theta_i. Then the Hessian of EE is continuous and periodic, so it is bounded on Rn\mathbb R^n by its maximum over one period box, and ∇E\nabla E is Lipschitz with that bound as Λ\Lambda.

What the result does not give. A stationary point need not be a solution. For the 2R arm with the target (2.5,0)(2.5, 0), the stretched pose θ=(0,0)\theta = (0, 0) has J=[[0,0],[2,1]]J = [[0, 0], [2, 1]] and e=(0.5,0)e = (0.5, 0), so JTe=0J^{\mathsf T}e = 0 with ∣e∣=0.5|e| = 0.5.

Local rate

Claim 5. Let m=nm = n, let θ∗\theta^* be a solution with J(θ∗)J(\theta^*) invertible and singular values σi\sigma_i, and let 0<α<2/σmax⁡20 < \alpha < 2/\sigma_{\max}^2. Let ρ=max⁡i∣1−ασi2∣<1\rho = \max_i|1 - \alpha\sigma_i^2| < 1. For every ρ′∈(ρ,1)\rho' \in (\rho, 1) there is r>0r > 0 such that every start with ∣θ0−θ∗∣≤r|\theta^0 - \theta^*| \le r gives ∣θk+1−θ∗∣≤ρ′∣θk−θ∗∣|\theta^{k+1} - \theta^*| \le \rho'|\theta^k - \theta^*| for all kk.

Proof. The iteration is θk+1=Φ(θk)\theta^{k+1} = \Phi(\theta^k) with Φ(θ)=θ+αJ(θ)T(xd−f(θ))\Phi(\theta) = \theta + \alpha J(\theta)^{\mathsf T}(x_d - f(\theta)), which is continuously differentiable, and Φ(θ∗)=θ∗\Phi(\theta^*) = \theta^*. At θ∗\theta^*, the factor xd−fx_d - f is zero, so DΦ(θ∗)=I−αJTJD\Phi(\theta^*) = I - \alpha J^{\mathsf T}J with J=J(θ∗)J = J(\theta^*). This matrix is symmetric with the eigenvalues 1−ασi21 - \alpha\sigma_i^2, so its norm is ρ\rho. Since σi>0\sigma_i > 0 and ασi2<2\alpha\sigma_i^2 < 2, every eigenvalue is in (−1,1)(-1, 1) and ρ<1\rho < 1. By continuity there is r>0r > 0 with ∣DΦ(θ)∣≤ρ′|D\Phi(\theta)| \le \rho' on the closed ball of radius rr around θ∗\theta^*. The mean value inequality on this convex ball gives ∣Φ(θ)−θ∗∣≤ρ′∣θ−θ∗∣|\Phi(\theta) - \theta^*| \le \rho'|\theta - \theta^*|, so every iterate stays in the ball and the distance falls by the factor ρ′\rho'. □\square

For the 2R arm at (0,π/2)(0, \pi/2), JTJ=[[2,1],[1,1]]J^{\mathsf T}J = [[2, 1], [1, 1]] has the eigenvalues (3±5)/2(3 \pm \sqrt 5)/2. The claim gives ρ=0.962\rho = 0.962, 0.8090.809 and 0.7450.745 for α=0.1\alpha = 0.1, 0.50.5 and 2/32/3, and the tests check that the measured error ratios approach these values. The value α=2/(σ12+σ22)\alpha = 2/(\sigma_1^2 + \sigma_2^2) makes ρ\rho smallest, with ρ=(σ12−σ22)/(σ12+σ22)\rho = (\sigma_1^2 - \sigma_2^2)/(\sigma_1^2 + \sigma_2^2). For α>2/σmax⁡2\alpha > 2/\sigma_{\max}^2 the claim does not apply; the page shows by measurement that the iteration then fails on the 2R arm. The gain of Buss and Kim changes from step to step, and we prove no rate for it.

Complexity

One step forms JTeJ^{\mathsf T}e in Θ(mn)\Theta(mn) and JgJg for the gain in Θ(mn)\Theta(mn), in the real RAM, plus the cost of ff and JJ, which is Θ(mn)\Theta(mn) for a serial chain. So kk steps cost Θ(kmn)\Theta(kmn), with no Θ(m3)\Theta(m^3) solve as in damped least squares. The extra space is the Jacobian, Θ(mn)\Theta(mn). The number of steps kk is not bounded in general. Near a regular solution with a fixed gain, the number of steps to reach ε\varepsilon grows like log⁡(1/ε)/log⁡(1/ρ)\log(1/\varepsilon)/\log(1/\rho) with ρ=max⁡i∣1−ασi2∣\rho = \max_i|1 - \alpha\sigma_i^2|, which becomes large when σmin⁡≪σmax⁡\sigma_{\min} \ll \sigma_{\max}.

Properties

  • No inverse. No solve and no factorization, only products with JJ and JTJ^{\mathsf T}.
  • Always defined. The step exists at singularities and for targets out of reach.
  • Linear convergence. The rate depends on the condition of JTJJ^{\mathsf T}J at the solution, and it is slow for an ill conditioned arm.
  • Not invariant. The step changes with the units and the scaling of the task coordinates and of the joints, unlike the Newton step.
  • Stationary points. The method can stop at any point with JTe=0J^{\mathsf T}e = 0, which includes singular configurations with ee orthogonal to the range of JJ.
  • Redundant arms. For m<nm < n, the step lies in the row space of JJ, so it has no null space component.

Variants and improvements

  • Gain choice. A fixed gain, the gain of Buss and Kim, or a backtracking line search on EE; the line search keeps the global descent property with no bound Λ\Lambda to know in advance.
  • A gain matrix. A step KJTeKJ^{\mathsf T}e with a symmetric positive definite KK keeps the descent property and can scale the joints.
  • Continuous time. The control law θ˙=KJTe\dot\theta = KJ^{\mathsf T}e makes 12∣e∣2\tfrac12|e|^2 a Lyapunov function for a fixed target, since its derivative is −eTJKJTe≤0-e^{\mathsf T}JKJ^{\mathsf T}e \le 0; this is the closed loop form in textbooks such as Siciliano et al.
  • Damped least squares and the pseudoinverse. Replacing JTJ^{\mathsf T} with JT(JJT+λ2I)−1J^{\mathsf T}(JJ^{\mathsf T} + \lambda^2I)^{-1} gives damped least squares, and with λ=0\lambda = 0 the pseudoinverse method; both converge faster per step.
  • Joint by joint. Cyclic coordinate descent also descends on the error, but it minimizes over one joint at a time exactly.

When to use it

Use the Jacobian transpose method when each step must be very cheap, for example for a long chain in an animation, or when a simple and robust closed loop law is wanted and the speed of convergence matters little. When a solution must be found fast and accurately, use damped least squares or Newton-Raphson inverse kinematics.

Pitfalls

  • Gain too large. A fixed gain above 2/σmax⁡22/\sigma_{\max}^2 near the solution makes the iteration swing and fail, as in the example.
  • Gain too small. A small fixed gain is safe but can need hundreds of steps for a 2R arm.
  • Scaling. Mixing positions in m and orientations in rad in ee changes the direction of the step; scale the task coordinates on purpose.
  • Stopping at the wrong place. A small step does not mean a small error; check ∣e∣|e|, not ∣Δθ∣|\Delta\theta|, before you accept the result.
  • Moving targets. In closed loop use, a fixed gain gives a tracking lag that grows with the speed of the target.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

jacobian_transpose_method.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/jacobian-transpose-method/. The authors are listed in prov/NPCR98.jsonl.
"""Inverse kinematics by Jacobian transpose steps.

- transpose_step(j, e, alpha) returns dq = alpha J^T e. With alpha=None
  the gain is the one of Buss and Kim, alpha = <e, J J^T e> / |J J^T e|^2, the
  gain that makes the linear prediction J dq closest to e.
- ik_transpose(fk, jac, x_d, q0, alpha, ...) is the iteration of the page.
  It returns (q, history, ok): the last joint vector, the error norms before
  each step, and whether the tolerance was met.
- planar_fk and planar_jacobian give a planar arm with n revolute joints,
  used by the tests and the example of the page.

Matrices are lists of rows. The module needs only the standard library.
"""

import math


def matvec(a, x):
    return [sum(r * v for r, v in zip(row, x)) for row in a]


def transpose(a):
    return [list(col) for col in zip(*a)]


def dot(x, y):
    return sum(a * b for a, b in zip(x, y))


def norm(x):
    return math.sqrt(dot(x, x))


def transpose_step(j, e, alpha=None):
    """Return alpha J^T e, with the gain of Buss and Kim when alpha is None."""
    g = matvec(transpose(j), e)  # @line L6
    if alpha is None:
        jg = matvec(j, g)
        den = dot(jg, jg)
        if den == 0.0:
            return [0.0] * len(g)  # J^T e = 0: a stationary point
        alpha = dot(e, jg) / den  # @line L7
    return [alpha * x for x in g]


def ik_transpose(fk, jac, x_d, q0, alpha=None, eps=1e-10, max_iter=5000):
    """Return (q, history, ok) for the task fk(q) = x_d by Jacobian transpose steps."""
    q = [float(x) for x in q0]  # @line L1
    history = []
    for _ in range(max_iter):  # @line L2
        e = [a - b for a, b in zip(x_d, fk(q))]  # @line L3
        history.append(norm(e))
        if history[-1] <= eps:  # @line L4
            return q, history, True
        j = jac(q)  # @line L5
        dq = transpose_step(j, e, alpha)
        q = [x + d for x, d in zip(q, dq)]  # @line L8
    return q, history, False  # @line L9


def planar_fk(lengths, q):
    """Return the hand position of a planar arm with the given link lengths."""
    x = y = a = 0.0
    for length, t in zip(lengths, q):
        a += t
        x += length * math.cos(a)
        y += length * math.sin(a)
    return [x, y]


def planar_jacobian(lengths, q):
    """Return the 2 by n Jacobian of planar_fk."""
    angles = []
    a = 0.0
    for t in q:
        a += t
        angles.append(a)
    n = len(q)
    row_x = [-sum(lengths[k] * math.sin(angles[k]) for k in range(i, n)) for i in range(n)]
    row_y = [sum(lengths[k] * math.cos(angles[k]) for k in range(i, n)) for i in range(n)]
    return [row_x, row_y]
test_jacobian_transpose_method.py 159 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/jacobian-transpose-method/. The authors are listed in prov/NPCR98.jsonl.
"""Tests of the Jacobian transpose method: the step against a finite
difference gradient of the squared error, the gain of Buss and Kim against a grid
search, descent for small gains, the example of the page, the local rate,
the 2R arm against the closed form solutions, and a target out of reach."""

import math
import random

from jacobian_transpose_method import dot, ik_transpose, matvec, norm, planar_fk, planar_jacobian, transpose, transpose_step

L2 = [1.0, 1.0]


def fk2(q):
    return planar_fk(L2, q)


def jac2(q):
    return planar_jacobian(L2, q)


def half_sq_error(lengths, x_d, q):
    e = [a - b for a, b in zip(x_d, planar_fk(lengths, q))]
    return 0.5 * dot(e, e)


def maxdiff(a, b):
    return max(abs(x - y) for x, y in zip(a, b))


def test_planar_jacobian_and_check_values():
    rng = random.Random(1)
    lengths = [1.0, 0.8, 0.5]
    h = 1e-6
    for _ in range(10):
        q = [rng.uniform(-math.pi, math.pi) for _ in range(3)]
        j = planar_jacobian(lengths, q)
        for i in range(3):
            qp, qm = list(q), list(q)
            qp[i] += h
            qm[i] -= h
            col = [(a - b) / (2.0 * h) for a, b in zip(planar_fk(lengths, qp), planar_fk(lengths, qm))]
            assert maxdiff(col, [j[0][i], j[1][i]]) < 1e-6
    j = jac2([0.0, math.pi / 2.0])
    assert maxdiff(j[0] + j[1], [-1.0, -1.0, 1.0, 0.0]) < 1e-15
    assert maxdiff(fk2([0.0, math.pi / 2.0]), [1.0, 1.0]) < 1e-15


def test_step_is_the_negative_gradient():
    rng = random.Random(2)
    lengths = [1.0, 0.8, 0.5]
    h = 1e-6
    for _ in range(20):
        q = [rng.uniform(-math.pi, math.pi) for _ in range(3)]
        x_d = [rng.uniform(-2.0, 2.0), rng.uniform(-2.0, 2.0)]
        e = [a - b for a, b in zip(x_d, planar_fk(lengths, q))]
        g = transpose_step(planar_jacobian(lengths, q), e, 1.0)
        for i in range(3):
            qp, qm = list(q), list(q)
            qp[i] += h
            qm[i] -= h
            num = (half_sq_error(lengths, x_d, qp) - half_sq_error(lengths, x_d, qm)) / (2.0 * h)
            assert abs(g[i] + num) < 1e-6


def test_buss_gain_against_grid_search():
    rng = random.Random(3)
    for m, n in ((2, 2), (2, 4), (3, 3), (6, 7)):
        for _ in range(10):
            j = [[rng.uniform(-1.0, 1.0) for _ in range(n)] for _ in range(m)]
            e = [rng.uniform(-1.0, 1.0) for _ in range(m)]
            dq = transpose_step(j, e)
            g = matvec(transpose(j), e)
            alpha = dot(dq, g) / dot(g, g)
            assert alpha > 0.0

            def resid(a):
                return norm([x - a * y for x, y in zip(e, matvec(j, g))])

            best = min(resid(alpha * k / 1000.0) for k in range(0, 3001))
            assert resid(alpha) <= best + 1e-12


def test_small_gain_decreases_the_error():
    rng = random.Random(4)
    lengths = [1.0, 0.8, 0.5, 0.3]
    for _ in range(50):
        q = [rng.uniform(-math.pi, math.pi) for _ in range(4)]
        x_d = [rng.uniform(-2.5, 2.5), rng.uniform(-2.5, 2.5)]
        e = [a - b for a, b in zip(x_d, planar_fk(lengths, q))]
        dq = transpose_step(planar_jacobian(lengths, q), e, 1e-3)
        q2 = [a + b for a, b in zip(q, dq)]
        assert half_sq_error(lengths, x_d, q2) < half_sq_error(lengths, x_d, q)


def test_example_of_the_page():
    q0 = [0.3, 1.2]
    e = [1.0 - a for a in fk2(q0)]
    g = matvec(transpose(jac2(q0)), e)
    assert maxdiff(g, [-0.266942, 0.005281]) < 1e-6
    assert maxdiff(transpose_step(jac2(q0), e), [-0.099972, 0.001978]) < 1e-6
    counts = {}
    for alpha in (0.1, 0.5, 2.0 / 3.0, None):
        q, hist, ok = ik_transpose(fk2, jac2, [1.0, 1.0], q0, alpha)
        assert ok and maxdiff(q, [0.0, math.pi / 2.0]) < 1e-9
        assert all(b <= a for a, b in zip(hist, hist[1:]))
        counts[alpha] = len(hist)
    assert counts == {0.1: 570, 0.5: 106, 2.0 / 3.0: 78, None: 15}
    # A gain above 2 / sigma_max^2 = 0.764 does not converge.
    q, hist, ok = ik_transpose(fk2, jac2, [1.0, 1.0], q0, 0.9, max_iter=2000)
    assert not ok and min(hist[1000:]) > 0.1


def test_local_rate():
    # Near (0, pi/2) the error ratio tends to max |1 - alpha s_i^2| with s_i^2 = (3 +- sqrt 5) / 2.
    s2 = [(3.0 + math.sqrt(5.0)) / 2.0, (3.0 - math.sqrt(5.0)) / 2.0]
    for alpha in (0.1, 0.5, 2.0 / 3.0):
        _, hist, ok = ik_transpose(fk2, jac2, [1.0, 1.0], [0.3, 1.2], alpha)
        rate = max(abs(1.0 - alpha * s) for s in s2)
        assert ok and abs(hist[-1] / hist[-2] - rate) < 1e-3


def closed_form_2r(x, y):
    c2 = (x * x + y * y - 2.0) / 2.0
    out = []
    for s in (1.0, -1.0):
        q2 = s * math.acos(c2)
        out.append((math.atan2(y, x) - math.atan2(math.sin(q2), 1.0 + math.cos(q2)), q2))
    return out


def test_2r_against_closed_form():
    rng = random.Random(5)
    done = 0
    for _ in range(40):
        r = rng.uniform(0.3, 1.9)
        a = rng.uniform(-math.pi, math.pi)
        x_d = [r * math.cos(a), r * math.sin(a)]
        q0 = [rng.uniform(-math.pi, math.pi), rng.uniform(-math.pi, math.pi)]
        q, hist, ok = ik_transpose(fk2, jac2, x_d, q0, None, max_iter=3000)
        if not ok:
            continue
        done += 1
        assert maxdiff(fk2(q), x_d) < 1e-9
        assert min(max(abs(math.remainder(u - v, 2.0 * math.pi)) for u, v in zip(q, s)) for s in closed_form_2r(*x_d)) < 1e-8
    assert done >= 36


def test_out_of_reach():
    # The arm stretches toward the target: a stationary point with J^T e = 0 and |e| = 0.5.
    for alpha in (0.1, None):
        q, hist, ok = ik_transpose(fk2, jac2, [2.5, 0.0], [0.2, 0.5], alpha, max_iter=3000)
        assert not ok
        assert abs(hist[-1] - 0.5) < 1e-12
        assert maxdiff(fk2(q), [2.0, 0.0]) < 1e-12
        e = [2.5 - fk2(q)[0], -fk2(q)[1]]
        assert norm(matvec(transpose(jac2(q)), e)) < 1e-12

History

Wolovich and Elliott (1984) and Balestrino, De Maria and Sciavicco (1984) proposed to move the joints along JTeJ^{\mathsf T}e as an inverse kinematics method that needs no inverse of the Jacobian. The method is the gradient method of optimization applied to the squared task error. It became popular in computer animation, and the survey of Buss (2004) compared it with the pseudoinverse and damped least squares methods. Buss and Kim (2005) gave the gain of line L7 together with their selectively damped least squares method.

References

  1. William A. Wolovich and H. Elliott, A computational technique for inverse kinematics, Proceedings of the 23rd IEEE Conference on Decision and Control, 1359-1363, 1984, the iteration with the transpose of the Jacobian
  2. A. Balestrino, G. De Maria and L. Sciavicco, Robust control of robotic manipulators, IFAC Proceedings Volumes (9th IFAC World Congress), 1984, the use of the Jacobian transpose for inverse kinematics
  3. Samuel R. Buss, Introduction to Inverse Kinematics with Jacobian Transpose, Pseudoinverse and Damped Least Squares methods, unpublished survey, University of California, San Diego, 2004, Section 3 (the Jacobian transpose method)
  4. Bruno Siciliano, Lorenzo Sciavicco, Luigi Villani and Giuseppe Oriolo, Robotics: Modelling, Planning and Control, Springer, 2009, Section 3.7 (inverse kinematics algorithms)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example