Tuesday, October 8, 2024
HomeMatlabA Treacherous SVD » Cleve’s Nook: Cleve Moler on Arithmetic and Computing

A Treacherous SVD » Cleve’s Nook: Cleve Moler on Arithmetic and Computing


Just a few days in the past, a bug report from our workplace in Cambridge caught my consideration. Computing the singular values and singular vectors of a selected matrix would typically trigger MATLAB to crash.

Contents

Two Computer systems

I exploit two completely different two computer systems recurrently. The machine in my residence workplace is a Lenovo ThinkPad® mannequin T14, loaded with two exterior screens, a number of exterior disc drives, a sound bar and a devoted web connection. My touring machine is a Lenovo ThinkPad X1 Nano with no exterior {hardware}.

The report of a crash within the SVD grew to become much more attention-grabbing when I discovered that it occurs on my workplace laptop, however not on the transportable. A fast verify revealed that the CPUs on the 2 machines come from completely different producers. The workplace laptop makes use of an AMD® Ryzen Professional 5 whereas the touring machine makes use of an Intel® Core i7.

Math Libraries

The crash happens a number of software program layers deep in CGESVD, the LAPACK driver for single precision complicated SVD. Varied chip producers present math libraries which have been optimized for his or her CPUs. Nonetheless, by default, MATLAB makes use of the reportedly quicker Intel Math Kernel Library, MKL. It’s potential to modify to different math libraries.

We have now consultants at MathWorks who know way more concerning the particulars of those libraries than I do. They are going to quickly kind this all out. Within the meantime, here’s what I’ve discovered concerning the offending matrix.

G3366394

We consult with the matrix within the crash report by its case quantity in our bug monitoring system. The matrix is of modest measurement however is in any other case uncommon for a number of causes. It’s rectangular with fewer rows than columns, it’s in single precision, and it’s complicated.

  clear
  load g3366394 X
  whos
  Title        Measurement              Bytes  Class     Attributes
  X         219x384            672768  single    complicated

The next code calling SVD with three outputs will crash MATLAB on my T14, however not on my X1.

  trysvd = false
  if trysvd
      [U,S,V] = svd(X);
      R = U*S*V' - X;
      relres = norm(R)/norm(X)
  finish
  trysvd =
    logical
      0

Rank

Computing the singular values with out the vectors could be finished on both machine. The next code makes use of double precision after which plots the singular values on a logarithmic scale with a line at single precision roundoff stage.

  S = svd(X);
  semilogy(S,'.-')
  ep = eps('single');
  line([0 230],[ep ep])
  axis([0 230 1e-15 10])
  legend({'singular values','eps(''single'')'})


We see that the matrix is way from full rank. About half of its singular values are negligible. That is confirmed by

   xrank = rank(X)
   xrank =
      110

Zero rows

The reason for the low rank is simple to search out. This surf plot reveals that just about half of the rows are flat zero.


Let’s take away the zero rows.

  c = sum(abs(X),2)==0;
  nnzc = nnz(c)
  X(c>0,:) = [];
  nnzc =
     109


The remaining matrix is full rank and it’s secure to compute its singular vectors.

  [U,S,V] = svd(X);
  R = U*S*V' - X;
  resnorm = norm(R)
  resnorm =
     2.8191e-06

Fuzz

Eradicating the zero rows was the primary work-around that I attempted. There are lots of others. You’ll be able to change the zeros with any nonzero “fuzz”.

  fuzz = 1.e-20;
  [U,S,V] = svd(X + fuzz*randn(measurement(X)));
  resnorm = norm(U*S*V'-X)
  resnorm =
     2.8222e-06

Flip

You’ll be able to reorder the matrix in order that the zero rows usually are not in the beginning.

  [U,S,V] = svd(flipud(X));
  U = flipud(U);
  resnorm = norm(U*S*V'-X)
  resnorm =
     2.3809e-06

Now what?

The way to keep away from the crash shouldn’t be a very powerful query. What causes the crash with the unique matrix? We are going to discover out and get it fastened.




Revealed with MATLAB® R2024a



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments