Re: [Jprogramming] Got stuck...

2022-07-07 Thread xash
In a tacit definition you can access x and y with [ and ]. So in your example, y (0=|) x gets your current output, and y (] #~ 0=|) x gets your wanted output, as it will be parsed as (] #~ (0 = |)), so two forks. On Thu Jul 7, 2022 at 1:24 PM CEST, wrote: > Hi, > > as a first step into the "land

Re: [Jprogramming] Sierpinski triangle

2022-02-02 Thread xash
> Now why steps 10-13 result in the next row of a sierpinski triangle, I > don't know, I guess it has to do with generating a Pascal triangle. It's not that important for learning J, but it gives a hint why the seemingly confusing `72#:~8#2` was used. `bm{~3#.\row` maps three consecutive bits to b

Re: [Jprogramming] Find nth duplicate in vector

2022-01-26 Thread xash
-.@~: gets a mask of duplicates, I. then their indices. With them you can get a list of (index, value) pairs: ((],.{~) I.@:-.@~:) a 5 2 8 3 11 3 12 2 With 0 ({ (],.{~) I.@:-.@~:)) a you can 0-index into this list. If you are only interested into the first duplicate of a value, you could filter d

Re: [Jprogramming] Permutations of Hanoi

2021-10-31 Thread xash
With mixed bases, for e.g. 3: 3 2 1 #: i. 6 An equivalent function of permh is thus: (>:@i.@- #: i.@!) 3 If you want to map arbitrary n to a permutation with unknown length, you could use something like this (I love J for things like !^:_1): (#:~ [:>:@i.@-@<.@>: !^:_1)"0 (2+i.4) NB. needs specia

Re: [Jprogramming] segregation boxing on bitvectors

2021-10-21 Thread xash
Oh, or even simpler I guess: (<;.1~ 1, }.~:}:) 0 1 0 1 1 1 0 0 On Thu Oct 21, 2021 at 7:14 PM CEST, xash wrote: > ( > On Thu Oct 21, 2021 at 7:01 PM CEST, Raoul Schorer wrote: > > Hi, > > > > Really trivial question I am stuck on: > > > > given a bit ve

Re: [Jprogramming] segregation boxing on bitvectors

2021-10-21 Thread xash
( Hi, > > Really trivial question I am stuck on: > > given a bit vector > 0 1 0 1 1 1 0 0 > > how to easily sequentially box 1s & 0s? Such as: > |0|1|0|1 1 1|0 0| > > Thanks! > Raoul > -- > For information about J forums see http:/

Re: [Jprogramming] Stacking differently sized CSV-files

2021-08-10 Thread xash
> I feel like there should be a way to avoid this undesirable effect in > order > to stack all files one onto the other. Or is the more idiomatic way to > first stack the data and then to filter out the fill rows? Box each file, then raze it: ; <@loadcsv"0 filenames ---

Re: [Jprogramming] ups & downs

2021-05-20 Thread xash
On Thu May 20, 2021 at 3:22 PM CEST, Hauke Rehr wrote: > I strongly expect one could do better than to > {.+i.@-.@-/ Maybe a little bit cleaner: (}.i.@>:)/ 2 6 (and good catch on that (<+_1*>) -> (<->), thanks!) -- For informatio

Re: [Jprogramming] ups & downs

2021-05-20 Thread xash
f=:((#~ 1&|.) ,&.> <;.1~) 0 1, 0= 2+/\ }: (<+_1*>) }. The first part assigns _1 to elements that are greater than the next one, and 1 to elements that are less than the next one. Where _1 1 or 1 _1 occur, a new group starts. Thus 0= 2+/\. This gets us the starting indices for <;.1. Because the bo

Re: [Jprogramming] Hashing primitives WAS: Farewell for now!

2021-04-14 Thread xash
Most k version support dictionaries. Might be worth to take a look. But basically you operate on them with the same array symbols and they do the expected beheaviour. So if x d: y would create a dict with x for keys and y for values, k supports: dict1=:(a;b) d: 1 2 dict2=:(b;c) d: 3 4 dict1

Re: [Jprogramming] String prefixes and tabulation

2021-04-09 Thread xash
You can get every substring in a list with ; <@<\\. s If k is quite large, your approach with every k : #) <@<\ ] tab=: (<:@# ; }: ; {:)&> count=: ~. ,. <@#/.~ f=: count@:tab@:subs 4 f s -- For information about J

Re: [Jprogramming] Interquartile range

2021-02-21 Thread xash
Without I.: q2=: (1r4 3r4 <.@* #) -/@:{ \:~ And a fun one: q3=: \:~ -&({~ # <.@% 4:) /:~ -- For information about J forums see http://www.jsoftware.com/forums.htm

Re: [Jprogramming] Squared Euclidean distance matrix calculation

2021-02-20 Thread xash
A minor improvement to your fast version is to work on both sides with the transposed data (which as you noted is faster, as more data in a row is usually better than a lot of short rows). -/"1&|: will create two big tables for 2d data quite efficient. (If you can store the data with shape 2 100 i

Re: [Jprogramming] k-mediods PAM algo, help with canonical form (xash)

2021-02-15 Thread xash
kM2 takes in `data`, not the `edist data`. Otherwise, it should work. Though after I realized what this all does, taking in the distance matrix makes more sense. Thus: kM2_step=: 4 : 0 groups=. (i. <./)@:{"1 sub=. {"1 {&x best=. [ {~ [:(i. <./) [:+/"1 sub (y groups x) best/. (i. # x) ) kM2=: ] kM2

Re: [Jprogramming] k-mediods PAM algo, help with canonical form

2021-02-13 Thread xash
closest mediods. On Sat Feb 13, 2021 at 2:47 PM CET, xash wrote: > This is already very good code! Some improvements other than edist: > > Using `each` for rank "0 like in the last line of kM_step should be > avoided, as it just boxes/unboxes the result unecessarily. > > Ins

Re: [Jprogramming] k-mediods PAM algo, help with canonical form

2021-02-13 Thread xash
This is already very good code! Some improvements other than edist: Using `each` for rank "0 like in the last line of kM_step should be avoided, as it just boxes/unboxes the result unecessarily. Instead of building up the groups per hand (I. & (= & s)), you can just use `/.` to group the indices.

Re: [Jprogramming] Partition by Contiguous Runs According to a Property

2021-01-07 Thread xash
l =: _1 _2 0 1 2 _1 4 5 _6 p =: <&0 (<;.1~ (~:_,}:)@p) l ┌─┬─┬──┬───┬──┐ │_1 _2│0 1 2│_1│4 5│_6│ └─┴─┴──┴───┴──┘ On Thu Jan 7, 2021 at 10:12 AM CET, Justin Paston-Cooper wrote: > I am sure this has been asked and formulated somewhere else. I don't > know what the name of it is. >

Re: [Jprogramming] Trouble understanding the Fold family

2020-12-25 Thread xash
Even simpler: M e. V :-) On Sat Dec 26, 2020 at 1:18 AM CET, Thomas Bulka wrote: > Hi Hauke, > > thank you very much. This works perfectly. Looks, like I’ve been > thinking to complicated, again... > > Regards, > > Thomas > > > On 26 Dec 2020, at 0:58, Hauke Rehr wrote: > > > +/ M e."_ 0 V > > d

Re: [Jprogramming] String replace puzzle

2020-12-18 Thread xash
|. ('()'i.n)} ')','(',: n or tacit ([:|. ('()'&i.)`(')','(',:])}) n On Fri Dec 18, 2020 at 10:35 PM CET, Skip Cave wrote: > Using curlyrt: (amend in place) > > n=.'1+(2*3)+(4*(5+6))' > > > |.(+/1 2*'()'=/n)}n,(17#')'),:(17#'(') > > ((6+5)*4)+(3*2)+1 > > > > Skip Cave > Cave Consulting LLC > > > O

Re: [Jprogramming] Multiple rotations

2020-10-31 Thread xash
A bit more concise, but the same approach - mostly to show off &. :-) If sep is implemented the same or has a proper obverse, you could of course use it instead of 10&#.inv. #m=.(#~ (1 */@p: (|.~ ,.@i.@#)&.(10&#.inv))"0) i. 1e6 On Sat Oct 31, 2020 at 3:10 AM CET, Skip Cave wrote: > Here's the pro

Re: [Jprogramming] varying path length relation to tree

2020-09-20 Thread xash
This is much simpler; groups based on the first element, recursively call itself until no path is left, and links the result back together. Well, was at least a nice exercise with L:n yesterday. :-) ] leaves =. 'g';1 ] paths =. (,0);1 0 group =: {.&>@[ )@[ $:&.> group)@.(0<#@;@[) pat

Re: [Jprogramming] varying path length relation to tree

2020-09-19 Thread xash
Using L: to apply /. on each leaf (splitting paths) until finished, then rebuilding the paths via {:: and using them to index into the elements. ] leaves =. 'g';1 ] paths =. (,0);1 0 NB. tree based on paths with <'' as elements: structure=: ({.@> )^:(*@#@;)L:1^:_ NB. <'' -> path ->

Re: [Jprogramming] Too unintuitive?

2020-08-31 Thread xash
Yet another variation on the overlap version: 'AA' (] #~ #@[ */\ (<:@##1:)@[ , -.@E.) 'abcAAAdefAAA' On Sun, 30 Aug 2020 21:03:37 -0400 Raul Miller wrote: > Well, if you want the alternate semantics you suggested in that comment: > >'AA' (] rplc '';~[) 'abcAAAdefAAA' > abcAdefA > > Or, if

Re: [Jprogramming] Strange output.

2020-08-21 Thread xash
… and depending on what you want, there might be a better approach: parse the numbers, raze the strings, itemize the characters, and interpret each as a digit. <@( [: ". @ ,. @ ; <@":"0)\ *: >: i. 15 On Fri, 21 Aug 2020 12:46:24 -0500 Skip Cave wrote: > Definitions: > ea =: each > > rab =: ]#

Re: [Jprogramming] Strange output.

2020-08-21 Thread xash
]m=:<\ *: >: i.15 (sep @ ". @ (,&'x') @ rab @ ": @ ,)each m Appends a 'x' after each number string, so it will get interpreted as an extended number. Otherwise big numbers get converted to floats, e.g. 149162536496481100121 -> 1.49163e20. On Fri, 21 Aug 2020 12:46:24 -0500 Skip Cave wrote: >

Re: [Jprogramming] Constructing arrays by filling "templates"

2020-08-08 Thread xash
(0 0;1 1)}&(2 2$0)"1 (arr1 ,. arr2) On Sat, 08 Aug 2020 10:13:08 +0200 Thomas Bulka wrote: > Hello everyone, > > I'm stuck with a problem, which (I think) should be really easy to solve > in J, but I somehow am not able to do it. This is what I want to do: > > Let's assume I have two arra

Re: [Jprogramming] Given a depth vector, pretty print its tree

2020-08-07 Thread xash
For your original format, with a transition matrix to simplify finding "catenaries" for each column: tm=:3 4 $ 0 1 0 1 2 2 2 2 3 1 3 1 start=: 0,~ = + 2*(=>:) NB. same height = 1, direct children = 2, others = 0 step=: {::&tm@, NB. apply tm nub=: ] ,:~ 4* 2= ] NB. '-'

[Jprogramming] Multidimensional indicies

2020-06-07 Thread xash
Hello everyone, I'm new to J and while working with 1d indices feels quite natural, using multidimensional indices not so. For example, finding the indices (3 2$1 2,1 5,4 1) of #'s in this bit mask^Wmap: map=:'#'= [;._2 (0 : 0) .. ..#..# .. .. .# .. ) With 1d indices it