Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions docs/ref/abs.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
---
title: abs – absolute value | Reference | kdb+ and q documentation
description: abs is a q keyword that returns the absolute value of its argument
author: Stephen Taylor
author: KX Systems, Inc., a subsidiary of KX Software Limited
---
# `abs`




_Absolute value_

```syntax
abs x abs[x]
```

Where `x` is a numeric or temporal, returns
the absolute value of `x`.
Null is returned if `x` is null.
Where `x` is a numeric, returns
the absolute value of `x`.
`x` is returned if `x` is null.
`abs` also works with temporal values, where it operates on the underlying numeric (refer to the examples below).

```q
q)abs -1.0
1f
q)abs 10 -43 0N
10 43 0N
q)abs 1999.01.01
2000.12.31
// if we convert these to longs, we can observe they're opposite
q)"j"$1999.01.01 2000.12.31
-365 365
```

`abs` is a [multithreaded primitive](../kb/mt-primitives.md).

`abs` is a [multithreaded primitive](mt-primitives.md).

## :fontawesome-solid-sitemap: Implicit iteration
## Implicit iteration

`abs` is an [atomic function](../basics/atomic.md).
`abs` is an [atomic function](atomic.md).

```q
q)abs(10;20 -30)
10
20 30
```

It applies to [dictionaries and tables](../basics/math.md#dictionaries-and-tables).

It applies to [dictionaries and tables](math.md#dictionaries-and-tables).

## Domain and range

Expand All @@ -51,8 +52,8 @@ range i . i h i j e f i . p m d z n u v t
Range: `ihjefpmdznuvt`

----
:fontawesome-solid-book:
[`signum`](signum.md)

[`signum`](signum.md)
<br>
:fontawesome-solid-book-open:
[Mathematics](../basics/math.md)

[Mathematics](math.md)
20 changes: 10 additions & 10 deletions docs/ref/accumulators.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Accumulators – Reference – kdb+ and q documentation
description: An accumulator is an iterator that takes an applicable value as argument and derives a function that evaluates the value, first on its entire (first) argument, then on the results of successive evaluations.
author: Stephen Taylor
author: KX Systems, Inc., a subsidiary of KX Software Limited
keywords: adverb, converge, dictionary, do, iterator, fold, kdb+, keyword, map, map reduce, mnemonic, operator, over, q, scan, unary, value, while
---
# Accumulators
Expand Down Expand Up @@ -32,7 +32,7 @@ t: unary truth map
x, y: arguments/indexes of v
</div>

An accumulator is an [iterator](iterators.md) that takes an [applicable value](../basics/glossary.md#applicable-value) as argument and derives a function that evaluates the value, first on its entire (first) argument, then on the results of **successive** evaluations.
An accumulator is an [iterator](iterators.md) that takes an [applicable value](glossary.md#applicable-value) as argument and derives a function that evaluates the value, first on its entire (first) argument, then on the results of **successive** evaluations.

There are two accumulators, Scan and Over. They have the same syntax and perform the same computation. But where the Scan-derived functions return the result of each evaluation, those of Over return only the last result.

Expand All @@ -56,7 +56,7 @@ q)(+/)2 3 4 / Over

The number of successive evaluations is determined differently for unary and for higher-rank values.

The domain of the accumulators is functions, lists, and dictionaries that represent [finite-state machines](../basics/glossary.md#finite-state-machine).
The domain of the accumulators is functions, lists, and dictionaries that represent [finite-state machines](glossary.md#finite-state-machine).

```q
q)yrp / a European tour
Expand Down Expand Up @@ -85,7 +85,7 @@ Berlin| London
x v1\y x v1/y / binary application
```

The function an accumulator derives from a unary value is [variadic](../basics/variadic.md).
The function an accumulator derives from a unary value is [variadic](variadic.md).
The result of the first evaluation is the right argument for the second evaluation. And so on.

!!! note "The value is evaluated on the entire right argument, not on items of it."
Expand Down Expand Up @@ -126,7 +126,7 @@ q)(route\)`Genoa / a circular tour
q)(not/) 42 / never returns!
```

Matching is governed by [comparison tolerance](../basics/precision.md#comparison-tolerance).
Matching is governed by [comparison tolerance](precision.md#comparison-tolerance).


### Do
Expand Down Expand Up @@ -213,11 +213,11 @@ In the last example, both applicable values are dictionaries.
x v\y x v/y
```

The function an accumulator derived from a binary value is [variadic](../basics/variadic.md).
The function an accumulator derived from a binary value is [variadic](variadic.md).
Functions derived by Scan are uniform; functions derived by Over are aggregates.
The number of evaluations is the count of the right argument.

![over](../img/over.png)
![over](svg/over.svg)
<br>
<small>_Unary and binary application of f/_</small>

Expand Down Expand Up @@ -308,7 +308,7 @@ q)m scan c / (m\)c
4 3 1 0 6 9
```

:fontawesome-regular-hand-point-right:

[`over`, `scan`](over.md)


Expand Down Expand Up @@ -455,6 +455,6 @@ q){x+y*z}/[`foo;mt;mt]
`foo
```

:fontawesome-solid-street-view:

_Q for Mortals_
[§6.7.6 Over (/) for Accumulation](/q4m3/6_Functions/#676-over-for-accumulation)
[§6.7.6 Over (/) for Accumulation](../learn/q4m/6_Functions.md/#676-over-for-accumulation)
27 changes: 11 additions & 16 deletions docs/ref/add.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ keywords: add, kdb+, mathematics, plus, q, sum
---
# `+` Add


```syntax
x+y +[x;y]
```

Where `x` and `y` are numerics or temporals, returns their
Where `x` and `y` are numerics or temporals, returns their
sum.

```q
Expand All @@ -35,20 +34,19 @@ msoft| 3005 103

Add is generally faster than [Subtract](subtract.md).

`+` is a [multithreaded primitive](../kb/mt-primitives.md).

`+` is a [multithreaded primitive](mt-primitives.md).

## Implicit iteration

Add is an [atomic function](../basics/atomic.md).
Add is an [atomic function](atomic.md).

```q
q)(10;20 30)+(2;3 4)
12
23 34
```

It applies to [dictionaries and tables](../basics/math.md#dictionaries-and-tables).
It applies to [dictionaries and tables](math.md#dictionaries-and-tables).

```q
q)k:`k xkey update k:`abc`def`ghi from t:flip d:`a`b!(10 -21 3;4 5 -6)
Expand Down Expand Up @@ -77,11 +75,10 @@ def| -42 10
ghi| 6 -12
```


## Range and domains

The following shows the resulting output type given the input type of `x` and `y`.
The character representation of the datatypes referenced can be found [`here`](../basics/datatypes.md).
The character representation of the datatypes referenced can be found [`here`](datatypes.md).

```txt
b g x h i j e f c s p m d z n u v t
Expand Down Expand Up @@ -119,18 +116,16 @@ q)type 1i+55
-7h
```


----
:fontawesome-solid-book:

[Subtract](subtract.md),
[`sum`](sum.md),
[`.Q.addmonths`](dotq.md#addmonths)
<br>
:fontawesome-solid-book-open:
[Datatypes](../basics/datatypes.md),
[Mathematics](../basics/math.md)

[Datatypes](datatypes.md),
[Mathematics](math.md)
<br>
:fontawesome-solid-street-view:
_Q for Mortals_
[§4.9.2 Temporal Arithmetic](/q4m3/4_Operators/#492-temporal-arithmetic)

_Q for Mortals_
[§4.9.2 Temporal Arithmetic](../learn/q4m/4_Operators.md/#492-temporal-arithmetic)
56 changes: 23 additions & 33 deletions docs/ref/aj.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ keywords: as-of, join, kdb+, q
---
# `aj`, `aj0`, `ajf`, `ajf0`




_As-of join_

```syntax
Expand All @@ -17,19 +14,19 @@ ajf [c; t1; t2]
ajf0[c; t1; t2]
```

Where
Where

- `t1` is a table. Since 4.1t 2023.08.04 if `t1` is the name of a table, it is updated in place.
- `t2` is a simple table
- `c` is a symbol list of `n` column names, common to `t1` and `t2`, and of matching type
- column `c[n]` is of a sortable type (typically time)
- `t1` is a table or the name of a table as a symbol (since 4.1t 2023.08.04, in the latter case, the table is updated in place with the result)
- `t2` is a simple table
- `c` is a symbol vector of `n` column names, common to `t1` and `t2`, and of matching type
- column `c`<sub>`n`</sub> is of a sortable type (typically time)

returns a table with records from the left-join of `t1` and `t2`.
In the join, columns `c[til n-1]` are matched for equality, and the last value of `c[n]` (most recent time) is taken.
In the join, columns `c`<sub>`0`</sub>`...c`<sub>`n-1`</sub> are matched for equality, and the last value of `c`<sub>`n`</sub> (most recent time) is taken.
For each record in `t1`, the result has one record with the items in `t1`, and

- if there are matching records in `t2`, the items of the last (in row order) matching record are appended to those of `t1`;
- otherwise the remaining columns are null.
- if there are matching records in `t2`, the items of the last (in row order) matching record are appended to those of `t1`;
- otherwise the remaining columns are null.

```q
q)t:([]time:10:01:01 10:01:03 10:01:04;sym:`msft`ibm`ge;qty:100 200 150)
Expand Down Expand Up @@ -57,11 +54,10 @@ time sym qty px
10:01:04 ge 150
```

`aj` is a [multithreaded primitive](../kb/mt-primitives.md).
`aj` is a [multithreaded primitive](mt-primitives.md).

!!! tip "There is no requirement for any of the join columns to be keys but the join is faster on keys."


## `aj`, `aj0`

`aj` and `aj0` return different times in their results:
Expand All @@ -71,10 +67,9 @@ aj boundary time from t1
aj0 actual time from t2
```


## `ajf`, `ajf0`

Since V3.6 2018.05.18 `ajf` and `ajf0` behave as V2.8 `aj` and `aj0`, i.e. they fill from LHS if RHS is null. e.g.
Since V3.6 2018.05.18 `ajf` and `ajf0` behave as V2.8 `aj` and `aj0`; they fill from `t1` if the corresponding value in `t2` is null. For example:

```q
q)t0:([]time:2#00:00:01;sym:`a`b;p:1 1;n:`r`s)
Expand All @@ -84,32 +79,28 @@ q)t0~ajf[`sym`time;t1;t2]
1b
```


## Performance

!!! warning "Order of search columns"

Ensure the first argument to `aj`, the columns to search on, is in the correct order, e.g. `` `sym`time``. Otherwise you’ll suffer a severe performance hit.

If the resulting time value is to be from the quote (actual time) instead of the (boundary time) from trade, use `aj0` instead of `aj`.

`aj` should run at a million or two trade records per second; whether the tables are mapped or not is irrelevant. However, for speed:

medium | t2\[c<sub>1</sub>\] | t2\[c<sub>2</sub>…\] | example
-------|---------------------|----------------------|-----------------------
memory | `g#` | sorted within <code>c<sub>1</sub></code> | `quote` has `` `g#sym`` and `time` sorted within `sym`
disk | `p#` | sorted within <code>c<sub>1</sub></code> | `quote` has `` `p#sym`` and `time` sorted within `sym`

Departure from this incurs a severe performance penalty.

Note that, on disk, the `g#` attribute does not help.
Departure from this incurs a severe performance penalty.

!!! warning "Select the virtual partition column only if you need it. It is fabricated on demand, which can be slow for large partitions."
Note that on disk, the `g#` attribute does not help.

!!! warning "Select the virtual partition column only if you need it. It is constructed on demand, which can be slow for large partitions."

## `select` from `t2`

In memory, there is no need to select from `t2`. Irrespective of the number of records, use, e.g.:
In memory, there is no need to select from `t2`. Irrespective of the number of records, use, for example:

```q
aj[`sym`time;select … from trade where …;quote]
Expand All @@ -122,9 +113,9 @@ aj[`sym`time;select … from trade where …;
select … from quote where …]
```

In contrast, on disk you must map in your splay or day-at-a-time partitioned database:
In contrast, on disk, you must map your splayed or partitioned database to memory first:

Splay:
Splayed:

```q
aj[`sym`time;select … from trade where …;select … from quote]
Expand All @@ -139,17 +130,16 @@ aj[`sym`time;select … from trade where …;

!!! warning "If further `where` constraints are used, the columns will be _copied_ instead of mapped into memory, slowing down the join."

If you are using a database where an individual day’s data is spread over multiple partitions the on-disk `p#` will be lost when retrieving data with a constraint such as `…date=2011.08.05`.
If you are using a database where an individual day’s data is spread over multiple partitions the on-disk `p#` will be lost when retrieving data with a constraint such as `…date=2011.08.05`.
In this case you will have to reduce the number of quotes retrieved by applying further constraints – or by re-applying the attribute.


----
:fontawesome-solid-book:
[`asof`](asof.md)

[`asof`](asof.md)
<br>
:fontawesome-solid-book-open:
[Joins](../basics/joins.md)

[Joins](joins.md)
<br>
:fontawesome-solid-street-view:

_Q for Mortals_
[§9.9.8 As-of Joins](/q4m3/9_Queries_q-sql/#998-as-of-joins)
[§9.9.8 As-of Joins](../learn/q4m/9_Queries_q-sql.md/#998-as-of-joins)
Loading