Spnull
From QETLAB
spnull | |
Returns a sparse orthonormal basis for the null space | |
Other toolboxes required | none |
---|---|
Related functions | sporth |
Function category | Helper functions |
License | license_spnull.txt |
This is a helper function that only exists to aid other functions in QETLAB. If you are an end-user of QETLAB, you likely will never have a reason to use this function. |
spnull is a function that computes an orthonormal basis for the null space of a full or sparse matrix. When the matrix is sparse, this computation is performed via the QR decomposition and is typically much faster than using null(full(S)).
Syntax
- Z = spnull(S)
- Z = spnull(S,varargin)
Argument descriptions
- S: The matrix to have its null space computed.
- varargin (optional): Extra arguments that, if S is full, will be passed to MATLAB's null function.
Examples
The following example gives a 4-by-4 matrix whose null space is spanned by the two vectors $[0,0,1,0]^T$ and $[0,0,0,1]^T$:
Note that the output is sparse because S is sparse. If the input is full then the output will be full as well:
Source code
Click on "expand" to the right to view the MATLAB source code for this function.
function Z = spnull(S, varargin)
% Z = SPNULL(S)
% returns a sparse orthonormal basis for the null space of S, that is,
% S*Z has negligible elements, and Z'*Z = I
%
% If S is sparse, Z is obtained from the QR decomposition.
% Otherwise, Z is obtained from the SVD decomposition
%
% Bruno Luong <brunoluong@yahoo.com>
% History
% 10-May-2010: original version
%
% See also SPORTH, NULL, QR, SVD, ORTH, RANK
if issparse(S)
[m n] = size(S);
try
[Q R E] = qr(S.'); %#ok %full QR
if m > 1
s = diag(R);
elseif m == 1
s = R(1);
else
s = 0;
end
s = abs(s);
tol = norm(S,'fro') * eps(class(S));
r = sum(s > tol);
Z = Q(:,r+1:n);
catch %#ok
% sparse QR is not available on old Matlab versions
err = lasterror(); %#ok
if strcmp(err.identifier, 'MATLAB:maxlhs')
Z = null(full(S), varargin{:});
else
rethrow(err);
end
end
else % Full matrix
Z = null(S, varargin{:});
end
end
External links
- Sparse null space and orthogonal: The source of this file on MATLAB File Exchange