Vector Operations Link to heading

Operations on two vectors are performed element-wise. For example, addition of two vectors a\vec{\mathbf{a}} and b\vec{\mathbf{b}} is done by adding corresponding elements of the two vectors.

Give the two vectors:

a=[a1a2an] , b=[b1b2bn]\vec{\mathbf{a}} = \begin{bmatrix} a_1 \\ a_2 \\ \vdots \\ a_n \end{bmatrix} \text{ , } \vec{\mathbf{b}} = \begin{bmatrix} b_1 \\ b_2 \\ \vdots \\ b_n \end{bmatrix}

Addition of vectors Link to heading

ci=ai+bi ,c=a+b=[a1+b1a2+b2an+bn]c_i = a_i + b_i \text{ ,} \quad \vec{\mathbf{c}}=\vec{\mathbf{a}}+\vec{\mathbf{b}} = \begin{bmatrix} a_1+b_1 \\ a_2+b_2 \\ \vdots \\ a_n+b_n \end{bmatrix}

Subtraction of vectors Link to heading

ci=aibi ,c=ab=[a1b1a2b2anbn]c_i = a_i - b_i \text{ ,} \quad \vec{\mathbf{c}} = \vec{\mathbf{a}} - \vec{\mathbf{b}} = \begin{bmatrix} a_1 - b_1 \\ a_2 - b_2 \\ \vdots \\ a_n - b_n \end{bmatrix}

Multiplication of vectors by a scalar Link to heading

Multiplying a vector by a scalar results in a new vector with the same size.

αRαa=[αa1αa2αan] \alpha \in \mathbb{R} \quad \Rightarrow \quad \alpha \vec{\mathbf{a}} = \begin{bmatrix} \alpha a_1 \\ \alpha a_2 \\ \vdots \\ \alpha a_n \end{bmatrix}

Dot product of vectors Link to heading

The dot product of two vectors is the sum of the products of their corresponding elements. So, the dot product of two vectors is a scalar value.

ab=a1b1+a2b2++anbn=ab=i=1naibi\vec{\mathbf{a}} \cdot \vec{\mathbf{b}} = a_1b_1 + a_2b_2 + \dots + a_nb_n = \vec{\mathbf{a}} \cdot \vec{\mathbf{b}} = \sum_{i=1}^{n} a_ib_i

The dot product of two vectors is also called the inner product.

Transpose of a Vector Link to heading

The transpose of a vector is a column vector represented as a row vector or vice versa.

For example, the vector a\vec{\mathbf{a}} is a column vector:

a=[a1a2an]\vec{\mathbf{a}} = \begin{bmatrix} a_1 \\ a_2 \\ \vdots \\ a_n \end{bmatrix}

The transpose of a\vec{\mathbf{a}} is a row vector which denoted as a\vec{\mathbf{a}}^\top:

a=[a1a2an]\vec{\mathbf{a}}^\top = \begin{bmatrix} a_1 & a_2 & \dots & a_n \end{bmatrix}

Matrix Element-wise Operations Link to heading

Element-wise operations are defined for matrices in the same shape. The operation performs on corresponding elements of the two matrices.

A=[a11a12a1na21a22a2nam1am2amn] , B=[b11b12b1nb21b22b2nbm1bm2bmn]A=\begin{bmatrix} a_{11} & a_{12} & \dots & a_{1n} \\ a_{21} & a_{22} & \dots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \dots & a_{mn} \end{bmatrix} \text{ , } B=\begin{bmatrix} b_{11} & b_{12} & \dots & b_{1n} \\ b_{21} & b_{22} & \dots & b_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ b_{m1} & b_{m2} & \dots & b_{mn} \end{bmatrix}

Addition of matrices Link to heading

C=A+B=[a11+b11a12+b12a1n+b1na21+b21a22+b22a2n+b2nam1+bm1am2+bm2amn+bmn]C = A + B = \begin{bmatrix} a_{11}+b_{11} & a_{12}+b_{12} & \dots & a_{1n}+b_{1n} \\ a_{21}+b_{21} & a_{22}+b_{22} & \dots & a_{2n}+b_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1}+b_{m1} & a_{m2}+b_{m2} & \dots & a_{mn}+b_{mn} \end{bmatrix}

Subtraction of matrices Link to heading

C=AB=[a11b11a12b12a1nb1na21b21a22b22a2nb2nam1bm1am2bm2amnbmn]C = A - B = \begin{bmatrix} a_{11}-b_{11} & a_{12}-b_{12} & \dots & a_{1n}-b_{1n} \\ a_{21}-b_{21} & a_{22}-b_{22} & \dots & a_{2n}-b_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1}-b_{m1} & a_{m2}-b_{m2} & \dots & a_{mn}-b_{mn} \end{bmatrix}

Element-wise Multiplication of matrices Link to heading

The element-wise multiplication of two matrices is called the Hadamard product. The result is a new matrix with the same shape as the input matrices. In this operation, each element of the first matrix is multiplied by the corresponding element of the second matrix.

C=AB=[a11b11a12b12a1nb1na21b21a22b22a2nb2nam1bm1am2bm2amnbmn]C = A \odot B = \begin{bmatrix} a_{11}b_{11} & a_{12}b_{12} & \dots & a_{1n}b_{1n} \\ a_{21}b_{21} & a_{22}b_{22} & \dots & a_{2n}b_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1}b_{m1} & a_{m2}b_{m2} & \dots & a_{mn}b_{mn} \end{bmatrix}

Sometimes the Hadamard product is denoted as ABA \circ B.

Division of matrices Link to heading

The division of two matrices is also performed element-wise. The division of two matrices AA and BB is defined as:

C=AB=[a11b11a12b12a1nb1na21b21a22b22a2nb2nam1bm1am2bm2amnbmn]C = A \oslash B = \begin{bmatrix} \frac{a_{11}}{b_{11}} & \frac{a_{12}}{b_{12}} & \dots & \frac{a_{1n}}{b_{1n}} \\ \frac{a_{21}}{b_{21}} & \frac{a_{22}}{b_{22}} & \dots & \frac{a_{2n}}{b_{2n}} \\ \vdots & \vdots & \ddots & \vdots \\ \frac{a_{m1}}{b_{m1}} & \frac{a_{m2}}{b_{m2}} & \dots & \frac{a_{mn}}{b_{mn}} \end{bmatrix}A=[1234] , B=[5678]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \text{ , } B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix}

Then the division of AA by BB is:

AB=AB=[15263748]=[0.20.3330.4290.5]\frac{A}{B} = A \oslash B = \begin{bmatrix} \frac{1}{5} & \frac{2}{6} \\ \\ \frac{3}{7} & \frac{4}{8} \end{bmatrix} = \begin{bmatrix} 0.2 & 0.333 \\ 0.429 & 0.5 \end{bmatrix}

Broadcasting (Operations on Matrices and Vectors) Link to heading

Broadcasting is a technique used in computational libraries (e.g. NumPy) to perform element-wise operations on arrays of different shapes. In this technique, the smaller array is broadcasted to match the shape of the larger array so that the operation can be performed.

In the case of matrices, broadcasting is used to perform element-wise operations on matrices of different shapes. The smaller matrix is broadcasted to match the shape of the larger matrix so that the operation can be performed.

Broadcasting is done by replicating the elements of the smaller matrix along the missing dimensions to match the shape of the larger matrix.

For example, given matrices AA and BB:

A=[1234] , B=[56]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \text{ , } B = \begin{bmatrix} 5 & 6 \end{bmatrix}

To add matrix BB to matrix AA, the smaller matrix BB is broadcasted to match the shape of matrix AA:

B=[5656]B = \begin{bmatrix} 5 & 6 \\ 5 & 6 \end{bmatrix}A+B=[1234]+[5656]A + B = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} + \begin{bmatrix} 5 & 6 \\ 5 & 6 \end{bmatrix}A+B=[1+52+63+54+6]=[68810]A + B = \begin{bmatrix} 1+5 & 2+6 \\ 3+5 & 4+6 \end{bmatrix} = \begin{bmatrix} 6 & 8 \\ 8 & 10 \end{bmatrix}

After the broadcasting, any types of element-wise operations can be performed on the matrices.

Multiplication of Matrices by a Scalar Link to heading

We can see a scalar value as a special case of a vector with 1×11 \times 1 shape. Then we can use the same broadcasting technique.

αRαA=[αa11αa12αa1nαa21αa22αa2nαam1αam2αamn]\alpha \in \mathbb{R} \quad \Rightarrow \quad \alpha A = \begin{bmatrix} \alpha a_{11} & \alpha a_{12} & \dots & \alpha a_{1n} \\ \alpha a_{21} & \alpha a_{22} & \dots & \alpha a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ \alpha a_{m1} & \alpha a_{m2} & \dots & \alpha a_{mn} \end{bmatrix}

Multiplying a matrix by a scalar is done by multiplying each element of the matrix by the scalar.

The computation of this using broadcasting is as follows:

B=[α]B = \begin{bmatrix} \alpha \end{bmatrix}A=[1234]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}

Then using broadcasting we broadcast the scalar to match the shape of the matrix:

B=[αααα]B = \begin{bmatrix} \alpha & \alpha \\ \alpha & \alpha \end{bmatrix}

Then the element-wise multiplication is performed as before:

αA=[1234][αααα]=[α1α2α3α4]\alpha A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \odot \begin{bmatrix} \alpha & \alpha \\ \alpha & \alpha \end{bmatrix} = \begin{bmatrix} \alpha \cdot 1 & \alpha \cdot 2 \\ \alpha \cdot 3 & \alpha \cdot 4 \end{bmatrix}

Addition of Matrices with Vectors Link to heading

Using the same technique, we can add a vector to a matrix.

Given a matrix AA and a vector b\vec{\mathbf{b}}:

A=[123456789] , b=[571]A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} \text{ , } \vec{\mathbf{b}} = \begin{bmatrix} 5 & 7 & 1\end{bmatrix}

To add the vector to the matrix, we first broadcast the vector to match the shape of the matrix:

B=[571571571]B = \begin{bmatrix} 5 & 7 & 1 \\ 5 & 7 & 1 \\ 5 & 7 & 1 \end{bmatrix}

Then the element-wise addition is performed:

A+B=[123456789]+[571571571]A + B = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} + \begin{bmatrix} 5 & 7 & 1 \\ 5 & 7 & 1 \\ 5 & 7 & 1 \end{bmatrix}A+B=[1+52+73+14+55+76+17+58+79+1]=[6949127121510]A + B = \begin{bmatrix} 1+5 & 2+7 & 3+1 \\ 4+5 & 5+7 & 6+1 \\ 7+5 & 8+7 & 9+1 \end{bmatrix} = \begin{bmatrix} 6 & 9 & 4 \\ 9 & 12 & 7 \\ 12 & 15 & 10 \end{bmatrix}

Note: For broadcasting the smaller vector should be a row or column vector with the same number of elements as the matrix row or column. Except for the case of a scalar, which can be broadcasted to any shape.

Therefore, if we have matrix with the same of m×nm \times n, only vectors with the shape of 1×n1 \times n or m×1m \times 1 can be broadcasted to the shape of the matrix and perform element-wise operations.

b=[57]\vec{\mathbf{b}} = \begin{bmatrix} 5 & 7 \end{bmatrix}

Using broadcasting, we can perform all types of operations on matrices and vectors.

Transpose of a Matrix Link to heading

The transpose of a matrix is obtained by swapping its rows and columns. The transpose of a matrix AA is denoted as AA^\top.

A=[a11a12a1na21a22a2nam1am2amn]A=[a11a21am1a12a22am2a1na2namn]A=\begin{bmatrix} a_{11} & a_{12} & \dots & a_{1n} \\ a_{21} & a_{22} & \dots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \dots & a_{mn} \end{bmatrix} \quad \Rightarrow \quad A^\top = \begin{bmatrix} a_{11} & a_{21} & \dots & a_{m1} \\ a_{12} & a_{22} & \dots & a_{m2} \\ \vdots & \vdots & \ddots & \vdots \\ a_{1n} & a_{2n} & \dots & a_{mn} \end{bmatrix}

Example:

A=[123456]A=[142536]A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} \quad \Rightarrow \quad A^\top = \begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix}

Matrix Multiplication (Dot Product) Link to heading

Matrix multiplication two matrices AA and BB is defined as dot product of rows of first matrix and columns of the second matrix.

This is different from element-wise multiplication which is performed on corresponding elements of the two matrices.

The requirement is that the number of columns of the first matrix must be equal to the number of rows of the second matrix. Because the dot product of two vectors is only defined when the two vectors have the same size.

If AA is of size m×nm \times n and BB is of size n×pn \times p:

A=[a11a12a1na21a22a2nam1am2amn] , B=[b11b12b1pb21b22b2pbn1bn2bnp]A = \begin{bmatrix} a_{11} & a_{12} & \dots & a_{1n} \\ a_{21} & a_{22} & \dots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \dots & a_{mn} \end{bmatrix} \text{ , } B = \begin{bmatrix} b_{11} & b_{12} & \dots & b_{1p} \\ b_{21} & b_{22} & \dots & b_{2p} \\ \vdots & \vdots & \ddots & \vdots \\ b_{n1} & b_{n2} & \dots & b_{np} \end{bmatrix}

Then the product C=ABC = A \cdot B is of size m×pm \times p.

AA of size m×nm \times n and BB of size n×pn \times p \Rightarrow CC of size m×pm \times p.

C=AB=[c11c12c1pc21c22c2pcm1cm2cmp]C = A \cdot B = \begin{bmatrix} c_{11} & c_{12} & \dots & c_{1p} \\ c_{21} & c_{22} & \dots & c_{2p} \\ \vdots & \vdots & \ddots & \vdots \\ c_{m1} & c_{m2} & \dots & c_{mp} \end{bmatrix}cij=k=1naikbkjc_{ij} = \sum_{k=1}^{n} a_{ik}b_{kj}

In other words, the element cijc_{ij} of the product matrix CC is the dot product of the ii-th row of matrix AA and the jj-th column of matrix BB.

cij=rows of Acolumns of Bc_{ij} = \text{rows of } A \cdot \text{columns of } Bc11=[a11a12a1n][b11b21bn1]=a11b11+a12b21++a1nbn1c_{11} = \begin{bmatrix} a_{11} & a_{12} & \dots & a_{1n} \end{bmatrix} \cdot \begin{bmatrix} b_{11} \\ b_{21} \\ \vdots \\ b_{n1} \end{bmatrix} = a_{11}b_{11} + a_{12}b_{21} + \dots + a_{1n}b_{n1}c12=[a11a12a1n][b12b22bn2]=a11b12+a12b22++a1nbn2c_{12} = \begin{bmatrix} a_{11} & a_{12} & \dots & a_{1n} \end{bmatrix} \cdot \begin{bmatrix} b_{12} \\ b_{22} \\ \vdots \\ b_{n2} \end{bmatrix} = a_{11}b_{12} + a_{12}b_{22} + \dots + a_{1n}b_{n2}\vdotscmp=[am1am2amn][b1pb2pbnp]=am1b1p+am2b2p++amnbnpc_{mp} = \begin{bmatrix} a_{m1} & a_{m2} & \dots & a_{mn} \end{bmatrix} \cdot \begin{bmatrix} b_{1p} \\ b_{2p} \\ \vdots \\ b_{np} \end{bmatrix} = a_{m1}b_{1p} + a_{m2}b_{2p} + \dots + a_{mn}b_{np}

The first row of AA is multiplied by the first column of BB to get the first element of CC. Then the first row of AA is multiplied by the second column of BB to get the second element of CC, and so on.

c11=a11b11+a12b21++a1nbn1c_{11} = a_{11}b_{11} + a_{12}b_{21} + \dots + a_{1n}b_{n1}c12=a11b12+a12b22++a1nbn2c_{12} = a_{11}b_{12} + a_{12}b_{22} + \dots + a_{1n}b_{n2}

Notes:

  • Both notations of A.BA.B or A×BA \times B are used for matrix multiplication.
  • Good way to remember it is that the result matrix has the same number of rows as the first matrix, and the same number of columns as the second matrix.
  • Think of A.B=CA.B=C, as the rows of AA will influene the rows of CC, and the columns of BB will influence the columns of CC. For example, the 2nd row and 3rd column of CC is the dot product of the 2nd row of AA and the 3rd column of BB.

For example, given matrices AA (2x3) and BB (3x2):

A=[123456] , B=[789101112]A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} \text{ , } B = \begin{bmatrix} 7 & 8 \\ 9 & 10 \\ 11 & 12 \end{bmatrix}

So for C=ABC = A \cdot B:

C=AB=[17+29+31118+210+31247+59+61148+510+612]=[5864139154]C=A \cdot B = \begin{bmatrix} 1 \cdot 7 + 2 \cdot 9 + 3 \cdot 11 & 1 \cdot 8 + 2 \cdot 10 + 3 \cdot 12 \\ 4 \cdot 7 + 5 \cdot 9 + 6 \cdot 11 & 4 \cdot 8 + 5 \cdot 10 + 6 \cdot 12 \end{bmatrix} = \begin{bmatrix} 58 & 64 \\ 139 & 154 \end{bmatrix}

We can think of matrix multiplication as a series of dot products between rows of the first matrix and columns of the second matrix.

C=[A row 1B column 1A row 1B column 2A row 2B column 1A row 2B column 2]C=\begin{bmatrix} \text{A row 1} \cdot \text{B column 1} & \text{A row 1} \cdot \text{B column 2} \\ \text{A row 2} \cdot \text{B column 1} & \text{A row 2} \cdot \text{B column 2} \end{bmatrix}

Or in a more general form:

Cij=k=1nAikBkjC_{ij} = \sum_{k=1}^{n} A_{ik} \cdot B_{kj}
Cij=k=1nAikBkjC_{ij} = \sum_{k=1}^{n} A_{ik} B_{kj}

Matrix Multiplication is not Commutative In general, matrix multiplication is not commutative, meaning that the order of matrices in a product matters. Swapping the order of matrices in a product usually yields a different result.

ABBAA \cdot B \neq B \cdot A

Vectors are Special Case of Matrices: A vector can be represented as a matrix with a single row or column.

Column vector a\vec{\mathbf{a}} is a matrix with shape of 1×n1 \times n:

a=[a1a2an]\vec{\mathbf{a}} = \begin{bmatrix} a_1 & a_2 & \dots & a_n \end{bmatrix}

Row vector a\vec{\mathbf{a}} is a matrix with shape of n×1n \times 1:

a=[a1a2an]\vec{\mathbf{a}} = \begin{bmatrix} a_1 \\ a_2 \\ \vdots \\ a_n \end{bmatrix}

Matrix-Vector Multiplication: This is a special case of matrix multiplication where one of the matrices has only one column or row, i.e. a vector.

A=[123456] , b=[78]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \end{bmatrix} \text{ , } \vec{\mathbf{b}} = \begin{bmatrix} 7 \\ 8 \end{bmatrix}c=Ab=[17+2837+4857+68]=[235383]\vec{\mathbf{c}} = A \cdot \vec{\mathbf{b}} = \begin{bmatrix} 1 \cdot 7 + 2 \cdot 8 \\ 3 \cdot 7 + 4 \cdot 8 \\ 5 \cdot 7 + 6 \cdot 8 \end{bmatrix} = \begin{bmatrix} 23 \\ 53 \\ 83 \end{bmatrix}

Note: In this case, there is no broadcasting happens, because the matrix and vector meet the requirements and are in shape of m×nm \times n and n×1n \times 1 respectively (first matrix columns = second matrix rows).

Dot Product of Vectors as Matrix Multiplication: We can also think of the dot product of two vectors as a special case of matrix multiplication.

If we have two vectors a\vec{\mathbf{a}} and b\vec{\mathbf{b}}:

a=[a1a2an] , b=[b1b2bn]\vec{\mathbf{a}} = \begin{bmatrix} a_1 \\ a_2 \\ \vdots \\ a_n \end{bmatrix} \text{ , } \vec{\mathbf{b}} = \begin{bmatrix} b_1 \\ b_2 \\ \vdots \\ b_n \end{bmatrix}

The dot product of a\vec{\mathbf{a}} and b\vec{\mathbf{b}} is the same as the matrix multiplication of a row vector and a column vector. So, we need to transpose one of the vectors to make it a row vector.

ab=ab\vec{\mathbf{a}} \cdot \vec{\mathbf{b}} = \vec{\mathbf{a}}^\top \cdot \vec{\mathbf{b}}

Which is as same as the matrix multiplication of matrix with 1×n1 \times n and n×1n \times 1:

[a1a2an][b1b2bn]=a1b1+a2b2++anbn\begin{bmatrix} a_1 & a_2 & \dots & a_n \end{bmatrix} \cdot \begin{bmatrix} b_1 \\ b_2 \\ \vdots \\ b_n \end{bmatrix} = a_1b_1 + a_2b_2 + \dots + a_nb_n

Tensor Link to heading

A tensor is a generalized multi-dimensional array that extends the concepts of scalars (0D), vectors (1D), and matrices (2D) to higher dimensions (3D, 4D, etc.).

In simple terms, a tensor is a generalized way to represent data of any number of dimensions, from 0D (scalar) to n-dimensional space.

  • 0D tensor is a scalar (single number).
  • 1D tensor is a vector (array of numbers).
  • 2D tensor is a matrix (2D array).
  • Higher-order tensor is a multi-dimensional array (3D, 4D, etc.) representing high-dimensional data such as high dimensional feature space (e.g. images, videos, etc.).

A tensor is just a flexible way to handle data of any dimension (shape) in mathematics (specially in linear algebra, differential geometry), physics and programming.

Example of tensors in Python using NumPy:

import numpy as np

# A scalar (0D tensor), shape: ()
scalar = np.array(5)
# 5

# A vector (1D tensor), shape: (3,) = 3 elements
vector = np.array([1, 2, 3])
# [1 2 3]

# A matrix (2D tensor), shape: (3, 2) = 3 rows, 2 columns
matrix = np.array([[1, 2], [3, 4], [5, 6]])
# [[1 2]
#  [3 4]
#  [5 6]]

# A 3D tensor, shape: (2, 2, 2) = 2x2x2 cube
tensor_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
# [[[1 2]
#   [3 4]]
#
#  [[5 6]
#   [7 8]]]

Matrix 2×32 \times 3: 2 rows and 3 columns

[123456]\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}
np.array([[1, 2, 3], [4, 5, 6]])

Matrix 3×23 \times 2: 3 rows and 2 columns

[123456]\begin{bmatrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \end{bmatrix}
np.array([[1, 2], [3, 4], [5, 6]])

Column Vector as a Matrix: A column vector can be represented as a matrix with a single column.

Column vector with 3 elements is a 3×13 \times 1 matrix:

[123]\begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix}
np.array([[1], [2], [3]])

Row Vector as a Matrix: A row vector can be represented as a matrix with a single row.

Row vector with 3 elements is a 1×31 \times 3 matrix:

[123]\begin{bmatrix} 1 & 2 & 3 \end{bmatrix}
np.array([[1, 2, 3]])

Machine learning libraries like TensorFlow, PyTorch uses 2D tensors to represent vectors and matrices for efficient computation. So, a 1D vector is represented as a 2D tensor with a single roj or column.

Both PyTorch and TensorFlow has their own tensor classes to represent multi-dimensional data. For example, a 1D row vector in PyTorch is represented as a 2D tensor with a single row.

vector = torch.tensor([[1, 2, 3]])