|
118 | 118 | :(promote_add(promote_contract(scalartype(a), scalartype(b)), scalartype(c))) |
119 | 119 | end |
120 | 120 |
|
| 121 | + @testset "index verification" begin |
| 122 | + using TensorOperations: verifyindices, normalizeindices |
| 123 | + |
| 124 | + # every label appears once or exactly twice within a single term; different terms of |
| 125 | + # a sum, different statements and explicit `tensorscalar` calls are separate scopes |
| 126 | + valid = ( |
| 127 | + :(a[-1, -2] := b[-1, 1] * c[1, -2]), |
| 128 | + :(a[-1, -2] := b[-1, 1] * c[1, -2] + d[-1, 1] * e[1, -2]), |
| 129 | + :(a[-1, -2] := b[-1, 1] * (c[1, 2] + d[1, 2]) * e[2, -2]), |
| 130 | + :(a[-1, -2] := b[-1, 1] * c[1, -2] * (d[2, 2])), |
| 131 | + :(a[x, y] := b[x, y] - 2 * c[y, x]), |
| 132 | + :(a[-1] := b[-1, 1, 1]), |
| 133 | + :(a[x, y] := b[x, z] * tensorscalar(c[z, z]) * d[z, y]), |
| 134 | + :(a[x, y] := b[x′, x] * c[x′, y]), |
| 135 | + # the divisor of `/` and `\` is a separate scope, but the tensor operand is not |
| 136 | + :(a[x, y] := b[x, z] * c[z, y] / tensorscalar(d[z, z])), |
| 137 | + :(a[x, y] := tensorscalar(d[z, z]) \ (b[x, z] * c[z, y])), |
| 138 | + :(a[x, y] := b[x, z] * c[z, y] / alpha), |
| 139 | + :(a[x, y] := 2 \ (b[x, z] * c[z, y])), |
| 140 | + # conjugation, adjoint and unary minus pass the labels through unchanged |
| 141 | + :(a[x, y] := conj(b[x, z]) * c[z, y]), |
| 142 | + :(a[x, y] := (b[x, z])' * c[z, y]), |
| 143 | + :(a[x, y] := -(b[x, z] * c[z, y])), |
| 144 | + # `@notensor` is not index-verified at all |
| 145 | + :(a[-1, -2] := (@notensor f(1)) * b[-1, -2]), |
| 146 | + :(a[-1, -2] := (@notensor g[1, 1, 1]) * b[-1, -2]), |
| 147 | + quote |
| 148 | + @notensor b = c[1, 1, 1] |
| 149 | + a[-1, -2] := d[-1, 1] * e[1, -2] |
| 150 | + end, |
| 151 | + quote |
| 152 | + a[-1, -2] := b[-1, 1] * c[1, -2] |
| 153 | + d[-1, -2] := e[-1, 1] * f[1, -2] |
| 154 | + end, |
| 155 | + ) |
| 156 | + for ex in valid |
| 157 | + @test verifyindices(normalizeindices(ex)) isa Expr |
| 158 | + end |
| 159 | + |
| 160 | + # a label consumed by a trace or contraction cannot be reused within the same term, |
| 161 | + # and all terms of a sum should have matching open indices |
| 162 | + invalid = ( |
| 163 | + :(a[-1, -2, -3; -4, -5] := b[-2, 1] * c[1, -3; 1, -5] * d[-1, 1; -4]), # issue #288 |
| 164 | + :(a[-1, -2] := b[-1, 1; 1, -2] * c[1, 1]), |
| 165 | + :(a[-1, -2] := b[-1, 1] * c[1, -2] * d[1, 1, -3, -3]), |
| 166 | + :(a[-1, -2] := b[-1, 1] * c[1, -2] * (d[1, 1])), |
| 167 | + # parentheses do not introduce a new scope for the labels |
| 168 | + :(a[-1, -2] := (b[-1, 1] * c[1, -3]) * (d[-3, 1] * e[1, -2])), |
| 169 | + :(a[-1, -2] := b[-1, 1] * c[1, -3] * d[-3, 1] * e[1, -2]), |
| 170 | + :(a[-1] := b[1, 1, 1]), |
| 171 | + :(a[-1, -2] := b[-1, -2] + c[-1, 1]), |
| 172 | + :(a[x, y] := b[x′, x] * c[x′, y] * d[x', 1]), # primes are equal after normalizing |
| 173 | + # the divisor of `/` and `\` is verified in its own scope |
| 174 | + :(a[x, y] := b[x, y] / tensorscalar(c[z, z, z])), |
| 175 | + :(a[x, y] := tensorscalar(c[z, z, z]) \ b[x, y]), |
| 176 | + # ... but the labels of the tensor operand do escape to the enclosing term |
| 177 | + :(a[-1] := (b[-1, 1, 1] / 2) * c[1, 1]), |
| 178 | + :(a[-1] := (2 \ b[-1, 1, 1]) * c[1, 1]), |
| 179 | + # the argument of an explicit `tensorscalar` is still verified |
| 180 | + :(a[x, y] := b[x, z] * tensorscalar(c[z, z, z]) * d[z, y]), |
| 181 | + :(a[x, y] := b[x, y] * tensorscalar(c[z, z] + d[z, w])), |
| 182 | + :(alpha = tensorscalar(c[z, z, z])), |
| 183 | + ) |
| 184 | + for ex in invalid |
| 185 | + @test_throws ArgumentError verifyindices(normalizeindices(ex)) |
| 186 | + end |
| 187 | + |
| 188 | + # errors are thrown at macro expansion time |
| 189 | + @test_throws ArgumentError @macroexpand( |
| 190 | + @tensor T[-1, -2, -3; -4, -5] := A[-2, 1] * B[1, -3; 1, -5] * C[-1, 1; -4] |
| 191 | + ) |
| 192 | + @test_throws ArgumentError @macroexpand( |
| 193 | + @tensoropt T[-1, -2] := A[-1, 1] * B[1, -2] * C[1, 1] |
| 194 | + ) |
| 195 | + end |
| 196 | + |
121 | 197 | @testset "parsecost" begin |
122 | 198 | using TensorOperations: parsecost, Power |
123 | 199 | @test parsecost(:(3 / 5)) === 3 / 5 |
|
0 commit comments