// Fibonacci difference table showing alternating left-edge magnitudes.
// Compile: typst compile fibonacci_diff.typ fibonacci_diff.png --ppi 500 --root ..
#import "../_diagram-style.typ": *
#set page(width: 620pt, height: 350pt, margin: 12pt)
#set text(font: base-font, size: label-size)

#let pos(n) = text(fill: green.text, weight: "bold")[#n]
#let neg-text(n) = text(fill: red.text, weight: "bold")[#n]
#let edge-text(n) = text(fill: blue.text, weight: "bold")[#n]

#align(center)[
  #grid(
    columns: (auto,) * 9,
    column-gutter: 2pt,
    row-gutter: 5pt,
    // Row 0: Fibonacci sequence
    box(inset: (x: 5pt, y: 2pt))[#edge-text[0]],
    box(inset: (x: 5pt, y: 2pt))[1],
    box(inset: (x: 5pt, y: 2pt))[1],
    box(inset: (x: 5pt, y: 2pt))[2],
    box(inset: (x: 5pt, y: 2pt))[3],
    box(inset: (x: 5pt, y: 2pt))[5],
    box(inset: (x: 5pt, y: 2pt))[8],
    box(inset: (x: 5pt, y: 2pt))[13],
    box(inset: (x: 5pt, y: 2pt))[21],
    // Row 1
    [],
    box(inset: (x: 5pt, y: 2pt))[#edge-text[1]],
    box(inset: (x: 5pt, y: 2pt))[0],
    box(inset: (x: 5pt, y: 2pt))[1],
    box(inset: (x: 5pt, y: 2pt))[1],
    box(inset: (x: 5pt, y: 2pt))[2],
    box(inset: (x: 5pt, y: 2pt))[3],
    box(inset: (x: 5pt, y: 2pt))[5],
    box(inset: (x: 5pt, y: 2pt))[8],
    // Row 2
    [], [],
    box(inset: (x: 5pt, y: 2pt))[#edge-text[#neg-text[$-$1]]],
    box(inset: (x: 5pt, y: 2pt))[1],
    box(inset: (x: 5pt, y: 2pt))[0],
    box(inset: (x: 5pt, y: 2pt))[1],
    box(inset: (x: 5pt, y: 2pt))[1],
    box(inset: (x: 5pt, y: 2pt))[2],
    box(inset: (x: 5pt, y: 2pt))[3],
    // Row 3
    [], [], [],
    box(inset: (x: 5pt, y: 2pt))[#edge-text[2]],
    box(inset: (x: 5pt, y: 2pt))[#neg-text[$-$1]],
    box(inset: (x: 5pt, y: 2pt))[1],
    box(inset: (x: 5pt, y: 2pt))[0],
    box(inset: (x: 5pt, y: 2pt))[1],
    box(inset: (x: 5pt, y: 2pt))[1],
    // Row 4
    [], [], [], [],
    box(inset: (x: 5pt, y: 2pt))[#edge-text[#neg-text[$-$3]]],
    box(inset: (x: 5pt, y: 2pt))[2],
    box(inset: (x: 5pt, y: 2pt))[#neg-text[$-$1]],
    box(inset: (x: 5pt, y: 2pt))[1],
    box(inset: (x: 5pt, y: 2pt))[0],
    // Row 5
    [], [], [], [], [],
    box(inset: (x: 5pt, y: 2pt))[#edge-text[5]],
    box(inset: (x: 5pt, y: 2pt))[#neg-text[$-$3]],
    box(inset: (x: 5pt, y: 2pt))[2],
    box(inset: (x: 5pt, y: 2pt))[#neg-text[$-$1]],
    // Row 6
    [], [], [], [], [], [],
    box(inset: (x: 5pt, y: 2pt))[#edge-text[#neg-text[$-$8]]],
    box(inset: (x: 5pt, y: 2pt))[5],
    box(inset: (x: 5pt, y: 2pt))[#neg-text[$-$3]],
    // Row 7
    [], [], [], [], [], [], [],
    box(inset: (x: 5pt, y: 2pt))[#edge-text[13]],
    box(inset: (x: 5pt, y: 2pt))[#neg-text[$-$8]],
    // Row 8
    [], [], [], [], [], [], [], [],
    box(inset: (x: 5pt, y: 2pt))[#edge-text[#neg-text[$-$21]]],
  )

  #v(10pt)
  #text(min-size, fill: blue.text)[
    Left edge: #edge-text[0], #edge-text[1], #edge-text[$-$1], #edge-text[2], #edge-text[$-$3], #edge-text[5], #edge-text[$-$8], #edge-text[13], #edge-text[$-$21] $= (-1)^(n+1) F_n$
  ]
  #v(4pt)
  #text(min-size, fill: fg2, style: "italic")[
    The signed binomial transform returns the Fibonacci magnitudes with alternating signs.
  ]
]
