j

J: Why does `f^:proposition^:_ y` stand for a while loop?

南笙酒味 提交于 2019-12-22 07:03:16
问题 As title says, I don't understand why f^:proposition^:_ y is a while loop. I have actually used it a couple times, but I don't understand how it works. I get that ^: repeats functions, but I'm confused by its double use in that statement. I also can't understand why f^:proposition^:a: y works. This is the same as the previous one but returns the values from all the iterations, instead of only the last one as did the one above. a: is an empty box and I get that has a special meaning used with

J: Why does `f^:proposition^:_ y` stand for a while loop?

安稳与你 提交于 2019-12-22 07:02:30
问题 As title says, I don't understand why f^:proposition^:_ y is a while loop. I have actually used it a couple times, but I don't understand how it works. I get that ^: repeats functions, but I'm confused by its double use in that statement. I also can't understand why f^:proposition^:a: y works. This is the same as the previous one but returns the values from all the iterations, instead of only the last one as did the one above. a: is an empty box and I get that has a special meaning used with

How to refactor this in J?

£可爱£侵袭症+ 提交于 2019-12-13 16:33:09
问题 My newbie solution to Project Euler #1 +/((0=3|1+i.1000-1) +. (0=5|1+i.1000-1)) * (1+i.1000-1) I know that this can be refactored, and transformed into a function, i don't know how to do it, and I would have to read all the labs to learn it. 回答1: It isn't necessary to "handle zero" because adding zero won't change the answer so you can just use i. to generate your list of numbers below 1000, for example: i. 10 0 1 2 3 4 5 6 7 8 9 J works best with arrays so you should be able to ask for the

Modifying one row of an array

荒凉一梦 提交于 2019-12-12 12:33:44
问题 I've only just started learning J and there's something I have no idea how to do properly Suppose i want to print a checkerboard of 2 symbols, for example baba abab baba To do this, I assumed you could just generate an array baba baba baba and reverse the second line. Generating the array is easy: 3 4 $ 'ba' . But reversing the second row is where I struggle. I can get the reverse of the second row doing |. 1 { 3 4 $ 'ba' but that only gives me the second row, not the entire array. I don't

How to write a function with more than 3 parameters in J?

喜夏-厌秋 提交于 2019-12-12 12:14:10
问题 For example, how to write the function g=(x-y)/(x-z)? I know how to write the function with 2 parameters. 回答1: One way is to use variable matching: f =: 3 : 0 'x y z' =. y (x-y)%(x-z) ) f 1; 2; 3 0.5 f 1 2 3 0.5 f 1.5; 2; 0.5 _0.5 Another way is to treat your variables as an array v -> x y z and define your function as a series of array operations. For example: multiply and add +/ 1 _1 0 * x y z , multiply and add +/ 1 0 _1 * x y z , divide %/ This can be written as: g =: 3 :'%/ F (+/ . *) y'

Function composition in J

牧云@^-^@ 提交于 2019-12-11 11:39:29
问题 This is a pretty simple question but I can't seem to find an answer anywhere -- to map a list of numbers to their percentages of the sum of the list values (e.g., 1 2 2 -> 0.2 0.4 0.4), you can write the function func =: %+/ but just writing %+/ numbers where numbers is a list of numbers doesn't work -- why is this? Why do you need to put parentheses around the function composition? 回答1: J evaluates every expression from right to left, unless it sees a parenthesis (in which case it evaluates

Avoiding a repeated verb name in a train

梦想与她 提交于 2019-12-11 05:14:05
问题 Consider a dyadic verb g , defined in terms of a dyadic verb f : g=. [ f&.|: f Is it possible to rewrite g so that the f term appears only once, but the behavior is unchanged? UPDATE: Local Context This question came up as part of my solution to this problem, which "expanding" a matrix in both directions like so: Original Matrix 1 2 3 4 5 6 7 8 9 Expanded Matrix 1 1 1 1 2 3 3 3 3 1 1 1 1 2 3 3 3 3 1 1 1 1 2 3 3 3 3 1 1 1 1 2 3 3 3 3 4 4 4 4 5 6 6 6 6 7 7 7 7 8 9 9 9 9 7 7 7 7 8 9 9 9 9 7 7 7

How do I access the n-th column of a boxed array?

三世轮回 提交于 2019-12-10 23:52:17
问题 Given I have a boxed array with shape 4 3 v =. 4 3$'x1'; 'y1'; 'z1'; 'x2'; 'y2'; 'z2'; 'x3'; 'y3'; 'z3' v NB. +--+--+--+ NB. |x1|y1|z1| NB. +--+--+--+ NB. |x2|y2|z2| NB. +--+--+--+ NB. |x3|y3|z3| NB. +--+--+--+ NB. |x1|y1|z1| NB. +--+--+--+ Selection I can get the second row via 1{::v 1{::v NB. Get the second row NB. +--+--+--+ NB. |x2|y2|z2| NB. +--+--+--+ Projection But how do I access the second column? NB. Get the second column NB. +--+ NB. |y1| NB. +--+ NB. |y2| NB. +--+ NB. |y3| NB. +--

J: Handy method to enter a matrix?

我们两清 提交于 2019-12-10 20:34:52
问题 I have a n -by- m matrix written at paper and I want to make calculations on it in J. I can enter matrices like that ( n = 3, m = 3): A =: 3 4 $ 1 3 2 4 7 8 1 2 2 0 0 1 The question is how to enter a matrix like that: A =: (something here) 1 3 2 4 7 8 1 2 2 0 0 1 ) The reason why I'm asking such a weird question is that I have seen in some book a similar method of typing matrices using verb 0 : 0 or something alike, but I cannot remember where. :{ 回答1: I use (something here) as (".;._2) 0 : 0

FoldList like primitive in J

可紊 提交于 2019-12-10 20:09:26
问题 Mathematica has a built-in function called FoldList FoldList function description. Is there a similar primitive verb in J? (I know that J has a ^: verb, which is like Nest and FixedPoint .) To clarify my question, J has dyadic verb, so usually u / x1 x2 x3 becomes x1 u (x2 u x3) , which works just like FoldList , with reverse order. Except if the function u takes y , in a different shape from x . In FoldList there is an initial x . In J, if x3 is a different shape, one has to rely on < to