Another day, another way it doesn’t do what it say

May 29, 2010 at 3:14 am (lying documentation, matlab is bad at math, Simple trivia about its fundamental behavior that you probably can't answer)

The documentation for sparse claims,

S = sparse(i,j,s,m,n,nzmax) uses vectors i, j, and s to generate an m-by-n sparse matrix such that S(i(k),j(k)) = s(k).

This claim is, plainly, false. That is to say, there are easy to find values of i, j and s where

sparse(i,j,s,m,n,nzmax)

produces numerically wildly different results from

X = zeros(m, n)
X(sub2ind([m n], i(:), j(:))) = s(:);

and, therefore,

S = sparse(i,j,s,m,n,nzmax)
if all( S( sub2ind(size(S), i, j) == s )
print("documentation passes!")
else
print("documentation is lying!")
end

produces the expected result (i.e. the result you expect from its being written on this blog.)

Can you figure out under what condition sparse behaves differently from the promises the documentation makes?

Hint: I’m populating a stochastic matrix that encodes dynamics over a discretized state space. That space has boundary conditions, so that while in the middle of the state space you might diffuse in N dimensions to your 2^N nearest grid points, at the edges of the state space you are stuck at the wall and have to reflect onto fewer points.

For extra credit, comment on the ease of converting code using non-sparse matrices to code using sparse matrices, given that it won’t even be numerically the same to begin with.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: