Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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
swap_rows(canonical, n + i, n + arg_min)
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
arg_min = numpy.argmin(diagonal[i:]) + i
if arg_min != i:
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
arg_min = numpy.argmin(diagonal[i:]) + i
# Check that input matrix is antisymmetric
matrix_plus_transpose = antisymmetric_matrix + antisymmetric_matrix.T
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):
# Check that input matrix is antisymmetric
matrix_plus_transpose = antisymmetric_matrix + antisymmetric_matrix.T
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)]
# 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
# 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