@@ -1092,6 +1092,7 @@ end
10921092end
10931093
10941094@inline function BSImpl. AddMul {T} (coeff, dict, variant:: AddMulVariant.T ; metadata = nothing , type, shape = default_shape (type), unsafe = false ) where {T}
1095+ @nospecialize coeff
10951096 metadata = parse_metadata (metadata)
10961097 shape = parse_shape (shape)
10971098 dict = parse_dict (T, dict)
@@ -1156,6 +1157,7 @@ struct ArrayOp{T} end
11561157end
11571158
11581159@inline function Add {T} (coeff, dict; kw... ) where {T}
1160+ @nospecialize coeff kw
11591161 coeff = unwrap (coeff)
11601162 dict = unwrap_dict (dict)
11611163 if isempty (dict)
@@ -1173,6 +1175,7 @@ end
11731175end
11741176
11751177@inline function Mul {T} (coeff, dict; kw... ) where {T}
1178+ @nospecialize coeff kw
11761179 coeff = unwrap (coeff)
11771180 dict = unwrap_dict (dict)
11781181 if isempty (dict)
@@ -1190,7 +1193,7 @@ end
11901193 k, v = first (dict)
11911194 if _isone (v)
11921195 @match k begin
1193- BSImpl. AddMul (; coeff = c2, dict = d2, variant) && if variant == AddMulVariant. ADD end => begin
1196+ BSImpl. AddMul (; coeff = c2, dict = d2, variant) && if variant === AddMulVariant. ADD end => begin
11941197 empty! (dict)
11951198 for (k, v) in d2
11961199 dict[k] = - v
@@ -1231,31 +1234,31 @@ end
12311234Simplify the coefficients of `n` and `d` (numerator and denominator).
12321235"""
12331236function simplify_coefficients (n, d)
1234- if safe_isinteger (n)
1235- n = Int (n)
1237+ return n, d
1238+ end
1239+
1240+ function safe_div (a:: Number , b:: Number ):: Number
1241+ # @nospecialize a b
1242+ if (! (a isa Integer) && safe_isinteger (a))
1243+ a = Int (a)
12361244 end
1237- if safe_isinteger (d )
1238- d = Int (d )
1245+ if ( ! (b isa Integer) && safe_isinteger (b) )
1246+ b = Int (b )
12391247 end
1240- nrat, nc = ratcoeff (n)
1241- drat, dc = ratcoeff (d)
1242- nrat && drat || return n, d
1243- g = gcd (nc, dc) * sign (dc) # make denominator positive
1244- invdc = isone (g) ? g : (1 // g)
1245- n = maybe_integer (invdc * n)
1246- d = maybe_integer (invdc * d)
1247-
1248- return n, d
1248+ if a isa Integer && b isa Integer
1249+ return a // b
1250+ end
1251+ return a / b
12491252end
12501253
12511254"""
12521255 $(TYPEDSIGNATURES)
12531256"""
12541257function Div {T} (n, d, simplified; type = promote_symtype (/ , symtype (n), symtype (d)), kw... ) where {T}
1255- n = unwrap (n)
1256- d = unwrap (d)
1258+ n = Const {T} ( unwrap (n) )
1259+ d = Const {T} ( unwrap (d) )
12571260
1258- if ! (type <: Number )
1261+ if ! _numeric_or_arrnumeric_type (type)
12591262 _iszero (n) && return Const {T} (n)
12601263 _isone (d) && return Const {T} (n)
12611264 return BSImpl. Div {T} (n, d, simplified; type, kw... )
@@ -1264,8 +1267,6 @@ function Div{T}(n, d, simplified; type = promote_symtype(/, symtype(n), symtype(
12641267 if ! (T === SafeReal)
12651268 n, d = quick_cancel (Const {T} (n), Const {T} (d))
12661269 end
1267- n = unwrap_const (n)
1268- d = unwrap_const (d)
12691270
12701271 _iszero (n) && return Const {T} (n)
12711272 _isone (d) && return Const {T} (n)
@@ -1279,10 +1280,42 @@ function Div{T}(n, d, simplified; type = promote_symtype(/, symtype(n), symtype(
12791280 return Div {T} (n * d. den, d. num, simplified; type, kw... )
12801281 end
12811282
1282- d isa Number && _isone (- d) && return Const {T} (- n)
1283- n isa Rat && d isa Rat && return Const {T} (n // d)
1283+ isconst (d) && _isone (- d) && return Const {T} (- n)
1284+ if isconst (n) && isconst (d)
1285+ nn = unwrap_const (n)
1286+ dd = unwrap_const (d)
1287+ nn isa Rat && dd isa Rat && return Const {T} (nn // dd)
1288+ return Const {T} (nn / dd)
1289+ end
12841290
1285- n, d = simplify_coefficients (n, d)
1291+ @match (n, d) begin
1292+ (BSImpl. Const (; val = v1), BSImpl. Const (; val = v2)) => Const {T} (safe_div (v1, v2))
1293+ (BSImpl. Const (; val = c1), BSImpl. AddMul (; coeff = c2, dict, type, shape, variant)) && if variant == AddMulVariant. MUL end => begin
1294+ if c1 isa Rat && c2 isa Rat
1295+ tmp = c1 // c2
1296+ c1 = tmp. num
1297+ c2 = tmp. den
1298+ end
1299+ n = Const {T} (c1)
1300+ d = Mul {T} (c2, dict, ; type, shape)
1301+ end
1302+ (BSImpl. AddMul (; coeff, dict, type, shape, variant), BSImpl. Const (; val)) && if variant == AddMulVariant. MUL end => begin
1303+ return Mul {T} (safe_div (coeff, val), dict, ; type, shape)
1304+ end
1305+ (BSImpl. AddMul (; coeff = c1, dict = d1, type = t1, shape = sh1, variant = v1),
1306+ BSImpl. AddMul (; coeff = c2, dict = d2, type = t2, shape = sh2, variant = v2)) &&
1307+ if v1 == AddMulVariant. MUL && v2 == AddMulVariant. MUL end => begin
1308+
1309+ if c1 isa Rat && c2 isa Rat
1310+ tmp = c1 // c2
1311+ c1 = tmp. num
1312+ c2 = tmp. den
1313+ end
1314+ n = Mul {T} (c1, d1, ; type = t1, shape = sh1)
1315+ d = Mul {T} (c2, d2, ; type = t2, shape = sh2)
1316+ end
1317+ _ => nothing
1318+ end
12861319
12871320 _isone (d) && return Const {T} (n)
12881321 _isone (- d) && return Const {T} (- n)
0 commit comments