def test_qr_raises():
    np.random.seed(10)
    dtype = np.float64
    num_charges = 1
    Ds = [20, 21]
    R1 = 1
    R = len(Ds)
    charges = [
        BaseCharge(np.random.randint(-5, 6, (num_charges, Ds[n])),
                   charge_types=[U1Charge] * num_charges) for n in range(R)
    ]
    flows = [True] * R
    A = BlockSparseTensor.random(
        [Index(charges[n], flows[n]) for n in range(R)], dtype=dtype)
    d1 = np.prod(Ds[:R1])
    d2 = np.prod(Ds[R1:])
    A = A.reshape([d1, d2])
    with pytest.raises(ValueError):
        qr(A, mode='fake_mode')
def test_qr_prod(dtype, Ds, R1, mode, num_charges):
    np.random.seed(10)
    R = len(Ds)
    charges = [
        BaseCharge(np.random.randint(-5, 6, (num_charges, Ds[n])),
                   charge_types=[U1Charge] * num_charges) for n in range(R)
    ]
    flows = [True] * R
    A = BlockSparseTensor.random(
        [Index(charges[n], flows[n]) for n in range(R)], dtype=dtype)
    d1 = np.prod(Ds[:R1])
    d2 = np.prod(Ds[R1:])
    A = A.reshape([d1, d2])
    Q, R = qr(A, mode=mode)
    A_ = Q @ R
    assert A_.dtype == A.dtype
    np.testing.assert_allclose(A.data, A_.data)
    for n in range(len(A._charges)):
        assert charge_equal(A_._charges[n], A._charges[n])
def test_qr_r_mode():
    Ds = [10, 11]
    dtype = np.float64
    np.random.seed(10)
    rank = len(Ds)
    charges = [
        BaseCharge(np.zeros((Ds[n], 1)), charge_types=[U1Charge] * 1)
        for n in range(rank)
    ]
    flows = [True] * rank
    A = BlockSparseTensor.random(
        [Index(charges[n], flows[n]) for n in range(rank)], dtype=dtype)
    d1 = np.prod(Ds[:1])
    d2 = np.prod(Ds[1:])
    A = A.reshape([d1, d2])
    R = qr(A, mode='r')
    R_np = np.linalg.qr(A.todense(), mode='r')
    np.testing.assert_allclose(np.abs(np.diag(R.todense())),
                               np.abs(np.diag(R_np)))
def test_qr_raises():
    np.random.seed(10)
    dtype = np.float64
    num_charges = 1
    Ds = [20, 21, 22]
    R1 = 2
    R = len(Ds)
    charges = [
        BaseCharge(np.random.randint(-5, 6, (Ds[n], num_charges)),
                   charge_types=[U1Charge] * num_charges) for n in range(R)
    ]
    flows = [True] * R
    A = BlockSparseTensor.random(
        [Index(charges[n], flows[n]) for n in range(R)], dtype=dtype)
    d1 = np.prod(Ds[:R1])
    d2 = np.prod(Ds[R1:])
    B = A.reshape([d1, d2])
    with pytest.raises(ValueError, match='unknown value'):
        qr(B, mode='fake_mode')
    with pytest.raises(NotImplementedError, match="mode `raw`"):
        qr(B, mode='raw')
    with pytest.raises(NotImplementedError, match="qr currently"):
        qr(A)