防止 TI BASIC 表达式扩展
我正在为 TI-Nspire CAS 计算器编写一个函数。我的表达式有两个部分:(x+2)^2
和 +3
。当表达式连接时,结果会扩展为 x^2+4x+7
,而我需要它保持为 (x+2)^2+3
。
我尝试过制作两个部分的字符串,并在加入后将它们转换回表达式,但是,这会导致相同的不需要的扩展。
我能做的最好的事情就是返回连接的两个字符串(每个术语),这太丑了: "(x+2)^2" + "3"
有没有办法阻止这种扩展?< br> 它破坏了整个函数(完成了正方形)!
根据记录,即使与字符串连接,表达式也会扩展。
(x+2)^2 + "3"
返回
x^2 + 2x + "3" + 4
(我发现这真的很奇怪)
I'm coding a function for my TI-Nspire CAS calculator. I have two parts of an expression: (x+2)^2
and +3
. When the expressions are joined the result is expanded to x^2+4x+7
while I need it to remain as (x+2)^2+3
.
I've tried making both parts strings and them converting them back to expressions after joining but, this results in the same unwanted expansion.
The best I can do is return the two strings (each term) concatenated, which is gosh ugly: "(x+2)^2" + "3"
Is there anyway to prevent this expansion?
It undermines the entire function (which completes the square)!
For the record, the expression is expanded even if concatenated with a string.
(x+2)^2 + "3"
returns
x^2 + 2x + "3" + 4
(which I found really bizarre)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在这里看到的是正在工作的自动简化器。这意味着没有简单的方法可以将其关闭。
在大多数符号代数系统中 (x+1)^2 和 (x+1)^2+3 不会自动扩展。然而 NSspire 很乐意扩展后者。没有办法阻止这种行为。
在 Mathematica 中,我们可以写 Hold(expr) 来表示 expr 不应该被简化——但是 NSpire 缺少这个功能。
从某种意义上说,您已经发现了 NSpire 的弱点:表达式的符号操作(例如:无法使用模式匹配来定义函数)。 [与 Mathematica、Maple、Maxima 等相比]
最佳解决方法是什么取决于您尝试使用表达式做什么。如果它是算法的一部分,您可以将求和表达式表示为术语列表。这需要您编写自定义函数来执行求和、乘积等操作。
What you are seeing here is the automatic simplifier at work. And that means there is no easy way to turn it off.
In most symbolic algebra systems (x+1)^2 and (x+1)^2+3 are not automatically expanded. NSpire however gladly expands the latter. There are no way to prevent this behaviour.
In Mathematica one can write Hold(expr) in order to signal, that expr should not be simplified - that functionality is missing from NSpire however.
In some sense you have found the weak spot of NSpire: symbolic manipuation of expressions (as an example: there is no way to define functions using pattern matching). [When compared to Mathematica, Maple, Maxima etc.]
What the best work-around is depends on what you are trying to do with the expressions. If it is part of an algorithm, you could represent a sum expression as a list of terms. That requires you write custom functions to perform sums , products, etc.