需要帮助将径向距离放入 lisp 程序的逆波兰概念中

发布于 2024-10-18 12:23:29 字数 435 浏览 7 评论 0原文

极坐标的距离公式如下所示: http://math.ucsd.edu/~wgarner/math4c/derivations /distance/distancepolar.htm

我尝试这样做:

(defun distant-formula (r1 r2 t1 t2)
(setq d ( sqrt (*(-(+ (expt r1 2)(expt r2 2))(* 2 r1 r2))
                (cos(- t1 t2))))))

但是减法和乘法之间的额外括号改变了公式,但是我不太确定如何在不正确的情况下做到这一点,任何帮助都是赞赏。

The distance formula for polar coordinated is shown here:
http://math.ucsd.edu/~wgarner/math4c/derivations/distance/distancepolar.htm

I've tried doing it this way:

(defun distant-formula (r1 r2 t1 t2)
(setq d ( sqrt (*(-(+ (expt r1 2)(expt r2 2))(* 2 r1 r2))
                (cos(- t1 t2))))))

but the extra parenthesis between the subtraction and multiplication changes the formula, however I'm not quite sure how to do it without correctly, any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稚然 2024-10-25 12:23:29

您代码中的变量 D 没有用。您不应该给未定义的变量赋值。 Lisp 还返回计算的值。这使得这样的任务特别无用。

您可以这样编写公式:

(defun distant-formula (r1 r2 t1 t2)
  (+ (expt r1 2)
     (expt r2 2)
     (* -2 r1 r2 (cos (- t1 t2)))))

请注意,三行代码的布局比仅使用一行更容易理解公式。使用编辑器的自动缩进来帮助布局。

The variable D in your code is of no use. You should not assign values to undefined variables. Lisp also returns the value of the computation. That makes such an assignment especially useless.

You can write the formula like this:

(defun distant-formula (r1 r2 t1 t2)
  (+ (expt r1 2)
     (expt r2 2)
     (* -2 r1 r2 (cos (- t1 t2)))))

Note that the layout of the code over three lines makes it easier to understand the formula, than using just one line. Use the automatic indentation of the editor to help with the layout.

银河中√捞星星 2024-10-25 12:23:29

查看原始公式:

sqrt(r1**2 + r2**2 - 2 * r1 * r2 * cos(t1 - t2))

您想要从外到内进行操作:

(sqrt (+ (* r1 r1) (* r1 r1) (* -2 r1 r2 (cos (- t1 t2)))))

您需要注意运算符的优先级。进行转换的一种方法是转换为中缀表示法中的所有隐式括号(因此 a * b + c 转到 ((a * b) + c))然后重新排列,使操作员位于前面。

Looking at the original formula:

sqrt(r1**2 + r2**2 - 2 * r1 * r2 * cos(t1 - t2))

You want to work from the outside to the inside:

(sqrt (+ (* r1 r1) (* r1 r1) (* -2 r1 r2 (cos (- t1 t2)))))

You need to be careful about the precedence of the operators. One way to do the conversion is to all all of the implicit parentheses in infix notation (so a * b + c goes to ((a * b) + c)) and then rearrange so the operators are at the front.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文