R语言 对数和幂函数

R语言 对数和幂函数

对数和幂函数是两个非常重要的数学函数,有助于计算随时间呈指数增长的数据。
首先是对数,计算基数中数值的对数的一般方法是使用 log() 函数,它需要两个参数作为数值和基数,默认情况下它计算的是自然对数,并且有普通和二进制对数的快捷方式,即基数10和2。值可以是数字或矢量。
第二是幂,计算一个基数上升到指数数的幂。在这篇文章中,有三种方法来计算同样的情况,即基数指数。

R中的对数函数

它是指数函数的逆函数,它代表的数量是固定数字(基数)的幂数,提高到给定的数字。它返回双倍的值。

公式

如果y = bx

那么logb y = x

例子:

 if 100 = 102
then log10100 = 2
R

各种log()函数的列表:

数字是数字或复数向量,基数是正数或复数向量,默认值设置为exp(1)。

  • R中的log函数[log(number)]返回自然对数,即以e为底。
log(10) = 2.302585 
R
  • [log10(number)]函数返回普通对数,即以10为基数。
log10(10) := 1 
R
  • [log2(number)] 返回二进制对数,即以2为底。
log2(10) := 3.321928 
R
  • [log(number, b)] 返回以b为底的对数。
log(10, 3) := 2.095903 
R
  • [log1p(number)] 对于number <<1,精确返回log(1+number)。
log1p(10) := 2.397895 
R
  • [exp(number)] 返回指数。
exp(10) := 22026.47 
R
  • [expm1(number)]精确返回number <<1的exp(number)-1。
expm1(10) := 22025.47 
R

例子

# R program to illustrate use of log functions
  
x <- 10
base <- 3
  
# Computes natural logarithm
log(x)
  
# Computes common logarithm
log10(x)
  
# Computes binary logarithm
log2(x)
  
# Computes logarithm of 
# x with base b
log(x, base)
  
# Computes accurately
# log(1+x) for x<<1
log1p(x)
  
# Computes exponential
exp(x)
  
# Computes accurately 
# exp(x)-1 for x<<1
expm1(x)
R

输出。

[1] 2.302585
[1] 1
[1] 3.321928
[1] 2.095903
[1] 2.397895
[1] 22026.47
[1] 22025.47
R

幂函数

如果有两个数字的基数和指数,它发现x提高到y的幂,即xy .
它返回双倍的值。它需要两个参数。

**x** = floating point base value
**y** = floating point power value
R

例子 :

 103 = 10*10*10 = 1000 
R
# R program to illustrate 
# the use of Power Function
x <- 10
y <- 3
  
# 1st Method
`^`(x, y)
  
# 2nd Method
x^y
  
# 3rd Method
x**y
R

输出。

[1] 1000
[1] 1000
[1] 1000
R

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册