Published
Edited
Aug 25, 2021
Insert cell
Insert cell
simplify('a+(b+c*d+a)').toString()
Insert cell
simplify('z+(y+x*w+z)').toString()
Insert cell
simplify('a+(b+c*d+a)').equals(simplify('z+(y+x*w+z)'))
Insert cell
function simplify(expr) {
let seq = 0
const table = {}
const subst = (name) => !table[name] ? table[name] = `x${seq++}` : table[name]
return mathjs
.simplify(expr)
.transform(n => n.isSymbolNode ? new mathjs.SymbolNode(subst(n.name)) : n)
}
Insert cell
function simplify2(expr) {
// 변수 일련 번호를 순차적으로 증가(예: x0, x1, ...)시키기 위한 변수
let seq = 0
// 동일한 변수가 여러 번 나오는 경우(예: a^a+b에서 a가 두 번 나옴)에
// 동일한 변수에는 동일한 일련번호를 부여하기 위해서 한 번 나왔던 변수에
// 어떤 번호를 부여했는지 저장해두는 심볼 테이블
const table = {}
// 원래의 수식에 있던 변수 이름을 인자로 받아서 치환할 이름을 반환하는 함수
const subst = function(name) {
if(!table[name]) {
// 처음 보는 변수이면 새 번호를 할당한 후 반환
table[name] = `x${seq++}`
return table[name]
} else {
// 이미 봤던 변수이면 기존에 부여한 번호를 그대로 반환
return table[name]
}
}
return mathjs
// 수식을 축약한 다음...
.simplify(expr)
// 모든 SymbolNode의 변수를 치환
.transform(n => n.isSymbolNode ? new mathjs.SymbolNode(subst(n.name)) : n)
}
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more