Main Content

単精度数学

この例では、単精度データを使用して線形代数と演算を実行する方法を示します。入力に応じて単精度または倍精度で結果が適切に計算される方法も示します。

倍精度データの作成

最初に、既定で倍精度のデータを作成します。

Ad = [1 2 0; 2 5 -1; 4 10 -1]
Ad = 3×3

     1     2     0
     2     5    -1
     4    10    -1

単精度に変換

関数 single を使用してデータを単精度に変換することができます。

A = single(Ad); % or A = cast(Ad,'single');

単精度の 0 と 1 を作成

さらに単精度の 0 と 1 をそれぞれの関数で作成できます。

n = 1000;
Z = zeros(n,1,'single');
O = ones(n,1,'single');

ワークスペースの変数を見てみましょう。

whos A Ad O Z n
  Name         Size            Bytes  Class     Attributes

  A            3x3                36  single              
  Ad           3x3                72  double              
  O         1000x1              4000  single              
  Z         1000x1              4000  single              
  n            1x1                 8  double              

いくつかの変数の型が single で、変数 A (Ad の単精度バージョン) が格納メモリのバイト数の半分を使用しているのがわかります。これは倍精度が 8 バイト (64 ビット) 必要なのに対し、単精度が 4 バイト (32 ビット) しか必要としないためです。

算術計算と線形代数

単精度に対して標準の算術計算と線形代数を実行します。

B = A'    % Matrix Transpose
B = 3x3 single matrix

     1     2     4
     2     5    10
     0    -1    -1

whos B
  Name      Size            Bytes  Class     Attributes

  B         3x3                36  single              

この演算の結果の B は single であることがわかります。

C = A * B % Matrix multiplication
C = 3x3 single matrix

     5    12    24
    12    30    59
    24    59   117

C = A .* B % Elementwise arithmetic
C = 3x3 single matrix

     1     4     0
     4    25   -10
     0   -10     1

X = inv(A) % Matrix inverse
X = 3x3 single matrix

     5     2    -2
    -2    -1     1
     0    -2     1

I = inv(A) * A % Confirm result is identity matrix
I = 3x3 single matrix

     1     0     0
     0     1     0
     0     0     1

I = A \ A  % Better way to do matrix division than inv
I = 3x3 single matrix

     1     0     0
     0     1     0
     0     0     1

E = eig(A) % Eigenvalues
E = 3x1 single column vector

    3.7321
    0.2679
    1.0000

F = fft(A(:,1)) % FFT
F = 3x1 single column vector

   7.0000 + 0.0000i
  -2.0000 + 1.7321i
  -2.0000 - 1.7321i

S = svd(A) % Singular value decomposition
S = 3x1 single column vector

   12.3171
    0.5149
    0.1577

P = round(poly(A)) % The characteristic polynomial of a matrix
P = 1x4 single row vector

     1    -5     5    -1

R = roots(P) % Roots of a polynomial
R = 3x1 single column vector

    3.7321
    1.0000
    0.2679

Q = conv(P,P) % Convolve two vectors
Q = 1x7 single row vector

     1   -10    35   -52    35   -10     1

R = conv(P,Q)
R = 1x10 single row vector

     1   -15    90  -278   480  -480   278   -90    15    -1

stem(R); % Plot the result

Figure contains an axes object. The axes object contains an object of type stem.

単精度または倍精度のいずれかに対し機能するプログラム

比率が single または double のデータ型に対して正確な計算機イプシロン (eps) より小さくなるように、フィボナッチ数列の十分な項を計算するための関数を見てみましょう。

% How many terms needed to get single precision results?
fibodemo('single')
ans = 19
% How many terms needed to get double precision results?
fibodemo('double')
ans = 41
% Now let's look at the working code.
type fibodemo
function nterms = fibodemo(dtype)
%FIBODEMO Used by SINGLEMATH demo.
% Calculate number of terms in Fibonacci sequence.

% Copyright 1984-2014 The MathWorks, Inc.

fcurrent = ones(dtype);
fnext = fcurrent;
goldenMean = (ones(dtype)+sqrt(5))/2;
tol = eps(goldenMean);
nterms = 2;
while abs(fnext/fcurrent - goldenMean) >= tol
   nterms = nterms + 1;
   temp  = fnext;
   fnext = fnext + fcurrent;
   fcurrent = temp;
end

いくつかの変数 (fcurrentfnextgoldenMean) が入力データ型に依存する値で初期化され、許容誤差 tol もその型に依存することに注意してください。単精度で計算に必要とされる項の数は、等価な倍精度計算より少なくなっています。