简体   繁体   English

一旦在默认情况下添加平方根,结果就会不同

[英]different result once square root is added inside tacit

Very new to J. I love it but still far from fluent in it. J 的新手。我喜欢它,但还远未精通。 I have managed to hit an issue and I don't know why it is happening.我设法遇到了一个问题,但我不知道为什么会这样。 If somebody could please explain why this is occuring I could learn more about this straight-to-the-point language.如果有人可以解释为什么会发生这种情况,我可以了解更多关于这种直截了当的语言。

Basically I'm doing difference (-) then squared (*:) then sum (+/) then want to take the square root (%:).基本上我在做差异 (-) 然后平方 (*:) 然后求和 (+/) 然后想取平方根 (%:)。 Now the sum square error part ( (+/@:*:@:-) ) works fine:现在误差平方和部分 ((+/@:*:@:-) ) 工作正常:

   1 2 3 4 5 (+/@:*:@:-) 2 2 2 2 2
15

Then I can get the square root this way (just add %: to left):然后我可以通过这种方式获得平方根(只需在左侧添加 %: ):

   %: 1 2 3 4 5 (+/@:*:@:-) 2 2 2 2 2
3.87298

But when I add square root to left of the atco (tacit?) part in the middle it doesn't give me the right result:但是当我在中间的 atco(默认?)部分的左侧添加平方根时,它并没有给我正确的结果:

   1 2 3 4 5 (%:@:+/@:*:@:-) 2 2 2 2 2
1.57001

It instead returns 1.57001 instead of 3.87298.它反而返回 1.57001 而不是 3.87298。

My understanding is all I have done is add a 'then do square root' but obviously I am wrong.我的理解是我所做的只是添加一个“然后求平方根”,但显然我错了。 But I don't understand why.但我不明白为什么。

When I dissect:当我解剖时:

require 'debug/dissect'
dissect '1 2 3 4 5 (%:@:+/@:*:@:-) 2 2 2 2 2'

I see diff (-) and square (*:) are in their own dissect boxes.我看到 diff (-) 和 square (*:) 在它们自己的解剖框中。 But then the last box combines sum (+/) and square root (%:) together, showing %:@:+/.但是最后一个框将和 (+/) 和平方根 (%:) 结合在一起,显示 %:@:+/。 Not a separate box for +/ and then %: .不是 +/ 然后 %: 的单独框。

As I said I am new to J and am struggling to understand why this is occuring here.正如我所说,我是 J 的新手,并且正在努力理解为什么会发生这种情况。

I've tried changing to &, &: and @ instead but as expected that didn't fix this issue.我试过改为 &、&: 和 @,但正如预期的那样并没有解决这个问题。 It doesn't appear to be due to some composition limit either since this works fine (has many more atco's combined):它似乎不是由于某些成分限制,因为它工作正常(有更多的 atco 组合):

   (>:@:>:@:>:@:>:@:>:@:>:@:>:@:>:@:>:) 2 2 2
11 11 11

Thank you谢谢

The issue that you are encountering is that您遇到的问题是

(%:@:+/@:*:@:-)

is being evaluated as被评估为

   1 2 3 4 5 ((%:@:+)/@:*:@:-) 2 2 2 2 2
1.57001

and not并不是

   1 2 3 4 5 %:@:(+/@:*:@:-) 2 2 2 2 2
3.87298

because of the way that conjunctions take as much of the left operand as possible.因为连词尽可能多地占用左操作数。 This means that your partial result from *:@:- is being processed by %:@:+/ when you want %: to apply to the result of +/@:*:@:-这意味着当您希望%:应用于+/@:*:@:-的结果时,您来自*:@:-的部分结果正在由%:@:+/处理

Since you are just starting your journey with J, this may be a bit advanced, but at some point you may want to read the tacit programming section of J for C programmers by Henry Rich.由于您刚刚开始使用 J,这可能有点高级,但在某些时候您可能想要阅读 Henry Rich 为 C 程序员编写的 J 的默认编程部分。 It has a very good explanation about how J sentences are parsed.关于J句是如何解析的,里面有很好的解释。 Parenthesis are your friend when you want to change the order of execution.当您想更改执行顺序时,括号是您的朋友。 https://www.jsoftware.com/help/jforc/contents.htm#_Toc191734581 https://www.jsoftware.com/help/jforc/contents.htm#_Toc191734581

Edit: As @Eelvex points out in the comment below the break actually occurs at (+/) , but the result is the same because of the way that conjunctions are evaluated left to right.编辑:正如@Eelvex 在下面的评论中指出的那样,中断实际上发生在(+/)处,但结果是相同的,因为从左到右评估连词的方式。

You are trying to form a dyadic verb for the expression:您正在尝试为表达式形成一个二元动词:

%: +/ *: x - y  NB. which we can write as

f g h x - y     NB. if we define:

f =: %:
g =: +/
h =: *:

f g h x - y
3.87298

One way of making this a dyadic verb is to apply @: everywhere:使它成为二元动词的一种方法是在任何地方应用@: ::

x (f@:g@:h@:-) y
3.87298

x (%: @: (+/) @: *: @: -) y
3.87298

A more idiomatic way of doing it uses under (&.) which inverses a verb after applying another:一种更惯用的方法是使用under (&.) ,它在应用另一个动词后反转动词:

f &.: g  y             NB. expands to
(g-inverse) f g y

g &.: h (x - y)
3.87298

x ( g &.: h @: -) y 
3.87298

NB. or if you prefer monadic verbs:
(g &.: h @: -/) x,: y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM