名稱空間
變體
操作

std::log(std::valarray)

來自 cppreference.com
< cpp‎ | numeric‎ | valarray
 
 
 
 
定義於標頭檔案 <valarray>
template< class T >
valarray<T> log( const valarray<T>& va );

對於 va 中的每個元素,計算該元素值的自然對數。

目錄

[編輯] 引數

va - 要應用操作的值陣列

[編輯] 返回值

包含 va 中各值自然對數的陣列。

[編輯] 注意

使用非限定函式 (log) 執行計算。如果此類函式不可用,則由於實參依賴查詢,將使用 std::log

該函式可以使用與 std::valarray 不同的返回型別實現。在這種情況下,替換型別具有以下屬性:

[編輯] 可能的實現

template<class T>
valarray<T> log(const valarray<T>& va)
{
    valarray<T> other = va;
    for (T& i : other)
        i = log(i);
 
    return other; // proxy object may be returned
}

[編輯] 示例

#include <iomanip>
#include <iostream>
#include <valarray>
 
void show(char const* title, const std::valarray<double>& va)
{
    std::cout << title << " :" << std::right << std::fixed;
    for (double x : va)
        std::cout << std::setw(10) << x;
    std::cout << '\n';
}
 
int main()
{
    const std::valarray<double> n{0.0, 1.0, 2.0, 3.0};
    const std::valarray<double> exp_n{std::exp(n)};
    const std::valarray<double> log_exp_n{std::log(exp_n)};
 
    show("n      ", n);
    show("eⁿ     ", exp_n);
    show("log(eⁿ)", log_exp_n);
}

輸出

n       :  0.000000  1.000000  2.000000  3.000000
eⁿ      :  1.000000  2.718282  7.389056 20.085537
log(eⁿ) :  0.000000  1.000000  2.000000  3.000000

[編輯] 參閱

將函式 std::log10 應用於 valarray 的每個元素
(函式模板) [編輯]
將函式 std::exp 應用於 valarray 的每個元素
(函式模板) [編輯]
(C++11)(C++11)
計算自然(底數為 e)對數(ln(x)
(函式) [編輯]
具有沿負實軸分支割線的複數自然對數
(函式模板) [編輯]