SPECIAL_ORTHO_GROUP
Overview
The SPECIAL_ORTHO_GROUP
function generates random matrices from the special orthogonal group SO(N), which are orthogonal matrices with determinant +1. These matrices are commonly used in statistics, physics, and engineering for random rotations and transformations that preserve length and orientation. The function wraps scipy.stats.special_ortho_group
, which samples matrices uniformly from SO(N) using advanced mathematical algorithms.
The special orthogonal group SO(N) consists of all real matrices such that:
where is the transpose of , is the identity matrix, and is the determinant.
This Excel wrapper exposes only the most commonly used parameters: dim
(matrix dimension) and size
(number of samples). Parameters for random seed or generator state are not supported. For more details, see the scipy documentation .
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=SPECIAL_ORTHO_GROUP(dim, [size])
dim
(int, required): Dimension of the square matrix (must be >= 2).size
(int, optional): Number of matrices to generate (must be >= 1). If omitted, returns a single matrix.
The function returns a 2D array of float values representing the orthogonal matrix (or matrices). If multiple matrices are requested, the output is a single 2D array with all matrices stacked vertically. If the input is invalid, an error message (string) is returned.
Examples
Example 1: Basic 3x3 Matrix
Inputs:
dim |
---|
3 |
Excel formula:
=SPECIAL_ORTHO_GROUP(3)
Expected output (rounded to 3 decimals):
Matrix |
---|
0.123 |
-0.456 |
0.789 |
… |
Example 2: Multiple 3x3 Matrices
Inputs:
dim | size |
---|---|
3 | 2 |
Excel formula:
=SPECIAL_ORTHO_GROUP(3, 2)
Expected output (rounded to 3 decimals):
Matrix |
---|
0.234 |
-0.567 |
0.890 |
… |
0.345 |
-0.678 |
0.901 |
… |
Example 3: Basic 4x4 Matrix
Inputs:
dim |
---|
4 |
Excel formula:
=SPECIAL_ORTHO_GROUP(4)
Expected output (rounded to 3 decimals):
Matrix |
---|
0.111 |
-0.222 |
0.333 |
0.444 |
… |
Example 4: Multiple 4x4 Matrices
Inputs:
dim | size |
---|---|
4 | 3 |
Excel formula:
=SPECIAL_ORTHO_GROUP(4, 3)
Expected output (rounded to 3 decimals):
Matrix |
---|
0.555 |
-0.666 |
0.777 |
0.888 |
… |
0.999 |
-0.111 |
0.222 |
0.333 |
… |
0.444 |
-0.555 |
0.666 |
0.777 |
… |
Python Code
from scipy.stats import special_ortho_group as scipy_special_ortho_group
from typing import List, Optional, Union
def special_ortho_group(dim: int, size: Optional[int] = None) -> Union[List[List[float]], str]:
"""
Draws random samples from the special orthogonal group SO(N), returning orthogonal matrices with determinant +1.
Args:
dim: Dimension of the matrices (int).
size: Number of samples to draw (int, optional). If None, returns a single matrix.
Returns:
2D list of float values representing the orthogonal matrix (or matrices), or an error message (str) if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate dim
if not isinstance(dim, int) or dim < 2:
return "Invalid input: dim must be an integer >= 2."
if size is not None:
if not isinstance(size, int) or size < 1:
return "Invalid input: size must be an integer >= 1 or None."
try:
# Draw samples, only pass size if not None
if size is None:
result = scipy_special_ortho_group.rvs(dim)
else:
result = scipy_special_ortho_group.rvs(dim, size=size)
except Exception as e:
return f"scipy.special_ortho_group error: {e}"
# Convert numpy arrays to 2D lists
import numpy as np
def to_2d_list(arr):
arr = np.asarray(arr)
if arr.ndim == 2:
return arr.tolist()
elif arr.ndim == 3:
# Multiple matrices: flatten into a single 2D list
return [row for mat in arr for row in mat.tolist()]
else:
return "Invalid output shape from scipy.special_ortho_group."
out = to_2d_list(result)
# Check for invalid values
def valid_matrix(matrix):
for row in matrix:
for val in row:
if not isinstance(val, (int, float)):
return False
if val is None or val != val or val in (float('inf'), float('-inf')):
return False
return True
if isinstance(out, list) and all(isinstance(row, list) for row in out):
if not valid_matrix(out):
return "Invalid output: matrix contains non-numeric or special values."
return out
return out