# Math Operations Comprehensive mathematical operations including arithmetic, trigonometric, linear algebra, and statistical functions. These operations provide the mathematical foundation for machine learning computations and numerical analysis. ## Capabilities ### Basic Arithmetic Fundamental arithmetic operations for tensor computations. ```python { .api } def add(x, y, name=None): """ Returns x + y element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, int16, int32, int64, complex64, complex128, string - y: A Tensor. Must have the same type as x - name: A name for the operation Returns: A Tensor. Has the same type as x """ def subtract(x, y, name=None): """ Returns x - y element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128 - y: A Tensor. Must have the same type as x - name: A name for the operation Returns: A Tensor. Has the same type as x """ def multiply(x, y, name=None): """ Returns x * y element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128 - y: A Tensor. Must have the same type as x - name: A name for the operation Returns: A Tensor. Has the same type as x """ def divide(x, y, name=None): """ Computes Python style division of x by y. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128 - y: A Tensor. Must have the same type as x - name: A name for the operation Returns: A Tensor. Has the same type as x """ def floordiv(x, y, name=None): """ Divides x / y elementwise, rounding toward the most negative integer. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64 - y: A Tensor. Must have the same type as x - name: A name for the operation Returns: A Tensor. Has the same type as x """ def mod(x, y, name=None): """ Returns element-wise remainder of division. Parameters: - x: A Tensor. Must be one of the following types: int8, int16, int32, int64, uint8, uint16, uint32, uint64, bfloat16, half, float32, float64 - y: A Tensor. Must have the same type as x - name: A name for the operation Returns: A Tensor. Has the same type as x """ def pow(x, y, name=None): """ Computes the power of one value to another. Parameters: - x: A Tensor of type float16, float32, float64, int32, int64, complex64, or complex128 - y: A Tensor of type float16, float32, float64, int32, int64, complex64, or complex128 - name: A name for the operation Returns: A Tensor """ ``` ### Mathematical Functions Common mathematical functions and operations. ```python { .api } def abs(x, name=None): """ Computes the absolute value of a tensor. Parameters: - x: A Tensor or SparseTensor of type float16, float32, float64, int8, int16, int32, int64, complex64 or complex128 - name: A name for the operation Returns: A Tensor or SparseTensor the same size, type, and sparsity as x with absolute values """ def sign(x, name=None): """ Returns an element-wise indication of the sign of a number. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ def sqrt(x, name=None): """ Computes element-wise square root of the input tensor. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ def square(x, name=None): """ Computes square of x element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ def exp(x, name=None): """ Computes exponential of x element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ def log(x, name=None): """ Computes natural logarithm of x element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ def log10(x, name=None): """ Computes element-wise log base 10 of x. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ ``` ### Trigonometric Functions Trigonometric and hyperbolic functions for mathematical computations. ```python { .api } def sin(x, name=None): """ Computes sine of x element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ def cos(x, name=None): """ Computes cosine of x element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ def tan(x, name=None): """ Computes tan of x element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ def asin(x, name=None): """ Computes the trignometric inverse sine of x element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ def acos(x, name=None): """ Computes acos of x element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ def atan(x, name=None): """ Computes the trignometric inverse tangent of x element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ def sinh(x, name=None): """ Computes hyperbolic sine of x element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ def cosh(x, name=None): """ Computes hyperbolic cosine of x element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ def tanh(x, name=None): """ Computes hyperbolic tangent of x element-wise. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ ``` ### Linear Algebra Matrix operations and linear algebra functions. ```python { .api } def matmul(a, b, transpose_a=False, transpose_b=False, adjoint_a=False, adjoint_b=False, a_is_sparse=False, b_is_sparse=False, output_type=None, grad_a=False, grad_b=False, name=None): """ Multiplies matrix a by matrix b, producing a * b. Parameters: - a: A Tensor of type float16, float32, float64, int32, int64, complex64, complex128 and rank > 1 - b: A Tensor with same type and rank as a - transpose_a: If True, a is transposed before multiplication - transpose_b: If True, b is transposed before multiplication - adjoint_a: If True, a is conjugated and transposed before multiplication - adjoint_b: If True, b is conjugated and transposed before multiplication - a_is_sparse: If True, a is treated as a sparse matrix - b_is_sparse: If True, b is treated as a sparse matrix - output_type: The output type of the operation (float16, float32, etc.) - grad_a: Whether to use gradient optimized version for matrix a - grad_b: Whether to use gradient optimized version for matrix b - name: Name for the operation Returns: A Tensor of the same type as a and b where each inner-most matrix is the product of the corresponding matrices in a and b """ def transpose(a, perm=None, conjugate=False, name="transpose"): """ Transposes a. Parameters: - a: A Tensor - perm: A permutation of the dimensions of a - conjugate: Setting it to True is mathematically equivalent to tf.math.conj(tf.transpose(input)) - name: A name for the operation Returns: A transposed Tensor """ def trace(x, name=None): """ Compute the trace of a tensor x. Parameters: - x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int32, int64, complex64, complex128 - name: A name for the operation Returns: A Tensor. Has the same type as x """ def det(input, name=None): """ Computes the determinant of one or more square matrices. Parameters: - input: A Tensor of type float32, float64, complex64 or complex128 of shape [..., M, M] - name: A name for the operation Returns: A Tensor of the same type as input with shape [...] """ def inv(input, adjoint=False, name=None): """ Computes the inverse of one or more square invertible matrices or their adjoints (conjugate transposes). Parameters: - input: A Tensor. Must be one of the following types: float64, float32, half, complex64, complex128 - adjoint: An optional bool. Defaults to False - name: A name for the operation Returns: A Tensor. Has the same type as input """ def norm(tensor, ord='euclidean', axis=None, keepdims=None, name=None): """ Computes the norm of vectors, matrices, and tensors. Parameters: - tensor: A Tensor - ord: Order of the norm. Supported values are 'fro', 'euclidean', 1, 2, np.inf and any positive real number yielding the corresponding p-norm - axis: If axis is None (the default), the input is considered a vector and a single vector norm is computed over the entire set of values in the tensor - keepdims: If True, the axis indicated in axis are kept with size 1 - name: The name of the op Returns: A Tensor with the same type as tensor, containing the vector or matrix norms """ ``` ### Reduction Operations Operations that reduce tensor dimensions through aggregation. ```python { .api } def reduce_sum(input_tensor, axis=None, keepdims=None, name=None): """ Computes the sum of elements across dimensions of a tensor. Parameters: - input_tensor: The tensor to reduce. Should have numeric type - axis: The dimensions to reduce. If None (the default), reduces all dimensions - keepdims: If true, retains reduced dimensions with length 1 - name: A name for the operation Returns: The reduced tensor """ def reduce_mean(input_tensor, axis=None, keepdims=None, name=None): """ Computes the mean of elements across dimensions of a tensor. Parameters: - input_tensor: The tensor to reduce. Should have numeric type - axis: The dimensions to reduce. If None (the default), reduces all dimensions - keepdims: If true, retains reduced dimensions with length 1 - name: A name for the operation Returns: The reduced tensor """ def reduce_max(input_tensor, axis=None, keepdims=None, name=None): """ Computes the maximum of elements across dimensions of a tensor. Parameters: - input_tensor: The tensor to reduce. Should have numeric type - axis: The dimensions to reduce. If None (the default), reduces all dimensions - keepdims: If true, retains reduced dimensions with length 1 - name: A name for the operation Returns: The reduced tensor """ def reduce_min(input_tensor, axis=None, keepdims=None, name=None): """ Computes the minimum of elements across dimensions of a tensor. Parameters: - input_tensor: The tensor to reduce. Should have numeric type - axis: The dimensions to reduce. If None (the default), reduces all dimensions - keepdims: If true, retains reduced dimensions with length 1 - name: A name for the operation Returns: The reduced tensor """ def reduce_prod(input_tensor, axis=None, keepdims=None, name=None): """ Computes the product of elements across dimensions of a tensor. Parameters: - input_tensor: The tensor to reduce. Should have numeric type - axis: The dimensions to reduce. If None (the default), reduces all dimensions - keepdims: If true, retains reduced dimensions with length 1 - name: A name for the operation Returns: The reduced tensor """ def reduce_all(input_tensor, axis=None, keepdims=None, name=None): """ Computes the "logical and" of elements across dimensions of a tensor. Parameters: - input_tensor: The boolean tensor to reduce - axis: The dimensions to reduce. If None (the default), reduces all dimensions - keepdims: If true, retains reduced dimensions with length 1 - name: A name for the operation Returns: The reduced tensor """ def reduce_any(input_tensor, axis=None, keepdims=None, name=None): """ Computes the "logical or" of elements across dimensions of a tensor. Parameters: - input_tensor: The boolean tensor to reduce - axis: The dimensions to reduce. If None (the default), reduces all dimensions - keepdims: If true, retains reduced dimensions with length 1 - name: A name for the operation Returns: The reduced tensor """ ``` ### Element-wise Comparisons Operations for comparing tensor elements. ```python { .api } def equal(x, y, name=None): """ Returns the truth value of (x == y) element-wise. Parameters: - x: A Tensor - y: A Tensor. Must have the same type as x - name: A name for the operation Returns: A Tensor of type bool """ def not_equal(x, y, name=None): """ Returns the truth value of (x != y) element-wise. Parameters: - x: A Tensor - y: A Tensor. Must have the same type as x - name: A name for the operation Returns: A Tensor of type bool """ def less(x, y, name=None): """ Returns the truth value of (x < y) element-wise. Parameters: - x: A Tensor - y: A Tensor. Must have the same type as x - name: A name for the operation Returns: A Tensor of type bool """ def less_equal(x, y, name=None): """ Returns the truth value of (x <= y) element-wise. Parameters: - x: A Tensor - y: A Tensor. Must have the same type as x - name: A name for the operation Returns: A Tensor of type bool """ def greater(x, y, name=None): """ Returns the truth value of (x > y) element-wise. Parameters: - x: A Tensor - y: A Tensor. Must have the same type as x - name: A name for the operation Returns: A Tensor of type bool """ def greater_equal(x, y, name=None): """ Returns the truth value of (x >= y) element-wise. Parameters: - x: A Tensor - y: A Tensor. Must have the same type as x - name: A name for the operation Returns: A Tensor of type bool """ def maximum(x, y, name=None): """ Returns the max of x and y (i.e. x > y ? x : y) element-wise. Parameters: - x: A Tensor - y: A Tensor. Must have the same type as x - name: A name for the operation Returns: A Tensor. Has the same type as x """ def minimum(x, y, name=None): """ Returns the min of x and y (i.e. x < y ? x : y) element-wise. Parameters: - x: A Tensor - y: A Tensor. Must have the same type as x - name: A name for the operation Returns: A Tensor. Has the same type as x """ ``` ## Usage Examples ```python import tensorflow as tf # Basic arithmetic a = tf.constant([1.0, 2.0, 3.0]) b = tf.constant([4.0, 5.0, 6.0]) sum_result = tf.add(a, b) # [5.0, 7.0, 9.0] diff_result = tf.subtract(b, a) # [3.0, 3.0, 3.0] prod_result = tf.multiply(a, b) # [4.0, 10.0, 18.0] div_result = tf.divide(b, a) # [4.0, 2.5, 2.0] # Mathematical functions sqrt_result = tf.sqrt(a) # [1.0, 1.414, 1.732] exp_result = tf.exp(a) # [2.718, 7.389, 20.086] log_result = tf.log(b) # [1.386, 1.609, 1.792] # Trigonometric functions sin_result = tf.sin(a) # [0.841, 0.909, 0.141] cos_result = tf.cos(a) # [0.540, -0.416, -0.990] # Matrix operations matrix_a = tf.constant([[1.0, 2.0], [3.0, 4.0]]) matrix_b = tf.constant([[5.0, 6.0], [7.0, 8.0]]) matmul_result = tf.matmul(matrix_a, matrix_b) # [[19.0, 22.0], [43.0, 50.0]] transpose_result = tf.transpose(matrix_a) # [[1.0, 3.0], [2.0, 4.0]] # Reduction operations tensor = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) sum_all = tf.reduce_sum(tensor) # 21.0 sum_axis0 = tf.reduce_sum(tensor, axis=0) # [5.0, 7.0, 9.0] mean_result = tf.reduce_mean(tensor) # 3.5 max_result = tf.reduce_max(tensor) # 6.0 # Comparisons x = tf.constant([1, 2, 3]) y = tf.constant([1, 4, 2]) eq_result = tf.equal(x, y) # [True, False, False] gt_result = tf.greater(x, y) # [False, False, True] max_xy = tf.maximum(x, y) # [1, 4, 3] ```