Part of the series

Several example codes

~1 min read • Updated Oct 13, 2025

Program Overview

This Python program reads two one-dimensional vectors a and b, computes their outer product, and stores the result in matrix c.
The outer product is defined as c[i][j] = a[i] × b[j].


Python Code:


def outer_product(a: list[float], b: list[float]) -> list[list[float]]:
    rows = len(a)
    cols = len(b)
    c = [[a[i] * b[j] for j in range(cols)] for i in range(rows)]
    return c

def print_matrix(matrix: list[list[float]]):
    print("Outer product matrix:")
    for row in matrix:
        print("  ".join(f"{val:.1f}" for val in row))

# Sample data
a = [2.2, 0.0, 4.4]
b = [2.0, 1.0, 3.0, 2.0]

# Compute outer product
c = outer_product(a, b)

# Display result
print_matrix(c)

Sample Output:


Outer product matrix:  
4.4   2.2   6.6   4.4  
0.0   0.0   0.0   0.0  
8.8   4.4  13.2   8.8

Written & researched by Dr. Shahin Siami