# 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
# -*- coding: ISO-8859-1 -*-
#-----------------------------------------------------------------------------
# Copyright (c) 2014, HFTools Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------



def cascadeS(S1, S2, *rest):
    """Cascade arrays containing S-parameters.
    """
    S1, S2 = make_same_dims(S1, S2)
    neworder = tuple([x for x in S1.dims if not isinstance(x, _DimMatrix)])
    S1 = S1.reorder_dimensions(*neworder)
    S2 = S2.reorder_dimensions(*neworder)
    denom = (1 - S1[..., 1, 1] * S2[..., 0, 0])
    s1det = det(S1)

# 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

# -*- coding: ISO-8859-1 -*-
#-----------------------------------------------------------------------------
# Copyright (c) 2014, HFTools Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------



def cascadeS(S1, S2, *rest):
    """Cascade arrays containing S-parameters.
    """
    S1, S2 = make_same_dims(S1, S2)
    neworder = tuple([x for x in S1.dims if not isinstance(x, _DimMatrix)])
    S1 = S1.reorder_dimensions(*neworder)
    S2 = S2.reorder_dimensions(*neworder)
    denom = (1 - S1[..., 1, 1] * S2[..., 0, 0])
    s1det = det(S1)

    s2det = det(S2)