|
1 | 1 | import pytest |
| 2 | +from sympy import Float |
2 | 3 |
|
3 | | -from mathics.core.atoms import Complex, Integer0, Integer1, Real, String |
| 4 | +from mathics.core.atoms import ( |
| 5 | + Complex, |
| 6 | + Integer0, |
| 7 | + Integer1, |
| 8 | + PrecisionReal, |
| 9 | + Rational, |
| 10 | + Real, |
| 11 | + String, |
| 12 | +) |
| 13 | + |
| 14 | +print("creating representations") |
| 15 | +ZERO_REPRESENTATIONS = { |
| 16 | + "Integer": Integer0, |
| 17 | + "MachineReal": Real(0.0), |
| 18 | + "PrecisionReal`2": PrecisionReal(Float(0, 2)), |
| 19 | + "PrecisionReal`5": PrecisionReal(Float(0, 5)), |
| 20 | + "PrecisionReal`10": PrecisionReal(Float(0, 10)), |
| 21 | + "PrecisionReal`20": PrecisionReal(Float(0, 20)), |
| 22 | + "PrecisionReal`22": PrecisionReal(Float(0, 22)), |
| 23 | + "PrecisionReal`40": PrecisionReal(Float(0, 40)), |
| 24 | +} |
| 25 | +ZERO_REPRESENTATIONS["Complex"] = Complex( |
| 26 | + ZERO_REPRESENTATIONS["MachineReal"], ZERO_REPRESENTATIONS["MachineReal"] |
| 27 | +) |
| 28 | +ZERO_REPRESENTATIONS["Complex`20"] = Complex( |
| 29 | + ZERO_REPRESENTATIONS["PrecisionReal`20"], ZERO_REPRESENTATIONS["PrecisionReal`20"] |
| 30 | +) |
| 31 | + |
| 32 | +ONE_REPRESENTATIONS = { |
| 33 | + "Integer": Integer1, |
| 34 | + "MachineReal": Real(1.0), |
| 35 | + "PrecisionReal`2": PrecisionReal(Float(1, 2)), |
| 36 | + "PrecisionReal`5": PrecisionReal(Float(1, 5)), |
| 37 | + "PrecisionReal`10": PrecisionReal(Float(1, 10)), |
| 38 | + "PrecisionReal`20": PrecisionReal(Float(1, 20)), |
| 39 | + "PrecisionReal`22": PrecisionReal(Float(1, 22)), |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +# Add some complex cases |
| 44 | +ONE_REPRESENTATIONS["Complex Integer"] = Complex( |
| 45 | + Integer1, ZERO_REPRESENTATIONS["PrecisionReal`10"] |
| 46 | +) |
| 47 | +ONE_REPRESENTATIONS["Complex"] = Complex( |
| 48 | + ONE_REPRESENTATIONS["MachineReal"], ZERO_REPRESENTATIONS["MachineReal"] |
| 49 | +) |
| 50 | +ONE_REPRESENTATIONS["Complex`5"] = Complex( |
| 51 | + ONE_REPRESENTATIONS["PrecisionReal`5"], ZERO_REPRESENTATIONS["PrecisionReal`5"] |
| 52 | +) |
| 53 | + |
| 54 | + |
| 55 | +ONE_FIFTH_REPRESENTATIONS = { |
| 56 | + "Rational": Rational(1, 5), |
| 57 | + "MachineReal": Real(0.2), |
| 58 | + "PrecisionReal`20": PrecisionReal(Float(".2", 20)), |
| 59 | + "PrecisionReal`22": PrecisionReal(Float(".2", 22)), |
| 60 | +} |
| 61 | +ONE_FIFTH_REPRESENTATIONS["Complex"] = Complex( |
| 62 | + ONE_FIFTH_REPRESENTATIONS["MachineReal"], ZERO_REPRESENTATIONS["MachineReal"] |
| 63 | +) |
| 64 | +ONE_FIFTH_REPRESENTATIONS["Complex`20"] = Complex( |
| 65 | + ONE_FIFTH_REPRESENTATIONS["PrecisionReal`20"], |
| 66 | + ZERO_REPRESENTATIONS["PrecisionReal`20"], |
| 67 | +) |
| 68 | + |
| 69 | + |
| 70 | +def test_sorting_numbers(): |
| 71 | + """ |
| 72 | + In WMA, canonical order for numbers with the same value in different representations: |
| 73 | + * Integer |
| 74 | + * Complex[Integer, PrecisionReal] |
| 75 | + * MachineReal |
| 76 | + * Complex[MachineReal, MachineReal] |
| 77 | + * PrecisionReal, Complex[PrecisionReal, PrecisionReal] if precision of the real parts are equal, |
| 78 | + * otherwise, sort by precision of the real part. |
| 79 | + * Rational |
| 80 | + Example: {1, 1 + 0``10.*I, 1., 1. + 0.*I, 1.`4., 1.`4. + 0``4.*I, 1.`4. + 0``3.*I, 1.`6.} |
| 81 | + and |
| 82 | + {0.2, 0.2 + 0.*I, 0.2`4., 0.2`10., 1/5} |
| 83 | + are lists in canonical order. |
| 84 | +
|
| 85 | + If the numbers are in different representations, numbers are sorted by their real parts, |
| 86 | + and then the imaginary part is considered: |
| 87 | + {0.2, 0.2 - 1.*I, 0.2 + 1.*I, 1/5} |
| 88 | + """ |
| 89 | + zero_canonical_order = ( |
| 90 | + "Integer", |
| 91 | + "MachineReal", |
| 92 | + "Complex", |
| 93 | + "PrecisionReal`20", |
| 94 | + "Complex`20", |
| 95 | + "PrecisionReal`22", |
| 96 | + ) |
| 97 | + one_canonical_order = ( |
| 98 | + "Integer", |
| 99 | + "MachineReal", |
| 100 | + "Complex", |
| 101 | + "Complex Integer", |
| 102 | + "PrecisionReal`2", |
| 103 | + "PrecisionReal`5", |
| 104 | + "Complex`5", |
| 105 | + "PrecisionReal`20", |
| 106 | + ) |
| 107 | + one_fifth_canonical_order = ( |
| 108 | + "MachineReal", |
| 109 | + "Complex", |
| 110 | + "PrecisionReal`20", |
| 111 | + "Complex`20", |
| 112 | + "PrecisionReal`22", |
| 113 | + "Rational", |
| 114 | + ) |
| 115 | + |
| 116 | + # Canonical order |
| 117 | + for order_equiv_forms in [ |
| 118 | + [ZERO_REPRESENTATIONS[pos] for pos in zero_canonical_order], |
| 119 | + [ONE_REPRESENTATIONS[pos] for pos in one_canonical_order], |
| 120 | + [ONE_FIFTH_REPRESENTATIONS[pos] for pos in one_fifth_canonical_order], |
| 121 | + ]: |
| 122 | + for elem, nelem in zip(order_equiv_forms[:-1], order_equiv_forms[1:]): |
| 123 | + e_order, ne_order = elem.element_order, nelem.element_order |
| 124 | + print("-------") |
| 125 | + print(type(elem), f"{elem}", e_order) |
| 126 | + print("vs", type(nelem), f"{nelem}", ne_order) |
| 127 | + assert e_order < ne_order and not ( |
| 128 | + ne_order <= e_order |
| 129 | + ), "wrong order or undefined." |
| 130 | + assert ( |
| 131 | + elem == nelem |
| 132 | + ), f"elements are not equal {elem} ({type(elem)}[{e_order}]) != {nelem}({type(nelem)}[{ne_order}])" |
| 133 | + assert ( |
| 134 | + nelem == elem |
| 135 | + ), f"elements are not equal {elem} ({type(elem)}[{e_order}]) != {nelem}({type(nelem)}[{ne_order}])" |
| 136 | + |
| 137 | + |
| 138 | +def test_sorting_complex(): |
| 139 | + one_fifth_rational = ONE_FIFTH_REPRESENTATIONS["Rational"] |
| 140 | + one_fifth_mr = ONE_FIFTH_REPRESENTATIONS["MachineReal"] |
| 141 | + one_fifth_pr = ONE_FIFTH_REPRESENTATIONS["PrecisionReal`20"] |
| 142 | + one_fifth_cplx_i = Complex(one_fifth_mr, ONE_REPRESENTATIONS["MachineReal"]) |
| 143 | + one_fifth_cplx_mi = Complex(one_fifth_mr, -ONE_REPRESENTATIONS["MachineReal"]) |
| 144 | + canonical_sorted = [ |
| 145 | + one_fifth_mr, |
| 146 | + one_fifth_cplx_mi, |
| 147 | + one_fifth_cplx_i, |
| 148 | + one_fifth_pr, |
| 149 | + one_fifth_rational, |
| 150 | + ] |
| 151 | + for elem, nelem in zip(canonical_sorted[:-1], canonical_sorted[1:]): |
| 152 | + e_order, ne_order = elem.element_order, nelem.element_order |
| 153 | + print("-------") |
| 154 | + print(type(elem), f"{elem}", e_order) |
| 155 | + print("vs", type(nelem), f"{nelem}", ne_order) |
| 156 | + assert e_order < ne_order and not ( |
| 157 | + ne_order <= e_order |
| 158 | + ), f"{e_order}, {ne_order}" |
4 | 159 |
|
5 | 160 |
|
6 | 161 | # Tests |
|
0 commit comments