> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8c05c8a2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 距離関数に関するドキュメント

# 距離関数

{/*AUTOGENERATED_START*/}

<div id="L1Distance">
  ## L1Distance
</div>

導入バージョン: v21.11.0

`L1` 空間における 2 点間の距離 (ベクトルの各要素が座標) を計算します (1-ノルム、[タクシー幾何学](https://en.wikipedia.org/wiki/Taxicab_geometry)距離) 。

**構文**

```sql theme={null}
L1Distance(vector1, vector2)
```

**別名**: `distanceL1`

**引数**

* `vector1` — 第1ベクトル。[`Tuple(T)`](/ja/reference/data-types/tuple) または [`Array(T)`](/ja/reference/data-types/array)
* `vector2` — 第2ベクトル。[`Tuple(T)`](/ja/reference/data-types/tuple) または [`Array(T)`](/ja/reference/data-types/array)

**戻り値**

1-ノルム距離を返します。`Array` 入力の場合、要素型の最小の共通スーパータイプが `Float32` または `BFloat16` であれば `Float32`、それ以外の場合は `Float64` を返します。`Tuple` 入力の場合、戻り値の型は要素ごとの演算の算術結果型に従います (整数型は保持されます) 。[`(U)Int*`](/ja/reference/data-types/int-uint) または [`Float*`](/ja/reference/data-types/float)

**例**

**基本的な使い方**

```sql title=Query theme={null}
SELECT L1Distance((1, 2), (2, 3))
```

```response title=Response theme={null}
┌─L1Distance((1, 2), (2, 3))─┐
│                          2 │
└────────────────────────────┘
```

<div id="L1Norm">
  ## L1Norm
</div>

導入バージョン: v21.11.0

ベクトル要素の絶対値の総和を計算します。

**構文**

```sql theme={null}
L1Norm(vector)
```

**別名**: `normL1`

**引数**

* `vector` — 数値からなるベクトルまたはタプル。[`Array(T)`](/ja/reference/data-types/array) または [`Tuple(T)`](/ja/reference/data-types/tuple)

**戻り値**

L1ノルム、または [タクシー幾何学](https://en.wikipedia.org/wiki/Taxicab_geometry) における距離を返します。[`UInt*`](/ja/reference/data-types/int-uint) または [`Float*`](/ja/reference/data-types/float) または [`Decimal`](/ja/reference/data-types/decimal)

**例**

**基本的な使い方**

```sql title=Query theme={null}
SELECT L1Norm((1, 2))
```

```response title=Response theme={null}
┌─L1Norm((1, 2))─┐
│              3 │
└────────────────┘
```

<div id="L1Normalize">
  ## L1Normalize
</div>

導入バージョン: v21.11.0

指定されたベクトルの `L1` 空間 ([タクシー幾何学](https://en.wikipedia.org/wiki/Taxicab_geometry)) における単位ベクトルを計算します (タプルの各要素は座標です) 。

**構文**

```sql theme={null}
L1Normalize(tuple)
```

**別名**: `normalizeL1`

**引数**

* `tuple` — 数値から成るタプル。[`Tuple(T)`](/ja/reference/data-types/tuple)

**戻り値**

単位ベクトルを返します。[`Tuple(Float64)`](/ja/reference/data-types/tuple)

**例**

**基本的な使い方**

```sql title=Query theme={null}
SELECT L1Normalize((1, 2))
```

```response title=Response theme={null}
┌─L1Normalize((1, 2))─────────────────────┐
│ (0.3333333333333333,0.6666666666666666) │
└─────────────────────────────────────────┘
```

<div id="L2Distance">
  ## L2Distance
</div>

導入バージョン: v21.11.0

ユークリッド空間における 2 点間の距離 (ベクトルの各要素が座標) を計算します ([ユークリッド距離](https://en.wikipedia.org/wiki/Euclidean_distance)) 。

**構文**

```sql theme={null}
L2Distance(vector1, vector2)
```

**別名**: `distanceL2`

**引数**

* `vector1` — 1 番目のベクトル。[`Tuple(T)`](/ja/reference/data-types/tuple) または [`Array(T)`](/ja/reference/data-types/array)
* `vector2` — 2 番目のベクトル。[`Tuple(T)`](/ja/reference/data-types/tuple) または [`Array(T)`](/ja/reference/data-types/array)

**戻り値**

2-ノルム距離を返します。入力が `Array` の場合、要素型の最小の共通スーパータイプが `Float32` または `BFloat16` であれば `Float32` を返し、それ以外の場合は `Float64` を返します。入力が `Tuple` の場合は、常に `Float64` を返します。[`Float*`](/ja/reference/data-types/float)

**例**

**基本的な使い方**

```sql title=Query theme={null}
SELECT L2Distance((1, 2), (2, 3))
```

```response title=Response theme={null}
┌─L2Distance((1, 2), (2, 3))─┐
│         1.4142135623730951 │
└────────────────────────────┘
```

<div id="L2DistanceTransposed">
  ## L2DistanceTransposed
</div>

導入バージョン: v25.10.0

ユークリッド空間における 2 点間の近似距離 (ベクトルの各値を座標とする) を計算します ([ユークリッド距離](https://en.wikipedia.org/wiki/Euclidean_distance)) 。

**構文**

```sql theme={null}
L2DistanceTransposed(vector1, vector2, p)
```

**別名**: `distanceL2Transposed`

**引数**

* `vectors` — ベクトル。[`QBit(T, UInt64)`](/ja/reference/data-types/qbit)
* `reference` — 参照ベクトル。[`Array(T)`](/ja/reference/data-types/array)
* `p` — 距離計算で各ベクトル要素から使用するビット数 (1 から要素のビット幅まで) 。量子化レベルによって、精度と速度のトレードオフが決まります。ビット数を少なくすると I/O と計算は高速になりますが精度は低下し、ビット数を多くすると精度は向上しますが、その分パフォーマンスは低下します。[`UInt`](/ja/reference/data-types/int-uint)

**戻り値**

近似的な 2-ノルム距離を返します。戻り値は常に `Float64` です。[`Float64`](/ja/reference/data-types/float)

**例**

**基本的な使い方**

```sql title=Query theme={null}
CREATE TABLE qbit (id UInt32, vec QBit(Float64, 2)) ENGINE = Memory;
INSERT INTO qbit VALUES (1, [0, 1]);
SELECT L2DistanceTransposed(vec, array(1, 2), 16) FROM qbit;
```

```response title=Response theme={null}
┌─L2DistanceTransposed([0, 1], [1, 2], 16)─┐
│                       1.4142135623730951 │
└──────────────────────────────────────────┘
```

<div id="L2Norm">
  ## L2Norm
</div>

導入バージョン: v21.11.0

ベクトル要素の二乗和の平方根を求めます。

**構文**

```sql theme={null}
L2Norm(vector)
```

**別名**: `normL2`

**引数**

* `vector` — 数値からなるベクトルまたはタプル。 [`Tuple(T)`](/ja/reference/data-types/tuple) または [`Array(T)`](/ja/reference/data-types/array)

**戻り値**

L2ノルムまたは[ユークリッド距離](https://en.wikipedia.org/wiki/Euclidean_distance)を返します。 [`UInt*`](/ja/reference/data-types/int-uint) または [`Float*`](/ja/reference/data-types/float)

**例**

**基本的な使い方**

```sql title=Query theme={null}
SELECT L2Norm((1, 2))
```

```response title=Response theme={null}
┌───L2Norm((1, 2))─┐
│ 2.23606797749979 │
└──────────────────┘
```

<div id="L2Normalize">
  ## L2Normalize
</div>

導入バージョン: v21.11.0

ユークリッド空間において、指定したベクトルの単位ベクトル (タプルの各要素は座標) を計算します ([ユークリッド距離](https://en.wikipedia.org/wiki/Euclidean_distance)を使用) 。

**構文**

```sql theme={null}
L2Normalize(tuple)
```

**別名**: `normalizeL2`

**引数**

* `tuple` — 数値からなるタプル。 [`Tuple(T)`](/ja/reference/data-types/tuple)

**戻り値**

単位ベクトルを返します。 [`Tuple(Float64)`](/ja/reference/data-types/tuple)

**例**

**基本的な使い方**

```sql title=Query theme={null}
SELECT L2Normalize((3, 4))
```

```response title=Response theme={null}
┌─L2Normalize((3, 4))─┐
│ (0.6,0.8)           │
└─────────────────────┘
```

<div id="L2SquaredDistance">
  ## L2SquaredDistance
</div>

導入バージョン: v22.7.0

2 つのベクトルの対応する要素間の差の二乗和を計算します。

**構文**

```sql theme={null}
L2SquaredDistance(vector1, vector2)
```

**別名**: `distanceL2Squared`

**引数**

* `vector1` — 1番目のベクトル。[`Tuple(T)`](/ja/reference/data-types/tuple) または [`Array(T)`](/ja/reference/data-types/array)
* `vector2` — 2番目のベクトル。[`Tuple(T)`](/ja/reference/data-types/tuple) または [`Array(T)`](/ja/reference/data-types/array)

**戻り値**

2つのベクトルの対応する要素の差の二乗和を返します。`Array` 入力の場合、要素型の最小共通スーパータイプが `Float32` または `BFloat16` なら `Float32`、それ以外は `Float64` を返します。`Tuple` 入力の場合、戻り値の型は要素ごとの演算の算術結果型に従います (整数型は保持されます) 。[`(U)Int*`](/ja/reference/data-types/int-uint) または [`Float*`](/ja/reference/data-types/float)

**例**

**基本的な使い方**

```sql title=Query theme={null}
SELECT L2SquaredDistance([1, 2, 3], [0, 0, 0])
```

```response title=Response theme={null}
┌─L2SquaredDis⋯ [0, 0, 0])─┐
│                       14 │
└──────────────────────────┘
```

<div id="L2SquaredNorm">
  ## L2SquaredNorm
</div>

導入バージョン: v22.7.0

ベクトル要素の二乗和の平方根である [`L2Norm`](#L2Norm) の二乗を計算します。

**構文**

```sql theme={null}
L2SquaredNorm(vector)
```

**別名**: `normL2Squared`

**引数**

* `vector` — 数値からなるベクトルまたはタプル。 [`Array(T)`](/ja/reference/data-types/array) または [`Tuple(T)`](/ja/reference/data-types/tuple)

**戻り値**

L2ノルムの二乗を返します。 [`UInt*`](/ja/reference/data-types/int-uint) または [`Float*`](/ja/reference/data-types/float) または [`Decimal`](/ja/reference/data-types/decimal)

**例**

**基本的な使い方**

```sql title=Query theme={null}
SELECT L2SquaredNorm((1, 2))
```

```response title=Response theme={null}
┌─L2SquaredNorm((1, 2))─┐
│                     5 │
└───────────────────────┘
```

<div id="LinfDistance">
  ## LinfDistance
</div>

導入バージョン: v21.11.0

`L_{inf}` 空間における 2 点間の距離を計算します (ベクトルの要素が座標を表します)  ([最大ノルム](https://en.wikipedia.org/wiki/Norm_\(mathematics\)#Maximum_norm_\(special_case_of:_infinity_norm,_uniform_norm,_or_supremum_norm\))) 。

**構文**

```sql theme={null}
LinfDistance(vector1, vector2)
```

**別名**: `distanceLinf`

**引数**

* `vector1` — 第1ベクトル。[`Tuple(T)`](/ja/reference/data-types/tuple) または [`Array(T)`](/ja/reference/data-types/array)
* `vector2` — 第2ベクトル。[`Tuple(T)`](/ja/reference/data-types/tuple) または [`Array(T)`](/ja/reference/data-types/array)

**戻り値**

無限大ノルム距離を返します。入力が `Array` の場合、要素型の最小共通スーパータイプが `Float32` または `BFloat16` であれば `Float32`、それ以外は `Float64` を返します。入力が `Tuple` の場合は、常に `Float64` を返します。[`Float*`](/ja/reference/data-types/float)

**例**

**基本的な使い方**

```sql title=Query theme={null}
SELECT LinfDistance((1, 2), (2, 3))
```

```response title=Response theme={null}
┌─LinfDistance((1, 2), (2, 3))─┐
│                            1 │
└──────────────────────────────┘
```

<div id="LinfNorm">
  ## LinfNorm
</div>

導入バージョン: v21.11.0

ベクトル要素の絶対値の最大値を計算します。

**構文**

```sql theme={null}
LinfNorm(vector)
```

**別名**: `normLinf`

**引数**

* `vector` — 数値のベクトルまたはタプル。[`Array(T)`](/ja/reference/data-types/array) または [`Tuple(T)`](/ja/reference/data-types/tuple)

**戻り値**

Linf ノルム、つまり絶対値の最大値を返します。[`Float64`](/ja/reference/data-types/float)

**例**

**基本的な使い方**

```sql title=Query theme={null}
SELECT LinfNorm((1, -2))
```

```response title=Response theme={null}
┌─LinfNorm((1, -2))─┐
│                 2 │
└───────────────────┘
```

<div id="LinfNormalize">
  ## LinfNormalize
</div>

導入バージョン: v21.11.0

与えられたベクトルの単位ベクトル (タプルの各要素が座標) を、`L_{inf}` 空間で計算します ([最大ノルム](https://en.wikipedia.org/wiki/Norm_\(mathematics\)#Maximum_norm_\(special_case_of:_infinity_norm,_uniform_norm,_or_supremum_norm\)) を使用) 。

**構文**

```sql theme={null}
LinfNormalize(tuple)
```

**別名**: `normalizeLinf`

**引数**

* `tuple` — 数値のタプルです。 [`Tuple(T)`](/ja/reference/data-types/tuple)

**戻り値**

単位ベクトルを返します。 [`Tuple(Float64)`](/ja/reference/data-types/tuple)

**例**

**基本的な使い方**

```sql title=Query theme={null}
SELECT LinfNormalize((3, 4))
```

```response title=Response theme={null}
┌─LinfNormalize((3, 4))─┐
│ (0.75,1)              │
└───────────────────────┘
```

<div id="LpDistance">
  ## LpDistance
</div>

導入バージョン: v21.11.0

`Lp` 空間における 2 点間の距離 (ベクトルの要素を座標とする) を計算します ([p-ノルム距離](https://en.wikipedia.org/wiki/Norm_\(mathematics\)#p-norm)) 。

**構文**

```sql theme={null}
LpDistance(vector1, vector2, p)
```

**別名**: `distanceLp`

**引数**

* `vector1` — 1つ目のベクトル。[`Tuple(T)`](/ja/reference/data-types/tuple) または [`Array(T)`](/ja/reference/data-types/array)
* `vector2` — 2つ目のベクトル。[`Tuple(T)`](/ja/reference/data-types/tuple) または [`Array(T)`](/ja/reference/data-types/array)
* `p` — べき指数。設定可能な値: `[1; inf)` の実数。[`UInt*`](/ja/reference/data-types/int-uint) または [`Float*`](/ja/reference/data-types/float)

**戻り値**

p-ノルム距離を返します。`Array` 入力の場合、要素型の最小共通スーパータイプが `Float32` または `BFloat16` であれば `Float32`、それ以外の場合は `Float64` を返します。`Tuple` 入力の場合は、常に `Float64` を返します。[`Float*`](/ja/reference/data-types/float)

**例**

**基本的な使い方**

```sql title=Query theme={null}
SELECT LpDistance((1, 2), (2, 3), 3)
```

```response title=Response theme={null}
┌─LpDistance((1, 2), (2, 3), 3)─┐
│            1.2599210498948732 │
└───────────────────────────────┘
```

<div id="LpNorm">
  ## LpNorm
</div>

導入バージョン: v21.11.0

ベクトルの p-ノルムを計算します。これは、各要素の絶対値を p 乗して合計し、その p 乗根を取ったものです。

特殊なケース:

* p=1 のとき、L1Norm (マンハッタン距離) と同等です。
* p=2 のとき、L2Norm (ユークリッド距離) と同等です。
* p=∞ のとき、LinfNorm (最大ノルム) と同等です。

**構文**

```sql theme={null}
LpNorm(vector, p)
```

**別名**: `normLp`

**引数**

* `vector` — 数値からなるベクトルまたはタプル。[`Tuple(T)`](/ja/reference/data-types/tuple) または [`Array(T)`](/ja/reference/data-types/array)
* `p` — 指数。設定可能な値は `[1; inf)` の範囲の実数です。[`UInt*`](/ja/reference/data-types/int-uint) または [`Float*`](/ja/reference/data-types/float)

**戻り値**

[Lp-ノルム](https://en.wikipedia.org/wiki/Norm_\(mathematics\)#p-norm) を返します。[`Float64`](/ja/reference/data-types/float)

**例**

**基本的な使い方**

```sql title=Query theme={null}
SELECT LpNorm((1, -2), 2)
```

```response title=Response theme={null}
┌─LpNorm((1, -2), 2)─┐
│   2.23606797749979 │
└────────────────────┘
```

<div id="LpNormalize">
  ## LpNormalize
</div>

導入バージョン: v21.11.0

指定したベクトルの単位ベクトルを `Lp` 空間内で ([p-ノルム](https://en.wikipedia.org/wiki/Norm_\(mathematics\)#p-norm)を用いて) 計算します。タプルの各要素は座標を表します。

**構文**

```sql theme={null}
LpNormalize(tuple, p)
```

**別名**: `normalizeLp`

**引数**

* `tuple` — 数値から成るタプル。[`Tuple(T)`](/ja/reference/data-types/tuple)
* `p` — べき指数。設定可能な値は `[1; inf)` の範囲内の任意の数値です。[`UInt*`](/ja/reference/data-types/int-uint) または [`Float*`](/ja/reference/data-types/float)

**戻り値**

単位ベクトルを返します。[`Tuple(Float64)`](/ja/reference/data-types/tuple)

**例**

**使用例**

```sql title=Query theme={null}
SELECT LpNormalize((3, 4), 5)
```

```response title=Response theme={null}
┌─LpNormalize((3, 4), 5)──────────────────┐
│ (0.7187302630182624,0.9583070173576831) │
└─────────────────────────────────────────┘
```

<div id="cosineDistance">
  ## cosineDistance
</div>

導入バージョン: v21.11.0

2 つのベクトル間の [コサイン距離](https://en.wikipedia.org/wiki/Cosine_similarity#Cosine_distance) を計算します (タプルの要素は座標です) 。戻り値が小さいほど、2 つのベクトルはより類似しています。

**構文**

```sql theme={null}
cosineDistance(vector1, vector2)
```

**別名**: `distanceCosine`

**引数**

* `vector1` — 1 番目のベクトル。[`Tuple(T)`](/ja/reference/data-types/tuple) または [`Array(T)`](/ja/reference/data-types/array)
* `vector2` — 2 番目のベクトル。[`Tuple(T)`](/ja/reference/data-types/tuple) または [`Array(T)`](/ja/reference/data-types/array)

**戻り値**

コサイン距離 (コサイン類似度から 1 を引いた値) を返します。`Array` を入力した場合、要素型の最小共通スーパータイプが `Float32` または `BFloat16` であれば `Float32`、それ以外の場合は `Float64` を返します。`Tuple` を入力した場合は、常に `Float64` を返します。[`Float*`](/ja/reference/data-types/float)

**例**

**基本的な使い方**

```sql title=Query theme={null}
SELECT cosineDistance((1, 2), (2, 3));
```

```response title=Response theme={null}
┌─cosineDistance((1, 2), (2, 3))─┐
│           0.007722123286332261 │
└────────────────────────────────┘
```

<div id="cosineDistanceTransposed">
  ## cosineDistanceTransposed
</div>

導入バージョン: v26.1.0

2 点間の近似的な [コサイン距離](https://en.wikipedia.org/wiki/Cosine_similarity#Cosine_distance) を計算します (ベクトルの値を座標とみなします) 。戻り値が小さいほど、ベクトルはより類似しています。

**構文**

```sql theme={null}
cosineDistanceTransposed(vector1, vector2, p)
```

**別名**: `distanceCosineTransposed`

**引数**

* `vectors` — ベクトル。[`QBit(T, UInt64)`](/ja/reference/data-types/qbit)
* `reference` — 参照ベクトル。[`Array(T)`](/ja/reference/data-types/array)
* `p` — 距離計算で各ベクトル要素から使用するビット数 (1 から要素のビット幅まで) 。量子化レベルによって、精度と速度のトレードオフが決まります。使用するビット数を減らすと、精度は低下しますが、I/O と計算は高速になります。一方、ビット数を増やすと精度は向上しますが、パフォーマンスは低下します。[`UInt`](/ja/reference/data-types/int-uint)

**戻り値**

近似コサイン距離 (コサイン類似度を 1 から引いた値) を返します。返り値は常に Float64 です。[`Float64`](/ja/reference/data-types/float)

**例**

**基本的な使い方**

```sql title=Query theme={null}
CREATE TABLE qbit (id UInt32, vec QBit(Float64, 2)) ENGINE = Memory;
INSERT INTO qbit VALUES (1, [0, 1]);
SELECT cosineDistanceTransposed(vec, array(1, 2), 16) FROM qbit;
```

```response title=Response theme={null}
┌─cosineDistanceTransposed([0, 1], [1, 2], 16)─┐
│                          0.10557281085638826 │
└──────────────────────────────────────────────┘
```
