# Sporth

 Other toolboxes required sporth Returns a sparse orthonormal basis for the range none spnull Helper functions license_sporth.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.

sporth is a function that computes an orthonormal basis for the range 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 orth(full(S)). This function is useful in particular for computing the rank of a sparse matrix without having to use the (slow) rank(full(S)) or the (often inaccurate) sprank(S).

## Syntax

• Q = sporth(S)
• Q = sporth(S,TOL)
• [Q,r] = sporth(S,TOL)

## Argument descriptions

### Input arguments

• S: The matrix to have its range computed.
• TOL (optional, default norm(S,'fro') * eps(class(S))): The numerical tolerance used.

### Output arguments

• Q: A matrix whose columns form an orthonormal basis for the range of S.
• r (optional): The rank of S.

## Examples

The following example gives a 4-by-4 matrix whose range is spanned by the two vectors $[1,0,0,0]^T$ and $[0,0,1,0]^T$:

>> S = sparse(4,4);
>> S(1,1) = 1; S(3,2) = 1;
>> sporth(S)

ans =

(1,1)        1
(3,2)        1

Note that the output is sparse because S is sparse. If the input is full then the output will be full as well:

>> sporth(full(S))

ans =

1     0
0     0
0    -1
0     0

If we just want to compute the rank of S, we can do the following:

>> [~,r] = sporth(S)

r =

2

## Source code

Click on "expand" to the right to view the MATLAB source code for this function.

1. function [Q r] = sporth(S,varargin)
2. % Q = sporth(S)
3. % returns an (sparse) orthonormal basis for the range of S.
4. % That is, Q'*Q = I, the columns of Q span the same space as
5. % the columns of S, and the number of columns of Q is the
6. % rank of S.
7. %
8. % [~, r] = sporth(S)
9. % returns the rank of S
10. %
11. % If S is sparse, Q is obtained from the QR decomposition.
12. % Otherwise, Q is obtained from the SVD decomposition
13. %
14. % Bruno Luong <brunoluong@yahoo.com>
15. % History
16. %   10-May-2010: original version
17. %   13-Dec-2012: added an optional second argument TOL, which specifies the
18. %   tolerance (Nathaniel Johnston <nathaniel@njohnston.ca>)
19. %
20. % See also SPNULL, NULL, QR, SVD, ORTH, RANK
21. 
22. if issparse(S)
23.     m = size(S,1);
24.     try
25.         [Q R E] = qr(S); %#ok %full QR
26.         if m > 1
27.             s = diag(R);
28.         elseif m == 1
29.             s = R(1);
30.         else
31.             s = 0;
32.         end
33.         s = abs(s);
34.         if(nargin == 1)
35.             tol = norm(S,'fro') * eps(class(S));
36.         else
37.             tol = varargin{1};
38.         end
39.         r = sum(s > tol);
40.         Q = Q(:,1:r);
41.     catch %#ok
42.         % sparse QR is not available on old Matlab versions
43.         err = lasterror(); %#ok
44.         if strcmp(err.identifier, 'MATLAB:maxlhs')
45.             Q = orth(full(S));
46.         else
47.             rethrow(err);
48.         end
49.     end
50. else % Full matrix
51.     Q = orth(S);
52. end
53. 
54. r = size(Q,2);
55. 
56. end