This is a follow-up to my previous post. Classical Gram-Schmidt and Modified Gram-Schmidt are two algorithms for orthogonalizing a set of vectors. Householder elementary reflectors can be used for the same task. The three algorithms have very different roundoff error properties.... read more >>

]]>This is a follow-up to my previous post. Classical Gram-Schmidt and Modified Gram-Schmidt are two algorithms for orthogonalizing a set of vectors. Householder elementary reflectors can be used for the same task. The three algorithms have very different roundoff error properties.

As I did in my previous post, I am using Pete Stewart's book *Matrix Algorithms, Volume I: Basic Decompositions*. His pseudocode is MATLAB ready.

The classic Gram-Schmidt algorithm is the first thing you might think of for producing an orthogonal set of vectors. For each vector in your data set, remove its projection onto the data set, normalize what is left, and include it in the orthogonal set. Here is the code. `X` is the original set of vectors, `Q` is the resulting set of orthogonal vectors, and `R` is the set of coefficients, organized into an upper triangular matrix.

```
type gs
```

function [Q,R] = gs(X) % Classical Gram-Schmidt. [Q,R] = gs(X); % G. W. Stewart, "Matrix Algorithms, Volume 1", SIAM, 1998. [n,p] = size(X); Q = zeros(n,p); R = zeros(p,p); for k = 1:p Q(:,k) = X(:,k); if k ~= 1 R(1:k-1,k) = Q(:,k-1)'*Q(:,k); Q(:,k) = Q(:,k) - Q(:,1:k-1)*R(1:k-1,k); end R(k,k) = norm(Q(:,k)); Q(:,k) = Q(:,k)/R(k,k); end end

This is a rather different algorithm, not just a simple modification of classical Gram-Schmidt. The idea is to orthogonalize against the emerging set of vectors instead of against the original set. There are two variants, a column-oriented one and a row-oriented one. They produce the same results, in different order. Here is the column version,

```
type mgs
```

function [Q,R] = mgs(X) % Modified Gram-Schmidt. [Q,R] = mgs(X); % G. W. Stewart, "Matrix Algorithms, Volume 1", SIAM, 1998. [n,p] = size(X); Q = zeros(n,p); R = zeros(p,p); for k = 1:p Q(:,k) = X(:,k); for i = 1:k-1 R(i,k) = Q(:,i)'*Q(:,k); Q(:,k) = Q(:,k) - R(i,k)*Q(:,i); end R(k,k) = norm(Q(:,k))'; Q(:,k) = Q(:,k)/R(k,k); end end

Compute `R` by applying Householder reflections to `X` a column at a time. Do not actually compute `Q`, just save the vectors that generate the reflections. See the description and codes from my previous post.

```
type house_qr
```

function [U,R] = house_qr(A) % Householder reflections for QR decomposition. % [U,R] = house_qr(A) returns % U, the reflector generators for use by house_apply. % R, the upper triangular factor. H = @(u,x) x - u*(u'*x); [m,n] = size(A); U = zeros(m,n); R = A; for j = 1:min(m,n) u = house_gen(R(j:m,j)); U(j:m,j) = u; R(j:m,j:n) = H(u,R(j:m,j:n)); R(j+1:m,j) = 0; end end

For various matrices `X`, let's check how the three algorithms perform on two tasks, accuracy and orthogonality. How close is `Q*R` to `X`? And, how close is `Q'*Q` to `I`.

```
type compare.m
```

function compare(X); % compare(X). Compare three QR decompositions. I = eye(size(X)); qrerr = zeros(1,3); ortherr = zeros(1,3); %% Classic Gram Schmidt [Q,R] = gs(X); qrerr(1) = norm(Q*R-X,inf)/norm(X,inf); ortherr(1) = norm(Q'*Q-I,inf); %% Modified Gram Schmidt [Q,R] = mgs(X); qrerr(2) = norm(Q*R-X,inf)/norm(X,inf); ortherr(2) = norm(Q'*Q-I,inf); %% Householder QR Decomposition [U,R] = house_qr(X); QR = house_apply(U,R); QQ = house_apply_transpose(U,house_apply(U,I)); qrerr(3) = norm(QR-X,inf)/norm(X,inf); ortherr(3) = norm(QQ-I,inf); %% Report results fprintf('\n Classic Modified Householder\n') fprintf('QR error %10.2e %10.2e %10.2e\n',qrerr) fprintf('Orthogonality %10.2e %10.2e %10.2e\n',ortherr)

First, try a well conditioned matrix, a magic square of odd order.

n = 7; X = magic(n)

X = 30 39 48 1 10 19 28 38 47 7 9 18 27 29 46 6 8 17 26 35 37 5 14 16 25 34 36 45 13 15 24 33 42 44 4 21 23 32 41 43 3 12 22 31 40 49 2 11 20

Check its condition.

kappa = condest(X)

kappa = 8.5681

Do the comparison.

compare(X);

Classic Modified Householder QR error 1.73e-16 6.09e-17 5.68e-16 Orthogonality 3.20e+00 1.53e-15 1.96e-15

All three algorithms do well with accuracy, but classic Gram-Schmidt fails with orthogonality.

Next, try a Hilbert matrix that is poorly conditioned, but not exactly singular.

n = 7; X = hilb(n)

X = 1.0000 0.5000 0.3333 0.2500 0.2000 0.1667 0.1429 0.5000 0.3333 0.2500 0.2000 0.1667 0.1429 0.1250 0.3333 0.2500 0.2000 0.1667 0.1429 0.1250 0.1111 0.2500 0.2000 0.1667 0.1429 0.1250 0.1111 0.1000 0.2000 0.1667 0.1429 0.1250 0.1111 0.1000 0.0909 0.1667 0.1429 0.1250 0.1111 0.1000 0.0909 0.0833 0.1429 0.1250 0.1111 0.1000 0.0909 0.0833 0.0769

Check its condition.

kappa = condest(X)

kappa = 9.8519e+08

Do the comparison.

compare(X)

Classic Modified Householder QR error 5.35e-17 5.35e-17 8.03e-16 Orthogonality 5.21e+00 1.22e-08 1.67e-15

All three algorithms do well with accuracy. Classic Gram-Schmidt fails completely with orthogonality. The orthogonality of MGS depends upon `kappa`. Householder does well with orthogonality.

Finally, an exactly singular matrix, a magic square of even order.

n = 8; X = magic(n)

X = 64 2 3 61 60 6 7 57 9 55 54 12 13 51 50 16 17 47 46 20 21 43 42 24 40 26 27 37 36 30 31 33 32 34 35 29 28 38 39 25 41 23 22 44 45 19 18 48 49 15 14 52 53 11 10 56 8 58 59 5 4 62 63 1

Check its rank.

rankX = rank(X)

rankX = 3

Do the comparison.

compare(X)

Classic Modified Householder QR error 1.43e-16 8.54e-17 4.85e-16 Orthogonality 5.41e+00 2.16e+00 1.30e-15

Again, all three algorithms do well with accuracy. Both Gram-Schmidts fail completely with orthogonality. Householder still does well with orthogonality.

All three of these algorithms provide `Q` and `R` that do a good job of reproducing the data `X`, that is

`Q`*`R`is always close to`X`for all three algorithms.

On the other hand, their behavior is very different when it comes to producing orthogonality.

- Classic Gram-Schmidt.
`Q'*Q`is almost never close to`I`. - Modified Gram-Schmidt.
`Q'*Q`depends upon condition of`X`and fails completely when`X`is singular. - Householder triangularization.
`Q'*Q`is always close to`I`

G. W. Stewart, *Matrix Algorithms: Volume 1: Basic Decompositions*, SIAM, xix+458, 1998. <http://epubs.siam.org/doi/book/10.1137/1.9781611971408>

Get
the MATLAB code

Published with MATLAB® R2016a

The QR decomposition is often the first step in algorithms for solving many different matrix problems, including linear systems, eigenvalues, and singular values. Householder reflections are the preferred tool for computing the QR decomposition.... read more >>

]]>The QR decomposition is often the first step in algorithms for solving many different matrix problems, including linear systems, eigenvalues, and singular values. Householder reflections are the preferred tool for computing the QR decomposition.

Alston Householder (1904-1993) is one of the pioneers of modern numerical linear algebra. He was a member of the mathematics division of Oak Ridge National Laboratory for over 20 years, from 1946 until 1969, and was also a Professor at the University of Tennessee.

Alston served as President of both SIAM and ACM. He introduced what he called *elementary Hermitian matrices* in a paper in the Journal of the ACM in 1958.

Alston was head of the organizing committee for the Gatlinburg Conferences on Numerical Algebra. A photo of the 1964 committee is available in your MATLAB `demos` directory.

```
load gatlin
image(X)
colormap(map)
```

The Gatlinburg Conferences are now called the Householder Conferences. I wrote about them in MATLAB News & Notes.

My colleague and friend G. W. Stewart is a Distinguished University Professor Emeritus at the Department of Computer Science, University of Maryland. Everybody knows him as "Pete". He has never been able to satisfactorily explain the origins of "Pete" to me. It somehow goes back through his father to his grandfather and maybe great grandfather, who were also nicknamed "Pete".

Pete was Householder's Ph.D. student at the University of Tennessee.

Pete has written several books on numerical linear algebra. The reference for my blog today is his book "Matrix Algorithms, Volume I: Basic Decompositions", published by SIAM. His pseudocode is MATLAB ready.

The QR decomposition expresses a matrix as the product of an orthogonal matrix and an upper triangular matrix. The letter Q is a substitute for the letter O from "orthogonal" and the letter R is from "right", an alternative for "upper".

The decomposition is available explicitly from the MATLAB function `qr`. But, more importantly, the decomposition is part of the fundamental MATLAB linear equation solver denoted by backslash, " `\` ", as well as both the `eig` and `svd` functions for dense matrices.

For any matrix $A$ we write

$$ A = QR $$

Operations based upon orthogonal matrices are very desirable in numeric computation because they do not magnify errors, either those inherited from the underlying data, or those introduced by floating point arithmetic.

A nifty example, taken from the *Wikipedia* page on "QR decomposition", is unusual because $A$, $R$, and a renormalization of $Q$ all have integer entries.

A = [12 -51 4; 6 167 -68; -4 24 -41] [Q,R] = qr(A)

A = 12 -51 4 6 167 -68 -4 24 -41 Q = -0.8571 0.3943 0.3314 -0.4286 -0.9029 -0.0343 0.2857 -0.1714 0.9429 R = -14.0000 -21.0000 14.0000 0 -175.0000 70.0000 0 0 -35.0000

Scale $Q$ to get integers.

cQ = 175*Q

cQ = -150.0000 69.0000 58.0000 -75.0000 -158.0000 -6.0000 50.0000 -30.0000 165.0000

Let's check that $Q$ and $R$ reproduce $A$.

QR = Q*R

QR = 12.0000 -51.0000 4.0000 6.0000 167.0000 -68.0000 -4.0000 24.0000 -41.0000

A Householder reflection is characterized by a vector $u$, which, following Pete's convention, is normalized to have

$$ ||u|| = \sqrt{2} $$

It is usual to define a Householder reflection by a matrix. But I want to define it in a different way, by a MATLAB anonymous function.

H = @(u,x) x - u*(u'*x);

This emphasizes the way in which the reflection should be computed. For any vector $x$, find its projection onto $u$ and then subtract that multiple of $u$ from $x$. In the following picture $u_p$ is a line perpendicular to $u$. In more dimensions $u_p$ would be the plane through the origin perpendicular to $u$. With the $\sqrt{2}$ normalization of $u$, the effect of $H$ is to transform any vector $x$ to its mirror image $Hx$ on the other side of this plane. (Vectors in the plane are left there by $H$.)

house_pic

Where do we get the vector `u` that characterizes the reflection? We want to operate on the columns of a matrix to introduce zeros below the diagonal. Let's begin with the first column, call it `x`. We want to find `u` so that `Hx = H(u,x)` is zero below the first element. And, since the reflection is to preserve length, the only nonzero element in `Hx` should have `abs(Hx(1)) == norm(x)`.

Start with `x` normalized to have length one.

u = x/norm(x)

Now add `+1` or `-1` to `u(1)`, choosing the sign to match. The other choice involves cancellation and is less desirable numerically.

u(1) = u(1) + sign(u(1))

Finally, scale `u` by `sqrt(abs(u(1)))`.

u = u/sqrt(abs(u(1)))

It turns out that this makes `norm(u)` equal to `sqrt(2)`. And a bit more algebra shows that the resulting reflection zeros out all but the first element of `x`.

The figure above illustrates the situation because `Hx` has only one nonzero component.

Here is the code, including the case where `x` is already all zero.

```
type house_gen
```

function u = house_gen(x) % u = house_gen(x) % Generate Householder reflection. % u = house_gen(x) returns u with norm(u) = sqrt(2), and % H(u,x) = x - u*(u'*x) = -+ norm(x)*e_1. % Modify the sign function so that sign(0) = 1. sig = @(u) sign(u) + (u==0); nu = norm(x); if nu ~= 0 u = x/nu; u(1) = u(1) + sig(u(1)); u = u/sqrt(abs(u(1))); else u = x; u(1) = sqrt(2); end end

Let's try this on the first column of the *Wikipedia* example

x = [12 6 -4]'

x = 12 6 -4

The vector `u` generated is

u = house_gen(x)

u = 1.3628 0.3145 -0.2097

The resulting reflection has the desired effect.

Hx = H(u,x)

Hx = -14.0000 -0.0000 0.0000

`Hx(1)` is equal to `-norm(x)` and the other elements of `Hx` are zero.

Our anonymous function can be represented by a matrix. This is the usual way of defining Householder reflections

I = eye(3); M = H(u,I)

M = -0.8571 -0.4286 0.2857 -0.4286 0.9011 0.0659 0.2857 0.0659 0.9560

In general

$$M = I - uu'$$

Multiplication by this matrix produces the same reflection as the anonymous function, but requires $n^2$ operations, instead of $2n$.

Recall that Gaussian elimination can be described by a sequence of matrix multiplications, but it is actually carried out by subtracting multiples of rows from other rows. There is an anonymous function for this elimination operation, but it is not so easy to express the pivoting.

We are now ready to compute the QR decomposition. Pass over the columns of the input matrix, using Householder reflections to introduce zeros below the diagonal. This produces the R matrix. It is inefficient and usually not necessary to actually compute Q. Just save the `u` 's.

Here is where the way we have expressed the anonymous function is important. When `x` is a vector, `u'*x` is a scalar and we could have written it in front of `u`, like this.

H = @(u,x) x - (u'*x)*u;

But we want to apply `H` to several columns of a matrix at once, so we have written `(u'*x)` after `u`, like this,

H = @(u,x) x - u*(u'*x);

```
type house_qr
```

function [R,U] = house_qr(A) % Householder reflections for QR decomposition. % [R,U] = house_qr(A) returns % R, the upper triangular factor, and % U, the reflector generators for use by house_apply. H = @(u,x) x - u*(u'*x); [m,n] = size(A); U = zeros(m,n); R = A; for j = 1:min(m,n) u = house_gen(R(j:m,j)); U(j:m,j) = u; R(j:m,j:n) = H(u,R(j:m,j:n)); R(j+1:m,j) = 0; end end

Use a magic square as an example.

A = magic(6)

A = 35 1 6 26 19 24 3 32 7 21 23 25 31 9 2 22 27 20 8 28 33 17 10 15 30 5 34 12 14 16 4 36 29 13 18 11

Compute its decomposition.

[R,U] = house_qr(A)

R = -56.3471 -16.4693 -30.0459 -39.0969 -38.0321 -38.6710 0 -54.2196 -34.8797 -23.1669 -25.2609 -23.2963 0 0 32.4907 -8.9182 -11.2895 -7.9245 0 0 0 -7.6283 3.9114 -7.4339 0 0 0 0 -3.4197 -6.8393 0 0 0 0 0 -0.0000 U = 1.2732 0 0 0 0 0 0.0418 1.2568 0 0 0 0 0.4321 0.0451 -1.1661 0 0 0 0.1115 0.3884 0.4557 1.0739 0 0 0.4182 -0.0108 0.5942 -0.6455 1.0796 0 0.0558 0.5171 0.2819 -0.6558 -0.9135 1.4142

The fact that `R(6,6)` is equal to zero tells us that the magic square of order 6 has rank less than 6 and so is singular

type house_apply type house_apply_transpose

function Z = house_apply(U,X) % Apply Householder reflections. % Z = house_apply(U,X), with U from house_qr % computes Q*X without actually computing Q. H = @(u,x) x - u*(u'*x); Z = X; [~,n] = size(U); for j = n:-1:1 Z = H(U(:,j),Z); end end function Z = house_apply_transpose(U,X) % Apply Householder transposed reflections. % Z = house_apply(U,X), with U from house_qr % computes Q'*X without actually computing Q'. H = @(u,x) x - u*(u'*x); Z = X; [~,n] = size(U); for j = 1:n Z = H(U(:,j),Z); end end

We can use `house_apply` to get the matrix $Q$ of the QR decomposition by applying the transformations to the identity matrix.

I = eye(size(U)); Q = house_apply(U,I)

Q = -0.6211 0.1702 -0.2070 -0.4998 0.2062 0.5000 -0.0532 -0.5740 -0.4500 -0.2106 -0.6487 -0.0000 -0.5502 0.0011 -0.4460 0.4537 0.2062 -0.5000 -0.1420 -0.4733 0.3763 -0.5034 0.3329 -0.5000 -0.5324 0.0695 0.6287 0.2096 -0.5220 -0.0000 -0.0710 -0.6424 0.1373 0.4501 0.3329 0.5000

Check that `Q` is orthogonal.

QQ = Q'*Q

QQ = 1.0000 -0.0000 0.0000 0.0000 -0.0000 -0.0000 -0.0000 1.0000 -0.0000 -0.0000 -0.0000 0.0000 0.0000 -0.0000 1.0000 -0.0000 -0.0000 0.0000 0.0000 -0.0000 -0.0000 1.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 1.0000 0.0000 -0.0000 0.0000 0.0000 -0.0000 0.0000 1.0000

And that `Q*R` regenerates our magic square.

QR = Q*R

QR = 35.0000 1.0000 6.0000 26.0000 19.0000 24.0000 3.0000 32.0000 7.0000 21.0000 23.0000 25.0000 31.0000 9.0000 2.0000 22.0000 27.0000 20.0000 8.0000 28.0000 33.0000 17.0000 10.0000 15.0000 30.0000 5.0000 34.0000 12.0000 14.0000 16.0000 4.0000 36.0000 29.0000 13.0000 18.0000 11.0000

Alston S. Householder, "Unitary Triangularization of a Nonsymmetric Matrix", Journal of the ACM 5, 339-242, 1958. <http://dl.acm.org/citation.cfm?doid=320941.320947>

G. W. Stewart, *Matrix Algorithms: Volume 1: Basic Decompositions*, SIAM, xix+458, 1998. <http://epubs.siam.org/doi/book/10.1137/1.9781611971408>

Get
the MATLAB code

Published with MATLAB® R2016a

A new app employs transformations of a graphic depicting a house to demonstrate matrix multiplication.... read more >>

]]>A new app employs transformations of a graphic depicting a house to demonstrate matrix multiplication.

The house in the animated gif has been featured in Gil Strang's textbook, *Introduction to Linear Algebra*. A quilt inspired by the house in on the cover of the third edition.

You can rotate the house with your mouse by clicking and dragging the peak of the roof. You can stretch the house horizontally by clicking and dragging either side. You can stretch the house vertically by dragging the floor.

The motion is effected by matrix multiplication. Rotating the roof through an angle $\theta$ defines a 2-by-2 orthogonal matrix $U$.

$$ U = \left( \begin{array}{rr} \cos\theta \ \ \sin\theta \\ -\sin\theta \ \ \cos\theta \\ \end{array} \right) $$

Moving a side defines a horizontal scaling $\sigma_1$. Moving the floor defines a vertical scaling $\sigma_2$. Together they form the diagonal scaling matrix $S$.

$$ S = \left( \begin{array}{rr} \sigma_1 \ \ 0 \\ 0 \ \ \sigma_2 \\ \end{array} \right) $$

The resulting 2-by-2 transformation matrix $A$ is the product of $U$ and $S$.

$$A = U S $$

Watch how the three matrices change as you manipulate the house. The scale factor $\sigma_1$ operates on the first column of $U$, while $\sigma_2$ scales the second.

The code for this app is available here.

Get
the MATLAB code

Published with MATLAB® R2016a

MATLAB Central is celebrating its 15th birthday this fall. In honor of the occasion, MathWorks bloggers are reminiscing about their first involvement with the Web site. My first contribution to the File Exchange was not MATLAB software, but rather a collection of documents that I called the Pentium Papers. I saved this material in November and December of 1994 when I was deeply involved in the Intel Pentium Floating Point Division Affair.... read more >>

]]>MATLAB Central is celebrating its 15th birthday this fall. In honor of the occasion, MathWorks bloggers are reminiscing about their first involvement with the Web site. My first contribution to the File Exchange was not MATLAB software, but rather a collection of documents that I called the Pentium Papers. I saved this material in November and December of 1994 when I was deeply involved in the Intel Pentium Floating Point Division Affair.

Pentium key chain. Image thanks to Thomas Johansson, thomas@cpucollection.se.

*Bad companies are destroyed by crises;
Good companies survive them;
Great companies are improved by them.;*

–Andy Grove, Chairman, Intel Corp., December 1994

As I said in my blog post in 2013, “The Pentium division bug episode in the fall of 1994 was a defining moment for the MathWorks, for the Internet, for Intel Corporation, and for me personally.”

In the fall of 1994 the Internet was not anything like it is today. The World Wide Web was in its infancy. All of the connections were text based. The Mosaic graphical browser was only a few months old and few people knew about it. Internet explorer did not yet exist and Google was four years in the future. We did have email. Some of us used FTP, File Transfer Protocol. And the social media of the day were the text only news groups, which had names like `comp.soft-sys.matlab` and `comp.soft-sys.intel`.

But calling the news groups “social media” is a stretch. The term “social media” didn’t exist in 1994. And almost all of the participants in the news groups were geeks and nerds in universities and industrial research labs.

Thomas Nicely, a Professor of Mathematics at Lynchburg College in Virginia, did research on prime numbers, especially *twin primes*. For his computational experiments he employed several IBM PCs running Intel 486 processors. In the summer of 1994 he added a PC with the new Pentium chip. Much to his surprise, he found the Pentium gave a different result for the reciprocal of one of his primes.

On October 30, Nicely emailed several other Pentium users that he had discovered a bug in the Pentium’s floating point arithmetic. The news soon reached Terje Mathisen, a computer jock at Norsk Hydro in Oslo, Norway, who had written about the accuracy of Intel’s transcendental functions. Mathisen confirmed the bug and, on November 3, posted a test program to `comp.soft-sys.intel`. A day later Andreas Kaiser in Germany used Mathisen’s program to post a list of numbers whose reciprocals were being computed to what appeared to be only single precision accuracy.

Tim Coe designed floating point hardware for an aerospace contractor in California. He didn’t have access to a Pentium machine. But from Kaiser’s list of erroneous reciprocals he was able to reverse engineer Intel’s division algorithm. He expected that certain divisions, involving quantities with specific bit patterns, would produce results that were much less accurate than even single precision. He drove to a local computer store, ran a calculator program on a Pentium in the showroom, and confirmed his prediction.

I first heard about the FDIV bug (FDIV is the mnemonic for Floating point Division on x86 processors) in early November from an email list for floating point arithmetic. It appeared to be a single/double precision hardware glitch. Annoying, but not surprising. I started to follow `comp.soft-sys.intel` anyway. But when Coe posted his results, and when MathWorks tech support got a couple of queries asking how this affect MATLAB, I got seriously interested.

On November 15, 1994, I made the first of what would become several postings to both `comp.soft-sys.intel` and `comp.soft-sys.matlab`, summarizing what I knew. I pointed out that the relative error in one of Coe’s examples is $6.1 \cdot 10^{-5}$. This is ten orders of magnitude larger than what we expect from MATLAB, or any other scientific computation using IEEE double precision. The error might not occur very often, but when it does, it can be very significant.

Within the next week the Net became very active. Several participants contacted newspapers and TV stations. Two engineers at the Jet Propulsion Laboratory convinced JPL to issue a press release announcing that they were no longer purchasing Pentium-based PCs. Reporters seeking more information found my posting and redistributed it.

On November 22 CNN sent a TV crew to MathWorks, interviewed me, and then led off their evening *Moneyline* with a story about Intel’s troubles. I spent the next day fielding phone calls from other reporters. A number of newspapers, including the *New York Times* and the *San Jose Mercury News*, ran stories on the 24th. The headline of the front page story in the *Boston Globe* was “Sorry, Wrong Number”.

As a corporation, Intel did not know how to react and handled the situation badly. They had little experience dealing with the public. Their customers were computer manufacturers, not individual computer users. They had no experience whatsoever dealing with the Internet. Their first response came in the form of canned statement that could be faxed back to anyone contacting tech support via fax. This FAXBACK document was soon posted to the Net, but not by Intel. I’ve included a copy in the Pentium Papers.

Intel’s statement said that they had already discovered the FDIV “flaw” themselves and had fixed it in recent releases of the Pentium chip. They claimed that it would occur only once in 9 billion divide operations and that the average spreadsheet user would encounter it only once in 27,000 years of use. They provided an 800 telephone number to call for anyone doing “prime number generation or other complex mathematics”. They would interview callers and offer to replace the chip for anyone they thought really required error-free divisions.

Needless to say, this aggressive non-apology only enflamed the criticism of Intel on the Net. Nobody believed their claims about the frequency of occurrence. The frequencies came from a study Intel had made, but initially refused to release. As far as I was concerned, the problem was not the likelihood of encountering the FDIV bug, it was the fact that we had to worry about it at all.

Tim Coe, Terje Mathisen and I devised a scheme where a Pentium FDIV hardware instruction could be replaced by less than a dozen lines of software that insured all divisions were done correctly. The idea is that the divisor of each prospective division operation would be checked for the presence of certain bit patterns in the floating point fraction that made it “at risk”. When an at risk divisor and the corresponding dividend are both scaled by 15/16, the quotient remains unchanged, but the operation can then be done safely by the FDIV instruction.

We wanted to make our workaround widely available. Intel contacted us and, along with Peter Tang, a computer scientist who was then at Argonne and who has been a consultant to Intel, we began to work, via conference calls, with a group at Intel. It was our intention to provide the workaround to compiler writers and major software vendors, and to announce its availability on the Net. The workaround macro would replace FDIV in all PC software being developed. (It was more complicated — functions like “mod” and “rem” and a few transcendental functions like “atan” that had Pentium hardware support were also involved.)

MATLAB was the proof-of-concept for the software workaround. We built and released a special “Pentium Aware” MATLAB. Its documentation says

MATLAB detects, and optionally repairs,

erroneous arithmetic results produced by Intel’s Pentium processor.

Erroneous results are infrequent, but can occur in many MATLAB operations

and functions. … When an erroneous result is detected, MATLAB prints a

message. … Options exist to suppress the printing of the messages,

count the number of occurrences, and suppress the corrections

altogether.

Our public relations firm had sent out a press release with the headline

THE MATHWORKS DEVELOPS FIX FOR THE INTEL PENTIUM(tm) FLOATING POINT ERROR

At the time, MathWorks had just reached its 10th anniversary. The company name was barely known in the industries we served, and completely unheard of in the public generally. So when a press release arrived saying this obscure little company in Massachusetts has fixed the Pentium bug, it created quite a stir. I got dozens of more phone calls.

I now have a folder of hundreds of press clippings from all over the world that resulted from the Pentium affair.

On December 12th IBM issued its own study. IBM had several reasons to get involved. They had invented, or at least named, the IBM PC, and one division was selling PCs employing the Pentium. Another division was developing and manufacturing its own chip, the PowerPC.

The IBM study claimed that typical spreadsheet calculations were likely to generate numbers with the “at risk” bit patterns and so FDIV errors were much more likely to occur than Intel claimed. The study also cleverly multiplied their predicted likelihood of an individual spreadsheet user encountering an error by an estimated total number of spreadsheet users worldwide to conclude “on a typical day a large number of people are making mistakes in their computation without realizing it.”

IBM announced they were suspending production of Pentium-based PCs.

Within hours of the IBM announcement, Intel’s stock price dropped 10 points. A week later Intel issued an apology and announced a no-questions-asked return policy on Pentium chips. They set up a network of service centers to handle the replacements and allocated $475 million to pay for the replacement program.

Months later, very few actual requests for replacements had been made. We had learned that encountering the FDIV error was, in fact, very unlikely. But more important, for most people, Intel’s apology was enough.

As all this was happening, I saved some of the postings from the `comp.soft-sys.intel` news groups, a few newspaper stories and a few other contemporary documents. I would respond to email requests for information with “If you have access to the Internet, you can download my Pentium Papers using anonymous ftp from `ftp.mathworks.com`.”

As the MathWorks Web site developed there was usually a note somewhere about how to get to the Pentium Papers. When we started MATLAB Central File Exchange I moved the collection there as my first contribution.

If you open the `.txt` files in the Pentium Papers with Microsoft Word, it will break up the long lines and produce a readable file.

Published with MATLAB® R2016a

]]>Jim Sanderson has had a fascinating professional life. He was my PhD student in math at the University of New Mexico in the 1970s. He spent almost 20 years as a computational scientist at Los Alamos National Laboratory, working on the lab's supercomputers. He then developed an interest in ecology, went back to school, and is now the world's leading authority on the preservation of small wild cats around the world.... read more >>

]]>Jim Sanderson has had a fascinating professional life. He was my PhD student in math at the University of New Mexico in the 1970s. He spent almost 20 years as a computational scientist at Los Alamos National Laboratory, working on the lab’s supercomputers. He then developed an interest in ecology, went back to school, and is now the world’s leading authority on the preservation of small wild cats around the world.

<http://globalwildlife.org/about-gwc/team/jim-sanderson-ph-d>

One of the great success stories of modern numerical linear algebra is the QR algorithm with the Francis shift for computing all the eigenvalues of a symmetric tridiagonal matrix. Jim Wilkinson showed that the algorithm was superior to all its competitors in the 1960s. So it was a key component of the Wilkinson and Reinsch *Handbook*, of *EISPACK*, and ultimately of the first MATLAB. It survives today as one of the primary routines in LAPACK and MATLAB.

Wilkinson established the algorithm’s global convergence and its asymptotic cubic convergence rate. But, curiously, he never analyzed its roundoff error behavior. This was Jim Sanderson’s PhD thesis. At the time Jim was writing his thesis, the academic computer science community was beginning to be interested in the notion of proving algorithms correct. Jim found it necessary to reorder the operations in Wilkinson’s implementation, but with this modification he was able to prove that the algorithm was correct. It was guaranteed to be globally, and cubically, convergent even in the presence of floating point roundoff errors.

After grad school, Jim joined the staff of the Los Alamos National Laboratory, located in the high desert mountains of northern New Mexico. Starting with the Manhattan Project, the Lab had originally developed the nation’s atomic and nuclear weapons. But with the limits imposed by international treaties, the Lab’s mission evolved from developing new weapons to maintaining existing stockpiles.

I’m not exactly sure what Jim did during his almost 20 years at Los Alamos. He is not allowed to tell me in much detail. At first it involved image processing. Later it involved large computer programs, very likely written in Fortran, run on whatever were the world’s fastest supercomputers of the day, that implemented partial differential equation models of complex nuclear reactions.

During his time at Los Alamos, Jim developed a hobby, nature photography. He says it was esoteric, artistic stuff, with photos of fallen leaves on the ground and dark clouds in a storm.

Eliot Porter was one of the first photographers to exhibit and publish color photographs of nature. He and his family eventually established a ranch in Tesuque, New Mexico, not far from Los Alamos. Jim came to work under Eliot and travel with him on several photography expeditions. Porter had a strong influence on Jim’s interest in nature photography and conservation.

In 1995 Jim left the Labs and, in middle age, returned to undergrad studies, this time majoring in biology, again at the University of New Mexico. After graduating for the second time from UNM, Jim went on to the highly regarded graduate program in Wildlife Ecology and Conservation at the University of Florida.

After completing grad school in Ecology at UFL, Jim joined Conservation International, working as a landscape ecologist.

By this time Jim was interested in small, wild cats. Big cats — lions, tigers, cheetahs, leopards — get most of the world’s attention. But there are over two dozen smaller wild cats, most of which we’ve never heard of and many of which are endangered. These cats have been an important focus in Jim’s second career.

Jim formed the Small Cat Conservation Alliance in 2006. That organization evolved into the Small Wild Cat Conservation Foundation in 2008.

One of the first cats to get Jim’s attention was the Andean Mountain Cat. Jim was the first scientist to confirm this cat’s existence. It lives above 14,000 feet in Andes of Chile and Bolivia. Before Jim’s work it was only known as a mythical creature that threatened the villagers’ chickens.

*Andean_cat_1_Jim_Sanderson.jpg*, Small Wild Cat Conservation Foundation.

Jim took this portrait of the cat in 1998. He says he brought all of his nature photography skills to bear when he took this shot. The photo was featured in an ad for Canon cameras that ran in the National Geographic magazine and that earned a sizable contribution to the Small Cat Conservation Alliance.

A few years ago Jim was on a long flight back from South America. He noticed a guy across the aisle doing some sort of mathematical looking puzzle in the airline magazine and asked him what it was. The guy introduced Jim (who had apparently been living away from the rest of civilization) to Sudoku. Jim returned to his seat, and to his laptop, and wrote a Fortran (more evidence of his lifestyle) program to play Sudoku.

When showed the solution to the puzzle, the guy across the aisle, not realizing that Jim had written an entire puzzle-solving program, was not impressed. “Took you a long time”, was the terse reaction. No more conversation there.

Jim told me about the incident some time later. I had a different reaction. “Time you learned to use MATLAB.”

I had been avoiding Sudoku because I was afraid I would get sucked in. To this day I’ve never done a puzzle by hand. But the program I wrote to demonstrate MATLAB to Jim is the same program that I’ve described in Cleve’s Corner, MathWorks News & Notes. I got sucked in in a different way.

Jared Diamond is a Professor of Geography and Physiology at UCLA. He is best known for the 1998 Pulitzer Prize winning book “Guns, Germs, and Steel: The Fates of Human Societies”. A documentary based on the book was produced by the National Geographic Society and broadcast on PBS.

Before writing about the great issues that made him famous, one of Diamond’s first interests in science was birds. How do various species of birds distribute themselves across strings of islands? Is the distribution random? Is it different from what it would be if the species did not interact?

In 1975, in a 102 page paper, “Assembly of species communities”, Diamond introduced the concepts of *competitive exclusion* and *checkboard distributions*. He suggests that species composition on a particular island is due to minimization of unutilized resources. The resulting distributions observed across an archipelago are alternating patterns that he called *checkerboards*. The data supporting his hypothesis came from his own extensive personal observations in the Bismarck Archipelago near Papua New Guinea in the western Pacific Ocean, as well as that of other visiting observers and natives.

In 1979, Edward Connor and Daniel Simberloff set off a controversy that continues to this day when they published “Assembly of species communities: chance or competition?” They pointed out that Diamond’s observed patterns had not been tested against appropriate null hypotheses. They suggested that the observed patterns could not be distinguished from patterns resulting from random distributions of the species across the subject area.

Sanderson became interested in the controversy when he read the papers while he was in school. He figured he could settle whether the patterns were random with some computer simulations. He needed to generate random matrices while maintaining constraints on the row and column sums. One approach involving the Knight’s Tour proved to be too slow to generate the $10^6$ random matrices required in this situation and faster alternatives were developed.

Jim’s simulations strongly supported Diamond’s thesis. Stuart Pimm, now a professor at Duke, had been a professor at Florida, knew Diamond, and sent Jim’s work to Diamond. A paper by the three of them, James Sanderson, Jared Diamond, and Stuart Pimm, “Pairwise Co-existence of Bismarck and Solomon Landbird Species”, was finally published in 2009. The paper criticized Connor and Simberloff’s methodology and conclusions in no uncertain terms.

Connor and Simberloff, together with Michael Collins, responded with “The checkered history of checkerboard distributions” in 2013. They claim that “Few, if any, ‘true checkerboards’ exist in these archipelagoes that could possibly have been influenced by competitive interactions.”

A pair of back-to-back notes published in December 2015 continue the interchange. One is by Diamond, Pimm, and Sanderson. “The checkered history of checkerboard distributions: comment”. The follow-up comes from Connor, Collins, and Simberloff. “The checkered history of checkerboard distributions: reply”. One of the arguments is over convex hulls on maps.

Jim and Stuart Pimm have written a book describing the whole affair, at least as of last year, that is accessible to a wider audience.

James G. Sanderson and Stuart L. Pimm, _Patterns in Nature_, The Analysis of Species Co-occurrences, The University of Chicago Press, 205 pages, 2015.

In the preface to the book they say, “Some of what happened thereafter was simply ugly, involving one of the most violent clashes of personalities in recent ecological history.” They quote Professor R. J. Putman of the University of Glasgow, even as far back as 1994, that the debate was “almost unprecedented in the apparent entrenchment and hostility of the opposing camps.”

Jim recently joined a conservation organization based in Austin that was established by some friends of his, Global Wildlife Conservation, <http://globalwildlife.org>. He is their new Director of Wild Cat Conservation.

Published with MATLAB® R2016a

]]>A few days ago we received email from Mike Hennessey, a mechanical engineering professor at the University of St. Thomas in St. Paul, Minnesota. He has been reading my book "Numerical Computing with MATLAB" very carefully. Chapter 7 is about "Eigenvalues and Singular Values" and section 10.3 is about one of my all-time favorite MATLAB demos, `eigshow`. Mike discovered an error in my description of the `svd` option of `eigshow` that has gone unnoticed in the over ten years that the book has been available from both the MathWorks web site and SIAM.... read more >>

A few days ago we received email from Mike Hennessey, a mechanical engineering professor at the University of St. Thomas in St. Paul, Minnesota. He has been reading my book “Numerical Computing with MATLAB” very carefully. Chapter 7 is about “Eigenvalues and Singular Values” and section 10.3 is about one of my all-time favorite MATLAB demos, `eigshow`. Mike discovered an error in my description of the `svd` option of `eigshow` that has gone unnoticed in the over ten years that the book has been available from both the MathWorks web site and SIAM.

The program `eigshow` has been in the MATLAB `demos` directory for many years. I wrote a three-part series of posts about `eigshow` in this blog three years ago, but I’m happy to write another post now.

The `svd` option of `eigshow` invites you to use your mouse to move the green vector `x` and make `A*x` perpendicular to `A*y`. The animated gif above simulates that motion. The following figure is the desired final result.

As you move `x`, the vector `y` follows along, staying perpendicular to `x`. The two trace out the green unit circle. The default matrix `A` is shown in the title. (If you run your own `eigshow` you can change the matrix by editing the text box in the title.)

A = [1 3; 4 2]/4

A = 0.2500 0.7500 1.0000 0.5000

The blue vectors `Ax` and `Ay` are the images of `x` and `y` under the multiplicative mapping induced by `A`. As you move `x` and `y` around the unit circle `Ax` and `Ay` sweep out the blue ellipse. And when you stop with `Ax` perpendicular to `Ay`, they turn out to be the major and minor axes of the ellipse.

*Numerical Computing with MATLAB*, which is known to its friends as simply *NCM*, was published over 10 years ago, in 2004. An electronic edition is available from MathWorks and a print edition from SIAM. In chapter 7, the singular value decomposition, the SVD, of a real matrix, $A$, is defined as the product

$$ A = U \Sigma V^T$$

In the simplest case where we assume $A$ is square, $U$ and $V$ are orthogonal and $\Sigma$ is diagonal.

This is illustrated by figure 10.3 in *NCM*, which is the same as the final figure here. It shows the action with the `svd` option on the default matrix. The explanation is given by the first sentence on page 7.

The vectors x and y are the columns of U in the SVD, the vectors

Ax and Ay are multiples of the columns of V, and the lengths of

the axes are the singular values.

Sounds OK, right? This is the sentence that Mike Hennessey questioned. Now that I call your attention to it, you should be able to spot the error right away. I’ll tell you what it is at the end of this post.

Here is the relevant paragraph from the `help` entry for `eigshow` . It does not get into the same trouble as *NCM*.

In svd mode, the mouse moves two perpendicular unit vectors, x and y.

The resulting A*x and A*y are plotted. When A*x is perpendicular to

A*y, then x and y are right singular vectors, A*x and A*y are

multiples of left singular vectors, and the lengths of A*x and A*y

are the corresponding singular values.

Here’s how I often think about the SVD. Take the definition,

$$A = U \Sigma V^T$$

Multiply both sides on the right by $V$.

$$A V = U \Sigma$$

The diagonal matrix $\Sigma$ is on the right so that the singular values can multiply the columns of $U$. When we write this out column by column, we have

$$A v_j = \sigma_j u_j, \ \ j = 1,\ …, n$$

A little bit more manipulation leads to

$$A^T u_j = \sigma_j v_j, \ \ j = 1,\ …, n$$

Let’s see how this works out with the default 2-by-2 matrix.

A

A = 0.2500 0.7500 1.0000 0.5000

Compute the SVD.

[U,S,V] = svd(A)

U = -0.5257 -0.8507 -0.8507 0.5257 S = 1.2792 0 0 0.4886 V = -0.7678 0.6407 -0.6407 -0.7678

The singular values are on the diagonal of `S`.

sigma = diag(S)

sigma = 1.2792 0.4886

These are the lengths of the two blue vectors.

It turns out that the components of $v_1$, the right singular vector corresponding to the largest singular value of an (irreducible) matrix with nonnegative elements all have the same sign. They could be all positive or all negative — take your pick. MATLAB happens to always pick negative. So, both components of V(:,1) are negative. But we stopped `eigshow` with a positive `x`. So we flip the sign of `V(:,1)`.

x = -V(:,1) y = V(:,2)

x = 0.7678 0.6407 y = 0.6407 -0.7678

These two look like the two green vectors in the final figure. And these look like the two blue vectors.

Ax = A*x Ay = A*y

Ax = 0.6725 1.0881 Ay = -0.4156 0.2569

Now check that the SVD relationship works.

AV = A*V USigma = U*S

AV = -0.6725 -0.4156 -1.0881 0.2569 USigma = -0.6725 -0.4156 -1.0881 0.2569

What was the error in *NCM* that Mike Hennessey reported and that prompted me to write this post? The matrices U and V should be switched. The sentence should read

The vectors x and y are the columns of V in the SVD, the vectors

Ax and Ay are multiples of the columns of U, and the lengths of

the axes are the singular values.

Every time I work with the SVD I have to think carefully about U and V. Which is which? Maybe writing this post will help.

Cleve Moler, *Numerical Computering With MATLAB*, 2004.

MathWorks, <http://www.mathworks.com/moler/chapters.html>,

SIAM, <http://epubs.siam.org/doi/book/10.1137/1.9780898717952>,

Published with MATLAB® R2016a

]]>Classical Gram-Schmidt and Modified Gram-Schmidt are two algorithms for orthogonalizing a set of vectors. Householder elementary reflectors can be used for the same task. The three algorithms have very different roundoff error properties.... read more >>

]]>Classical Gram-Schmidt and Modified Gram-Schmidt are two algorithms for orthogonalizing a set of vectors. Householder elementary reflectors can be used for the same task. The three algorithms have very different roundoff error properties.

My colleague and friend G. W. Stewart is a Distinguished University Professor Emeritus at the Department of Computer Science, University of Maryland. Everybody knows him as “Pete”. He has never been able to satisfactorily explain the origins of “Pete” to me. It somehow goes back through his father to his grandfather and maybe great grandfather, who were also nicknamed “Pete”.

Pete has written several books on numerical linear algebra. For my blog today I am going to rely on the descriptions and pseudocode from his book “Matrix Algorithms, Volume I: Basic Decompositions”. His pseudocode is MATLAB ready.

The classic Gram-Schmidt algorithm is the first thing you might think of for producing an orthogonal set of vectors. For each vector in your data set, remove its projection onto the data set, normalize what is left, and add it to the orthogonal set. Here is the code. `X` is the original set of vectors, `Q` is the resulting set of orthogonal vectors, and `R` is the set of coefficients, organized into an upper triangular matrix.

```
type gs
```

function [Q,R] = gs(X) % Classical Gram-Schmidt. [Q,R] = gs(X); % G. W. Stewart, "Matrix Algorithms, Volume 1", SIAM, 1998. [n,p] = size(X); Q = zeros(n,p); R = zeros(p,p); for k = 1:p Q(:,k) = X(:,k); if k ~= 1 R(1:k-1,k) = Q(:,k-1)'*Q(:,k); Q(:,k) = Q(:,k) - Q(:,1:k-1)*R(1:k-1,k); end R(k,k) = norm(Q(:,k)); Q(:,k) = Q(:,k)/R(k,k); end end

The entire process can be expressed by matrix multiplication, with orthogonal $Q$ and upper triangular $R$.

$$X = QR$$

This is a rather different algorithm, not just a simple modification of classical Gram-Schmidt. The idea is to orthogonalize against the emerging set of vectors instead of against the original set. There are two variants, a column-oriented one and a row-oriented one. They produce the same results, in different order. Here is the column version,

```
type mgs
```

function [Q,R] = mgs(X) % Modified Gram-Schmidt. [Q,R] = mgs(X); % G. W. Stewart, "Matrix Algorithms, Volume 1", SIAM, 1998. [n,p] = size(X); Q = zeros(n,p); R = zeros(p,p); for k = 1:p Q(:,k) = X(:,k); for i = 1:k-1 R(i,k) = Q(:,i)'*Q(:,k); Q(:,k) = Q(:,k) - R(i,k)*Q(:,i); end R(k,k) = norm(Q(:,k))'; Q(:,k) = Q(:,k)/R(k,k); end end

A Householder reflection $H$ transforms a given vector $x$ into a multiple of a unit vector $e_k$ while preserving length, so $Hx = \pm \sigma e_k$ where $\sigma = ||x||$ .

The matrix $H$ is never formed. The reflection is expressed as a rank-one modification of the identity

$$H = I – uu^T \ \mbox{where} \ ||u|| = \sqrt{2}$$

(The use of $\sqrt{2}$ here is Pete’s signature normalization.)

```
type housegen
```

function [u,nu] = housegen(x) % [u,nu] = housegen(x) % Generate Householder reflection. % G. W. Stewart, "Matrix Algorithms, Volume 1", SIAM, 1998. % [u,nu] = housegen(x). % H = I - uu' with Hx = -+ nu e_1 % returns nu = norm(x). u = x; nu = norm(x); if nu == 0 u(1) = sqrt(2); return end u = x/nu; if u(1) >= 0 u(1) = u(1) + 1; nu = -nu; else u(1) = u(1) - 1; end u = u/sqrt(abs(u(1))); end

Now to apply $H$ to any other $y$, compute the inner product

$$\tau = u^Ty$$

and then simply subtract

$$Hy = x – \tau u$$

This program does not actually compute the QR orthogonalization, but rather computes `R` and a matrix `U` containing vectors that generate the Householder reflectors whose product is Q.

```
type hqrd
```

function [U,R] = hqrd(X) % Householder triangularization. [U,R] = hqrd(X); % Generators of Householder reflections stored in U. % H_k = I - U(:,k)*U(:,k)'. % prod(H_m ... H_1)X = [R; 0] % where m = min(size(X)) % G. W. Stewart, "Matrix Algorithms, Volume 1", SIAM, 1998. [n,p] = size(X); U = zeros(size(X)); m = min(n,p); R = zeros(m,m); for k = 1:min(n,p) [U(k:n,k),R(k,k)] = housegen(X(k:n,k)); v = U(k:n,k)'*X(k:n,k+1:p); X(k:n,k+1:p) = X(k:n,k+1:p) - U(k:n,k)*v; R(k,k+1:p) = X(k,k+1:p); end end

All three of these algorithms compute `Q` and `R` that do a good job of reproducing the data `X`, that is

`Q`*`R`is always close to`X`for all three algorithms.

On the other hand, their behavior is very different when it comes to producing orthogonality. Is `Q'*Q` close to the identity?

- Classic Gram-Schmidt. Usually very poor orthogonality.
- Modified Gram-Schmidt. Depends upon condition of
`X`. Fails completely when`X`is singular. - Householder triangularization. Always good orthogonality.

```
type compare.m
```

function compare(X); % compare(X) % Compare three QR decompositions, % I = eye(size(X)); %% Classic Gram Schmidt [Q,R] = gs(X); qrerr_gs = norm(Q*R-X,inf)/norm(X,inf); ortherr_gs = norm(Q'*Q-I,inf); %% Modified Gram Schmidt [Q,R] = mgs(X); qrerr_mgs = norm(Q*R-X,inf)/norm(X,inf); ortherr_mgs = norm(Q'*Q-I,inf); %% Householder QR Decomposition [U,R] = hqrd(X); QR = R; E = I; for k = size(X,2):-1:1 uk = U(:,k); QR = QR - uk*(uk'*QR); E = E - uk*(uk'*E) - (E*uk)*uk' + uk*(uk'*E*uk)*uk'; end qrerr_h = norm(QR-X,inf)/norm(X,inf); ortherr_h = norm(E-I,inf); %% Report results fprintf('QR error\n') fprintf('Classic: %10.3e\n',qrerr_gs) fprintf('Modified: %10.3e\n',qrerr_mgs) fprintf('Householder: %10.3e\n',qrerr_h) fprintf('\n') fprintf('Orthogonality error\n') fprintf('Classic: %10.3e\n',ortherr_gs) fprintf('Modified: %10.3e\n',ortherr_mgs) fprintf('Householder: %10.3e\n',ortherr_h)

Well conditioned.

compare(magic(7))

QR error Classic: 1.726e-16 Modified: 6.090e-17 Householder: 3.654e-16 Orthogonality error Classic: 3.201e+00 Modified: 1.534e-15 Householder: 1.069e-15

Poorly conditioned, nonsingular.

compare(hilb(7))

QR error Classic: 5.352e-17 Modified: 5.352e-17 Householder: 7.172e-16 Orthogonality error Classic: 5.215e+00 Modified: 1.219e-08 Householder: 1.686e-15

Singular.

compare(magic(8))

QR error Classic: 1.435e-16 Modified: 8.540e-17 Householder: 2.460e-16 Orthogonality error Classic: 5.413e+00 Modified: 2.162e+00 Householder: 2.356e-15

G. W. Stewart, “Matrix Algorithms, Volume 1: Basic Decompositions”, SIAM, 1998.

https://www.amazon.com/Matrix-Algorithms-1-Basic-Decompositions/dp/0898714141

Published with MATLAB® R2016a

]]>At a minisymposium honoring Charlie Van Loan this week during the SIAM Annual Meeting, I will describe several dubious methods for computing the zeros of polynomials. One of the methods is the Graeffe Root-squaring method, which I will demonstrate using my favorite cubic, $x^3-2x-5$.... read more >>

]]>At a minisymposium honoring Charlie Van Loan this week during the SIAM Annual Meeting, I will describe several dubious methods for computing the zeros of polynomials. One of the methods is the Graeffe Root-squaring method, which I will demonstrate using my favorite cubic, $x^3-2x-5$.

What is today often called the Graeffe Root-Squaring method was discovered independently by Dandelin, Lobacevskii, and Graeffe in 1826, 1834 and 1837. A 1959 article by Alston Householder referenced below straightens out the history.

The idea is to manipulate the coefficients of a polynomial to produce a second polynomial whose roots are the squares of the roots of the first. If the original has a dominant real root, it will become even more dominant. When the process is iterated you eventually reach a point where the dominant root can be read off as the ratio of the first two coefficients. All that remains is to take an n-th root to undo the iteration.

Here is an elegant bit of code for producing a cubic whose roots are the squares of the roots of a given cubic.

```
type graeffe
```

function b = graeffe(a) % a = a(1)*x^3 + ... + a(4) is a cubic. % b = graffe(a) is a cubic whose roots are the squares of the roots of a. b = [ a(1)^2 -a(2)^2 + 2*a(1)*a(3) a(3)^2 - 2*a(2)*a(4) -a(4)^2 ];

I discussed my favorite cubic, $z^3-2z-5$, in a series of posts beginning with a historic cubic last December 21st.

A contour plot of the magnitude of this cubic on a square region in the plane shows the dominant real root at approximately $x=2.09$ and the pair of complex conjugate roots with smaller magnitude approximately $|z|=1.55$.

graphic

Repeated application of the transformation essentially squares the coefficients. So the concern is overflow. When I first ran this years ago as a student on the Burroughs B205, I had a limited floating point exponent range and overflow was a severe constraint. Today with IEEE doubles we're a little better off.

Here is a run on my cubic. I'm just showing a few significant digits of the polynomial coefficients because the important thing is their exponents.

a = [1 0 -2 -5]; k = 0; f = '%5.0f %4.0f %5.0f %8.3e %8.3e %8.3e %20.15f \n'; fprintf(f,k,1,a,Inf); while all(isfinite(a)) k = k+1; a = graeffe(a); n = 2^k; r = (-a(2))^(1/n); fprintf(f,k,n,a,r) end

0 1 1 0.000e+00 -2.000e+00 -5.000e+00 Inf 1 2 1 -4.000e+00 4.000e+00 -2.500e+01 2.000000000000000 2 4 1 -8.000e+00 -1.840e+02 -6.250e+02 1.681792830507429 3 8 1 -4.320e+02 2.386e+04 -3.906e+05 2.135184796196703 4 16 1 -1.389e+05 2.316e+08 -1.526e+11 2.096144583759898 5 32 1 -1.883e+10 1.125e+16 -2.328e+22 2.094553557477412 6 64 1 -3.547e+20 -7.504e+32 -5.421e+44 2.094551481347086 7 128 1 -1.258e+41 1.786e+65 -2.939e+89 2.094551481542327 8 256 1 -1.582e+82 -4.203e+130 -8.636e+178 2.094551481542327 9 512 1 -2.504e+164 -9.665e+260 -Inf 2.094551481542327

So after seven steps we have computed the dominant root to double precision accuracy, but it takes the eighth step to confirm that. And after nine steps we run out of exponent range.

This is a really easy example. To make a robust polynomial root finder we would have to confront under/overflow, scaling, multiplicities, complex roots, and higher degree. As far as I know, no one has ever made a serious piece of mathematical software out of the Graeffe Root-squaring method. If you know of one, please contribute a comment.

Alston S. Householder, "Dandelin, Lobacevskii, or Graeffe?," *Amer. Math. Monthly*, 66, 1959, pp. 464–466. <http://www.jstor.org/stable/2310626?seq=1#>

Get
the MATLAB code

Published with MATLAB® R2016a

During the SIAM Annual Meeting this summer in Boston there will be a special minisymposium Wednesday afternoon, July 13, honoring Charlie Van Loan, who is retiring at Cornell. (I use "at" because he's not leaving Ithaca.) I will give a talk titled "19 Dubious Way to Compute the Zeros of a Polynomial", following in the footsteps of the paper about the matrix exponential that Charlie and I wrote in 1978 and updated 25 years later. I really don't have 19 ways to compute polynomial zeros, but then I only have a half hour for my talk. Most of the methods have been described previously in this blog. Today's post is mostly about "roots".... read more >>

]]>During the SIAM Annual Meeting this summer in Boston there will be a special minisymposium Wednesday afternoon, July 13, honoring Charlie Van Loan, who is retiring at Cornell. (I use "at" because he's not leaving Ithaca.) I will give a talk titled "19 Dubious Way to Compute the Zeros of a Polynomial", following in the footsteps of the paper about the matrix exponential that Charlie and I wrote in 1978 and updated 25 years later. I really don't have 19 ways to compute polynomial zeros, but then I only have a half hour for my talk. Most of the methods have been described previously in this blog. Today's post is mostly about "roots".

Almost 40 years ago, in the late 1970s, when I was developing the original Fortran-based MATLAB, I wanted to have a command to find the roots of a polynomial. At the time MATLAB was just a primitive matrix calculator. It was not yet a programming environment; I did not yet have M-files or toolboxes.

I had two objectives. I needed to keep the amount of software to a minimum. Mike Jenkins was a friend of mine who had recently submitted his Ph. D. thesis, under Joe Traub's direction, to Stanford, with what is now known as the Jenkins-Traub algorithm for polynomial zero finding. But that would require way too much memory for my MATLAB. And I wanted to turn the tables on the traditional role the characteristic polynomial plays in linear algebra. First year students are taught to compute eigenvalues of a matrix by finding the zeros of the determinant of its companion matrix. Wilkinson had shown that in the presence of roundoff error this is a bad idea. I wanted to emphasize Wilkinson's point by using powerful matrix eigenvalue techniques on the companion matrix to compute polynomial roots.

I already had the EISPACK code for the Francis double shift QR iteration to find the eigenvalues of a Hessenberg matrix. It took just a few more lines to find polynomial roots.

For example, MATLAB represents this polynomial $z^5 - 15 z^4 + 85 z^3 - 225 z^2 + 274 z - 120$ by a vector of coefficients.

p = [1 -15 85 -225 274 -120]

p = 1 -15 85 -225 274 -120

The `roots` function finds its roots.

roots(p)

ans = 5.0000 4.0000 3.0000 2.0000 1.0000

MATLAB does this by forming the companion matrix, which is upper Hessenberg, and computing its eigenvalues.

A = [15 -85 225 -274 120 eye(4,5) ] eig(A)

A = 15 -85 225 -274 120 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 ans = 5.0000 4.0000 3.0000 2.0000 1.0000

In the original documentation for "roots" I wrote

It uses order n^2 storage and order n^3 time. An algorithm designed specifically for polynomial roots might use order n storage and n^2 time. And the roundoff errors introduced correspond to perturbations in the elements of the companion matrix, not the polynomial coefficients.

So, at the time I knew that "roots" had impeccable numerical properties from the matrix eigenvalue point of view, but I wasn't sure about the numerical properties from the polynomial of view. Were we computing the exact roots of some polynomial near the original one? Maybe not. It depends crucially upon the scaling.

"Roots" is possibly dubious numerically, and certainly dubious from a computation cost point of view.

Almost twenty years later, in 1994 and 1995, a pair of papers, one by Nick Trefethen and K. C. Toh, and one by Alan Edelman and H. Murakami, finally gave "roots" a passing grade numerically, provided the companion matrix is first scaled by balancing.

Balancing was introduced by Parlett and Reinsch in the Algol procedures that spawned EISPACK. Balancing is a diagonal similarity transformation by a matrix of powers of two to attempt to bring a nonsymmetric matrix closer to symmetric by making its row and column norms equal. It is available in MATLAB through the function `balance` and is used by default by `eig`.

To see the importance of balancing, consider the companion matrix of $W_{20}$, the Wilkinson polynomial of degree 20 whose roots are the integers from 1 to 20. The coefficients of this polynomial are huge. The constant term is 20!, which is 2432902008176640000. But that's not the largest. The coefficient of $z^2$ is 13803759753640704000. Five of the coefficients, those of $z^3$ through $z^7$ are so large that they cannot be exactly represented as double precision floating pointing numbers. So

roots(poly(1:20))

starts with a companion matrix that has coefficients of magnitude `10^19` along its first row and ones on its subdiagonal. It is seriously nonsymmetric.

The elements of the balanced matrix are much more reasonable. They range from just 1/4 to 256. This is the matrix involved in the actual eigenvalue calculation. All the scaling action is captured in the diagonal similarity, whose elements range from $2^{-35}$ to $2^{27}$. This matrix is only involved in scaling the eigenvectors.

So I was lucky on the numerical side. Balancing turned out to provide "roots" satisfactory stability.

Let's measure the execution time and memory requirements for "roots" with polynomials of very large degree -- tens of thousands. Here I just wanted to see how bad it is. Short answer: it takes "roots" almost 40 minutes to find the zeros of a polynomial of order sixteen thousand on my Lenovo X250 laptop.

Here is the experiment. Polynomials of degree `1000*n` for `n = 1:16`.

T = zeros(16,1); for n = 1:16 n r = randn(1,1000*n); tic roots(r); t = toc T(n) = t; !systeminfo | grep 'In Use' end

As expected, the observed exection times are fit nicely by a cubic in the polynomial degree.

time_roots

c = 0 1.8134 0.4632

On the other hand, the measured virtual memory requirements are all other the map. A least squares fit by a quadratic in the polynomial degree $n$ shows very little dependence on the $n^2$ term. It's fair to say that on today's machines memory requirements are not a significant restriction for this algorithm.

memory_roots

c = 1.0e+03 * 3.0328 0.0177 0.0002

AMVW are the initials of the last names of Jared Aurentz, Thomas Mach, Raf Vandebril, and David Watkins. It is also the name of their Fortran code for what they claim is a competitor to "roots". Their recent paper in SIMAX, referenced below, summarizes all the work that has been done on computing the eigenvalues of companion matrices, exploiting their special structure.

They also present their own algorithm where the companion matrix is never formed explicitly, but is represented as a product of Givens rotations. The QR algorithm is carried out on this representation. As a result only $O(n)$ storage and $O(n^2)$ operations are required.

But their software is in Fortran. I was a big Fortran fan for a long time. I used to be considered one of the Fortran gurus at MathWorks. I now don't even have access to a Fortran compiler. So I can't try their stuff myself.

So far I've been talking about the classic companion matrix where the polynomial coefficients occupy the entire first or last row or column. In 2003 Miroslav Fiedler introduced another version of a companion matrix, a pentadiagonal matrix with the polynomial coefficients alternating with zeros on the super- and subdiagonal and ones and zeros alternating on the next diagonals of the nonsymmetric pentadiagonal matrix. I wrote about it my blog just before last Christmas.

Here's an example, W_8. First, the coefficients.

c = poly(1:8)'

c = 1 -36 546 -4536 22449 -67284 118124 -109584 40320

The Fiedler companion matrix.

F = fiedler(c(2:end))

F = Columns 1 through 6 36 -546 1 0 0 0 1 0 0 0 0 0 0 4536 0 -22449 1 0 0 1 0 0 0 0 0 0 0 67284 0 -118124 0 0 0 1 0 0 0 0 0 0 0 109584 0 0 0 0 0 1 Columns 7 through 8 0 0 0 0 0 0 0 0 1 0 0 0 0 -40320 0 0

Balance it.

B = balance(F)

B = Columns 1 through 7 36.0000 -17.0625 16.0000 0 0 0 0 32.0000 0 0 0 0 0 0 0 8.8594 0 -5.4807 8.0000 0 0 0 8.0000 0 0 0 0 0 0 0 0 2.0533 0 -0.9012 1.0000 0 0 0 4.0000 0 0 0 0 0 0 0 0 0.8361 0 0 0 0 0 0 0.5000 0 Column 8 0 0 0 0 0 0 -0.6152 0

Check the eigenvalues

e = eig(B)

e = 8.0000 7.0000 6.0000 5.0000 4.0000 3.0000 2.0000 1.0000

If we could find a way to compute the eigenvalues and eigenvectors of the Fiedler companion matrix while exploiting its structure, then we would have a gorgeous algorithm. Beresford Parlett tells me that he's working on it.

If you have software that I don't know about, please comment.

Kim-Chuan Toh and Lloyd N. Trefethen, "Pseudozeros of polynomials and pseudospectra of companion matrices," *Numer. Math.*, 68, 1994. <http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.49.9072&rep=rep1&type=pdf>

Alan Edelman and H. Murakami, "Polynomial roots from companion matrices," *Mathematics of Computation*, 64, 1995. <http://www.ams.org/journals/mcom/1995-64-210/S0025-5718-1995-1262279-2/>

Jared L. Aurentz, Thomas Mach, Raf Vandebril, and David S. Watkins, "Fast and backward stable computation of roots of polynomials," *SIAM J. Matrix Anal. Appl.*, 36, 2015. <http://www.math.wsu.edu/faculty/watkins/pdfiles/AMVW15_SIMAX.pdf>

Miroslav Fiedler, A note on companion matrices, Linear Algebra and its Applications 372 (2003), 325-331, <http://citeseerx.ist.psu.edu/viewdoc/download>

Get
the MATLAB code

Published with MATLAB® R2016a

What does $\sqrt[12]{2}$ have to do with music? What are *equal temperament* and *just intonation*? How can the MATLAB function `rats` help tune a piano? (This post is based in part on the *Music* chapter in my online book, *Experiments in MATLAB*.)... read more >>

What does $\sqrt[12]{2}$ have to do with music? What are *equal temperament* and *just intonation*? How can the MATLAB function `rats` help tune a piano? (This post is based in part on the *Music* chapter in my online book, *Experiments in MATLAB*.)

In the theory of music, an *octave* is an interval with frequencies that range over a factor of two. In most Western music, an octave is divided into 12 *semitones* in a geometric progression. In other words the notes have equal frequency ratios. Since 12 semitones comprise a factor of 2, a single semitone is a factor of $\sqrt[12]{2}$. And because this quantity occurs so often in this discussion, let

$$ \sigma = \sqrt[12]{2} $$

Our MATLAB programs use

```
format short
sigma = 2^(1/12)
```

sigma = 1.0595

Think of $\sigma$ as an important mathematical constant, like $\pi$ and $\phi$.

Here is our miniature piano keyboard with 25 keys.

small_keyboard

This keyboard has two octaves, with white keys labeled **C D … G A B**, plus another **C** key. Counting both white and black, there are twelves keys in each octave. The frequency of each key is a semitone above or below its neighbors. Each black key can be regarded as either the *sharp* of the white below it or the *flat* of the white above it. So the black key between **C** and **D** is both **C** $\sharp$ and **D** $\flat$. There are no **E** $\sharp$ / **F** $\flat$ or **B** $\sharp$ / **C** $\flat$.

A conventional full piano keyboard has 88 keys. Seven complete octaves account for $7 \times 12 = 84$ keys. There are three additional keys at the lower left and one additional key at the upper end. If the octaves are numbered 0 through 7, then a key letter followed by an octave number specifies a unique key. In this notation, two important keys are **C4** and **A4**. The **C4** key is near the center of the keyboard and so is also known as *middle C*. A piano is usually tuned so that the frequency of the **A4** key is 440 Hz. **C4** is nine keys to the left of **A4** so its frequency is

$$\mbox{C4} = 440 \sigma^{-9} \approx 261.6256 \mbox{Hz}$$

Our miniature keyboard has the **A4** key colored green and the **C4** key colored blue.

A piano is almost always tuned with *equal temperament*, the system used by musical instruments with fixed pitch. Starting with **A4** at 440 Hz the keys are tuned in strict geometric progression with ratio $\sigma$. Once this is done, a piano rarely needs adjustment.

The alternative system, *just intonation*, is defined by a vector of ratios involving small integers.

```
format rat
just = [1 16/15 9/8 6/5 5/4 4/3 7/5 3/2 8/5 5/3 7/4 15/8 2]'
```

just = 1 16/15 9/8 6/5 5/4 4/3 7/5 3/2 8/5 5/3 7/4 15/8 2

These ratios allow the generation of overtones, chords and harmony. More about that in a moment. Many musical instruments can be tuned to the key of a particular composition just before playing it. Singers, choirs, and barbershop quartets can naturally use just intonation.

Here is a comparison of equal temperament and just intonation from a strictly numerical point of view. Equal temperament is defined by repeated powers of $\sigma$. while just intonation is defined by a sequence of ratios.

sigma = 2^(1/12); k = (0:12)'; equal = sigma.^k; num = [1 16 9 6 5 4 7 3 8 5 7 15 2]'; den = [1 15 8 5 4 3 5 2 5 3 4 8 1]'; just = num./den; delta = (equal - just)./equal; T = [k equal num den just delta]; fprintf(' k equal just delta\n') fprintf('%4d %12.6f %7d/%d %11.6f %10.4f\n',T')

k equal just delta 0 1.000000 1/1 1.000000 0.0000 1 1.059463 16/15 1.066667 -0.0068 2 1.122462 9/8 1.125000 -0.0023 3 1.189207 6/5 1.200000 -0.0091 4 1.259921 5/4 1.250000 0.0079 5 1.334840 4/3 1.333333 0.0011 6 1.414214 7/5 1.400000 0.0101 7 1.498307 3/2 1.500000 -0.0011 8 1.587401 8/5 1.600000 -0.0079 9 1.681793 5/3 1.666667 0.0090 10 1.781797 7/4 1.750000 0.0178 11 1.887749 15/8 1.875000 0.0068 12 2.000000 2/1 2.000000 0.0000

The last column in the table, `delta`, is the relative difference between the two. We see that `delta` is less than one percent, except for `k = 10`.

This is why the 12-note scale has proved to be so popular. Powers of $\sqrt[12]{2}$ turn out to come very close to important rational numbers, especially 5/4, 4/3, and 3/2. A pair of pitches with a ratio of 3:2 is known as a *perfect fifth*; a ratio of 4:3 is a *perfect fourth*; a ratio of 5:4 is a *major third*.

One of the first songs you learned to sing was

*Do Re Mi Fa So La Ti Do*

If you start at middle C, you will be singing the *major scale* in the key of C. This scale is played on a piano using only the white keys. The steps are not equally spaced. Most of the steps skip over black keys and so are two semitones. But the steps between *Mi* and *Fa* and *Ti* and *Do* are the steps from **E** to **F** and **B** to **C**. There are no intervening black keys and so these steps are only one semitone. In terms of $\sigma$, the C-major scale is

$$\sigma^0 \ \sigma^2 \ \sigma^4 \ \sigma^5 \ \sigma^7 \ \sigma^9

\ \sigma^{11} \ \sigma^{12}$$

The number of semitones between the notes is given by the vector

```
format short
diff([0 2 4 5 7 9 11 12])
```

ans = 2 2 1 2 2 2 1

This sequence of frequencies in our most common scale is surprising.

Like everything else, it all comes down to *eigenvalues*. Musical instruments create sound through the action of vibrating strings or vibrating columns of air that, in turn, produce vibrations in the body of the instrument and the surrounding air. Mathematically, vibrations can be modeled by weighted sums of characteristic functions known as *modes* or *eigenfunctions*. Different modes vibrate at different characteristic frequencies or *eigenvalues*. These frequencies are determined by physical parameters such as the length, thickness and tension in a string, or the geometry of the air cavity. Short, thin, tightly stretched strings have high frequencies, while long, thick, loosely stretched strings have low frequencies.

The simplest model is a one-dimensional vibrating string, held fixed at its ends. The units of the various physical parameters can be chosen so that the length of the string is $2 \pi$. The modes are then simply the functions

$$v_n(x) = \sin{n x}, \ \, n = 1, 2, …$$

Each of these functions satisfy the fixed end point conditions

$$v_n(0) = v_n(2 \pi) = 0$$

The time-dependent modal vibrations are

$$u_n(x,t) = \sin{n x} \sin{n t}, \ \, n = 1, 2, …$$

and the frequency is simply the integer $n$. (Two- and three-dimensional models are much more complicated. This one-dimensional model is all we need here.)

Our Experiments with MATLAB program `vibrating_string` provides a dynamic view. Here is a snapshot showing the first nine modes and the resulting wave traveling along the string.

The ratios in just intonation or equal temperament produce instruments with tunings that generate vibrations with these desired frequencies.

The MATLAB expression `rat(X,tol)` creates a truncated continued fraction approximation of `X` with an accuracy of `tol`. The default `tol` is `1.e-6*norm(X,1)`.

rat(sigma.^(0:12)')

ans = 1 1 + 1/(17 + 1/(-5 + 1/(-2))) 1 + 1/(8 + 1/(6)) 1 + 1/(5 + 1/(4 + 1/(-2))) 1 + 1/(4 + 1/(-7 + 1/(2 + 1/(5)))) 1 + 1/(3 + 1/(-74)) 1 + 1/(2 + 1/(2 + 1/(2 + 1/(2 + 1/(2 + 1/(2)))))) 1 + 1/(2 + 1/(147)) 2 + 1/(-2 + 1/(-2 + 1/(-3 + 1/(4 + 1/(2))))) 2 + 1/(-3 + 1/(-7 + 1/(-81))) 2 + 1/(-5 + 1/(2 + 1/(3 + 1/(-2 + 1/(-15))))) 2 + 1/(-9 + 1/(11)) 2

These are unconventional continued fractions because they contain negative terms. This algorithm is used for `format rat`. The default tolerance is much too picky. The equal temperament ratios do not give small integers.

```
format rat
sigma.^(0:12)'
```

ans = 1 1657/1564 1769/1576 1785/1501 635/504 3544/2655 1393/985 2655/1772 1008/635 3002/1785 1527/857 2943/1559 2

This would not be a satisfactory way to try to tune an instrument. But if I’m much more tolerant and allow a 2 percent error, I get short continued fractions.

rat(sigma.^(0:12)',.02)

ans = 1 1 + 1/(17) 1 + 1/(8) 1 + 1/(5) 1 + 1/(4) 1 + 1/(3) 1 + 1/(2 + 1/(2)) 1 + 1/(2) 2 + 1/(-2 + 1/(-2)) 2 + 1/(-3) 2 + 1/(-5) 2 + 1/(-9) 2

I can make `format rat` use these fractions by telling `rats` that it has only 6 columns to work with. Now I get almost the same ratios as just intonation.

rats(sigma.^(0:12)',6)

ans = 1 1 9/8 6/5 5/4 4/3 7/5 3/2 8/5 5/3 9/5 17/9 2

If any of the music theorists from ancient Greece or the Renaissance had access to MATLAB, they could have used our rational approximation to generate a tuning strategy.

Published with MATLAB® R2016a

]]>