s LearnSqueak.com

SMALLTALK LAB

Matrix powers: leap through the Fibonacci sequence

Turn a two-number recurrence into a matrix, square it efficiently, and verify exact Fibonacci values with native Smalltalk experiments and a computed state replay.

8 tested checkpointsNative Squeak screenshotsComputed visual replay

Before you begin

Use native Squeak 6.1, a Workspace, and Print it. Arrays here hold four entries in row order: top-left, top-right, bottom-left, bottom-right. Each checkpoint is independent. This lesson extends scalar fast powers to transformations of a two-value state; no matrix package is required.

These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.

01 - Represent one recurrence step

Fibonacci starts F(0)=0 and F(1)=1. Keep state #(F(n+1) F(n)). The next state adds the two entries for its first value and copies the old first value for its second. This makes the recurrence a repeatable transformation.

Independent Workspace expression - select all, then Print it
| state |
state := #(1 0).
5 timesRepeat: [state := {state first + state second. state first}].
state

Expected: #(8 5)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Represent one recurrence step
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: The second entry is F(5), not F(6). Keeping the state convention explicit avoids the most common off-by-one error.

Download this checkpoint

02 - Apply a two-by-two matrix

The matrix #(1 1 1 0), conventionally called Q, performs exactly that transformation. Each output is a row dotted with the input vector. Parentheses matter because Smalltalk evaluates binary messages left to right.

Independent Workspace expression - select all, then Print it
| matrix vector |
matrix := #(1 1 1 0). vector := #(8 5).
{((matrix at: 1) * vector first) + ((matrix at: 2) * vector second).
 ((matrix at: 3) * vector first) + ((matrix at: 4) * vector second)}

Expected: #(13 8)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Apply a two-by-two matrix
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Replace the vector with #(1 0). You should get #(1 1), the state after one recurrence step.

Download this checkpoint

03 - Compose transformations by multiplication

To apply b and then a to a column vector, use a*b. Each product entry combines a row from a with a column from b. The block returns a fresh Array and never edits either input.

Independent Workspace expression - select all, then Print it
| multiply q |
multiply := [:a :b | {
 ((a at: 1) * (b at: 1)) + ((a at: 2) * (b at: 3)).
 ((a at: 1) * (b at: 2)) + ((a at: 2) * (b at: 4)).
 ((a at: 3) * (b at: 1)) + ((a at: 4) * (b at: 3)).
 ((a at: 3) * (b at: 2)) + ((a at: 4) * (b at: 4))}].
q := #(1 1 1 0).
multiply value: q value: q

Expected: #(2 1 1 1)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Compose transformations by multiplication
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Apply this squared matrix to #(1 0) mentally: it produces #(2 1), exactly two recurrence steps.

Download this checkpoint

04 - Preserve the order of arbitrary matrices

Matrix multiplication is associative, but usually not commutative. Swapping two arbitrary transformations changes the result. Q powers happen to commute with one another; that does not justify reordering unrelated matrices.

Independent Workspace expression - select all, then Print it
| multiply firstMatrix secondMatrix |
multiply := [:a :b | {
 ((a at: 1) * (b at: 1)) + ((a at: 2) * (b at: 3)).
 ((a at: 1) * (b at: 2)) + ((a at: 2) * (b at: 4)).
 ((a at: 3) * (b at: 1)) + ((a at: 4) * (b at: 3)).
 ((a at: 3) * (b at: 2)) + ((a at: 4) * (b at: 4))}].
firstMatrix := #(1 2 0 1). secondMatrix := #(1 0 3 1).
{multiply value: firstMatrix value: secondMatrix. multiply value: secondMatrix value: firstMatrix}

Expected: #(#(7 2 3 1) #(1 2 3 7))

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Preserve the order of arbitrary matrices
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Compute the top-left entry of each product by hand. One is 1+6 and the other is 1+0.

Download this checkpoint

05 - Use identity for zero steps

The identity matrix leaves every vector unchanged. It is the starting accumulator for repeated multiplication and the correct result for exponent zero. A zero matrix would erase the state instead.

Independent Workspace expression - select all, then Print it
| multiply q identity |
multiply := [:a :b | {
 ((a at: 1) * (b at: 1)) + ((a at: 2) * (b at: 3)).
 ((a at: 1) * (b at: 2)) + ((a at: 2) * (b at: 4)).
 ((a at: 3) * (b at: 1)) + ((a at: 4) * (b at: 3)).
 ((a at: 3) * (b at: 2)) + ((a at: 4) * (b at: 4))}].
q := #(1 1 1 0). identity := #(1 0 0 1).
{multiply value: identity value: q. multiply value: q value: identity}

Expected: #(#(1 1 1 0) #(1 1 1 0))

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Use identity for zero steps
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Both left and right identity work. Test this with the nonsymmetric matrices from step 4 too.

Download this checkpoint

06 - Square the factor and halve the exponent

At each loop, consume an odd remaining exponent by multiplying its factor into result, then halve the remaining exponent. Square factor for the next bit. The invariant is result * factor^remaining = the requested original power. Associativity is enough; a matrix inverse is never needed.

Independent Workspace expression - select all, then Print it
| multiply power |
multiply := [:a :b | {
 ((a at: 1) * (b at: 1)) + ((a at: 2) * (b at: 3)).
 ((a at: 1) * (b at: 2)) + ((a at: 2) * (b at: 4)).
 ((a at: 3) * (b at: 1)) + ((a at: 4) * (b at: 3)).
 ((a at: 3) * (b at: 2)) + ((a at: 4) * (b at: 4))}].
power := [:matrix :exponent | | result factor remaining |
 (exponent isInteger and: [exponent >= 0])
  ifFalse: [Error signal: 'Use a nonnegative integer exponent'].
 result := #(1 0 0 1). factor := matrix. remaining := exponent.
 [remaining > 0] whileTrue: [
  remaining odd ifTrue: [result := multiply value: result value: factor].
  remaining := remaining // 2.
  remaining > 0 ifTrue: [factor := multiply value: factor value: factor]].
 result].
{power value: #(1 1 1 0) value: 0.
 power value: #(1 1 1 0) value: 10}

Expected: #(#(1 0 0 1) #(89 55 55 34))

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Square the factor and halve the exponent
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Q^10 applied to #(1 0) gives #(89 55). Its bottom-left entry is F(10)=55. The guard rejects negative and fractional exponents.

Download this checkpoint

07 - Extract a large exact Fibonacci value

For every n>=0, the bottom-left entry of Q^n is F(n). Squeak grows integers automatically, so this calculation stays exact rather than passing through a floating-point approximation. The number of matrix multiplications grows logarithmically in n.

Independent Workspace expression - select all, then Print it
| multiply power |
multiply := [:a :b | {
 ((a at: 1) * (b at: 1)) + ((a at: 2) * (b at: 3)).
 ((a at: 1) * (b at: 2)) + ((a at: 2) * (b at: 4)).
 ((a at: 3) * (b at: 1)) + ((a at: 4) * (b at: 3)).
 ((a at: 3) * (b at: 2)) + ((a at: 4) * (b at: 4))}].
power := [:matrix :exponent | | result factor remaining |
 (exponent isInteger and: [exponent >= 0])
  ifFalse: [Error signal: 'Use a nonnegative integer exponent'].
 result := #(1 0 0 1). factor := matrix. remaining := exponent.
 [remaining > 0] whileTrue: [
  remaining odd ifTrue: [result := multiply value: result value: factor].
  remaining := remaining // 2.
  remaining > 0 ifTrue: [factor := multiply value: factor value: factor]].
 result].
{(power value: #(1 1 1 0) value: 50) at: 3.
 (power value: #(1 1 1 0) value: 100) at: 3}

Expected: #(12586269025 354224848179261915075)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 7: Extract a large exact Fibonacci value
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Logarithmically many matrix multiplications does not mean constant-cost arithmetic: larger Fibonacci values require more digits and larger integer operations.

Download this checkpoint

08 - Compare against an independent recurrence

A slow iterative recurrence is a useful oracle because it does not use matrix multiplication. Compare every n from 0 through 20, then check the invalid-exponent guard. This tests the zero case and several changes in the exponent bit pattern.

Independent Workspace expression - select all, then Print it
| multiply power iterative |
multiply := [:a :b | {
 ((a at: 1) * (b at: 1)) + ((a at: 2) * (b at: 3)).
 ((a at: 1) * (b at: 2)) + ((a at: 2) * (b at: 4)).
 ((a at: 3) * (b at: 1)) + ((a at: 4) * (b at: 3)).
 ((a at: 3) * (b at: 2)) + ((a at: 4) * (b at: 4))}].
power := [:matrix :exponent | | result factor remaining |
 (exponent isInteger and: [exponent >= 0])
  ifFalse: [Error signal: 'Use a nonnegative integer exponent'].
 result := #(1 0 0 1). factor := matrix. remaining := exponent.
 [remaining > 0] whileTrue: [
  remaining odd ifTrue: [result := multiply value: result value: factor].
  remaining := remaining // 2.
  remaining > 0 ifTrue: [factor := multiply value: factor value: factor]].
 result].
iterative := [:n | | a b next |
 a := 0. b := 1.
 n timesRepeat: [next := a + b. a := b. b := next]. a].
{((0 to: 20) allSatisfy: [:n |
 ((power value: #(1 1 1 0) value: n) at: 3) = (iterative value: n)]).
 ([power value: #(1 1 1 0) value: -1. false]
  on: Error do: [:error | error messageText = 'Use a nonnegative integer exponent'])}

Expected: #(true true)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 8: Compare against an independent recurrence
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Try exponent 21 or 32, where different bits are consumed. Test noninteger input separately and expect the same guard error.

Download this checkpoint

Watch the computed states

NATIVE SQUEAK CAPTURE
Matrix powers: leap through the Fibonacci sequence native visual trace
Replay of actual Morphic window captures at successive computed stages; not mouse footage.

Download the visual experiment. Select the entire expression and Do it. Change stage := 0. to another supported stage, close the previous window, and run again. The GIF replays those native states. This example needs native Squeak; no new browser playground compatibility is claimed.

Reason about correctness and limits

The multiply block assumes two four-element numeric Arrays in row-major order. It is deliberately not a general matrix library: dimensions and element types are not validated. Parenthesize every scalar product before adding it in Smalltalk.

The power block accepts nonnegative integer exponents only. Negative powers require an inverse, and not every matrix is invertible. Exponent zero returns identity regardless of the input matrix.

Fast powering uses O(log n) matrix multiplications and a constant number of four-slot Arrays at once, aside from arithmetic temporaries. Integer storage and arithmetic costs still increase with the number of digits; F(n) has a number of bits proportional to n.

The invariant preserves multiplication order. Do not generalize the behavior of Q powers into a claim that all matrices commute. This block is safe because each factor is a power of the same input matrix.

With column vectors #(F(n+1) F(n)), the bottom-left entry of Q^n gives F(n). Choosing a different state or row-vector convention changes which entry you read.

Three practice challenges

Change the recurrence

Use the transition #(2 1 1 0), initial state #(1 0). What is the second state entry after three steps?

Show solution

The states are #(2 1), #(5 2), then #(12 5), so the answer is 5. The same powering code works: only the transition matrix changes.

Explain exponent one

Why does exponent one not need to square factor in this implementation?

Show solution

It first multiplies identity by the input matrix, then remaining becomes zero. No further factor will be consumed, so the remaining>0 check skips an unnecessary square.

Check a composition law

How could you test Q^(a+b)=Q^a*Q^b for a=7 and b=9?

Show solution

Compute power value: Q value: 16 and compare it with multiply value: (power value: Q value: 7) value: (power value: Q value: 9). Both equal #(1597 987 987 610). This law applies to powers of one matrix, not arbitrary reordered products.

Continue learning

Fast Power | Fractions | Pascal Triangle

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.