Reputation: 765
v=: ((1 2);(3 4);(0 5);<(2 1))
d =: (1,0.5,1,0.25);(0.5,1,0.75,0.25);(1,0.75,1,0);(0.75,0.25,0,1)
force=:(v ((0{>"0 v);])@{~ ] i.4) ,"1 0 <"0>(0{d)
force=:(v ((1{>"0 v);])@{~ ] i.4) ,"1 0 <"0>(1{d)
force=:(v ((2{>"0 v);])@{~ ] i.4) ,"1 0 <"0>(2{d)
force=:(v ((3{>"0 v);])@{~ ] i.4) ,"1 0 <"0>(3{d)
force=:(v ((4{>"0 v);])@{~ ] i.4) ,"1 0 <"0>(4{d)
force=:(v ((y{>"0 v);])@{~ ] i.4) ,"1 0 <"0>(y{d)
Given v and d, 1st column of force gives us (n+1)th vector from v.
2nd column of force gives us each vector from v.
3rd column of force gives us a constant between 2 vectors.
That is, (1 2);(1 2)
will have 1 on 3rd column of force, but (1 2);(3 4)
might not.
I want to make a monad function which gives us
force=:(v ((1{>"0 v);])@{~ ] i.4) ,"1 0 <"0>(1{d)
if we type force_constant 1
or force=:(v ((2{>"0 v);])@{~ ] i.4) ,"1 0 <"0>(2{d)
if we type force_constant 2
Could someone help?
Upvotes: 2
Views: 83
Reputation: 2324
You wrote most of it yourself already. Just take your final version of force
, the one you wrote using y
, and wrap it in an explicit definition:
v =: ((1 2);(3 4);(0 5);<(2 1))
d =: (1,0.5,1,0.25);(0.5,1,0.75,0.25);(1,0.75,1,0);(0.75,0.25,0,1)
force_constant =: monad def '(v ((y{>"0 v);])@{~ ] i.4) ,"1 0 <"0>(y{d)'
force_constant 1
+---+---+----+
|3 4|1 2|0.5 |
+---+---+----+
|3 4|3 4|1 |
+---+---+----+
|3 4|0 5|0.75|
+---+---+----+
|3 4|2 1|0.25|
+---+---+----+
force_constant 2
+---+---+----+
|0 5|1 2|1 |
+---+---+----+
|0 5|3 4|0.75|
+---+---+----+
|0 5|0 5|1 |
+---+---+----+
|0 5|2 1|0 |
+---+---+----+
Now, this formulation depends on the nouns v
and d
being globally defined. You might consider changing that so force_constant
or related verbs take these arrays as inputs. The simplest method would be change the monads to dyads, and let v
and d
come in as the left argument, x
¹.
¹But we can keep it simple for now. If you want more feedback on your code, feel free to post it over on http://codereview.stackexchange.com/.
Upvotes: 4