# Path: hftools/dataset/dim.py
# class _DimMatrix(DiagAxis):
#     sortprio = 1000
# 
# Path: hftools/dataset/arrayobj.py
# class hfarray(_hfarray):
#     def __new__(subtype, data, dims=None, dtype=None, copy=True, order=None,
#                 subok=False, ndmin=0, unit=None, outputformat=None, info=None):
#         if hasattr(data, "__hfarray__"):
#             data, dims = data.__hfarray__()
#             if unit is None and len(dims) == 1:
#                 unit = dims[0].unit
#             if outputformat is None and len(dims) == 1:
#                 outputformat = dims[0].outputformat
#         return _hfarray.__new__(subtype, data, dims=dims, dtype=dtype,
#                                 copy=copy, order=order, subok=subok,
#                                 ndmin=ndmin, unit=unit,
#                                 outputformat=outputformat, info=info)
# 
# def make_same_dims(A, B):
#     u"""Anropas med lista med *Arrays*. Returnerar arrayer som har samma *dims*
#     dvs vi har anropat change_shape med en *newinfo* som innehaller unionen
#     av de Axis objekt som finns i *dims* av *Arrayerna*.
# 
#     """
#     if not isinstance(B, _hfarray):
#         B = A.__class__(B.view(), dims=A.dims, copy=False)
#     return make_same_dims_list((A, B))
# 
# Path: hftools/math.py
# def det(A):
#     det = linalg.det
#     result = det(A)
#     result = hfarray(result, dims=A.dims[:-2])
#     return result
# 
# def inv(A):
#     inv = linalg.inv
#     result = inv(A)
#     result = hfarray(result, dims=A.dims)
#     return result
# 
# def matrix_multiply(a, b):
#     """Multiply arrays of matrices.
# 
#     a and b are hfarrays containing dimensions DimMatrix_i and DimMatrix_j.
#     Matrix multiplication is done by broadcasting the other dimensions first.
#     """
#     A, B = make_same_dims(a, b)
#     res = np.einsum("...ij,...jk->...ik", A, B)
#     return hfarray(res, dims=A.dims)


import numpy as np
from hftools.dataset import make_same_dims, _DimMatrix, hfarray
from hftools.math import det, inv, matrix_multiply
    The S-parameters of S are scaled such that the highest eigenvalue of S'
    becomes |lambda|max < 1 - delta.
    """
    eig = np.linalg.eigvals
    out = []
    for s in S:
        lambda_max = max(abs(eig(s)))
        if lambda_max > 1:
            out.append(s / lambda_max * (1 - delta))
        else:
            out.append(s)
    return S.__class__(out, dims=S.dims)


def make_reciprocal(S):
    s12 = S[..., 0, 1]
    s21 = S[..., 1, 0]
    S12 = np.sqrt(s12 * s21)
    A12 = np.where(np.angle(S12 / s12) < 1, S12, -S12)
    S[..., 0, 1] = A12
    S[..., 1, 0] = A12
    return S


def check_passive(S):
    eig = np.linalg.eigvals
    out = []
    for s in S:
        lambda_max = max(abs(eig(s)))
        out.append(lambda_max)

# Path: hftools/dataset/dim.py
# class _DimMatrix(DiagAxis):
#     sortprio = 1000
# 
# Path: hftools/dataset/arrayobj.py
# class hfarray(_hfarray):
#     def __new__(subtype, data, dims=None, dtype=None, copy=True, order=None,
#                 subok=False, ndmin=0, unit=None, outputformat=None, info=None):
#         if hasattr(data, "__hfarray__"):
#             data, dims = data.__hfarray__()
#             if unit is None and len(dims) == 1:
#                 unit = dims[0].unit
#             if outputformat is None and len(dims) == 1:
#                 outputformat = dims[0].outputformat
#         return _hfarray.__new__(subtype, data, dims=dims, dtype=dtype,
#                                 copy=copy, order=order, subok=subok,
#                                 ndmin=ndmin, unit=unit,
#                                 outputformat=outputformat, info=info)
# 
# def make_same_dims(A, B):
#     u"""Anropas med lista med *Arrays*. Returnerar arrayer som har samma *dims*
#     dvs vi har anropat change_shape med en *newinfo* som innehaller unionen
#     av de Axis objekt som finns i *dims* av *Arrayerna*.
# 
#     """
#     if not isinstance(B, _hfarray):
#         B = A.__class__(B.view(), dims=A.dims, copy=False)
#     return make_same_dims_list((A, B))
# 
# Path: hftools/math.py
# def det(A):
#     det = linalg.det
#     result = det(A)
#     result = hfarray(result, dims=A.dims[:-2])
#     return result
# 
# def inv(A):
#     inv = linalg.inv
#     result = inv(A)
#     result = hfarray(result, dims=A.dims)
#     return result
# 
# def matrix_multiply(a, b):
#     """Multiply arrays of matrices.
# 
#     a and b are hfarrays containing dimensions DimMatrix_i and DimMatrix_j.
#     Matrix multiplication is done by broadcasting the other dimensions first.
#     """
#     A, B = make_same_dims(a, b)
#     res = np.einsum("...ij,...jk->...ik", A, B)
#     return hfarray(res, dims=A.dims)


# Path: hftools/networks/spar_functions.py
import numpy as np
from hftools.dataset import make_same_dims, _DimMatrix, hfarray
from hftools.math import det, inv, matrix_multiply

    The S-parameters of S are scaled such that the highest eigenvalue of S'
    becomes |lambda|max < 1 - delta.
    """
    eig = np.linalg.eigvals
    out = []
    for s in S:
        lambda_max = max(abs(eig(s)))
        if lambda_max > 1:
            out.append(s / lambda_max * (1 - delta))
        else:
            out.append(s)
    return S.__class__(out, dims=S.dims)


def make_reciprocal(S):
    s12 = S[..., 0, 1]
    s21 = S[..., 1, 0]
    S12 = np.sqrt(s12 * s21)
    A12 = np.where(np.angle(S12 / s12) < 1, S12, -S12)
    S[..., 0, 1] = A12
    S[..., 1, 0] = A12
    return S


def check_passive(S):
    eig = np.linalg.eigvals
    out = []
    for s in S:
        lambda_max = max(abs(eig(s)))
        out.append(lambda_max)

    return hfarray(out, dims=S.dims[:1])