conditional test in D-Fns

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

Re: conditional test in D-Fns

Had some spare time, and tried to find different ways to solve the problem.
`      foo1←(((2|⊢)×1+3×⊢)+(~2|⊢)×0.5×⊢)       foo2←(6÷⍨¯1+⊢)@(2|⊢)(1+3×⊢)      foo3←{(b×1+3×⍵)+⍵×0.5×~b←2|⍵}      foo4←{2÷⍨@(⍸~b)⊢(1+3×⊢)@(⍸b←2|⍵)⊢⍵}      foo5←{(⊂⍋⍋b)⌷(0.5×(~b)/⍵),1+3×⍵/⍨b←2|⍵}       foo6←{b←2|v←⍵ ⋄ v[⍸b]←1+3×b/v ⋄ v[⍸~b]←0.5×(~b)/v ⋄ v}       foo7←{2|⍵:1+3×⍵ ⋄ ⍵÷2}¨      foo8←{b←2|⍵ ⋄ (~b){2÷⍨⍣⍺⊢⍵}¨b{(1+3×⊢)⍣⍺⊢⍵}¨⍵}      w←1e5?1e6       ]runtime -c "foo1 w" "foo2 w" "foo3 w" "foo4 w" "foo5 w" "foo6 w" "foo7 w" "foo8 w"                                                                       foo1 w → 5.8E¯4 |      0%   foo2 w → 7.3E¯4 |    +25%   foo3 w → 8.9E¯4 |    +54%   foo4 w → 1.0E¯3 |    +81% ⎕   foo5 w → 1.5E¯3 |   +154% ⎕   foo6 w → 1.7E¯3 |   +190% ⎕   foo7 w → 2.0E¯2 |  +3345% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕   foo8 w → 7.2E¯2 | +12363% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕`

-wm
Veli-Matti

Posts: 92
Joined: Sat Nov 28, 2009 3:12 pm

Re: conditional test in D-Fns

Try a 3d array and watch everything change:

Code: Select all
` w←100 100 10⍴100000?1000000]runtime -c "foo1 w" "foo2 w" "foo3 w" "foo4 w"  "foo7 w" "foo8 w" "foo9 w"                                                                       foo1 w → 3.6E¯4 |      0%                                            foo2 w → 4.3E¯3 |  +1091% ⎕⎕⎕                                        foo3 w → 2.5E¯4 |    -31%                                            foo4 w → 8.1E¯3 |  +2143% ⎕⎕⎕⎕⎕                                      foo7 w → 1.6E¯2 |  +4469% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕                                 foo8 w → 6.6E¯2 | +18334% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕   foo9 w → 3.1E¯3 |   +750% ⎕⎕    `

foo5 and foo6 omitted as they don't work on high rank arrays and one addition:

Code: Select all
`foo9←{⊃(⊂⍤¯1)(2|⍵)⊖↑(⍵÷2)(1+3×⍵)}`
paulmansour

Posts: 404
Joined: Fri Oct 03, 2008 4:14 pm

Re: conditional test in D-Fns

Hi Paul, what a nice surprise!

I _thought_ that the original problem was meant for simple vectors, but you are right, higher ranks should be taken into account as well.

A couple of other possible (but not necessarily effective) algorithms:
`      foo10←{b←2|⍵ ⋄ (~b){2÷⍨⍣⍺⊢⍵}¨b{(1+3×⊢)⍣⍺⊢⍵}¨⍵}      foo11←{(⍴⍵)⍴1↑(2|⍵)⊖(⍵÷2),[0.1]1+3×⍵}`

It's funny how some simple problems can have so many different approaches.

-wm (looking forward to Olhão)
Veli-Matti

Posts: 92
Joined: Sat Nov 28, 2009 3:12 pm

Previous

Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest