# Tensor Dimension

### Tensor Dimension

A tensor's dimension refers to the number of indices required to uniquely specify an element within the tensor. Mathematically, it can be expressed as:

#### **Formal Definition**:

For a tensor T, its dimension n is the number of independent indices $$(i₁, i₂, ..., iₙ)$$ needed to specify any component $$Tᵢ₁ᵢ₂...ᵢₙ$$

#### Mathematical Examples:

* Scalar (0D): $$a$$
* Vector (1D): $$a\_i$$
* Matrix (2D): $$a\_{ij}$$
* 3D Tensor: $$a\_{ijk}$$

#### Real Mathematical Examples:

* CopyScalar: 5
* Vector: $$v = \[2, 3, 4]$$
* Matrix:\
  $$\mathbf{M} = \begin{bmatrix} 1 & 2 \ 3 & 4 \end{bmatrix}$$
* 3D Tensor:\
  $$\mathbf{T} = \begin{bmatrix} \begin{bmatrix} 1 & 2 \ 3 & 4 \end{bmatrix}, \begin{bmatrix} 5 & 6 \ 7 & 8 \end{bmatrix} \end{bmatrix}$$

#### Classification by Dimension

**a. Scalar (0-dimension)**

* **Definition**: No indices needed
* **Notation**: $$s$$ or $$s₀$$
* **Examples**:
  * Temperature: 25°C
  * Mass: 5 kg
  * Potential: $$V = 10V$$

**b. Vector (1-dimension)**

* **Definition**: One index needed
* **Notation**: $$vᵢ$$ or $$v\[i]$$
* **Examples**:
  * Position vector: $$r = \[x, y, z]$$
  * Force vector: $$F = \[Fx, Fy, Fz]$$
  * Momentum: $$p = \[px, py, pz]$$

#### c. Matrix (2-dimension)

* **Definition**: Two indices needed
* **Notation**: $$Mᵢⱼ$$ or $$M\[i,j]$$
* **Examples**:\
  \
  Stress tensor:\
  $$\sigma\_{ij} = \begin{bmatrix} \sigma\_{xx} & \sigma\_{xy} & \sigma\_{xz} \ \sigma\_{yx} & \sigma\_{yy} & \sigma\_{yz} \ \sigma\_{zx} & \sigma\_{zy} & \sigma\_{zz} \end{bmatrix}$$\
  \
  Inertia tensor:\
  $$I\_{ij} = \begin{bmatrix} I\_{xx} & I\_{xy} & I\_{xz} \ I\_{yx} & I\_{yy} & I\_{yz} \ I\_{zx} & I\_{zy} & I\_{zz} \end{bmatrix}$$

**d. 3-Dimensional Tensor**

* **Definition**: Three indices needed
* **Notation**: $$Tᵢⱼₖ$$ or $$T\[i,j,k]$$
* **Examples**:
  * Piezoelectric tensor: $$d\_{ᵢⱼₖ}$$
  * Elastic stiffness tensor: $$C\_{ᵢⱼₖₗ}$$

#### Mathematical Properties Based on Dimension

**a. Transformation Rules**

For a tensor $$T$$ of dimension $$n$$, under coordinate transformation $$R$$:

$$T{\prime}{i\_1 i\_2 \ldots i\_n} = R{i\_1 j\_1} R\_{i\_2 j\_2} \ldots R\_{i\_n j\_n} T\_{j\_1 j\_2 \ldots j\_n}$$

**b. Components Count**

For a tensor in n-dimensional space:

* 0D (Scalar): $$1$$ component
* 1D (Vector): $$n$$ components
* 2D (Matrix): $$n²$$ components
* 3D Tensor: $$n³$$ components

#### Mathematical Operations by Dimension

**a. Inner Products**

* Vectors (1D): $$\mathbf{a} \cdot \mathbf{b} = \sum\_{i} a\_i b\_i$$
* Matrices (2D): $$\mathbf{A} : \mathbf{B} = \sum\_{i,j} A\_{ij} B\_{ij}$$
* 3D Tensors: $$T\_1 \cdot T\_2 = \sum\_{i,j,k} T\_{1\_{ijk}} T\_{2\_{ijk}}$$

**b. Outer Products**

* Vectors to Matrix: $$(a \otimes b)\_{ij} = a\_i b\_j$$
* Matrices to 4D Tensor: $$(A \otimes B){ijkl} = A{ij} B\_{kl}$$

### Implementation with PHP

<details>

<summary>Tensor Dimensions</summary>

```php
<?php

// 0-dimensional tensor (Scalar)
$scalar = 5;

// 1-dimensional tensor (Vector)
$vector = [1, 2, 3];

// 2-dimensional tensor (Matrix)
$matrix = [
    [1, 2, 3],
    [4, 5, 6]
];

// 3-dimensional tensor
$tensor_3d = [
    [[1, 2], [3, 4]],
    [[5, 6], [7, 8]]
];

// Function to determine dimension
function getTensorDimension($tensor) {
    if (!is_array($tensor)) {
        return 0;  // Scalar
    }
    
    $dim = 1;
    $current = $tensor;
    
    while (is_array(current($current))) {
        $dim++;
        $current = current($current);
    }
    
    return $dim;
}

// Examples
echo "Scalar dimension: " . getTensorDimension($scalar) . "\n";  // 0
echo "Vector dimension: " . getTensorDimension($vector) . "\n";  // 1
echo "Matrix dimension: " . getTensorDimension($matrix) . "\n";  // 2
echo "3D Tensor dimension: " . getTensorDimension($tensor_3d) . "\n";  // 3
```

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://apphp.gitbook.io/artificial-intelligence-with-php/machine-learning/mathematics-for-ml/linear-algebra/tensors/tensor-properties/tensor-dimension.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
