Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
maxval = numpy.max(numpy.abs(matrix_plus_transpose))
if not numpy.isclose(maxval, 0.0):
raise ValueError('The input matrix must be antisymmetric.')
# Compute Schur decomposition
canonical, orthogonal = schur(antisymmetric_matrix, output='real')
# The returned form is block diagonal; we need to permute rows and columns
# to put it into the form we want
n = p // 2
for i in range(1, n, 2):
swap_rows(canonical, i, n + i - 1)
swap_columns(canonical, i, n + i - 1)
swap_columns(orthogonal, i, n + i - 1)
if n % 2 != 0:
swap_rows(canonical, n - 1, n + i)
swap_columns(canonical, n - 1, n + i)
swap_columns(orthogonal, n - 1, n + i)
# Now we permute so that the upper right block is non-negative
for i in range(n):
if canonical[i, n + i] < 0.0:
swap_rows(canonical, i, n + i)
swap_columns(canonical, i, n + i)
swap_columns(orthogonal, i, n + i)
# Now we permute so that the nonzero entries are ordered by magnitude
# We use insertion sort
diagonal = canonical[range(n), range(n, 2 * n)]
for i in range(n):
# Insert the smallest element from the unsorted part of the list into
# index i
# The returned form is block diagonal; we need to permute rows and columns
# to put it into the form we want
n = p // 2
for i in range(1, n, 2):
swap_rows(canonical, i, n + i - 1)
swap_columns(canonical, i, n + i - 1)
swap_columns(orthogonal, i, n + i - 1)
if n % 2 != 0:
swap_rows(canonical, n - 1, n + i)
swap_columns(canonical, n - 1, n + i)
swap_columns(orthogonal, n - 1, n + i)
# Now we permute so that the upper right block is non-negative
for i in range(n):
if canonical[i, n + i] < 0.0:
swap_rows(canonical, i, n + i)
swap_columns(canonical, i, n + i)
swap_columns(orthogonal, i, n + i)
# Now we permute so that the nonzero entries are ordered by magnitude
# We use insertion sort
diagonal = canonical[range(n), range(n, 2 * n)]
for i in range(n):
# Insert the smallest element from the unsorted part of the list into
# index i
arg_min = numpy.argmin(diagonal[i:]) + i
if arg_min != i:
# Permute the upper right block
swap_rows(canonical, i, arg_min)
swap_columns(canonical, n + i, n + arg_min)
swap_columns(orthogonal, n + i, n + arg_min)
# Permute the lower left block
diagonal = canonical[range(n), range(n, 2 * n)]
for i in range(n):
# Insert the smallest element from the unsorted part of the list into
# index i
arg_min = numpy.argmin(diagonal[i:]) + i
if arg_min != i:
# Permute the upper right block
swap_rows(canonical, i, arg_min)
swap_columns(canonical, n + i, n + arg_min)
swap_columns(orthogonal, n + i, n + arg_min)
# Permute the lower left block
swap_rows(canonical, n + i, n + arg_min)
swap_columns(canonical, i, arg_min)
swap_columns(orthogonal, i, arg_min)
# Update diagonal
swap_rows(diagonal, i, arg_min)
return canonical, orthogonal.T
for i in range(n):
if canonical[i, n + i] < 0.0:
swap_rows(canonical, i, n + i)
swap_columns(canonical, i, n + i)
swap_columns(orthogonal, i, n + i)
# Now we permute so that the nonzero entries are ordered by magnitude
# We use insertion sort
diagonal = canonical[range(n), range(n, 2 * n)]
for i in range(n):
# Insert the smallest element from the unsorted part of the list into
# index i
arg_min = numpy.argmin(diagonal[i:]) + i
if arg_min != i:
# Permute the upper right block
swap_rows(canonical, i, arg_min)
swap_columns(canonical, n + i, n + arg_min)
swap_columns(orthogonal, n + i, n + arg_min)
# Permute the lower left block
swap_rows(canonical, n + i, n + arg_min)
swap_columns(canonical, i, arg_min)
swap_columns(orthogonal, i, arg_min)
# Update diagonal
swap_rows(diagonal, i, arg_min)
return canonical, orthogonal.T