POLAR
The polar decomposition factors a matrix A into the product of an orthonormal matrix U and a positive semidefinite Hermitian matrix P.
Excel Usage
=POLAR(matrix, polar_side, polar_ret_type)
matrix(list[list], required): 2D array of numeric values to decompose.polar_side(str, optional, default: “right”): Determines whether a right (A=UP) or left (A=PU) polar decomposition is computed.polar_ret_type(str, optional, default: “u”): The component of the decomposition to return.
Returns (list[list]): 2D array representing the requested component (U or P).
Example 1: Unitary matrix U of 2x2
Inputs:
| matrix | polar_ret_type | |
|---|---|---|
| 1 | -1 | u |
| 2 | 4 |
Excel formula:
=POLAR({1,-1;2,4}, "u")
Expected output:
| Result | |
|---|---|
| 0.857493 | -0.514496 |
| 0.514496 | 0.857493 |
Example 2: Positive semidefinite matrix P of 2x2
Inputs:
| matrix | polar_ret_type | |
|---|---|---|
| 1 | -1 | p |
| 2 | 4 |
Excel formula:
=POLAR({1,-1;2,4}, "p")
Expected output:
| Result | |
|---|---|
| 1.88648 | 1.20049 |
| 1.20049 | 3.94447 |
Python Code
import numpy as np
from scipy.linalg import polar as scipy_polar
def polar(matrix, polar_side='right', polar_ret_type='u'):
"""
Compute the polar decomposition of a matrix.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.polar.html
This example function is provided as-is without any representation of accuracy.
Args:
matrix (list[list]): 2D array of numeric values to decompose.
polar_side (str, optional): Determines whether a right (A=UP) or left (A=PU) polar decomposition is computed. Valid options: Right (A=UP), Left (A=PU). Default is 'right'.
polar_ret_type (str, optional): The component of the decomposition to return. Valid options: Unitary/Orthonormal (U), Positive Semidefinite (P). Default is 'u'.
Returns:
list[list]: 2D array representing the requested component (U or P).
"""
try:
def to2d(x):
return [[x]] if not isinstance(x, list) else x
matrix = to2d(matrix)
if not isinstance(matrix, list) or not matrix or not all(isinstance(row, list) for row in matrix):
return "Error: matrix must be a non-empty 2D list"
try:
a = np.array(matrix, dtype=float)
except (ValueError, TypeError):
return "Error: matrix must contain numeric values"
if not np.all(np.isfinite(a)):
return "Error: matrix must contain only finite numbers"
try:
u_matrix, p_matrix = scipy_polar(a, side=polar_side.lower())
except Exception as e:
return f"Error: {str(e)}"
if polar_ret_type.lower() == "u":
return u_matrix.tolist()
else:
return p_matrix.tolist()
except Exception as e:
return f"Error: {str(e)}"Online Calculator
2D array of numeric values to decompose.
Determines whether a right (A=UP) or left (A=PU) polar decomposition is computed.
The component of the decomposition to return.