matlab.unittest.constraints.AbsoluteTolerance クラス
パッケージ: matlab.unittest.constraints
スーパークラス: matlab.unittest.constraints.Tolerance
数値の絶対許容誤差
説明
この数値 Tolerance
により、実際の値と期待された値の差分の振幅が評価されます。許容誤差を満たすには、abs(expVal - actVal) <= absTol
が成り立たなければなりません。
構築
AbsoluteTolerance(tolVals)
は、実際の値と期待される値との差の大きさを評価する絶対許容誤差オブジェクトを作成します。
AbsoluteTolerance
コンストラクターへの入力のデータ型により、許容誤差でサポートされるデータ型が決まります。たとえば、AbsoluteTolerance(10*eps)
は倍精度数値配列を比較するための AbsoluteTolerance
を作成し、AbsoluteTolerance(int8(2))
は int8
型の数値配列を比較するための AbsoluteTolerance
を作成します。比較される実際の値と期待された値に複数の数値データ型が含まれる場合は、コンストラクターに渡された値によって指定されたデータ型のみに許容誤差が適用されます。
異なるデータ型に対して別々の許容誤差値を指定するために、コンストラクターに複数の許容誤差値を渡すことができます。たとえば、AbsoluteTolerance(10*eps, 10*eps('single'), int8(1))
は次の絶対許容誤差を適用する AbsoluteTolerance
オブジェクトを作成します。
10*eps
は、倍精度数値配列に対して10*eps
の絶対許容誤差を適用します。10*eps('single')
は、単精度数値配列に対して10*eps
の絶対許容誤差を適用します。int8(1)
は、int8
型の数値配列に対して1
の絶対許容誤差を適用します。
許容誤差と &
および |
演算子を組み合わせることにより、特定のデータ型に対して複数の許容誤差を指定できます。2 つの許容誤差を組み合わせるには、各データ型の許容誤差値のサイズの間に互換性がなければなりません。
入力引数
|
数値許容誤差。数値配列のコンマ区切りリストとして指定します。各入力引数には、特定のデータ型に対する許容誤差の指定が含まれます。各数値配列には、実際の値および期待された値と同じサイズの配列またはスカラーを指定できます。 |
プロパティ
|
数値許容誤差。入力引数 |
コピーのセマンティクス
値。値クラスがコピー操作に与える影響については、オブジェクトのコピーを参照してください。
例
絶対許容誤差を使用したテスト
対話型テスト用にテスト ケースを作成します。
import matlab.unittest.TestCase import matlab.unittest.constraints.IsEqualTo import matlab.unittest.constraints.AbsoluteTolerance testCase = TestCase.forInteractiveUse;
実際の値 4.1
と期待された値 4.5
の差が 0.5
未満であることをアサートします。
testCase.assertThat(4.1, IsEqualTo(4.5, ... 'Within', AbsoluteTolerance(0.5)))
Assertion passed.
異なるデータ型に対する絶対許容誤差の指定
対話型テスト用にテスト ケースを作成します。
import matlab.unittest.TestCase import matlab.unittest.constraints.IsEqualTo import matlab.unittest.constraints.AbsoluteTolerance testCase = TestCase.forInteractiveUse;
実際の cell 配列と期待された cell 配列を、次のように作成します。
act = {'abc', 123, single(106), int8([1, 2, 3])}; exp = {'abc', 122, single(105), int8([2, 4, 6])};
値 2
の範囲内で、配列が AbsoluteTolerance
制約を満たすかどうかをテストします。
testCase.verifyThat(act, IsEqualTo(exp, ... 'Within', AbsoluteTolerance(2)))
Verification failed. --------------------- Framework Diagnostic: --------------------- IsEqualTo failed. --> Path to failure: <Value>{3} --> NumericComparator failed. --> The numeric values are not equal using "isequaln". --> The tolerance was ignored. The tolerance as specified does not support comparisons of single values. --> Failure table: Actual Expected Error RelativeError ______ ________ _____ _____________ 106 105 1 0.00952381 Actual single: 106 Expected single: 105 Actual cell: 'abc' [123] [106] [1×3 int8] Expected cell: 'abc' [122] [105] [1×3 int8]
許容誤差は double
データ型のにみ適用されるため、テストは失敗します。
異なるデータ型に対して異なる許容誤差を指定する許容誤差オブジェクトを作成します。
tolObj = AbsoluteTolerance(2, single(3), int8([2, 3, 5]));
double
値のデータに許容誤差 2
が適用されます。single
値のデータに許容誤差 3
が適用されます。int8
値のデータの対応する配列要素に許容誤差 [2 3 5]
が適用されます。
期待された値と実際の値が AbsoluteTolerance
制約を満たすことを検証します。
testCase.verifyThat(act, IsEqualTo(exp, 'Within', tolObj))
Interactive verification passed.
絶対許容誤差と相対許容誤差の組み合わせ
対話型テスト用にテスト ケースを作成します。
import matlab.unittest.TestCase import matlab.unittest.constraints.IsEqualTo import matlab.unittest.constraints.AbsoluteTolerance import matlab.unittest.constraints.RelativeTolerance testCase = TestCase.forInteractiveUse;
pi
の実際の値の近似を定義します。
act = 3.14;
実際の値と期待された値の差異が 0.001
および 0.25%
以内であることをテストするために、許容誤差オブジェクトを作成します。
tolObj = AbsoluteTolerance(0.001) & RelativeTolerance(0.0025);
実際の値が pi
の期待された値の許容誤差の範囲内であることを検証します。
testCase.verifyThat(act, IsEqualTo(pi, 'Within', tolObj))
Verification failed. --------------------- Framework Diagnostic: --------------------- IsEqualTo failed. --> NumericComparator failed. --> The numeric values are not equal using "isequaln". --> AndTolerance failed. --> AbsoluteTolerance failed. --> The error was not within absolute tolerance. --> RelativeTolerance passed. --> The error was within relative tolerance. --> Failure table: Actual Expected Error RelativeError AbsoluteTolerance RelativeTolerance ______ ________________ ____________________ _____________________ _________________ _________________ 3.14 3.14159265358979 -0.00159265358979299 -0.000506957382897213 0.001 0.0025 Actual double: 3.140000000000000 Expected double: 3.141592653589793
実際の値は、AbsoluteTolerance
制約を満たしません。
値が 0.001
または 0.25%
以内の場合に満たされる制約を作成し、実際の値を再テストします。
tolObj = AbsoluteTolerance(0.001) | RelativeTolerance(0.0025);
testCase.verifyThat(act, IsEqualTo(pi, 'Within', tolObj))
Verification passed.
絶対許容誤差と相対許容誤差を組み合わせて小さな値と大きな値をテスト
許容誤差を組み合わせて、値の等価性のテストで値が 0 に近いと絶対 (負方向の丸め) 許容誤差が優位になり、大きな値では相対許容誤差が優位になるように設定します。
対話型テスト用にテスト ケースを作成します。
import matlab.unittest.TestCase import matlab.unittest.constraints.IsEqualTo import matlab.unittest.constraints.AbsoluteTolerance import matlab.unittest.constraints.RelativeTolerance testCase = TestCase.forInteractiveUse;
真空の電磁特性を含む 2 つの構造体を定義します。1 つは、真空内の光の透磁率と速度の近似値を含む構造体 approxVacuumProps
です。
approxVacuumProps.Permeability = 1.2566e-06; % Approximate approxVacuumProps.Permitivity = 8.854187817*10^-12; approxVacuumProps.LightSpeed = 2.9979e+08; % Approximate baselineVacuumProps.Permeability = 4*pi*10^-7; baselineVacuumProps.Permitivity = 8.854187817*10^-12; baselineVacuumProps.LightSpeed = 1/sqrt(... baselineVacuumProps.Permeability*baselineVacuumProps.Permitivity);
近似値とベースライン値の相対差が eps*1e11
以内であることをテストします。
testCase.verifyThat(approxVacuumProps, IsEqualTo(baselineVacuumProps, ... 'Within', RelativeTolerance(eps*1e11)))
Verification failed. --------------------- Framework Diagnostic: --------------------- IsEqualTo failed. --> Path to failure: <Value>.Permeability --> NumericComparator failed. --> The numeric values are not equal using "isequaln". --> RelativeTolerance failed. --> The error was not within relative tolerance. --> Failure table: Actual Expected Error RelativeError RelativeTolerance __________ ____________________ _____________________ _____________________ ____________________ 1.2566e-06 1.25663706143592e-06 -3.70614359173257e-11 -2.94925536216295e-05 2.22044604925031e-05 Actual double: 1.256600000000000e-06 Expected double: 1.256637061435917e-06 Actual struct: Permeability: 1.256600000000000e-06 Permitivity: 8.854187816999999e-12 LightSpeed: 299790000 Expected struct: Permeability: 1.256637061435917e-06 Permitivity: 8.854187816999999e-12 LightSpeed: 2.997924580105029e+08
透磁率の相対差が許容誤差の範囲内にないため、テストは失敗します。2 つの値の差が小さくても、数値が 0 に近いため、サイズに対する相対的差異は許容誤差を満たすほど十分に小さくありません。
近似値とベースライン値の絶対差が 1e-4
以内であることをテストするために、許容誤差オブジェクトを作成します。
testCase.verifyThat(approxVacuumProps, IsEqualTo(baselineVacuumProps, ... 'Within', AbsoluteTolerance(1e-4)))
Verification failed. --------------------- Framework Diagnostic: --------------------- IsEqualTo failed. --> Path to failure: <Value>.LightSpeed --> NumericComparator failed. --> The numeric values are not equal using "isequaln". --> AbsoluteTolerance failed. --> The error was not within absolute tolerance. --> Failure table: Actual Expected Error RelativeError AbsoluteTolerance _________ ________________ _________________ ____________________ _________________ 299790000 299792458.010503 -2458.01050287485 -8.1990404935028e-06 0.0001 Actual double: 299790000 Expected double: 2.997924580105029e+08 Actual struct: Permeability: 1.256600000000000e-06 Permitivity: 8.854187816999999e-12 LightSpeed: 299790000 Expected struct: Permeability: 1.256637061435917e-06 Permitivity: 8.854187816999999e-12 LightSpeed: 2.997924580105029e+08
光の速度の絶対差が許容誤差の範囲内ではないため、テストは失敗します。2 つの値の差異は、そのサイズに対しては小さくても、許容誤差を満たすには大きすぎます。
近似値とベースライン値の絶対差が 1e-4
以内であること、または相対差が eps*1e11
以内であることをテストするために、許容誤差オブジェクトの論理和を作成します。0 に近い透磁率の値が絶対 (負方向の丸め) 許容誤差を満たし、大きな値の光の速度の値が相対許容誤差を満たすように、この許容誤差を使用します。
testCase.verifyThat(approxVacuumProps, IsEqualTo(baselineVacuumProps, ... 'Within', RelativeTolerance(eps*1e11)| AbsoluteTolerance(1e-4)))
Verification passed.
バージョン履歴
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)