def setUp(self): self.d = DataBlock() self.d.P = VA([0.5, -.3, .5]) Sdims = (DimMatrix_i("i", 2), DimMatrix_j("j", 2)) self.d.S = VA([[11, 12], [21, 22]], dims=Sdims) Wdims = (DimSweep("g", 1), DimMatrix_i("i", 2), DimMatrix_j("j", 2)) self.d.W = VA([[[11, 12], [21, 22]]], dims=Wdims)
def setUp(self): Test_DataBlock.setUp(self) self.a.P = hfarray([0.5, -.3, .5]) Sdims = (DimMatrix_i("i", 2), DimMatrix_j("j", 2)) self.a.S = hfarray([[11, 12], [21, 22]], dims=Sdims) Wdims = (DimSweep("g", 1), DimMatrix_i("i", 2), DimMatrix_j("j", 2)) self.a.W = hfarray([[[11, 12], [21, 22]]], dims=Wdims) self.a.T = hfarray(["Text"], dims=(DimSweep("text_i", ["index0"]), ))
def setUp(self): self.I = I = DimSweep("I", [1, 2]) self.J = J = DimSweep("J", [10, 20, 30]) self.K = K = DimSweep("K", [100, 200, 300, 400]) self.mi = mi = DimMatrix_i("i", [0, 1]) self.mj = mj = DimMatrix_j("j", [0, 1]) self.i, self.j, self.k = (i, j, k) = list(map(hfarray, (I, J, K))) self.a = i * j * k self.b = hfarray(i * j * k, unit="Hz", outputformat="%.3f") cdims = (DimMatrix_i("i", 2), DimMatrix_j("j", 2)) self.c = hfarray([[11, 12], [21, 22]], dims=cdims) self.m = hfarray([[[1., 2], [3, 4]]] * 3, dims=(J, mi, mj)) self.m2 = hfarray([[1., 2], [3, 4]], dims=(mi, mj))
def random_complex_matrix(N, maxsize, minsize=1, Nmatrix=2, Nmatrixj=None): if Nmatrixj is None: # pragma: no cover Nmatrixj = Nmatrix dims = (random_dims(N, maxsize, minsize) + (DimMatrix_i("i", Nmatrix), DimMatrix_j("j", Nmatrixj))) return (random_value_array_from_dims(dims) + random_value_array_from_dims(dims) * 1j)
def setUp(self): self.db = db = DataBlock() Sdims = (DimRep("freq", [1, 2]), DimRep("r", [1]), DimMatrix_i("i", 2), DimMatrix_j("j", 2)) db.S = hfarray(np.array([[11, 12], [21, 22]])[None, None, :, :] * np.array([[10], [20]])[..., None, None], dims=Sdims) db.V = hfarray([1.23], Sdims[1:2]) db.Y = hfarray([1.23], (DimRep("k", [1]), ))
def test_make_matrix_1(self): a = hfarray([1, 10]) b = hfarray([2, 20]) c = hfarray([3, 30]) d = hfarray([4, 40]) i = DimMatrix_i("i", 2) j = DimMatrix_j("j", 2) fi = DimSweep("freq", [0, 1]) matrix = hfmath.make_matrix(a, b, c, d) self.assertTrue(isinstance(matrix, hfarray)) self.assertEqual(matrix.dims, (fi, i, j)) self.assertAllclose(matrix, [[[1, 2], [3, 4]], [[10, 20], [30, 40]]])
def make_matrix(a, b, c, d): a, b, c, d = make_same_dims_list([a, b, c, d]) abcdshape = zip(a.shape, b.shape, c.shape, d.shape) maxshape = (tuple(max(x) for x in abcdshape) + (2, 2)) res = zeros(maxshape, a.dtype) res[..., 0, 0] = a res[..., 0, 1] = b res[..., 1, 0] = c res[..., 1, 1] = d dims = a.dims + (DimMatrix_i("i", 2), DimMatrix_j("j", 2),) out = hfarray(res, dims=dims) return out
def test_make_col_from_matrix_2(self): header = ["S"] dims = ( DimSweep("f", 1), DimMatrix_i("i", 2), DimMatrix_j("j", 2), ) columns = [hfarray([[[11, 12], [21, 22]]], dims=dims)] res = common.make_col_from_matrix(header, columns, "%s%s%s", fortranorder=True) self.assertEqual(res, (["S11", "S21", "S12", "S22"], [11, 21, 12, 22]))
def Hconj(A): i_idx = A.dims_index("i") j_idx = A.dims_index("j") order = list(range(len(A.shape))) order[i_idx], order[j_idx] = order[j_idx], order[i_idx] out = A.transpose(*order) dims = list(out.dims) dims[i_idx] = DimMatrix_i(dims[i_idx], name="i") dims[j_idx] = DimMatrix_j(dims[j_idx], name="j") out.dims = dims out2 = out.conj() return out2
def test_make_col_from_matrix_1(self): header = ["S", "P"] dims = ( DimSweep("f", 1), DimMatrix_i("i", 2), DimMatrix_j("j", 2), ) columns = [ hfarray([[[11, 12], [21, 22]]], dims=dims), hfarray([10], dims=dims[:1]) ] res = common.make_col_from_matrix(header, columns, "%s%s%s") self.assertEqual( res, (["S11", "S12", "S21", "S22", "P"], [11, 12, 21, 22, 10]))
def test_1(self): db = DataBlock() Sdims = (DimRep("freq", [1, 2]), DimRep("r", [1]), DimMatrix_i("i", 2), DimMatrix_j("j", 2)) db.S = hfarray( np.array([[11, 12], [21, 22]])[np.newaxis, np.newaxis, :, :] * np.array([[10], [20]])[..., np.newaxis, np.newaxis], dims=Sdims) db.V = hfarray([1.23], Sdims[1:2]) db.Y = hfarray([1.23], (DimRep("k", [1]), )) out = dset.subset_datablock_by_dims( dset.convert_matrices_to_elements(db), Sdims[:-2]) self.assertTrue("V" in out) self.assertTrue("S11" in out) self.assertTrue("S12" in out) self.assertTrue("S21" in out) self.assertTrue("S22" in out) self.assertFalse("Y" in out)
def test_2(self): d = DataBlock() d.comments = Comments(["Vg=10"]) fi = DimSweep("freq", [0e9, 1e9, 2e9], outputformat="%15.2f") dims = ( fi, DimMatrix_i("i", 2), DimMatrix_j("j", 2), ) d.S = hfarray([[[1 + 1j, 1 + 2j], [2 + 1j, 2 + 2j]]] * 3, dims=dims, outputformat="%.3f") filename = testpath / "testdata/touchstone/savetest/res_2.txt" hftools.file_formats.touchstone.save_touchstone(d, filename) resfilename = testpath / "testdata/touchstone/savetest/res_2.txt" facitfilename = testpath / "testdata/touchstone/savetest/facit_2.txt" with open(resfilename) as resfil: with open(facitfilename) as facitfil: for idx, (rad1, rad2) in enumerate(zip(resfil, facitfil)): msg = ("\nFailed on line %d\n result: %r\n facit: %r" % (idx + 1, rad1, rad2)) self.assertEqual(rad1, rad2, msg=msg)
make_complex=make_complex, property_to_vars=property_to_vars, guess_unit=guess_unit, normalize=normalize, make_matrix=make_matrix, merge=merge, verbose=verbose) return res if __name__ == "__main__": a = read_touchstone("tests/testdata/touchstone/test4.s2p", make_complex=True, property_to_vars=True, guess_unit=True, normalize=True, make_matrix=True, merge=True, verbose=True) d = DataBlock() d.comments = Comments([]) fi = DimSweep("freq", [0e9, 1e9, 2e9]) d.S = hfarray([[[1 + 1j, 1 + 2j], [2 + 1j, 2 + 2j]]] * 3, dims=( fi, DimMatrix_i("i", 2), DimMatrix_j("j", 2), )) save_touchstone(d, "tests/testdata/touchstone/savetest/res_1.txt")
self.assertEqual(res.shape, (3, 2, 2)) self.assertEqual(res.dims, (self.J, self.mi, self.mj)) def test_nparray(self): self.assertRaises(Exception, hfmath.matrix_multiply, np.array(self.m), np.array(self.m)) class TestDet(TestInv): def test_1(self): res = hfmath.det(self.m) self.assertAllclose(res, -2) self.assertEqual(res.shape, (3, )) self.assertEqual(res.dims, (self.J, )) if __name__ == '__main__': I = DimSweep("I", [1, 2]) J = DimSweep("J", [10, 20, 30]) K = DimSweep("K", [100, 200, 300, 400]) mi = DimMatrix_i("i", [0, 1]) mj = DimMatrix_j("j", [0, 1]) (i, j, k) = map(hfarray, (I, J, K)) a = i * j * k b = hfarray(i * j * k, unit="Hz", outputformat="%.3f") cdims = (DimMatrix_i("i", 2), DimMatrix_j("j", 2)) c = hfarray([[11, 12], [21, 22]], dims=cdims) m = hfarray([[[1., 2], [3, 4]]] * 3, dims=(J, mi, mj)) m2 = hfarray([[1., 2], [3, 4]], dims=(mi, mj))