Verbs from functions?

I have a hunch this must be a very simple question but for the life ofme I can’t seem to figure it out.The function deltas is defined as (-‘:) and it appears to beapplicable to everything from simple lists to tables of numericcolumns. However, I can’t seem to use something like ({x-y}’:) toreproduce the same behaviour. (It works for simple lists like 0{x-y}':1 2 3, but not for tables or more complicated structures.)The reason I’m asking is that I’m looking to change deltas slightly todo weighted differences (ie, {(0.5*y)-0.1*x}) but produce a functionas general as deltas if possible.I am ready to offer up sacrifices at the altars of the q gods.Thanks,Victor

actually your solution works on tables.

t:( a:10?10;b:10?1f)
f:{(0.5*y)-0.1*x}[0;]

f t
a?? b???

2?? 0.1963762
3?? 0.2585456
3?? 0.2579898
0.5 0.2033321
4?? 0.08904193
2.5 0.1508861
2?? 0.3925165
4.5 0.2673548
1?? 0.3555858
3.5 0.2057985

Xi

2010/1/8 Victor Wong <victor.wong@gmail.com>

I have a hunch this must be a very simple question but for the life of
me I can’t seem to figure it out.

The function deltas is defined as (-‘:) and it appears to be
applicable to everything from simple lists to tables of numeric
columns. ?However, I can’t seem to use something like ({x-y}’:) to
reproduce the same behaviour. ?(It works for simple lists like 0{x-y}':
1 2 3, but not for tables or more complicated structures.)

The reason I’m asking is that I’m looking to change deltas slightly to
do weighted differences (ie, {(0.5*y)-0.1*x}) but produce a function
as general as deltas if possible.

I am ready to offer up sacrifices at the altars of the q gods.

Thanks,
Victor

Submitted via Google Groups

I think several things got mixed up here.1) you don’t need that 0 as left argument to deltas because that isthe same as the default behaviour2) f:{(0.5*y)-0.1*x}[0;] and then f t is not a delta! after partialapplication of the currying it’s just (.5*), which of course works fine withevery number-valued structure: it is just applied to every number3) I assume you want to perform a delta along the columns; if so, use “select deltas a, deltas b from t” or the equivalent with your owndeltas- like verb4) if your table has an arbitrary structure and you don’t know inadvance the column names, then you need to use a functional form: ?[t;();0b;(cols t)!deltas,'cols t]5) you’ll probably want to drop the first row of the resulting tablewith “1_” to get rid of that spurious first member of the deltas output [*];giving deltas a left argument lets you monkey with the value (and even shape) ofthat first element but you’re probably better off dropping italtogetherOlivier[*] yes, yes, I know: it’s so that (+) and deltas are true inversesbut it’s stillnot what 99% of the world means by a delta

Sorry my last post is such an eyesore…

some verb+adverb pairs have special code for them

compare for example
q)\t (+)over til 10000000
80
q)\t {x+y}over til 10000000
2472

Victor rightly uses 0{x-y}': as the 0 is needed to get the same result as -':
Otherwise the first element of the result would be null:
q)({x-y}':)5 1 2
0N -4 1

What he says about tables is true:
q)(-':)([]5 1 2)
x
--
5
-4
1
q)0{x-y}':([]5 1 2)
'nyi

to make it work for tables and keyed tables as -': is a bit more trouble
something like this might work
q)f:{$[98=t:type y;flip .z.s[x]each flip y;(99=t)and 98=type k:key y;k!.z.s[x]value y;0 x':y]}
q)f[-]each(5 1 2;1 2 3!5 1 2;([]5 1 2);([1 2 3]y:5 1 2))
5 -4 1
1 2 3!5 -4 1
+(,`x)!,5 -4 1
(+(,`x)!,1 2 3)!+(,`y)!,5 -4 1
q)f[{x-y}]each(5 1 2;1 2 3!5 1 2;([]5 1 2);([1 2 3]y:5 1 2))
5 -4 1
1 2 3!5 -4 1
+(,`x)!,5 -4 1
(+(,`x)!,1 2 3)!+(,`y)!,5 -4 1
q)f[{(.5*y)-.1*x}]each(5 1 2;1 2 3!5 1 2;([]5 1 2);([1 2 3]y:5 1 2))
-0.5 2.4 0.3
1 2 3!-0.5 2.4 0.3
+(,`x)!,-0.5 2.4 0.3
(+(,`x)!,1 2 3)!+(,`y)!,-0.5 2.4 0.3

But by the way general usage of ': is not really good for this
Compare:
q)x:til 1000000
q)\t 0{(.5*y)-.1*x}':x
947
q)\t (.5*0,-1_x)-.1*x
58

Regards,
Attila

Thanks for everyone’s help. I hadn’t thought about the speed
implications, but it looks like ': might be too slow for general use,
unless the particular verb has been optimized.

Victor