4个f-words

四大F: factor, factorial, factorization, fibonacci.

看/写parser时,时常遇到expression, term, factor之流,或更糟,E, T, F.

Expression- Term, Factor, Coefficient

多项式的、因子的,因式分解的,这是小学数学啊。:-)

byjus.com/maths/polynomial: The terms of polynomials are the parts of the expression that are generally separated by “+” or “-” signs.

In mathematics, polynomial functions, or polynomials, are an important class of simple and smooth functions. Here, simple means they are constructed using only multiplication and addition. Smooth means they are infinitely differentiable, i.e., they have derivatives of all finite orders.

Because of their simple structure, polynomials are very easy to evaluate, and are used extensively in numerical analysis for polynomial interpolation or to numerically integrate more complex functions.

In linear algebra, the characteristic polynomial of a square matrix encodes several important properties of the matrix. In graph theory the chromatic polynomial of a graph encodes the different ways to vertex color the graph using x colors.

Integer factorization | Factorization of polynomials | LU decomposition

factorial: the result when you multiply a whole number by all the numbers below it. 以上这些f-words都是乘法相关。

我们再看个加法相关的:

复制代码
#include <stdio.h>

int fibonacci(int n) {
  if (n == 0) return 0;
  if (n == 1) return 1;
  return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
  enum { N = 10 };
  int f[N], f2[N], i;

  for (i = 0; i < N; i++) f2[i] = fibonacci(i);

  f[0] = 0; f[1] = 1;
  for (i = 0; i < N - 2; i++) f[i + 2] = f[i] + f[i + 1];

  for (i = 0; i < N; i++) printf("%d %d\n", f[i], f[i] - f2[i]);
  getchar(); return 0;
}
复制代码

F(n)=F(n-1) + F(n-2); 令i=n-2, 得F(i+2)=F(i+1) + F(i);再交换=两边,F(i+1) + F(i)=F(i+2). 递归是“你想知道F(3)吗?你得先知道F(2)和F(1)”,"动态规划"是“如果我有了F(1)和F(2),我就能算出F(3)”,殊途同归。

自顶向下递归下降分析和自底向上用状态机,也是殊途同归。递归在堆栈里记录状态。

为啥好像没有根据BNF自动生成递归下降分析器的工具?可能是因为看着BNF手写并不麻烦。看到expr显然要写个parse_expr(). 可如果规则是:

expr : expr '+' expr | expr '*‘ expr | '(' expr ')' | NUMBER;

parse_expr()的头一句就是parse_expr(); 那不就无限递归了吗?上面的规则,不指定'+'和'*'的结合性和优先级是不行的?或者用上f-word: 

expr : term '+' term ;

term : factor '*' factor ;

factor : NUMBER | '(' expr ')' ;

parse_expr -> parse_term -> parse_factor:

if (current_token == NUMBER) ...
else if (current_token == '(') {
  parse_expr();
  if (current_token != ')') error;
}
else error;

非终结符是“定海神针”。


二维向量的夹角的余弦值为啥能用点积来求?cos(α-β) = cosαcosβ + sinαsinβ 高中数学。

虽然我不知道为啥三维和高维也对,但是一维的不是1就是-1,很合理。:-)

posted @   Fun_with_Words  阅读(36)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
历史上的今天:
2022-01-10 A Child's History of England.119
2022-01-10 A Child's History of England.118
2022-01-10 A Child's History of England.117
2022-01-10 A Child's History of England.116
2022-01-10 A Child's History of England.115
2022-01-10 A Child's History of England.114
2022-01-10 ARM, RISC-V和8086简单比较









 和4张牌。

点击右上角即可分享
微信分享提示