KDB文档 - Operator

228 阅读3分钟

Operators and Keywords Are Functions

Function Notation

An atomic function acts recursively on data structures. For example, applying it to a list is the same as applying it to each item in the list.

Primitives, Verbs and Functional Notation

q)2+3
5

Operators and keywords can also be used with ordinary function notation. For example, we can also use + as a binary function that takes two numeric arguments and returns a numeric result. You probably wouldn't think twice at seeing sum[a;b] but you might blink at the following perfectly logical equivalent.

q)+[2;3]
5
q)=[2;3]
0b

It is even possible to apply an operator using a combination of infix and functional notation.

q)(2+)[3]
5
q)(2+)3
5

Extension of Atomic Functions

q)neg 1 2 3
-1 -2 -3

This applies to nested lists as well, provided they conform in shape for multivalent functions.

q)(1 2 3; 4 5)+(100 200 300; 400 500)
101 202 303
404 505

q)100+1 2 3
101 102 103
q)1 2 3+100
101 102 103

Match ~

The non-atomic binary Match operator ~ applies to any two q entities, returning the boolean result 1b if they are identical and 0b otherwise.

q)42~40+2
1b

Equality and Relational Operators

Equality = and Disequality <>

The equality operator = differs from Match ~ in that it is atomic in both operands, meaning it tests its operands atom-wise instead of in entirety. All atoms of numeric, temporal or char type are mutually compatible for equality, but symbols are compatible only with symbols.

q)42=42.0
1b

A symbol and a character are not compatible and an error results from the test,

q)`a="a"
'type

The not-equal primitive is <>

q)42<>0x42
1b

Not Zero not

The not keyword generalizes the reversal of true and false bits to any entity having an underlying numeric value.

q)not 1b
0b
q)not 42
0b

Order: <, <=, >, >=

Less Than <, Greater Than > Up To <= and At Least >= are atomic and are defined for all compatible atom types.

Basic Arithmetic: +, -, *, %

Greater | and Lesser &

q)42|43
43
q)98.6&101.9
98.6

 Being atomic they operate item-wise on lists.

q)2|0 1 2 3 4
2 2 2 3 4
q)11010101b&01100101b
01000101b
q)"zaphod"|"arthur"
"zrthur"

For readability of logical operations on binary data, | can also be written as or and & can be written as and.

q)1b or 0b
1b
q)1b and 0b
0b
q)42 or 43
43

Amend :

An overload of : that is “assign in place.”

Simple q Amend

q)x:42
q)x+:1
q)x
43
q)x&:43
q)x
43

Amend with Lists

q)L:100 200 300 400
q)L[1]+:99
q)L1:(1 2 3; 10 20 30)
q)L1[;2]+:100
q)L1
1  2  103
10 20 130

A very useful idiom is ,: which appends to a list in place.

q)L:1 2 3
q)L,:4
q)L
1 2 3 4

Exponential Primitives: sqrt, exp, log, xexp, xlog

sqrt

q)sqrt 2

exp

q)exp 1

log

q)log 42.0

xexp

The atomic binary xexp has as domain all numeric values in both operands and returns a float representing the left operand raised to the power of the right operand. When the mathematical operation is undefined, the result is null.

q)2 xexp 5
32f

xlog

q)2 xlog 32
5f

More Numeric Primitives

Integer Division div and Modulus mod

q)7 div 2
3
q)3 4 5 div 2 3 4
1 1 1
q)3 4 5 mod 2 3 4
1 1 1

Sign signum

The atomic unary signum has domain all numeric and temporal types and returns an int representing the sign of its input, where 1i represents positive, -1i represents negative and 0i represents a zero.

q)signum 42
1i
q)signum -42.0
-1i

reciprocal

q)reciprocal 0.02380952
42.00001
q)reciprocal 0.0
0w
q)reciprocal -0.0
-0w

floor and ceiling

The atomic unary floor has as domain integer and floating-point types and returns a long representing the largest integer that is less than or equal to its argument.

q)floor 4.2
4
q)floor -4.2
-5
q)ceiling 4.2
5

Absolute Value abs

q)abs -42
42

Operations on Temporal Values

q)`int$12:00:00.123
43200123i

Temporal Comparison

q)2000.01.02=2000.01.02D00:00:00.000000000
1b
q)`timestamp$2001.01.02
2001.01.02D00:00:00.000000000
q)2000.01.01<2000.01.01D12:00:00.000000000
1b

Temporal Arithmetic

q)2015.01.01+til 31 / all days in January
2015.01.01 2015.01.02 2015.01.03 2015.01.04 2015.01.05 2015.01.06 2015.01.07 ..

Operations on Infinities and Nulls

q)(0%0)=0%0
1b
q)not 0W
0b
q)not -0w
0b
q)not 0N
0b
q)neg 0W
-0W
q)neg -0w
0w
q)neg 0N
0N

We saw previously that for any numeric type

null < negative infinity < normal value < positive infinity

Nulls of different type, while equal, are not otherwise comparable – i.e., any relational comparison results in 0b.

Infinities of different type are ordered by their width. For positive infinities

short < int < long < real < float

For negative infinities

-float < -real < -long < -int < -short

 Alias ::

An alias is a variable that is an expression – i.e., it is not the result of expression evaluation but the expression itself.

Creating an Alias with Double Colon

q)b::a:42
q)b
42
q)w::(x*x)+y*y
q)x:3
q)y:4
q)w
25

Alias vs. Function

q)fu:{(x*x)+y*y}
q)fu[3;4]
25

Dependencies

An alias variable depends on the entities in its associated expression. 

q)w::(x*x)+y*y
q).z.b
x| w
y| w

q)u::w*w
q).z.b
x| w
y| w
w| u

Views

Aliasing is commonly used to provide a database view by specifying a query as the expression.

q)t:([]c1:`a`b`c`a;c2:20 15 10 20;c3:99.5 99.45 99.42 99.4)
q)v::select sym:c1,px:c3 from t where c1=`a
q)v
sym px
--------
a 99.5
a 99.4

出处:code.kx.com/q4m3/4\_Ope…

作者:Jeffry A. Borror