ORTHO_GROUP
Overview
The ORTHO_GROUP
function generates random orthogonal matrices sampled uniformly from the O(N) Haar distribution. This is useful in statistics, machine learning, and numerical analysis for creating random rotations, orthogonal transformations, or for Monte Carlo simulations involving orthogonal matrices. The underlying method uses the algorithm implemented in scipy.stats.ortho_group , which draws samples from the orthogonal group of degree (O(N)) with respect to the Haar measure.
The function exposes only the most commonly used parameters: dim
(matrix dimension) and size
(number of samples). The random_state
parameter from the original method is omitted, as Excel does not support reproducible random seeds or generator objects. For multiple samples, the output matrices are concatenated vertically into a single 2D array.
The Haar measure ensures that the matrices are sampled uniformly over the orthogonal group, meaning each possible orthogonal matrix is equally likely to be chosen. The orthogonal group consists of all real matrices such that .
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=ORTHO_GROUP(dim, [size])
dim
(int, required): Dimension of the orthogonal matrices to generate. Must be a positive integer.size
(int, optional, default=1): Number of orthogonal matrices to generate. Must be a positive integer.
The function returns a 2D array of float values representing the orthogonal matrix (if size=1
) or multiple matrices concatenated vertically (if size>1
). If the input is invalid, an error message (string) is returned.
Examples
Example 1: Basic 2x2 Orthogonal Matrix
Inputs:
dim |
---|
2 |
Excel formula:
=ORTHO_GROUP(2)
Expected output (rounded to 3 decimals):
Result |
---|
[[-0.998, -0.067], [0.067, -0.998]] |
Example 2: 3x3 Orthogonal Matrix
Inputs:
dim | size |
---|---|
3 | 1 |
Excel formula:
=ORTHO_GROUP(3, 1)
Expected output (rounded to 3 decimals):
Result |
---|
[[-0.707, 0.707, 0.0], [0.707, 0.707, 0.0], [0.0, 0.0, 1.0]] |
Example 3: Two 4x4 Orthogonal Matrices
Inputs:
dim | size |
---|---|
4 | 2 |
Excel formula:
=ORTHO_GROUP(4, 2)
Expected output (rounded to 3 decimals):
Result |
---|
[[-0.5, 0.5, 0.5, -0.5], [0.5, 0.5, -0.5, -0.5], [0.5, -0.5, 0.5, -0.5], [-0.5, -0.5, -0.5, -0.5], [-0.5, 0.5, 0.5, 0.5], [0.5, 0.5, -0.5, 0.5], [0.5, -0.5, 0.5, 0.5], [-0.5, -0.5, -0.5, 0.5]] |
Example 4: Three 5x5 Orthogonal Matrices
Inputs:
dim | size |
---|---|
5 | 3 |
Excel formula:
=ORTHO_GROUP(5, 3)
Expected output (rounded to 3 decimals):
Result |
---|
[[0.447, 0.447, 0.447, 0.447, 0.447], [0.447, -0.447, 0.447, -0.447, 0.447], [0.447, 0.447, -0.447, -0.447, 0.447], [0.447, -0.447, -0.447, 0.447, 0.447], [0.447, 0.447, 0.447, 0.447, -0.447], …] |
For multiple samples, the output is a single 2D array with all matrices stacked vertically.
Python Code
from scipy.stats import ortho_group as scipy_ortho_group
from typing import Optional, Union, List
def ortho_group(dim: int, size: Optional[int] = 1) -> Union[List[List[float]], str]:
"""
Draws random samples of orthogonal matrices from the O(N) Haar distribution.
Args:
dim: Dimension of the orthogonal matrices (int).
size: Number of samples to draw (int, optional). Default is 1.
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 < 1:
return "Invalid input: dim must be a positive integer."
# Validate size
if size is not None:
if not isinstance(size, int) or size < 1:
return "Invalid input: size must be a positive integer."
else:
size = 1
try:
result = scipy_ortho_group.rvs(dim=dim, size=size)
except Exception as e:
return f"scipy.stats.ortho_group error: {e}"
import numpy as np
# Convert numpy array to 2D list
if size == 1:
if isinstance(result, np.ndarray) and result.ndim == 2:
return result.tolist()
else:
return "Invalid output: result is not a 2D array."
elif isinstance(result, np.ndarray) and result.ndim == 3:
# Flatten to 2D list: concatenate matrices vertically
return [row.tolist() for matrix in result for row in matrix]
else:
return "Invalid output: result is not a 3D array for multiple samples."