awk — Linear Algebra

Dot product of two vectors
echo "3 4 5" | awk '{
    split("1 2 3", b)
    dot=0
    for(i=1;i<=NF;i++) dot+=$i*b[i]
    printf "a·b = %d\n", dot
}'
# a=[3,4,5] b=[1,2,3] → 3*1 + 4*2 + 5*3 = 26
Vector magnitude (Euclidean norm)
echo "3 4" | awk '{sum=0; for(i=1;i<=NF;i++) sum+=$i^2; printf "|v| = %.4f\n", sqrt(sum)}'
# |[3,4]| = 5.0000
Cross product of two 3D vectors
awk 'BEGIN{
    split("2 3 4", a); split("5 6 7", b)
    printf "a×b = [%d, %d, %d]\n",
        a[2]*b[3]-a[3]*b[2],
        a[3]*b[1]-a[1]*b[3],
        a[1]*b[2]-a[2]*b[1]
}'
Unit vector (normalize)
echo "3 4 0" | awk '{
    sum=0; for(i=1;i<=NF;i++) sum+=$i^2
    mag=sqrt(sum)
    printf "unit = ["
    for(i=1;i<=NF;i++) printf "%s%.4f", (i>1?", ":""), $i/mag
    printf "]\n"
}'
2x2 matrix multiplication
awk 'BEGIN{
    # A = [[1,2],[3,4]]  B = [[5,6],[7,8]]
    split("1 2 3 4", A); split("5 6 7 8", B)
    # C[i,j] = sum(A[i,k] * B[k,j])
    for(i=0;i<2;i++) for(j=0;j<2;j++){
        s=0; for(k=0;k<2;k++) s+=A[i*2+k+1]*B[k*2+j+1]
        C[i*2+j+1]=s
    }
    printf "| %3d %3d |\n| %3d %3d |\n", C[1],C[2],C[3],C[4]
}'
3x3 matrix multiplication
awk 'BEGIN{
    # A and B as flat 9-element arrays (row-major)
    split("1 2 3 4 5 6 7 8 9", A)
    split("9 8 7 6 5 4 3 2 1", B)
    for(i=0;i<3;i++) for(j=0;j<3;j++){
        s=0; for(k=0;k<3;k++) s+=A[i*3+k+1]*B[k*3+j+1]
        C[i*3+j+1]=s
    }
    for(i=0;i<3;i++) printf "| %4d %4d %4d |\n", C[i*3+1],C[i*3+2],C[i*3+3]
}'
Matrix transpose (3x3)
awk 'BEGIN{
    split("1 2 3 4 5 6 7 8 9", A)
    printf "Original:\n"
    for(i=0;i<3;i++) printf "| %d %d %d |\n", A[i*3+1],A[i*3+2],A[i*3+3]
    printf "\nTranspose:\n"
    for(j=0;j<3;j++) printf "| %d %d %d |\n", A[j+1],A[j+4],A[j+7]
}'
2x2 determinant
echo "3 8 4 6" | awk '{printf "det = %d\n", $1*$4 - $2*$3}'
# | 3 8 |
# | 4 6 | = 3*6 - 8*4 = -14
3x3 determinant (Sarrus' rule)
awk 'BEGIN{
    split("1 2 3 4 5 6 7 8 9", a)
    det = a[1]*(a[5]*a[9]-a[6]*a[8]) - a[2]*(a[4]*a[9]-a[6]*a[7]) + a[3]*(a[4]*a[8]-a[5]*a[7])
    printf "det = %d\n", det
}'
2x2 matrix inverse
echo "4 7 2 6" | awk '{
    det=$1*$4-$2*$3
    if(det==0){print "singular — no inverse"; exit 1}
    printf "1/det * | %6.3f %6.3f |\n", $4/det, -$2/det
    printf "        | %6.3f %6.3f |\n", -$3/det, $1/det
}'
Solve 2x2 system with Cramer’s rule
# 3x + 2y = 16
# 1x + 4y = 18
awk 'BEGIN{
    a=3; b=2; e=16
    c=1; d=4; f=18
    det=a*d-b*c
    x=(e*d-b*f)/det
    y=(a*f-e*c)/det
    printf "x=%.2f y=%.2f\n", x, y
}'
# x=2.80 y=3.80
Solve 3x3 system with Cramer’s rule
# 2x + 1y - 1z = 8
# -3x - 1y + 2z = -11
# -2x + 1y + 2z = -3
awk 'BEGIN{
    # Coefficient matrix and constants
    split("2 1 -1 -3 -1 2 -2 1 2", a)
    split("8 -11 -3", b)

    # det(A)
    D = a[1]*(a[5]*a[9]-a[6]*a[8]) - a[2]*(a[4]*a[9]-a[6]*a[7]) + a[3]*(a[4]*a[8]-a[5]*a[7])

    # det with column 1 replaced by b
    Dx = b[1]*(a[5]*a[9]-a[6]*a[8]) - a[2]*(b[2]*a[9]-a[6]*b[3]) + a[3]*(b[2]*a[8]-a[5]*b[3])

    # det with column 2 replaced by b
    Dy = a[1]*(b[2]*a[9]-a[6]*b[3]) - b[1]*(a[4]*a[9]-a[6]*a[7]) + a[3]*(a[4]*b[3]-b[2]*a[7])

    # det with column 3 replaced by b
    Dz = a[1]*(a[5]*b[3]-b[2]*a[8]) - a[2]*(a[4]*b[3]-b[2]*a[7]) + b[1]*(a[4]*a[8]-a[5]*a[7])

    printf "x=%.2f y=%.2f z=%.2f\n", Dx/D, Dy/D, Dz/D
}'
# x=2.00 y=3.00 z=-1.00
2D rotation matrix — rotate point by angle
# Rotate point (3, 4) by 45 degrees
awk 'BEGIN{
    x=3; y=4
    theta=45 * atan2(0,-1)/180   # degrees to radians
    rx = x*cos(theta) - y*sin(theta)
    ry = x*sin(theta) + y*cos(theta)
    printf "(%.4f, %.4f) rotated 45° = (%.4f, %.4f)\n", x, y, rx, ry
}'
Scale and translate a 2D point (affine transform)
awk 'BEGIN{
    x=5; y=10
    sx=2; sy=3     # scale factors
    tx=10; ty=20   # translation
    printf "original: (%d,%d)\n", x, y
    printf "scaled:   (%d,%d)\n", x*sx, y*sy
    printf "transformed: (%d,%d)\n", x*sx+tx, y*sy+ty
}'
Distance between two points (2D and 3D)
awk 'BEGIN{
    # 2D
    x1=1; y1=2; x2=4; y2=6
    d2=sqrt((x2-x1)^2+(y2-y1)^2)
    printf "2D distance: %.4f\n", d2

    # 3D
    z1=0; z2=3
    d3=sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2)
    printf "3D distance: %.4f\n", d3
}'
Angle between two vectors (via dot product)
awk 'BEGIN{
    split("1 0 0", a); split("0 1 0", b)
    dot=0; ma=0; mb=0
    for(i=1;i<=3;i++){dot+=a[i]*b[i]; ma+=a[i]^2; mb+=b[i]^2}
    cos_theta=dot/(sqrt(ma)*sqrt(mb))
    # acos via atan2
    theta=atan2(sqrt(1-cos_theta^2), cos_theta)
    printf "angle = %.2f radians = %.2f degrees\n", theta, theta*180/atan2(0,-1)
}'