Main Content

matlab.unittest.constraints.IsReal 类

命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.BooleanConstraint

测试数组元素是否为实数值

描述

matlab.unittest.constraints.IsReal 类提供一个约束来测试数组的元素是否为实数值。

创建对象

描述

示例

c = matlab.unittest.constraints.IsReal 创建一个约束来测试数组的所有元素是否均为实数值。不使用复数存储的数值数组满足该约束。

示例

全部折叠

使用 IsReal 约束测试数值数组。

首先,导入此示例中使用的类。

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsReal

创建一个供交互测试的测试用例。

testCase = TestCase.forInteractiveUse;

验证值 5 是否为实数。

testCase.verifyThat(5,IsReal)
Verification passed.

立即测试值 complex(5)。尽管虚部的值为零,测试仍失败,因为该值存储为复数。

testCase.verifyThat(complex(5),IsReal)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsReal failed.
    --> The value must be real.
    
    Actual Value:
      5.000000000000000 + 0.000000000000000i

测试值 sqrt(-1)。测试失败。

testCase.verifyThat(sqrt(-1),IsReal)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsReal failed.
    --> The value must be real.
    
    Actual Value:
      0.000000000000000 + 1.000000000000000i

测试向量 [1 1 2 3 5 8 13] 是否满足 IsReal 约束。测试通过,因为所有向量元素均为实数。

testCase.verifyThat([1 1 2 3 5 8 13],IsReal)
Verification passed.

测试包含无限值和 NaN 值的向量。测试通过。

testCase.verifyThat([-Inf 5 NaN],IsReal)
Verification passed.

验证矩阵 [1 NaN; -Inf 3i] 不是实矩阵。

testCase.verifyThat([1 NaN; -Inf 3i],~IsReal)
Verification passed.

版本历史记录

在 R2013a 中推出