Fancy DSA Fancy DSA
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

想的更多,也要想的更远
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • Roadmap 路线
  • 算法简介
  • Sort 排序

  • Search 搜索

  • Recursive 递归

  • Graph 图

  • Tree 树

  • Math 数学

    • Abs [绝对值]
    • AliquotSum [真因数和]
    • Area [面积]
    • ArithmeticGeometricMean [算术-几何平均数]
      • 介绍
      • 实现
      • 扩展
      • 参考
    • ArmstrongNumber [阿姆斯特朗数]
    • AverageMean [平均数]
    • AverageMedian [中位数]
    • BinaryExponentiation [二分求幂]
    • BisectionMethod [二分法]
    • BinaryConvert [二进制转换]
    • CheckKishnamurthyNumber [克里希纳穆尔西数]
    • Coordinate
    • CoPrimeCheck
    • DecimalIsolate
    • DegreeToRadian
    • DecimalExpansion
  • Hash 哈希

  • String 字符串

  • BitManipulation 位操纵

  • Backtracking 回溯

  • DynamicProgramming 动态规划

  • Cache 缓存

  • Array 数组

  • Ciphers 密码学

  • Conversions 转换

  • ProjectEuler 欧拉计划

  • 其他

  • 算法
  • Math 数学
jonsam
2022-05-01
目录

ArithmeticGeometricMean [算术-几何平均数]

# 介绍

算术 - 几何平均是一种特殊平均,即算术平均与几何平均的合成平均。两个正实数 xxx 和 yyy 的算术 - 几何平均数定义如下:

首先计算 xxx 和 yyy 算术平均数 (相加平均), 称其为 a1a_1a1​ 。然后计算 xxx 和 yyy 几何平均数 (相乘平均), 称其为 g1g_1g1​; 这是 xyx yxy 的算术平方根。

a1=x+y2g1=xy\begin{aligned} &a_1=\frac{x+y}{2} \\ &g_1=\sqrt{x y} \end{aligned} ​a1​=2x+y​g1​=xy​​

然后重复这个步骤,这样便得到了两个数列 (an)\left(a_n\right)(an​) 和 (gn)\left(g_n\right)(gn​) :

an+1=an+gn2gn+1=angn.\begin{aligned} &a_{n+1}=\frac{a_n+g_n}{2} \\ &g_{n+1}=\sqrt{a_n g_n} . \end{aligned} ​an+1​=2an​+gn​​gn+1​=an​gn​​.​

这两个数列收敛于相同的数,这个数称为 xxx 和 yyy 的算术 - 几何平均数,记为 M(x,y)\mathrm{M}(x, y)M(x,y), 或 agm (x,y)(x, y)(x,y) 。

# 实现

# JavaScript

/**
 * @function agm
 * @description This finds the Arithmetic-Geometric Mean between any 2 numbers.
 * @param {Number} a - 1st number, also used to store Arithmetic Mean.
 * @param {Number} g - 2nd number, also used to store Geometric Mean.
 * @return {Number} - AGM of both numbers.
 * @see [AGM](https://en.wikipedia.org/wiki/Arithmetic%E2%80%93geometric_mean)
 */

export const agm = (a, g) => {
  if (a === Infinity && g === 0) return NaN
  if (Object.is(a, -0) && !Object.is(g, -0)) return 0
  if (a === g) return a // avoid rounding errors, and increase efficiency
  let x // temp var
  do {
    [a, g, x] = [(a + g) / 2, Math.sqrt(a * g), a]
  } while (a !== x && !isNaN(a))
  /*
  `x !== a` ensures the return value has full precision,
  and prevents infinite loops caused by rounding differences between `div` and `sqrt` (no need for "epsilon").
  If we were to compare `a` with `g`, some input combinations (not all) can cause an infinite loop,
  because the rounding mode never changes at runtime.
  Precision is not the same as accuracy, but they're related.
  This function isn't always 100% accurate (round-errors), but at least is more than 95% accurate.
  `!isNaN(x)` prevents infinite loops caused by invalid inputs like: negatives, NaNs and Infinities.
  `x !== a`确保返回值具有全精度并防止因`div`和`sqrt`之间的四舍五入引起的无限循环(不需要 "epsilon")。
  如果我们将`a`与`g`进行比较,一些输入组合(不是全部)会导致无限循环。因为四舍五入的模式在运行时不会改变。
  精度不等同于准确度,但它们是相关的。这个函数并不总是100%准确(四舍五入错误),但至少是95%以上的准确。
  `!isNaN(x)`防止由无效输入引起的无限循环,如:负数、NaN和无穷大。
  */
  return a
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

Object.is() 方法判断两个值是否是相同的值。这种相等性判断逻辑和传统的 == 运算不同, == 运算符会对它两边的操作数做隐式类型转换(如果它们类型不同),然后才进行相等性比较,(所以才会有类似 "" == false 等于 true 的现象),但 Object.is 不会做这种类型转换。这与 === 运算符的判定方式也不一样。 === 运算符(和 == 运算符)将数字值 -0 和 +0 视为相等,并认为 Number.NaN 不等于 NaN 。

# 扩展

  • 1 个动画,4 个均值:平方平均、算术平均、几何平均、调和平均:

# 参考

  • Arithmetic–geometric mean - Wikiwand (opens new window)
  • 算术 - 几何平均数 - Wikiwand (opens new window)
  • 算术几何平均 - 快懂百科 (opens new window)
  • Object.is() - JavaScript | MDN (opens new window)
编辑 (opens new window)
上次更新: 2022/10/18, 20:15:06
Area [面积]
ArmstrongNumber [阿姆斯特朗数]

← Area [面积] ArmstrongNumber [阿姆斯特朗数]→

最近更新
01
0-20题解
10-31
02
本章导读
10-31
03
算法与转换:Part1
10-28
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy DSA | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式