Skip to content

Commit

Permalink
update op_add related
Browse files Browse the repository at this point in the history
  • Loading branch information
yqyq-w committed May 30, 2024
1 parent d6152e7 commit 718f7bb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
21 changes: 14 additions & 7 deletions course12/course_en.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
marp: true
marp: false
math: mathjax
paginate: true
backgroundImage: url('../pics/background_moonbit.png')
Expand Down Expand Up @@ -145,8 +145,9 @@ headingDivider: 1
let symbol : Symbol = example() // Abstract syntax tree of the function
@assertion.assert_eq(symbol.compute(input), 600.0)?
// Expression of df/dx
inspect(symbol.differentiate(0), content="Add(Add(Mul(Mul(Constant(5.0), Var(0)), Constant(1.0)), Mul(Add(Mul(Constant(5.0), Constant(1.0)), Mul(Constant(0.0), Var(0))), Var(0))), Constant(0.0))")?
@assertion.assert_eq(symbol.differentiate(0).compute(input), 50.0)?
inspect(symbol.differentiate(0),
content="Add(Add(Mul(Mul(Constant(5.0), Var(0)), Constant(1.0)), Mul(Add(Mul(Constant(5.0), Constant(1.0)), Mul(Constant(0.0), Var(0))), Var(0))), Constant(0.0))")?
@assertion.assert_eq(symbol.differentiate(0).compute(input), 100.0)?
}
```
- Here, `diff_0` is:
Expand All @@ -163,10 +164,16 @@ headingDivider: 1
```moonbit
fn Symbol::op_add_simplified(f1 : Symbol, f2 : Symbol) -> Symbol {
match (f1, f2) {
(Constant(0.0), a) => a // 0 + a = a
(Constant(a), Constant(b)) => Constant(a * b)
(a, Constant(_) as const) => const + a
_ => Add(f1, f2)
(Constant(0.0), a) => a
(Constant(a), Constant(b)) => Constant(a + b)
(a, Constant(_) as const) => const + a
(Mul(n, Var(x1)), Mul(m, Var(x2))) =>
if x1 == x2 {
Mul(m + n, Var(x1))
} else {
Add(f1, f2)
}
_ => Add(f1, f2)
} }
```
Expand Down
Binary file removed course12/course_en.pdf
Binary file not shown.
10 changes: 8 additions & 2 deletions course12/lec12.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ fn Symbol::op_add(f1 : Symbol, f2 : Symbol) -> Symbol {
// With simplification 以下为化简实现
match (f1, f2) {
(Constant(0.0), a) => a
(Constant(a), Constant(b)) => Constant(a * b)
(Constant(a), Constant(b)) => Constant(a + b)
(a, Constant(_) as const) => const + a
(Mul(n, Var(x1)), Mul(m, Var(x2))) =>
if x1 == x2 {
Mul(m + n, Var(x1))
} else {
Add(f1, f2)
}
_ => Add(f1, f2)
}
}
Expand Down Expand Up @@ -67,7 +73,7 @@ test "Symbolic differentiation 符号微分" {
@assertion.assert_eq(symbol.compute(input), 600.0)?
// Expression of df/dx 关于x的偏微分表达式树
inspect(symbol.differentiate(0), ~content="Mul(Constant(5.0), Var(0))")?
@assertion.assert_eq(symbol.differentiate(0).compute(input), 50.0)?
@assertion.assert_eq(symbol.differentiate(0).compute(input), 100.0)?
@assertion.assert_eq(symbol.differentiate(1).compute(input), 1.0)?
}

Expand Down
2 changes: 1 addition & 1 deletion course12/lec12_script.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 现代编程思想:案例:语法解析器
# 现代编程思想:案例:自动微分

大家好,欢迎来到由IDEA研究院基础软件中心为大家带来的现代编程思想公开课。今天我们继续案例学习,主题是自动微分。我会避开一些复杂的数学概念。

Expand Down

0 comments on commit 718f7bb

Please sign in to comment.