No history yet

Vector Operations

The Vector Philosophy

In many programming and database environments, data is processed one piece at a time. To work with a collection of numbers, you might loop through them, handling each one individually. The q language, which powers the kdb+ database, operates on a different principle: everything is a vector.

A vector is simply a list of values. Instead of iterating, q applies operations to entire lists at once. This is known as vectorised arithmetic. It’s not just a neater way to write code; it’s a fundamental shift that enables incredible speed, which is critical in financial applications where every microsecond counts.

q) 10 20 30 + 4 5 6
14 25 36

Notice there are no loops or keywords. The + operator understands it's working with two lists and performs element-wise addition automatically. This direct approach is the foundation of q's efficiency.

Automatic Conformance

Vector operations generally require lists to be of the same length, or conformant in q terminology. If you try to add a three-item list to a four-item list, q will raise an error. However, there's a powerful exception to this rule: operations between a single item (an 'atom') and a list.

When q sees an operation between an atom and a vector, it automatically applies the atom's operation to every element of the vector. This is a core feature that eliminates the need for many common types of loops.

/ Add 100 to each element of the list
q) 100 + 1 2 3
101 102 103

/ Divide each element by 2
q) (10; 20; 30) % 2
5 10 15

This feature is incredibly useful in financial data analysis. Imagine you have lists of bid and ask prices for a stock. Calculating the mid-price or spread for every tick of data becomes a single, simple expression.

/ Sample bid and ask prices for a stock
q) bids: 100.01 100.02 100.01 100.03
q) asks: 100.03 100.04 100.04 100.05

/ Calculate the mid-price for all ticks at once
q) midprices: (bids + asks) % 2
q) midprices
100.02 100.03 100.025 100.04

/ Calculate the spread
q) spreads: asks - bids
q) spreads
0.02 0.02 0.03 0.02

Without any explicit iteration, we've performed calculations across entire datasets. This is the elegance and power of vector-native thinking.

The Need for Speed

Why is this vector-first approach so much faster? The answer lies in how kdb+ organises data. Traditional relational databases are typically row-oriented stores. They store all the data for a single record (like a trade's price, size, and time) together in memory. To calculate the average price of all trades, the database has to skip over the size and time data for every single row, which is inefficient.

kdb+ is a column-oriented database. It stores all values for a single column together in one contiguous block of memory. All the prices are together, all the sizes are together, and so on. When you perform a vector operation like sum prices, the CPU can rip through a single, unbroken chunk of memory containing only the data it needs. There's no wasted effort reading and discarding irrelevant data.

This efficiency is a result of improved cache performance and reduced data transfer. The CPU can load a large chunk of relevant data into its fastest memory and operate on it without interruption. This combination of a simple, vector-based language and a column-oriented memory model is what gives kdb+ its legendary performance.

Quiz Questions 1/5

What is the fundamental principle of data processing in the q language?

Quiz Questions 2/5

True or False: The q language requires explicit loops (like for or while) to perform calculations on a list of numbers.