Main Content

fixed.svd

Fixed-point Golub-Kahan-Reinsch singular value decomposition

Since R2022b

Description

S = fixed.svd(A) returns the singular values of matrix A in descending order.

example

[U,S,V] = fixed.svd(A) performs a singular value decomposition of matrix A such that A = U*S*V'. S is a diagonal matrix of the same dimension as A with nonnegative diagonal elements in decreasing order. U and V are unitary matrices.

example

[___] = fixed.svd(A,"econ") produces an economy-size decomposition of A. If A is an m-by-n matrix, then:

  • m > n — Only the first n columns of U are computed and S is n-by-n.

  • m = nfixed.svd(A,"econ") is equivalent to fixed.svd(A).

  • m < n — Only the first m columns of V are computed, and S is m-by-m.

example

[___] = fixed.svd(A,0) produces a different economy-size decomposition of A. If A is an m-by-n matrix, then:

  • m > nfixed.svd(A,0) is equivalent to fixed.svd(A,"econ").

  • m <= nfixed.svd(A,0) is equivalent to fixed.svd(A).

Syntax is not recommended. Use the "econ" option instead.

example

[___] = fixed.svd(___,sigmaForm) optionally specifies the output format for the singular values. You can use this option with any of the previous input or output combinations. Specify "vector" to return the singular values as a column vector. Specify "matrix" to return the singular values in a diagonal matrix.

example

Examples

collapse all

Compute the singular values of a full rank scaled-double matrix.

A = [1 0 1; -1 -2 0; 0 1 -1];

Define fixed-point types that will never overflow. First, use the fixed.singularValueUpperBound function to determine the upper bound on the singular values. Then, define the integer length based on the value of the upper bound, with one additional bit for the sign and another additional bit for intermediate CORDIC growth. Compute the fraction length based on the integer length and the desired word length.

svdUpperBound = fixed.singularValueUpperBound(3,3,max(abs(A(:))));
integerLength = ceil(log2(svdUpperBound)) + 2;
wordLength = 16;
fractionLength = wordLength - integerLength;

Cast the matrix A to the scaled-double type.

T.A = fi([],1,wordLength,fractionLength,'DataType','ScaledDouble');
A = cast(A,'like',T.A)
A = 
     1     0     1
    -1    -2     0
     0     1    -1


          DataTypeMode: Scaled double: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 11

Compute the singular values.

s = fixed.svd(A)
s = 
    2.4605
    1.6996
    0.2392


          DataTypeMode: Scaled double: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 11

The singular values are returned in a column vector in decreasing order, and have the same data type as A.

Find the singular value decomposition of the rectangular fixed-point matrix A.

Define the rectangular matrix A.

m = 4;
n = 2;
rng('default');
A = 10*randn(m,n);

Define fixed-point types that will never overflow. First, use the fixed.singularValueUpperBound function to determine the upper bound on the singular values. Then, define the integer length based on the value of the upper bound, with one additional bit for the sign and another additional bit for intermediate CORDIC growth. Compute the fraction length based on the integer length and the desired word length.

svdUpperBound = fixed.singularValueUpperBound(m,n,max(abs(A(:))));
integerLength = ceil(log2(svdUpperBound)) + 2;
wordLength = 32;
fractionLength = wordLength - integerLength;

Cast the matrix A to the signed fixed-point type.

T.A = fi([],1,wordLength,fractionLength,'DataType','Fixed');
A = cast(A,'like',T.A)
A = 
    5.3767    3.1877
   18.3389  -13.0769
  -22.5885   -4.3359
    8.6217    3.4262

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 24

Find the singular value decomposition of the fixed-point matrix A.

[U,S,V] = fixed.svd(A)
U = 
    0.1590    0.2717   -0.9387   -0.1403
    0.6397   -0.7548   -0.1219    0.0790
   -0.7049   -0.5057   -0.3224    0.3787
    0.2619    0.3174         0    0.9114

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 30
S = 
   31.0141         0
         0   14.1289
         0         0
         0         0

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 24
V = 
    0.9920    0.1259
   -0.1259    0.9920

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 30

Confirm the relation A = U*S*V'.

U*S*V'
ans = 
    5.3767    3.1877
   18.3389  -13.0769
  -22.5885   -4.3359
    8.6217    3.4262

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 99
        FractionLength: 84

Calculate the complete and economy-size decomposition of a rectangular fixed-point matrix.

Define the matrix A.

A = [1 2; 3 4; 5 6; 7 8];

Define fixed-point types that will never overflow. First, use the fixed.singularValueUpperBound function to determine the upper bound on the singular values. Then, define the integer length based on the value of the upper bound, with one additional bit for the sign and another additional bit for intermediate CORDIC growth. Compute the fraction length based on the integer length and the desired word length.

svdUpperBound = fixed.singularValueUpperBound(4,2,max(abs(A(:))));
integerLength = ceil(log2(svdUpperBound)) + 2;
wordLength = 32;
fractionLength = wordLength - integerLength;

Cast the matrix A to the signed fixed-point type.

T.A = fi([],1,wordLength,fractionLength,'DataType','Fixed');
A = cast(A,'like',T.A)
A = 
     1     2
     3     4
     5     6
     7     8

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 25

Compute the complete decomposition.

[U,S,V] = fixed.svd(A)
U = 
   -0.1525    0.8226   -0.4082    0.3651
   -0.3499    0.4214    0.8165   -0.1826
   -0.5474    0.0201   -0.4082   -0.7303
   -0.7448   -0.3812         0    0.5477

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 30
S = 
   14.2691         0
         0    0.6268
         0         0
         0         0

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 25
V = 
   -0.6414   -0.7672
   -0.7672    0.6414

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 30

Compute the economy-size decomposition.

[U,S,V] = fixed.svd(A,"econ")
U = 
   -0.1525    0.8226
   -0.3499    0.4214
   -0.5474    0.0201
   -0.7448   -0.3812

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 30
S = 
   14.2691         0
         0    0.6268

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 25
V = 
   -0.6414   -0.7672
   -0.7672    0.6414

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 30

Since A is 4-by-2, fixed.svd(A,"econ") returns fewer columns in U and fewer rows in S compared to a complete decomposition. Extra rows of zeros in S are excluded, along with the corresponding columns in U that would multiply with those zeros in the expression A = U*S*V'.

Create a 3-by-3 magic square matrix and calculate the singular value decomposition. By default, the fixed.svd function returns the singular values in a diagonal matrix when you specify multiple outputs.

Define the matrix A.

m = 3; 
n = m;
A = magic(m);

Define fixed-point types that will never overflow. First, use the fixed.singularValueUpperBound function to determine the upper bound on the singular values. Then, define the integer length based on the value of the upper bound, with one additional bit for the sign and another additional bit for intermediate CORDIC growth. Compute the fraction length based on the integer length and the desired word length.

svdUpperBound = fixed.singularValueUpperBound(m,n,max(abs(A(:))));
integerLength = ceil(log2(svdUpperBound)) + 2;
wordLength = 32;
fractionLength = wordLength - integerLength;

Cast the matrix A to the signed fixed-point type.

T.A = fi([],1,wordLength,fractionLength,'DataType','Fixed');
A = cast(A,'like',T.A)
A = 
     8     1     6
     3     5     7
     4     9     2

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 25

Compute the singular value decomposition.

[U,S,V] = fixed.svd(A)
U = 
    0.5774   -0.7071   -0.4082
    0.5774    0.0000    0.8165
    0.5774    0.7071   -0.4082

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 30
S = 
   15.0000         0         0
         0    6.9282         0
         0         0    3.4641

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 25
V = 
    0.5774   -0.4082   -0.7071
    0.5774    0.8165   -0.0000
    0.5774   -0.4082    0.7071

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 30

Specify the "vector" option to return the singular values in a column vector.

[U,S,V] = fixed.svd(A,"vector")
U = 
    0.5774   -0.7071   -0.4082
    0.5774    0.0000    0.8165
    0.5774    0.7071   -0.4082

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 30
S = 
   15.0000
    6.9282
    3.4641

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 25
V = 
    0.5774   -0.4082   -0.7071
    0.5774    0.8165   -0.0000
    0.5774   -0.4082    0.7071

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 30

If you specify one output argument, such as S = fixed.svd(A), then fixed.svd switches behavior to return the singular values in a column vector by default. In that case, you can specify the "matrix" option to return the singular values as a diagonal matrix.

Compute the fixed-point singular value decomposition, verify the results, and generate purely integer C code.

Define the input matrix A.

m = 10; 
n = 4;
rng('default');
A = 10*randn(m,n);

The fixed.svd function also accepts complex inputs.

A = 10*complex(rand(m,n),rand(m,n));

Define fixed-point types that will never overflow. Use the fixed.singularValueUpperBound function to determine the upper bound on the singular values. Define the integer length based on the value of the upper bound, with one additional bit for the sign and another additional bit for intermediate CORDIC growth. Compute the fraction length based on the integer length and the desired word length.

svdUpperBound = fixed.singularValueUpperBound(m,n,max(abs(A(:))));
integerLength = ceil(log2(svdUpperBound)) + 2;
wordLength = 32;
fractionLength = wordLength - integerLength;

Specify the desired data type for the input matrix A.

dataType = 'Fixed';
T.A = fi([],1,wordLength,fractionLength,'DataType',dataType);
disp(T.A)
[]

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 23

Cast the matrix A to the signed fixed-point type.

A = cast(A,'like',T.A);

Generate a MATLAB® executable (MEX) file for execution speed. Use the "econ" flag to compute the economy-size singular-value decomposition. Use the "vector" flag to return the singular values as a vector, s. The flags must be constant for code generation. Use the -nargout 3 flag to indicate to the codegen function that it is to generate code for the three-output syntax.

codegen fixed.svd -o fixedSVD_mex -args {A,coder.Constant("econ"),coder.Constant("vector")} -nargout 3
Code generation successful.

Run the MEX file.

[U,s,V] = fixedSVD_mex(A,"econ","vector")
U = 
   0.2509 + 0.1236i   0.1980 + 0.1578i   0.1745 + 0.0268i  -0.0755 + 0.2443i
   0.1601 + 0.2073i  -0.3227 - 0.1684i   0.1420 - 0.3385i  -0.3686 + 0.1899i
   0.2937 + 0.1868i   0.0574 - 0.2108i   0.0884 - 0.0633i   0.1079 - 0.0866i
   0.2071 + 0.3019i   0.0678 + 0.1740i  -0.3790 + 0.1518i   0.3808 - 0.1483i
   0.2262 + 0.2405i   0.5884 - 0.1889i  -0.0693 - 0.3624i  -0.0547 + 0.1581i
   0.2435 + 0.2111i  -0.0568 + 0.3536i  -0.1593 + 0.1445i  -0.3714 + 0.0026i
   0.2195 + 0.2411i  -0.0713 - 0.2517i  -0.2848 + 0.2641i  -0.4055 + 0.0481i
   0.1461 + 0.3184i  -0.1657 + 0.0477i   0.0478 + 0.2812i   0.2972 + 0.0396i
   0.2204 + 0.1354i  -0.2868 - 0.0745i   0.3628 - 0.2391i   0.0565 - 0.1251i
   0.1863 + 0.2334i  -0.1495 + 0.0491i   0.1422 - 0.1789i   0.3594 - 0.0775i

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 30
s = 
   45.9444
   16.1174
   10.7897
    8.3153

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 23
V = 
   0.5637 + 0.0000i  -0.4038 + 0.0000i  -0.2061 + 0.0000i   0.6904 + 0.0000i
   0.4261 + 0.0228i  -0.3782 - 0.4565i   0.0076 + 0.3326i  -0.5667 - 0.1863i
   0.4980 + 0.0276i   0.3223 + 0.4193i  -0.5081 - 0.2300i  -0.3698 + 0.1540i
   0.5014 + 0.0041i   0.4531 - 0.0243i   0.7280 - 0.0758i   0.0729 - 0.0402i

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 30

Verify the singular values. Since singular values are unique, you can use the svd function to verify that fixed.svd gives a comparable result within the precision of the selected fixed-point type.

sExpected = svd(double(A))
sExpected = 4×1

   45.9444
   16.1174
   10.7897
    8.3153

singularValueRelativeError = norm(double(s)-double(sExpected))/norm(double(sExpected))
singularValueRelativeError = 
4.8247e-07

Singular vectors are not unique. You can verify the singular vectors by confirming that A ≈ U*S*V' and that the singular vector matrices are orthonormal.

First, expand the singular value vector s into matrix S.

S = zeros(size(U,2),size(V,2),'like',s);
for i = 1:min(m,n)
    S(i,i) = s(i);
end
S
S = 
   45.9444         0         0         0
         0   16.1174         0         0
         0         0   10.7897         0
         0         0         0    8.3153

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 32
        FractionLength: 23

Verify that U*S*V' is approximately equal to A.

decompositionRelativeError = norm(double(U*S*V')-double(A))/norm(double(A))
decompositionRelativeError = 
4.5856e-07

U and V are orthonormal. Verify that U'U and V'V are approximately equal to the identity matrix.

UtransposeU = double(U'*U)
UtransposeU = 4×4 complex

   1.0000 + 0.0000i  -0.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 - 0.0000i
  -0.0000 - 0.0000i   1.0000 + 0.0000i  -0.0000 + 0.0000i   0.0000 + 0.0000i
   0.0000 - 0.0000i  -0.0000 - 0.0000i   1.0000 + 0.0000i   0.0000 + 0.0000i
   0.0000 + 0.0000i   0.0000 - 0.0000i   0.0000 - 0.0000i   1.0000 + 0.0000i

VtransposeV = double(V'*V)
VtransposeV = 4×4 complex

   1.0000 + 0.0000i  -0.0000 - 0.0000i  -0.0000 - 0.0000i  -0.0000 - 0.0000i
  -0.0000 + 0.0000i   1.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 - 0.0000i
  -0.0000 + 0.0000i   0.0000 - 0.0000i   1.0000 + 0.0000i  -0.0000 - 0.0000i
  -0.0000 + 0.0000i   0.0000 + 0.0000i  -0.0000 + 0.0000i   1.0000 + 0.0000i

Generate C code. If the input is fixed point, you can verify that the generated C code consists only of integer types.

cfg = coder.config('lib');

if isfi(A) && isfixed(A)
    cfg.PurelyIntegerCode = true;
end

codegen fixed.svd -args {A, coder.Constant("econ"), coder.Constant("vector")} -config cfg -nargout 3 -launchreport
Code generation successful: To view the report, open('codegen/lib/fixed_svd/html/report.mldatx')

The MATLAB code for fixed.svd does not appear in the code generation report because fixed.svd is a MATLAB toolbox function.

Input Arguments

collapse all

Input matrix, specified as a matrix. A can be a signed fixed-point fi, a signed scaled double fi, double, or single data type.

Data Types: single | double | fi
Complex Number Support: Yes

Output format of singular values, specified as one of these values:

  • "vector"S is a column vector. This behavior is the default when you specify one output, S = fixed.svd(A).

  • "matrix"S is a diagonal matrix. This behavior is the default when you specify multiple outputs, [U,S,V] = fixed.svd(A).

Example: [U,S,V] = fixed.svd(X,"vector") returns S as a column vector instead of a diagonal matrix.

Example: S = fixed.svd(X,"matrix") returns S as a diagonal matrix instead of a column vector.

Data Types: char | string

Output Arguments

collapse all

Left singular vectors, returned as the columns of a matrix.

For fixed-point and scaled-double inputs, U is returned as a signed fixed-point or scaled-double fi with the same word length as A and fraction length equal to two less than the word length. One of these integer bits is used for the sign. The other integer bit allows +1 to be represented exactly.

For floating-point input, U has the same data type as A.

Singular values, returned as a diagonal matrix or column vector. The singular values are nonnegative and returned in decreasing order. The singular values S have the same data type as A.

Right singular vectors, returned as the columns of a matrix.

For fixed-point input and scaled-double inputs, V is returned as a signed fixed-point or scaled-double fi with the same word length as A and fraction length equal to two less than the word length. One of these integer bits is used for the sign. The other integer bit allows +1 to be represented exactly.

For floating-point input, V has the same data type as A. One of these integer bits is used for the sign, and the other integer bit allows +1 to be represented exactly.

Tips

The fixed.svd function allows full control over the fixed-point types. fixed.svd computes in-place in the same data type as the input, which may overflow but will produce efficient code. The svd function adjusts the data type of a fixed-point input to avoid overflow and increase precision.

Algorithms

The Golub-Kahan-Reinsch algorithm is a sequential method that performs well on serial computers. For parallel computing, as in FPGA and ASIC applications, use the fixed.jacobiSVD function.

Extended Capabilities

Version History

Introduced in R2022b

See Also

|