Algorithm Search⌘K

Techniqueadvanced

Standard form dyad synthesis

Also called complex number dyad synthesis, Erdman-Sandor dyad method.

Writes each side of a linkage as a dyad of two complex vectors and solves the loop equations for the given positions, linear for up to three positions.

Standard form dyad synthesis designs a planar linkage one side at a time. Each side is a dyad: a grounded link WW and a vector ZZ from its moving pivot to the precision point, both written as complex numbers. If the link turns by βj\beta_j and the coupler by αj\alpha_j between position 1 and position jj, the loop closure gives the standard form W(eiβj−1)+Z(eiαj−1)=δjW(e^{i\beta_j} - 1) + Z(e^{i\alpha_j} - 1) = \delta_j, with δj=Pj−P1\delta_j = P_j - P_1. For two or three positions the designer picks the free rotations and the standard form is a linear system for WW and ZZ; for four and five positions it leads to Burmester theory.

In plain words

A four-bar is two arms joined by a middle piece. Each arm is fixed to the ground at one end and turns about that pivot. The middle piece carries the point or the part that must move.

This method looks at one arm and the line from its end to the moving point. It writes both as arrows, with a length and a direction. When the linkage moves to a new position, each arrow turns by some angle, and the arrows must still end at the new point. That gives one equation per new position.

If you choose how far the arm turns, the equations become simple and you can solve them exactly by hand. You then repeat the work for the other arm and join the two arms with the middle piece.

When it applies

  • The task is motion generation, path generation with prescribed timing, or function generation, for a planar linkage made of dyads (four-bars, slider-cranks, and the loops of six-bars).
  • The number of positions is two or three, so that the system is linear after the free choices. With four or five positions the same equations are still the right model, but they are nonlinear.
  • The rotations that the task does not give are free choices of the designer. In motion generation the coupler rotations αj\alpha_j are given and the link rotations βj\beta_j are free; in path generation with timing the βj\beta_j of the input are given and the αj\alpha_j are free.

The template

The input is the three precision points P1,P2,P3P_1, P_2, P_3, the coupler rotations α2,α3\alpha_2, \alpha_3, and the link rotations β2,β3\beta_2, \beta_3, of which some are free choices.

  1. STANDARD-FORM-DYAD(P1, P2, P3, α2, α3, β2, β3)
  2. 1for j ∈ {2, 3}: Bj ← e^{iβj} − 1; Aj ← e^{iαj} − 1; δj ← Pj − P1
  3. 2D ← B2·A3 − B3·A2
  4. 3if D = 0 then return "singular: change the free choices"
  5. 4W ← (δ2·A3 − δ3·A2)/D
  6. 5Z ← (B2·δ3 − B3·δ2)/D
  7. 6G ← P1 − W − Z
  8. 7return the fixed pivot G, the link W and the vector Z

Line L1 forms the coefficients. Lines L2, L4 and L5 are Cramer's rule for the 2×22 \times 2 complex system of the standard form, and L3 rejects the singular case. Line L6 places the fixed pivot so that the dyad ends at P1P_1 in position 1. The moving pivot in position jj is G+WeiβjG + W e^{i\beta_j}, and ∣W∣|W| is the length of the grounded link. Run the template twice, with two different sets of free choices but the same αj\alpha_j, to get two dyads. They share the coupler, so together they are a four-bar with ground GAGBG_A G_B, links ∣WA∣|W_A| and ∣WB∣|W_B|, and coupler ∣ZA−ZB∣|Z_A - Z_B|.

Derivation from the loop closure

Put the fixed pivot at GG. In position 1 the dyad reaches the precision point by the link and then the coupler-side vector:

G+W+Z=P1.G + W + Z = P_1.

In position jj the grounded link has turned about GG by βj\beta_j, which multiplies its vector by eiβje^{i\beta_j}, and the coupler has turned by αj\alpha_j, which multiplies every vector fixed in the coupler by eiαje^{i\alpha_j}. The lengths stay the same because the links are rigid. So

G+Weiβj+Zeiαj=Pj.G + W e^{i\beta_j} + Z e^{i\alpha_j} = P_j.

Subtract the equation of position 1. The unknown pivot GG cancels, and

W(eiβj−1)+Z(eiαj−1)=Pj−P1=δj.W(e^{i\beta_j} - 1) + Z(e^{i\alpha_j} - 1) = P_j - P_1 = \delta_j.

This is the standard form. It is the vector loop closure of the loop formed by the dyad in position 1, the dyad in position jj, and the displacement δj\delta_j. Conversely, if WW and ZZ satisfy the standard form for each jj, then G=P1−W−ZG = P_1 - W - Z gives a dyad that reaches every PjP_j with the coupler at the rotation αj\alpha_j, because adding the equation of position 1 back gives the equation of position jj. ■\blacksquare

The count of free choices

The unknowns of one dyad for nn positions are WW and ZZ (four reals) and the n−1n - 1 unknown rotations, one per position after the first (the βj\beta_j in motion generation). Each position after the first gives one complex equation, so two real equations. The number of free real choices is

4+(n−1)−2(n−1)=5−n,4 + (n - 1) - 2(n - 1) = 5 - n,

which is 3, 2, 1, 0 for n=2,3,4,5n = 2, 3, 4, 5. For two positions the designer picks β2\beta_2 and ZZ and solves one complex equation for WW. For three positions the designer picks β2\beta_2 and β3\beta_3, and the template solves for WW and ZZ.

When the system is singular

The system has the matrix (B2A2B3A3)\begin{pmatrix} B_2 & A_2 \\ B_3 & A_3 \end{pmatrix} and determinant D=B2A3−B3A2D = B_2A_3 - B_3A_2. It has a unique solution exactly when D≠0D \ne 0. With eiθ−1=2isin⁡(θ/2) eiθ/2e^{i\theta} - 1 = 2i\sin(\theta/2)\,e^{i\theta/2},

D=−4(sin⁡β22sin⁡α32 ei(β2+α3)/2−sin⁡β32sin⁡α22 ei(β3+α2)/2),D = -4\left(\sin\tfrac{\beta_2}{2}\sin\tfrac{\alpha_3}{2}\,e^{i(\beta_2 + \alpha_3)/2} - \sin\tfrac{\beta_3}{2}\sin\tfrac{\alpha_2}{2}\,e^{i(\beta_3 + \alpha_2)/2}\right),

so D=0D = 0 exactly when the rows (B2,A2)(B_2, A_2) and (B3,A3)(B_3, A_3) are proportional over the complex numbers. Cases that occur in practice:

  • The link turns with the coupler, β2=α2\beta_2 = \alpha_2 and β3=α3\beta_3 = \alpha_3. Then WW and ZZ enter only as W+ZW + Z, and the dyad is one rigid body turning about GG. It exists only if the three positions are rotations about one point.
  • The link does not turn, β2=β3=0\beta_2 = \beta_3 = 0. Then B2=B3=0B_2 = B_3 = 0 and WW drops out.
  • The coupler does not turn, α2=α3=0\alpha_2 = \alpha_3 = 0, a pure translation. Then ZZ drops out, and the task needs a parallelogram or other special linkage.
  • Positions 2 and 3 have the same rotations, β2=β3\beta_2 = \beta_3 and α2=α3\alpha_2 = \alpha_3.

When D=0D = 0 the system has no solution unless δ3\delta_3 is the same multiple of δ2\delta_2, and then it has a one-parameter family. A DD close to zero gives very long links. The tests check that the template returns no dyad in each case above.

Cost

One three-position dyad is a fixed number of complex operations. With exact data (rotations that are rational points ((1−t2)/(1+t2),2t/(1+t2))((1 - t^2)/(1 + t^2), 2t/(1 + t^2)) of the unit circle and rational points PjP_j), every step is exact in rational arithmetic, which the tests use.

Classic examples

Recovering a known four-bar. Take the four-bar with O2=(0,0)O_2 = (0, 0), O4=(9,0)O_4 = (9, 0), a=15a = 15, b=5b = 5, c=13c = 13 and the midpoint PP of the coupler, at its three exact positions from the kinematic synthesis page: P1=(72/5,97/10)P_1 = (72/5, 97/10), P2=(23/2,12)P_2 = (23/2, 12), P3=(2,27/2)P_3 = (2, 27/2). The coupler turns by α2=−16.26°\alpha_2 = -16.26° and α3=−53.13°\alpha_3 = -53.13°, with

eiα2=2425−725i,eiα3=35−45i.e^{i\alpha_2} = \tfrac{24}{25} - \tfrac{7}{25}i, \qquad e^{i\alpha_3} = \tfrac{3}{5} - \tfrac{4}{5}i.

With the input rotations eiβ2=2425+725ie^{i\beta_2} = \tfrac{24}{25} + \tfrac{7}{25}i and eiβ3=35+45ie^{i\beta_3} = \tfrac{3}{5} + \tfrac{4}{5}i (16.26°16.26° and 53.13°53.13°), the template returns, in exact rational arithmetic,

G=0,W=12+9i,Z=125+710i,G = 0, \qquad W = 12 + 9i, \qquad Z = \tfrac{12}{5} + \tfrac{7}{10}i,

which is the input link O2AO_2A and the half coupler from AA to PP. With the output rotations eiβ2=6365+1665ie^{i\beta_2} = \tfrac{63}{65} + \tfrac{16}{65}i and eiβ3=3365+5665ie^{i\beta_3} = \tfrac{33}{65} + \tfrac{56}{65}i (14.25°14.25° and 59.49°59.49°) it returns G=9G = 9, W=395+525iW = \tfrac{39}{5} + \tfrac{52}{5}i, Z=−125−710iZ = -\tfrac{12}{5} - \tfrac{7}{10}i, the output link O4BO_4B and the half coupler from BB to PP.

A new dyad by a free choice. Keep the same positions and pick β2=10°\beta_2 = 10°, β3=60°\beta_3 = 60°. In floating point, the template gives G=(16.9106,2.2282)G = (16.9106, 2.2282), W=(3.5547,11.4201)W = (3.5547, 11.4201) with ∣W∣=11.9606|W| = 11.9606, and Z=(−6.0653,−3.9483)Z = (-6.0653, -3.9483). The dyad reaches the three points with errors below 4⋅10−154 \cdot 10^{-15}. Joined with the output dyad above, it gives a four-bar with ground 8.21848.2184, links 11.960611.9606 and 1313, and coupler 4.89764.8976, a Grashof linkage whose shortest link is the coupler. Driving the new link through 0°0°, 10°10°, 60°60° in 3000 steps moves the linkage through the three positions on one branch. The transmission angle at the three positions is 168.4°168.4°, 137.9°137.9° and 55.8°55.8°, so position 1 is close to a toggle and the design would be rejected. The free choices must be searched with this check, which is what design programs do.

Two positions. For two positions the designer chooses β2\beta_2 and ZZ. Then W=(δ2−Z(eiα2−1))/(eiβ2−1)W = (\delta_2 - Z(e^{i\alpha_2} - 1))/(e^{i\beta_2} - 1), one complex division. The motion generation page has a two-position example worked with the perpendicular bisector, which is the graphical form of the same equation.

Path generation with prescribed timing. For path generation with timing the βj\beta_j of the input dyad are data and the αj\alpha_j are free. For three points the designer picks α2\alpha_2, α3\alpha_3 and the same template applies with the roles of the choices exchanged. The output dyad then uses the same αj\alpha_j and its own free βj\beta_j.

Function generation. For function generation with three precision points, put the precision point at the moving pivot of the output link in the frame of the input link (a kinematic inversion). The relative rotations play the role of αj\alpha_j and βj\beta_j, and the template gives the coupler and the output link.

Variations

  • Four positions. A third equation WB4+ZA4=δ4W B_4 + Z A_4 = \delta_4 must be consistent with the first two. The condition is that the 3×33 \times 3 complex determinant of the rows (Bj,Aj,δj)(B_j, A_j, \delta_j) vanishes, one complex equation in β2,β3,β4\beta_2, \beta_3, \beta_4. One real choice remains, and the solutions trace the circle-point and center-point curves of Burmester theory.
  • Five positions. Two such determinants must vanish, and the solutions are the at most four Burmester dyads.
  • Prescribed fixed pivot. If the designer fixes GG and not the rotations, the equations are nonlinear in the βj\beta_j. Write W=M1−GW = M_1 - G with M1M_1 unknown and use the inverse motion; for three positions this is again a center-of-circle construction.
  • Slider dyads. A prismatic joint replaces WeiβjW e^{i\beta_j} by W+sjuW + s_j u with a fixed direction uu and unknown slides sjs_j, which gives the slider-crank form of the same method.
  • Six-bars. Each loop of a Watt or Stephenson chain is written as dyads, and the method is applied loop by loop.

Pitfalls

  • Wrong sign of the rotations. All rotations are measured counterclockwise from position 1. A βj\beta_j taken from the wrong end of the link or in the wrong sense gives a dyad that fits other positions.
  • Singular or near singular choices. The cases listed above, and any choice with ∣D∣|D| small, give no dyad or very long links. Check ∣D∣|D| before you accept the result.
  • Each dyad is checked alone. The template proves only that each dyad reaches each position. The four-bar made of two dyads can still have a branch defect, an order defect, or a poor transmission angle, as in the second example. Simulate the four-bar through the positions.
  • Free choices are not neutral. The two free values for three positions move the fixed pivot over the plane. Pick them by a search with the side conditions, not by habit.
  • Floating point and exact data. With rotations given in degrees the computed eiβe^{i\beta} are rounded, and the residuals are of order 10−1510^{-15} times the size of the dyad. With rational points of the unit circle the method is exact.

Code

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.

standard_form_dyad_synthesis.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/standard-form-dyad-synthesis/. The authors are listed in prov/PC7C1B.jsonl.

"""Standard form dyad synthesis for three positions.

A dyad is a grounded link W (from the fixed pivot G to the moving pivot) and a
coupler-side vector Z (from the moving pivot to the precision point). In
position j the link has turned by beta_j and the coupler by alpha_j from
position 1, so

    G + W e^{i beta_j} + Z e^{i alpha_j} = P_j,

and subtracting position 1 gives the standard form

    W (e^{i beta_j} - 1) + Z (e^{i alpha_j} - 1) = delta_j = P_j - P_1.

Rotations are passed as unit numbers u = e^{i angle}, not as angles, so the
same code runs on Python complex numbers and on exact QC numbers (complex
numbers with Fraction parts), with rational points of the unit circle.
"""

import math
from fractions import Fraction


class QC:
    """An exact complex number with Fraction real and imaginary parts."""

    __slots__ = ("re", "im")

    def __init__(self, re, im=0):
        self.re = Fraction(re)
        self.im = Fraction(im)

    @staticmethod
    def of(z):
        return z if isinstance(z, QC) else QC(z)

    def __add__(self, o):
        o = QC.of(o)
        return QC(self.re + o.re, self.im + o.im)

    __radd__ = __add__

    def __sub__(self, o):
        o = QC.of(o)
        return QC(self.re - o.re, self.im - o.im)

    def __rsub__(self, o):
        return QC.of(o) - self

    def __neg__(self):
        return QC(-self.re, -self.im)

    def __mul__(self, o):
        o = QC.of(o)
        return QC(self.re * o.re - self.im * o.im, self.re * o.im + self.im * o.re)

    __rmul__ = __mul__

    def __truediv__(self, o):
        o = QC.of(o)
        n = o.re * o.re + o.im * o.im
        if n == 0:
            raise ZeroDivisionError("division by zero")
        return QC((self.re * o.re + self.im * o.im) / n, (self.im * o.re - self.re * o.im) / n)

    def __eq__(self, o):
        if isinstance(o, (int, Fraction)):
            o = QC(o)
        return isinstance(o, QC) and self.re == o.re and self.im == o.im

    def __hash__(self):
        return hash((self.re, self.im))

    def conj(self):
        return QC(self.re, -self.im)

    def abs2(self):
        return self.re * self.re + self.im * self.im

    def __repr__(self):
        return "QC(%s, %s)" % (self.re, self.im)


def rational_unit(t):
    """The point ((1 - t^2)/(1 + t^2), 2t/(1 + t^2)) of the unit circle, e^{i angle} with tan(angle/2) = t."""
    t = Fraction(t)
    return QC((1 - t * t) / (1 + t * t), 2 * t / (1 + t * t))


def unit(angle):
    """e^{i angle} as a Python complex number."""
    return complex(math.cos(angle), math.sin(angle))


def is_zero(z, tol):
    if isinstance(z, QC):
        return z.re == 0 and z.im == 0
    return abs(z) <= tol


def three_position_dyad(p1, p2, p3, a2, a3, b2, b3, tol=1e-12):
    """Solve the 2 x 2 standard form system for (G, W, Z), or None when it is singular.

    p1, p2, p3 are the precision points; a2, a3 the coupler rotations e^{i alpha_j};
    b2, b3 the link rotations e^{i beta_j}. tol is the singularity test for floats.
    """
    d2, d3 = p2 - p1, p3 - p1
    bb2, aa2 = b2 - 1, a2 - 1  # @line L1
    bb3, aa3 = b3 - 1, a3 - 1
    det = bb2 * aa3 - bb3 * aa2  # @line L2
    scale = 1.0 if isinstance(det, QC) else max(1.0, abs(bb2) * abs(aa3) + abs(bb3) * abs(aa2))
    if is_zero(det, tol * scale):  # @line L3
        return None
    w = (d2 * aa3 - d3 * aa2) / det  # @line L4
    z = (bb2 * d3 - bb3 * d2) / det  # @line L5
    g = p1 - w - z  # @line L6
    return g, w, z  # @line L7


def two_position_dyad(p1, p2, a2, b2, z, tol=1e-12):
    """Two positions: with Z and beta_2 chosen, W = (delta_2 - Z (e^{i alpha_2} - 1)) / (e^{i beta_2} - 1)."""
    bb2 = b2 - 1
    if is_zero(bb2, tol):
        return None
    w = (p2 - p1 - z * (a2 - 1)) / bb2
    return p1 - w - z, w, z


def dyad_point(g, w, z, a, b):
    """The precision point of the dyad when the link has turned by b and the coupler by a."""
    return g + w * b + z * a


def free_choices(n):
    """Free real parameters of one dyad for n positions in motion generation: 5 - n."""
    unknowns = 4 + (n - 1)
    equations = 2 * (n - 1)
    return unknowns - equations


def four_bar(dyad_a, dyad_b):
    """Ground pivots and link lengths of the four-bar made of two dyads that share the coupler.

    Returns (G_A, G_B, |W_A|, |W_B|, coupler length |Z_A - Z_B|) for complex dyads.
    """
    ga, wa, za = dyad_a
    gb, wb, zb = dyad_b
    return ga, gb, abs(wa), abs(wb), abs(za - zb)
test_standard_form_dyad_synthesis.py 205 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/standard-form-dyad-synthesis/. The authors are listed in prov/PC7C1B.jsonl.

import cmath
import math
import random
import unittest
from fractions import Fraction

from standard_form_dyad_synthesis import (
    QC,
    dyad_point,
    four_bar,
    free_choices,
    rational_unit,
    three_position_dyad,
    two_position_dyad,
    unit,
)


def gauss_real(m, r):
    """Oracle: Gaussian elimination with partial pivoting on a real square system; None if singular."""
    n = len(m)
    a = [list(row) + [rhs] for row, rhs in zip(m, r)]
    for col in range(n):
        piv = max(range(col, n), key=lambda i: abs(a[i][col]))
        if abs(a[piv][col]) < 1e-13:
            return None
        a[col], a[piv] = a[piv], a[col]
        for i in range(col + 1, n):
            f = a[i][col] / a[col][col]
            for k in range(col, n + 1):
                a[i][k] -= f * a[col][k]
    x = [0.0] * n
    for i in range(n - 1, -1, -1):
        x[i] = (a[i][n] - sum(a[i][k] * x[k] for k in range(i + 1, n))) / a[i][i]
    return x


def oracle_wz(p1, p2, p3, a2, a3, b2, b3):
    """Oracle: the standard form as four real equations in Wx, Wy, Zx, Zy."""
    rows, rhs = [], []
    for p, a, b in ((p2, a2, b2), (p3, a3, b3)):
        bb, aa, d = b - 1, a - 1, p - p1
        # (bb)(Wx + i Wy) + (aa)(Zx + i Zy) = d, split into real and imaginary parts.
        rows.append([bb.real, -bb.imag, aa.real, -aa.imag])
        rhs.append(d.real)
        rows.append([bb.imag, bb.real, aa.imag, aa.real])
        rhs.append(d.imag)
    x = gauss_real(rows, rhs)
    if x is None:
        return None
    return complex(x[0], x[1]), complex(x[2], x[3])


def rand_rational(rng, lo=-9, hi=9, den=7):
    return Fraction(rng.randint(lo * den, hi * den), rng.randint(1, den))


def rand_qc(rng):
    return QC(rand_rational(rng), rand_rational(rng))


class TestExact(unittest.TestCase):
    def setUp(self):
        # Coupler of the four-bar O2 = (0, 0), O4 = (9, 0), a = 15, b = 5, c = 13; P is the midpoint of AB.
        self.p = [QC(Fraction(72, 5), Fraction(97, 10)), QC(Fraction(23, 2), 12), QC(2, Fraction(27, 2))]
        e3 = [QC(Fraction(24, 25), Fraction(7, 25)), QC(1), QC(Fraction(4, 5), Fraction(-3, 5))]
        self.alpha = [e3[j] / e3[0] for j in range(3)]
        e2 = [QC(Fraction(4, 5), Fraction(3, 5)), QC(Fraction(3, 5), Fraction(4, 5)), QC(0, 1)]
        e4 = [QC(Fraction(3, 5), Fraction(4, 5)), QC(Fraction(5, 13), Fraction(12, 13)), QC(Fraction(-5, 13), Fraction(12, 13))]
        self.beta_a = [e2[j] / e2[0] for j in range(3)]
        self.beta_b = [e4[j] / e4[0] for j in range(3)]

    def solve(self, beta):
        p, al = self.p, self.alpha
        return three_position_dyad(p[0], p[1], p[2], al[1], al[2], beta[1], beta[2])

    def test_recovers_the_two_sides_of_the_four_bar(self):
        g, w, z = self.solve(self.beta_a)
        self.assertEqual((g, w, z), (QC(0), QC(12, 9), QC(Fraction(12, 5), Fraction(7, 10))))
        g, w, z = self.solve(self.beta_b)
        self.assertEqual((g, w, z), (QC(9), QC(Fraction(39, 5), Fraction(52, 5)), QC(Fraction(-12, 5), Fraction(-7, 10))))

    def test_rotations_are_the_stated_values(self):
        self.assertEqual(self.alpha[1], QC(Fraction(24, 25), Fraction(-7, 25)))
        self.assertEqual(self.alpha[2], QC(Fraction(3, 5), Fraction(-4, 5)))
        self.assertEqual(self.beta_a[1], QC(Fraction(24, 25), Fraction(7, 25)))
        self.assertEqual(self.beta_b[2], QC(Fraction(33, 65), Fraction(56, 65)))

    def test_dyads_reach_every_position_exactly(self):
        for beta in (self.beta_a, self.beta_b):
            g, w, z = self.solve(beta)
            for j in range(3):
                self.assertEqual(dyad_point(g, w, z, self.alpha[j], beta[j]), self.p[j])

    def test_random_exact_dyads(self):
        rng = random.Random(1)
        checked = 0
        for _ in range(300):
            g, w, z = rand_qc(rng), rand_qc(rng), rand_qc(rng)
            al = [QC(1)] + [rational_unit(rand_rational(rng, -3, 3, 5)) for _ in range(2)]
            be = [QC(1)] + [rational_unit(rand_rational(rng, -3, 3, 5)) for _ in range(2)]
            p = [dyad_point(g, w, z, al[j], be[j]) for j in range(3)]
            out = three_position_dyad(p[0], p[1], p[2], al[1], al[2], be[1], be[2])
            det = (be[1] - 1) * (al[2] - 1) - (be[2] - 1) * (al[1] - 1)
            if det == 0:
                self.assertIsNone(out)
                continue
            self.assertEqual(out, (g, w, z))
            for j in range(3):
                self.assertEqual(dyad_point(out[0], out[1], out[2], al[j], be[j]), p[j])
            checked += 1
        self.assertGreater(checked, 250)

    def test_four_bar_of_two_exact_dyads(self):
        da, db = self.solve(self.beta_a), self.solve(self.beta_b)
        coupler = (da[2] - db[2]).abs2()
        for j in range(3):
            ma = da[0] + da[1] * self.beta_a[j]
            mb = db[0] + db[1] * self.beta_b[j]
            self.assertEqual((ma - mb).abs2(), coupler)
            self.assertEqual((ma - da[0]).abs2(), Fraction(225))
            self.assertEqual((mb - db[0]).abs2(), Fraction(169))
        self.assertEqual(coupler, Fraction(25))

    def test_exact_singular_cases(self):
        p, al = self.p, self.alpha
        # Link turning with the coupler.
        self.assertIsNone(three_position_dyad(p[0], p[1], p[2], al[1], al[2], al[1], al[2]))
        # Link that does not turn.
        self.assertIsNone(three_position_dyad(p[0], p[1], p[2], al[1], al[2], QC(1), QC(1)))
        # Coupler that does not turn.
        self.assertIsNone(three_position_dyad(p[0], p[1], p[2], QC(1), QC(1), self.beta_a[1], self.beta_a[2]))


class TestFloat(unittest.TestCase):
    def test_random_positions_against_real_oracle(self):
        rng = random.Random(42)
        for _ in range(2000):
            p = [complex(rng.uniform(-10, 10), rng.uniform(-10, 10)) for _ in range(3)]
            al = [1] + [unit(rng.uniform(-math.pi, math.pi)) for _ in range(2)]
            be = [1] + [unit(rng.uniform(-math.pi, math.pi)) for _ in range(2)]
            out = three_position_dyad(p[0], p[1], p[2], al[1], al[2], be[1], be[2])
            ref = oracle_wz(p[0], p[1], p[2], al[1], al[2], be[1], be[2])
            if out is None or ref is None:
                continue
            g, w, z = out
            scale = 1 + abs(w) + abs(z)
            self.assertLess(abs(w - ref[0]), 1e-9 * scale)
            self.assertLess(abs(z - ref[1]), 1e-9 * scale)
            for j in range(3):
                self.assertLess(abs(dyad_point(g, w, z, al[j], be[j]) - p[j]), 1e-12 * scale * 10)

    def test_example_with_chosen_rotations(self):
        p = [14.4 + 9.7j, 11.5 + 12j, 2 + 13.5j]
        th3 = [math.atan2(1.4, 4.8), 0.0, math.atan2(-3, 4)]
        al = [unit(t - th3[0]) for t in th3]
        be = [1, unit(math.radians(10)), unit(math.radians(60))]
        g, w, z = three_position_dyad(p[0], p[1], p[2], al[1], al[2], be[1], be[2])
        self.assertAlmostEqual(g.real, 16.9106, delta=1e-4)
        self.assertAlmostEqual(g.imag, 2.2282, delta=1e-4)
        self.assertAlmostEqual(abs(w), 11.9606, delta=1e-4)
        for j in range(3):
            self.assertLess(abs(dyad_point(g, w, z, al[j], be[j]) - p[j]), 1e-12)
        # The moving pivot lies on a circle about G.
        for j in range(3):
            self.assertAlmostEqual(abs(w * be[j]), abs(w), delta=1e-12)
        ga, gb, la, lb, lc = four_bar((g, w, z), (9 + 0j, 7.8 + 10.4j, -2.4 - 0.7j))
        self.assertAlmostEqual(lb, 13.0, delta=1e-12)
        self.assertAlmostEqual(lc, 4.8976, delta=1e-4)

    def test_float_singular_and_near_singular(self):
        p = [0j, 1 + 1j, 2 - 1j]
        a2, a3 = unit(0.3), unit(0.8)
        self.assertIsNone(three_position_dyad(p[0], p[1], p[2], a2, a3, a2, a3))
        self.assertIsNone(three_position_dyad(p[0], p[1], p[2], 1, 1, unit(0.2), unit(0.5)))
        out = three_position_dyad(p[0], p[1], p[2], a2, a3, unit(0.3 + 1e-3), a3)
        self.assertIsNotNone(out)
        self.assertGreater(abs(out[1]), 100.0)

    def test_two_positions(self):
        rng = random.Random(3)
        for _ in range(500):
            p1, p2, z = (complex(rng.uniform(-5, 5), rng.uniform(-5, 5)) for _ in range(3))
            a2, b2 = unit(rng.uniform(-3, 3)), unit(rng.uniform(0.1, 3))
            g, w, z2 = two_position_dyad(p1, p2, a2, b2, z)
            self.assertLess(abs(dyad_point(g, w, z2, 1, 1) - p1), 1e-12 * (1 + abs(w)))
            self.assertLess(abs(dyad_point(g, w, z2, a2, b2) - p2), 1e-11 * (1 + abs(w)))
        self.assertIsNone(two_position_dyad(0j, 1j, unit(0.5), 1, 1 + 0j))

    def test_free_choice_count(self):
        self.assertEqual([free_choices(n) for n in (2, 3, 4, 5, 6)], [3, 2, 1, 0, -1])

    def test_numbers_from_angles(self):
        self.assertLess(abs(unit(math.atan2(3, 4)) - complex(0.8, 0.6)), 1e-15)
        u = rational_unit(Fraction(1, 2))
        self.assertEqual(u, QC(Fraction(3, 5), Fraction(4, 5)))
        self.assertEqual(u.abs2(), 1)
        self.assertLess(abs(cmath.phase(complex(float(u.re), float(u.im))) - 2 * math.atan(0.5)), 1e-15)


if __name__ == "__main__":
    unittest.main()

Exercises

  1. Show that for three positions the fixed pivot GG found by the template is the center of the circle through the three positions G+WeiβjG + W e^{i\beta_j} of the moving pivot. Why is this the graphical method of the motion generation page?
  2. In the first example, choose β2=−16.26°\beta_2 = -16.26° and β3=−53.13°\beta_3 = -53.13°. Show that D=0D = 0, and explain the result with the first singular case.
  3. Prove that D=0D = 0 when β2=β3\beta_2 = \beta_3 and α2=α3\alpha_2 = \alpha_3, and that the system then has a solution only if δ2=δ3\delta_2 = \delta_3.
  4. Write the condition for four positions as a 3×33 \times 3 determinant, and count its real equations and unknowns.
  5. Derive the standard form of a dyad whose grounded joint is prismatic, and solve it for three positions.

References

  1. Arthur G. Erdman, George N. Sandor and Sridhar Kota, Mechanism Design, Analysis and Synthesis, Volume 1, 4th edition, Prentice Hall, 2001, Chapter 8, Introduction to Kinematic Synthesis, Graphical and Linear Analytical Methods (the standard form of the dyad, the table of free choices, and three-position synthesis by Cramer's rule)
  2. Robert L. Norton, Design of Machinery, 6th edition, McGraw-Hill, 2019, Chapter 5 (two- and three-position motion generation by analytical synthesis with the W and Z dyads, and path generation with prescribed timing)
  3. J. Michael McCarthy and Gim Song Soh, Geometric Design of Linkages, 2nd edition, Springer, 2011, Chapter 5 (pp. 93 to 123), algebraic synthesis of planar chains
  4. Richard S. Hartenberg and Jacques Denavit, Kinematic Synthesis of Linkages, McGraw-Hill, New York, 1964, the chapters on the complex number method for finitely separated positions (general locator)

Where this leads

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 proofH0 no proof
  • add a proof in proofs/ (06.5)
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
  • three classic examples