PASCAL
Constructs an n-by-n Pascal matrix. A Pascal matrix contains the binomial coefficients as its entries.
Excel Usage
=PASCAL(n, pascal_kind)
n(int, required): The order of the matrix.pascal_kind(str, optional, default: “symmetric”): Type of Pascal matrix to return (symmetric, lower, or upper).
Returns (list[list]): 2D array representing the Pascal matrix.
Example 1: Symmetric Pascal matrix of order 3
Inputs:
| n | pascal_kind |
|---|---|
| 3 | symmetric |
Excel formula:
=PASCAL(3, "symmetric")
Expected output:
| Result | ||
|---|---|---|
| 1 | 1 | 1 |
| 1 | 2 | 3 |
| 1 | 3 | 6 |
Example 2: Lower triangular Pascal matrix
Inputs:
| n | pascal_kind |
|---|---|
| 3 | lower |
Excel formula:
=PASCAL(3, "lower")
Expected output:
| Result | ||
|---|---|---|
| 1 | 0 | 0 |
| 1 | 1 | 0 |
| 1 | 2 | 1 |
Example 3: Upper triangular Pascal matrix
Inputs:
| n | pascal_kind |
|---|---|
| 4 | upper |
Excel formula:
=PASCAL(4, "upper")
Expected output:
| Result | |||
|---|---|---|---|
| 1 | 1 | 1 | 1 |
| 0 | 1 | 2 | 3 |
| 0 | 0 | 1 | 3 |
| 0 | 0 | 0 | 1 |
Example 4: Pascal matrix with single-cell range order
Inputs:
| n | pascal_kind |
|---|---|
| 5 | symmetric |
Excel formula:
=PASCAL({5}, "symmetric")
Expected output:
| Result | ||||
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 1 |
| 1 | 2 | 3 | 4 | 5 |
| 1 | 3 | 6 | 10 | 15 |
| 1 | 4 | 10 | 20 | 35 |
| 1 | 5 | 15 | 35 | 70 |
Python Code
import numpy as np
from scipy.linalg import pascal as scipy_pascal
def pascal(n, pascal_kind='symmetric'):
"""
Construct a Pascal matrix.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.pascal.html
This example function is provided as-is without any representation of accuracy.
Args:
n (int): The order of the matrix.
pascal_kind (str, optional): Type of Pascal matrix to return (symmetric, lower, or upper). Valid options: Symmetric, Lower Triangular, Upper Triangular. Default is 'symmetric'.
Returns:
list[list]: 2D array representing the Pascal matrix.
"""
try:
if isinstance(n, list):
if not n or not isinstance(n[0], list) or not n[0]:
return "Error: n must be a scalar or single-cell range"
n = n[0][0]
try:
n_val = int(n)
except (TypeError, ValueError):
return "Error: n must be an integer"
if n_val <= 0:
return "Error: n must be a positive integer"
if not isinstance(pascal_kind, str):
return "Error: pascal_kind must be a string"
kind = pascal_kind.lower()
if kind not in ["symmetric", "lower", "upper"]:
return "Error: pascal_kind must be one of symmetric, lower, or upper"
res = scipy_pascal(n_val, kind=kind)
return res.tolist()
except Exception as e:
return f"Error: {str(e)}"Online Calculator
The order of the matrix.
Type of Pascal matrix to return (symmetric, lower, or upper).