from typing import Sequence
import torch


def affine_transformation_scale(s: Sequence[float]) -> torch.Tensor:
    """
        Defines an affine scaling transformation (2D or 3D)

        For a 3D transformation, returns 4x4 matrix:

               | Sx 0  0  0 |
           M = | 0  Sy 0  0 |
               | 0  0  Sz 0 |
               | 0  0  0  1 |
        Args:
            s: a (Sx, Sy, Sz) or (Sx, Sy) tuple

        Returns:
            a transformation matrix
        """
    d = len(s)
    assert d == 2 or d == 3
    tfm = torch.zeros((d + 1, d + 1), dtype=torch.float32)
    for n in range(d):
        tfm[n, n] = float(s[n])
    tfm[d, d] = 1
    return tfm