![]() |
Home | Libraries | People | FAQ | More |
#include <boost/math/tools/test.hpp>
template <class T> T relative_error(T a, T b); template <class A, class F1, class F2> test_result<see-below> test(const A& a, F1 test_func, F2 expect_func);
template <class T> T relative_error(T a, T v);
使用一般的方程來返回a 與 v 之間的相對誤差:
除此之外,如果出現下面情況,則返回0:
否則,如果a 或 v 之一為0,那麼返回 1。
template <class A, class F1, class F2> test_result<see-below> test(const A& a, F1 test_func, F2 expect_func);
這個函數用於使用一個已製成表的測試數據來測試一個函數。
返回值類型包含相對誤差的統計數據 (最大值,均值,方差,以及測試次數等等),以及產生最大相對誤差的測試數據所在行。類型 test_result 的公有成員函數為:
unsigned
worst()const;返回最大誤差出現的行。
T
min()const;返回最小相對誤差。
T
max()const;返回最大相對誤差。
T
mean()const;返回平均誤差
boost::uintmax_t
count()const;返回測試次數
T
variance()const;返回誤差的方差
T
variance1()const;返回誤差的無偏方差(unbiased variance)。
T
rms()const返回均方根( Root Mean Square),或者誤差的二次平均( quadratic mean )。
test_result& operator+=(const test_result&
t)將兩個 test_result合併為一個測試結果。
test_result的模板參數與傳遞給函數test的二維數組的類型是一樣的,,粗略地說就是A::value_type::value_type.
參數a 是測試數據的一個矩陣:並且必須為標準庫的序列類型,包含另外一個序列類型:典型情況下是一個二維的boost::array實例。矩陣a 的每一行應當包含所有的傳遞給正在測試的函數的所有參數以及預期的結果。
參數test_func 是正在進行測試的函數,對矩陣a中的每行數據調用這個函數。典型情況下,類型 F1 使用 Boost.Lambda來生成:參看下面的例子:
參數expect_func 是一個從矩陣a中提取數據的函數對象。典型情況下 類型F2使用 Boost.Lambda來生成:參看下面的例子:
如果預期返回一個有限值,但測試的函數返回一個無限值,或者如果發現了過失誤差(gross error ),那麼報錯消息被發送到std::cerr,並調用 BOOST_ERROR()
(要包含這個頭文件要求你使用 Boost.Test)。這主要是一個調試/開發的輔助(一個設置斷點的好地方)。
假設我們想要測試函數 tgamma 和 lgamma ,我們可以生成一個測試數據的二維矩陣,每一行是一個測試用例,並包含三個元素:輸出數據,分別為tgamma和lgamma函數的預期結果。
static const boost::array<boost::array<TestType, 3>, NumberOfTests> factorials = { /*大量的測試數據 */ };
現在我們調用測試函數來測試函數 tgamma:
using namespace boost::math::tools; using namespace boost::lambda; // 獲取正在測試的函數的指針: TestType (*funcp)(TestType) = boost::math::tgamma; // 聲明一個對像來存儲結果: test_result<TestType> result; // //測試tgamma函數: // result = test( factorials, bind(funcp, ret<TestType>(_1[0])), // 使用factorials[row][0]來調用函數 ret<TestType>(_1[1]) // 從 factorials[row][1]中提取預期結果 ); // // 打印一些結果: // std::cout << "The Mean was " << result.mean() << std::endl; std::cout << "The worst error was " << (result.max)() << std::endl; std::cout << "The worst error was at row " << result.worst_case() << std::endl; // // 測試lgamma函數: // funcp = boost::math::lgamma; result = test( factorials, bind(funcp, ret<TestType>(_1[0])), // 使用factorials[row][0]來調用函數 ret<TestType>(_1[2]) // 從 factorials[row][2]中提取預期結果 // // etc ... //