Ejemplo n.º 1
0
def test_parse_complex():
    assert parse_complex('0') == 0
    assert parse_complex('1') == 1
    assert parse_complex('i') == 1j
    assert parse_complex('2i') == 2j
    assert parse_complex('-i') == -1j
    assert parse_complex('+i') == 1j
    assert parse_complex('1 + i - i') == 1
    assert parse_complex('1 + 2i - 3 i') == 1 - 1j
    np.testing.assert_allclose(parse_complex('exp 2'), np.e**2, atol=1e-8)
Ejemplo n.º 2
0
def test_parse_complex_raw_cases_from_quirk():
    assert parse_complex("0") == 0
    assert parse_complex("1") == 1
    assert parse_complex("-1") == -1
    assert parse_complex("i") == 1j
    assert parse_complex("-i") == -1j
    assert parse_complex("2") == 2
    assert parse_complex("2i") == 2j
    assert parse_complex("-2i") == -2j

    assert parse_complex("3-2i") == 3 - 2j
    assert parse_complex("1-i") == 1 - 1j
    assert parse_complex("1+i") == 1 + 1j
    assert parse_complex("-5+2i") == -5 + 2j
    assert parse_complex("-5-2i") == -5 - 2j

    assert parse_complex("3/2i") == 1.5j

    assert parse_complex("\u221A2-\u2153i") == np.sqrt(2) - 1j / 3

    assert parse_complex("1e-10") == 0.0000000001
    assert parse_complex("1e+10") == 10000000000
    assert parse_complex("2.5e-10") == 0.00000000025
    assert parse_complex("2.5E-10") == 0.00000000025
    assert parse_complex("2.5e+10") == 25000000000
Ejemplo n.º 3
0
def test_parse_complex_raw_cases_from_quirk():
    assert parse_complex("0") == 0
    assert parse_complex("1") == 1
    assert parse_complex("-1") == -1
    assert parse_complex("i") == 1j
    assert parse_complex("-i") == -1j
    assert parse_complex("2") == 2
    assert parse_complex("2i") == 2j
    assert parse_complex("-2i") == -2j

    assert parse_complex("3-2i") == 3 - 2j
    assert parse_complex("1-i") == 1 - 1j
    assert parse_complex("1+i") == 1 + 1j
    assert parse_complex("-5+2i") == -5 + 2j
    assert parse_complex("-5-2i") == -5 - 2j

    assert parse_complex("3/2i") == 1.5j

    assert parse_complex("\u221A2-\u2153i") == np.sqrt(2) - 1j / 3

    assert parse_complex("1e-10") == 0.0000000001
    assert parse_complex("1e+10") == 10000000000
    assert parse_complex("2.5e-10") == 0.00000000025
    assert parse_complex("2.5E-10") == 0.00000000025
    assert parse_complex("2.5e+10") == 25000000000
    assert parse_complex("2.e+10") == 20000000000
    np.testing.assert_allclose(parse_complex("e"), np.e)
    np.testing.assert_allclose(parse_complex("pi e"), np.pi * np.e)
    np.testing.assert_allclose(parse_complex("pi e 2"), np.pi * np.e * 2)
    np.testing.assert_allclose(parse_complex("2       pi"), 2 * np.pi)
Ejemplo n.º 4
0
def test_parse_complex_expression_cases_from_quirk():
    assert parse_complex("1/3") == 1 / 3
    assert parse_complex("2/3/5") == (2 / 3) / 5
    assert parse_complex("2/3/5*7/13") == ((((2 / 3) / 5)) * 7) / 13
    assert parse_complex("2-3-5") == -6
    assert parse_complex("1/3+2i") == 1 / 3 + 2j
    assert parse_complex("(1/3)+2i") == 1 / 3 + 2j
    np.testing.assert_allclose(parse_complex("1/(3+2i)"),
                               1 / (3 + 2j),
                               atol=1e-8)
    np.testing.assert_allclose(parse_complex("1/sqrt(3+2i)"),
                               1 / ((3 + 2j)**0.5),
                               atol=1e-8)

    np.testing.assert_allclose(parse_complex("i^i"),
                               0.20787957635076193,
                               atol=1e-8)
    assert parse_complex("√i") == np.sqrt(0.5) + 1j * np.sqrt(0.5)
    assert parse_complex("√4i") == 2j
    assert parse_complex("sqrt4i") == 2j
    # TODO(craiggidney): support nested implicit function application.
    # assert parse_complex("sqrt√4i") == np.sqrt(2)*1j
    # assert parse_complex("sqrt√4-i") ==  np.sqrt(2) - 1j
    assert parse_complex("----------1") == 1
    assert parse_complex("---------1") == -1
    # TODO(craiggidney): support nested unary operators.
    # assert parse_complex("---+--+--1") == -1
    # assert parse_complex("0---+--+--1") == -1
    # assert parse_complex("0---+--+--1*") == -1
    # TODO(craiggidney): support implicit identity binary operator arguments.
    # assert parse_complex("2+3^") == 5
    np.testing.assert_allclose(parse_complex("cos(pi/4) + i sin(pi/4)"),
                               np.sqrt(0.5) + 1j * np.sqrt(0.5),
                               atol=1e-8)
    np.testing.assert_allclose(parse_complex("cos(pi) + i (sin pi)"),
                               -1,
                               atol=1e-8)
    np.testing.assert_allclose(parse_complex("e^(pi i)"), -1, atol=1e-8)
    np.testing.assert_allclose(parse_complex("exp(ln(2))"), 2, atol=1e-8)
    np.testing.assert_allclose(parse_complex("sin(arcsin(0.5))"),
                               0.5,
                               atol=1e-8)
    np.testing.assert_allclose(parse_complex("cos(arccos(0.5))"),
                               0.5,
                               atol=1e-8)
    np.testing.assert_allclose(parse_complex("sin(asin(0.5))"), 0.5, atol=1e-8)
    np.testing.assert_allclose(parse_complex("cos(acos(0.5))"), 0.5, atol=1e-8)
Ejemplo n.º 5
0
def test_parse_complex_expression_cases_from_quirk():
    np.testing.assert_allclose(parse_complex("1/3"), 1 / 3)
    np.testing.assert_allclose(parse_complex("2/3/5"), (2 / 3) / 5)
    np.testing.assert_allclose(parse_complex("2/3/5*7/13"),
                               ((((2 / 3) / 5)) * 7) / 13)
    np.testing.assert_allclose(parse_complex("2-3-5"), -6)
    np.testing.assert_allclose(parse_complex("1/3+2i"), 1 / 3 + 2j)
    np.testing.assert_allclose(parse_complex("(1/3)+2i"), 1 / 3 + 2j)
    np.testing.assert_allclose(parse_complex("1/(3+2i)"),
                               1 / (3 + 2j),
                               atol=1e-8)
    np.testing.assert_allclose(parse_complex("1/sqrt(3+2i)"),
                               1 / ((3 + 2j)**0.5),
                               atol=1e-8)

    np.testing.assert_allclose(parse_complex("i^i"),
                               0.20787957635076193,
                               atol=1e-8)
    np.testing.assert_allclose(parse_complex("√i"), np.sqrt(1j))
    np.testing.assert_allclose(parse_complex("√4i"), 2j)
    np.testing.assert_allclose(parse_complex("sqrt4i"), 2j)
    np.testing.assert_allclose(parse_complex("sqrt√4i"), np.sqrt(2) * 1j)
    np.testing.assert_allclose(parse_complex("sqrt√4-i"), np.sqrt(2) - 1j)
    np.testing.assert_allclose(parse_complex("----------1"), 1)
    np.testing.assert_allclose(parse_complex("---------1"), -1)
    np.testing.assert_allclose(parse_complex("---+--+--1"), -1)
    np.testing.assert_allclose(parse_complex("0---+--+--1"), -1)
    np.testing.assert_allclose(parse_complex("0---+--+--1*"), -1)
    np.testing.assert_allclose(parse_complex("2+3^"), 5)
    np.testing.assert_allclose(parse_complex("cos(45) + i sin(45)"),
                               np.sqrt(0.5) + 1j * np.sqrt(0.5),
                               atol=1e-8)
    np.testing.assert_allclose(parse_complex("cos(45) + i (sin 45)"),
                               np.sqrt(0.5) + 1j * np.sqrt(0.5),
                               atol=1e-8)
    np.testing.assert_allclose(parse_complex("e^(pi i)"), -1, atol=1e-8)
    np.testing.assert_allclose(parse_complex("exp(ln(2))"), 2, atol=1e-8)
    np.testing.assert_allclose(parse_complex("sin(arcsin(0.5))"),
                               0.5,
                               atol=1e-8)
    np.testing.assert_allclose(parse_complex("cos(arccos(0.5))"),
                               0.5,
                               atol=1e-8)
    np.testing.assert_allclose(parse_complex("sin(asin(0.5))"), 0.5, atol=1e-8)
    np.testing.assert_allclose(parse_complex("cos(acos(0.5))"), 0.5, atol=1e-8)