Netlogo减少多项式示例
在“reduce”的Netlogo字典中,它们显示了一个带有一个“+”运算符的示例 它们扩展为((1+2)+3)+4) 后来他们举了一个例子:Netlogo减少多项式示例,netlogo,reduce,Netlogo,Reduce,在“reduce”的Netlogo字典中,它们显示了一个带有一个“+”运算符的示例 它们扩展为((1+2)+3)+4) 后来他们举了一个例子: ;; evaluate the polynomial, with given coefficients, at x to-report evaluate-polynomial [coefficients x] report reduce [(x * ?1) + ?2] coefficients end ;; evaluate 3x^2 + 2x +
;; evaluate the polynomial, with given coefficients, at x
to-report evaluate-polynomial [coefficients x]
report reduce [(x * ?1) + ?2] coefficients
end
;; evaluate 3x^2 + 2x + 1 at x = 4
show evaluate-polynomial [3 2 1] 4
=> 57
该计算的等效展开(使用括号)是什么
observer> show (4 * ((4 * 3) + 2)) + 1
observer: 57
理解它的关键是一步一步地去做reduce
首先获取列表的前两个元素,并将它们插入?1
和?2
,因此
(x * ?1) + ?2
变成
(x * 3) + 2
然后整个表达式变为?1
,列表的最后一个元素1
,变为?2
。再次替换初始表达式中的?1
和?2
,我们得到:
(x * ((x * 3) + 2)) + 1
剩下的就是将x
替换为4
:
(4 * ((4 * 3) + 2)) + 1
值得注意的是,这种从左到右的缩减假设多项式的传统系数表示。现代的表现是相反的。
(4 * ((4 * 3) + 2)) + 1