| @@ -3,7 +3,9 @@ import Foundation |
| 3 | 3 | /// Calc's arithmetic on formulas it can't reduce to a number: variables, `inf`, `uinf`, and |
| 4 | 4 | /// quotients by zero. This ports `math-add`, `math-mul`, `math-div`, `math-pow` and `math-neg` |
| 5 | 5 | /// with their `-symb-fancy` helpers from calc.el and calc-arith.el, as `calc-eval` runs them |
| 6 | | /// with `calc-simplify-mode` nil, and Calc's flat formula printer from calccomp.el. Anything |
| 6 | /// with `calc-simplify-mode` nil; `math-sqrt`, `math-abs`, `math-mod`, `calcFunc-exp`, `calcFunc-ln`, |
| 7 | /// `calcFunc-min`, `calcFunc-max` and the comparisons and logical functions of calc-prog.el on |
| 8 | /// formulas; and Calc's flat formula printer from calccomp.el. Anything |
| 7 | 9 | /// those functions would decide from declarations, units, matrices or intervals throws |
| 8 | 10 | /// `Calc.Unsupported`. |
| 9 | 11 | extension Calc { |
| @@ -163,7 +165,7 @@ extension Calc { |
| 163 | 165 | return 7 |
| 164 | 166 | } |
| 165 | 167 | if f == "neg" { return negSigns(try signs(a1, try origin.map(neg))) } |
| 166 | | let origin = origin.flatMap { zerop($0) ? nil : $0 } |
| 168 | var origin = origin.flatMap { zerop($0) ? nil : $0 } |
| 167 | 169 | let zero = Term.number(.int(0)) |
| 168 | 170 | if f == "*" || (f == "/" && origin != nil), objectp(a1) { |
| 169 | 171 | let s = f == "*" |
| @@ -211,6 +213,32 @@ extension Calc { |
| 211 | 213 | case "i": return 8 |
| 212 | 214 | default: return 15 |
| 213 | 215 | } |
| 216 | case .op("%", let args): |
| 217 | let s2 = try signs(args[1]) |
| 218 | sign = s2 >= 8 ? 7 : s2 == 2 ? 2 : [4, 6].contains(s2) ? 6 : [1, 3].contains(s2) ? 3 : 7 |
| 219 | case .op("abs", let args): |
| 220 | let s1 = try signs(args[0]) |
| 221 | sign = s1 == 2 ? 2 : [1, 4, 5].contains(s1) ? 4 : 6 |
| 222 | case .op("exp", let args): |
| 223 | let s1 = try signs(args[0]) |
| 224 | if s1 >= 8 { |
| 225 | sign = 15 |
| 226 | } else if origin == nil || negp(origin!) { |
| 227 | sign = 4 |
| 228 | } else { |
| 229 | origin = try sub(origin!, .number(.int(1))) |
| 230 | if zerop(origin!) { origin = nil } |
| 231 | sign = s1 |
| 232 | } |
| 233 | case .op("ln", let args): |
| 234 | sign = try knownNonneg(args[0]) ? try signs(args[0], .number(.int(1))) : 15 |
| 235 | case .op("sqrt", let args): |
| 236 | let s1 = try signs(args[0]) |
| 237 | sign = [2, 4, 6].contains(s1) ? s1 : 15 |
| 238 | case .op(let f, _) where ["eq", "neq", "lt", "gt", "leq", "geq", "lnot", "max", "min"].contains(f): |
| 239 | sign = 7 |
| 240 | // Nothing is declared about the other functions. |
| 241 | case .op(let f, _) where functions.contains(f) || f == "if": return 15 |
| 214 | 242 | default: throw Unsupported("function \(f ?? "")") |
| 215 | 243 | } |
| 216 | 244 | guard let origin else { return sign } |
| @@ -220,6 +248,35 @@ extension Calc { |
| 220 | 248 | |
| 221 | 249 | static func knownNonneg(_ t: Term) throws -> Bool { [2, 4, 6].contains(try signs(t)) } |
| 222 | 250 | |
| 251 | static func knownReal(_ t: Term) throws -> Bool { try signs(t) < 8 } |
| 252 | |
| 253 | /// `math-known-evenp`. |
| 254 | static func knownEven(_ t: Term) throws -> Bool { |
| 255 | switch t { |
| 256 | case .number(.int(let n)): return n % 2 == 0 |
| 257 | case .number(.float(let m, let e)) where e >= 0: return e > 0 || m % 2 == 0 |
| 258 | case .op("*", let args): |
| 259 | if try knownEven(args[0]) { return try knownNumInteger(args[1]) } |
| 260 | return try knownNumInteger(args[0]) && knownEven(args[1]) |
| 261 | case .op("+", let args), .op("-", let args): |
| 262 | return try (knownEven(args[0]) && knownEven(args[1])) || (knownOdd(args[0]) && knownOdd(args[1])) |
| 263 | case .op("neg", let args): return try knownEven(args[0]) |
| 264 | default: return false |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /// `math-known-oddp`. |
| 269 | static func knownOdd(_ t: Term) throws -> Bool { |
| 270 | switch t { |
| 271 | case .number(.int(let n)): return n % 2 != 0 |
| 272 | case .number(.float(let m, let e)) where e >= 0: return e == 0 && m % 2 != 0 |
| 273 | case .op("+", let args), .op("-", let args): |
| 274 | return try (knownEven(args[0]) && knownOdd(args[1])) || (knownOdd(args[0]) && knownEven(args[1])) |
| 275 | case .op("neg", let args): return try knownOdd(args[0]) |
| 276 | default: return false |
| 277 | } |
| 278 | } |
| 279 | |
| 223 | 280 | /// `math-known-num-integerp`. |
| 224 | 281 | static func knownNumInteger(_ t: Term) throws -> Bool { |
| 225 | 282 | if let v = number(t) { return isWhole(v) } |
| @@ -244,8 +301,18 @@ extension Calc { |
| 244 | 301 | case .number: throw Unsupported("symbolic arithmetic") |
| 245 | 302 | case .variable: return 63 |
| 246 | 303 | case .op("neg", let args): return try possibleTypes(args[0]) |
| 247 | | case .op(let f, let args) where args.count == 2 && ["+", "-", "*", "/", "^"].contains(f): |
| 248 | | let (t1, t2) = (try possibleTypes(args[0], num: num), try possibleTypes(args[1], num: num)) |
| 304 | case .op("sqrt", let args): |
| 305 | let s1 = try signs(args[0]) |
| 306 | let t = (s1 & 2 > 0 ? 3 : 0) | (s1 & 1 > 0 ? 16 : 0) | (s1 & 4 > 0 ? 15 : 0) | (s1 & 8 > 0 ? 32 : 0) |
| 307 | return t < 16 ? t : 63 |
| 308 | case .op("abs", let args): |
| 309 | let t1 = try possibleTypes(args[0]) |
| 310 | return t1 >= 16 ? 15 : t1 |
| 311 | case .op(let f, let args) where args.count == 2 && ["+", "-", "*", "/", "^", "%"].contains(f): |
| 312 | // `%` keeps only the real bits, which the merged values above 15 don't give. |
| 313 | if f == "%", args.contains(where: { containsHead($0, "sqrt") }) { throw Unsupported("symbolic arithmetic") } |
| 314 | var (t1, t2) = (try possibleTypes(args[0], num: num), try possibleTypes(args[1], num: num)) |
| 315 | if f == "%" { (t1, t2) = (t1 & 15, t2 & 15) } |
| 249 | 316 | let t12 = t1 | t2 |
| 250 | 317 | if f == "^" { |
| 251 | 318 | guard t2 <= 3, try knownNonneg(args[1]), t1 < 16 else { |
| @@ -260,12 +327,18 @@ extension Calc { |
| 260 | 327 | return t1 == 4 || t2 == 4 || fractions ? 5 : 15 |
| 261 | 328 | } |
| 262 | 329 | let mask = t12 <= 3 ? 1 |
| 263 | | : ((t1 <= 3 && t2 & 3 == 0) || (t2 <= 3 && t1 & 3 == 0)) && f != "*" ? 4 : 5 |
| 330 | : ((t1 <= 3 && t2 & 3 == 0) || (t2 <= 3 && t1 & 3 == 0)) && (f == "+" || f == "-") ? 4 : 5 |
| 264 | 331 | return merge(t1, t2, mask) |
| 332 | case .op(let f, _) where functions.contains(f) || f == "if": return 63 |
| 265 | 333 | default: throw Unsupported("function \(head(a) ?? "")") |
| 266 | 334 | } |
| 267 | 335 | } |
| 268 | 336 | |
| 337 | static func containsHead(_ t: Term, _ f: String) -> Bool { |
| 338 | guard case .op(let g, let args) = t else { return false } |
| 339 | return g == f || args.contains { containsHead($0, f) } |
| 340 | } |
| 341 | |
| 269 | 342 | // MARK: Arithmetic |
| 270 | 343 | |
| 271 | 344 | /// `math-normalize`: the operations applied again from the leaves, floats rounded |
| @@ -274,6 +347,12 @@ extension Calc { |
| 274 | 347 | switch t { |
| 275 | 348 | case .number(.float(let m, let e)): return .number(try makeFloat(Int128(m), e)) |
| 276 | 349 | case .number, .variable: return t |
| 350 | // `math-normalize-logical-op`: the branches stay as written unless the condition |
| 351 | // is a number. |
| 352 | case .op("if", let args) where args.count == 3: |
| 353 | let c = try normalize(args[0]) |
| 354 | guard let v = number(c) else { return .op("if", [c, args[1], args[2]]) } |
| 355 | return try normalize(isZero(v) ? args[2] : args[1]) |
| 277 | 356 | case .op(let f, let args): |
| 278 | 357 | let args = try args.map(normalize) |
| 279 | 358 | do { return try apply(f, args) } catch is Rejected { return .op(f, args) } |
| @@ -288,7 +367,9 @@ extension Calc { |
| 288 | 367 | case ("/", 2): return try div(args[0], args[1]) |
| 289 | 368 | case ("^", 2): return try pow(args[0], args[1]) |
| 290 | 369 | case ("neg", 1): return try neg(args[0]) |
| 291 | | default: throw Unsupported("function \(f)") |
| 370 | default: |
| 371 | guard functions.contains(f) else { throw Unsupported("function \(f)") } |
| 372 | return try function(f, args) |
| 292 | 373 | } |
| 293 | 374 | } |
| 294 | 375 | |
| @@ -547,7 +628,9 @@ extension Calc { |
| 547 | 628 | func split(_ t: Term) throws -> (Term, Term) { |
| 548 | 629 | var (base, power) = (t, Term.number(.int(1))) |
| 549 | 630 | if head(t) == "^", objectp(arg(t, 2)) { (base, power) = (arg(t, 1), arg(t, 2)) } |
| 550 | | if head(t) == "sqrt" || head(t) == "exp" { throw Unsupported("symbolic arithmetic") } |
| 631 | if head(t) == "sqrt" { (base, power) = (arg(t, 1), .number(.frac(1, 2))) } |
| 632 | // Only in symbolic mode does `exp` of a number stay. |
| 633 | if head(t) == "exp", objectp(arg(t, 1)) { throw Unsupported("symbolic arithmetic") } |
| 551 | 634 | if case .number(.frac(let n, let d)) = base, n < d { |
| 552 | 635 | (base, power) = (try div(.number(.int(1)), base), try neg(power)) |
| 553 | 636 | } |
| @@ -567,17 +650,22 @@ extension Calc { |
| 567 | 650 | if looksNeg(sum), ratp(baseA), posp(baseA) { |
| 568 | 651 | (baseA, sum) = (try div(.number(.int(1)), baseA), try neg(sum)) |
| 569 | 652 | } |
| 570 | | if sum == .number(.frac(1, 2)) || sum == .number(.frac(-1, 2)) { throw Unsupported("symbolic arithmetic") } |
| 571 | | return try pow(baseA, sum) |
| 653 | return try root(baseA, sum) |
| 572 | 654 | } |
| 573 | 655 | } |
| 574 | 656 | if powA == powB, case .number(.int(let x)) = baseA, case .number(.int(let y)) = baseB, x >= 0, y >= 0 { |
| 575 | | if powA == .number(.frac(1, 2)) || powA == .number(.frac(-1, 2)) { throw Unsupported("symbolic arithmetic") } |
| 576 | | return try pow(try mul(baseA, baseB), powA) |
| 657 | return try root(try mul(baseA, baseB), powA) |
| 577 | 658 | } |
| 578 | 659 | return nil |
| 579 | 660 | } |
| 580 | 661 | |
| 662 | /// `a^p`, with Calc's square root form for a half. |
| 663 | static func root(_ a: Term, _ p: Term) throws -> Term { |
| 664 | if p == .number(.frac(1, 2)) { return .op("sqrt", [a]) } |
| 665 | if p == .number(.frac(-1, 2)) { return try div(.number(.int(1)), .op("sqrt", [a])) } |
| 666 | return try pow(a, p) |
| 667 | } |
| 668 | |
| 581 | 669 | /// `math-pow`. |
| 582 | 670 | static func pow(_ a: Term, _ b: Term) throws -> Term { |
| 583 | 671 | if b == nan { return b } |
| @@ -617,16 +705,19 @@ extension Calc { |
| 617 | 705 | return try mul(try pow(a1, b), try pow(try div(.number(.int(1)), a2), b)) |
| 618 | 706 | case "^" where try knownNumInteger(b) || knownNonneg(a1): |
| 619 | 707 | return try pow(a1, try mul(a2, b)) |
| 708 | case "sqrt" where try knownNumInteger(b) || knownNonneg(a1): |
| 709 | return try pow(a1, try div(b, .number(.int(2)))) |
| 620 | 710 | case "^": |
| 621 | | // `math-known-evenp` of an exponent with a variable is false. |
| 622 | | if case .number(let n) = a2, isWhole(n), try trunc(n) % 2 == 0, let y = number(b), let q = quarter(y), |
| 623 | | (1...3).contains(q), try signs(a1) < 8 { throw Unsupported("symbolic arithmetic") } |
| 711 | if let y = number(b), let q = quarter(y), (1...3).contains(q), try knownEven(a2), try knownReal(a1) { |
| 712 | return try absTerm(try pow(a1, try mul(a2, b))) |
| 713 | } |
| 624 | 714 | default: break |
| 625 | 715 | } |
| 626 | 716 | if looksNeg(a), case .number(.int(let n)) = b { |
| 627 | 717 | let p = try pow(try neg(a), b) |
| 628 | 718 | return n % 2 == 0 ? p : try neg(p) |
| 629 | 719 | } |
| 720 | if head(a) == "abs", try knownReal(a1), try knownEven(b) { return try pow(a1, b) } |
| 630 | 721 | if let infinity = infinitep(a) { |
| 631 | 722 | if infinity == nan { return infinity } |
| 632 | 723 | if head(a) == "neg" { return try mul(try pow(.number(.int(-1)), b), try pow(a1, b)) } |
| @@ -647,6 +738,197 @@ extension Calc { |
| 647 | 738 | return .op("^", [a, b]) |
| 648 | 739 | } |
| 649 | 740 | |
| 741 | // MARK: Functions |
| 742 | |
| 743 | /// The functions `function` applies to formulas; `%` is `math-mod`. |
| 744 | static let functions: Set<String> = ["sqrt", "abs", "exp", "ln", "min", "max", "eq", "neq", "lt", "gt", "leq", "geq", |
| 745 | "land", "lor", "lnot", "%"] |
| 746 | |
| 747 | /// `name` of `args`, at least one a formula, as `math-normalize` calls it: simplified, |
| 748 | /// or kept with its arguments when the function rejects them. |
| 749 | static func function(_ name: String, _ args: [Term]) throws -> Term { |
| 750 | do { return try simplify(name, args) } catch is Rejected { return .op(name, args) } |
| 751 | } |
| 752 | |
| 753 | static func simplify(_ name: String, _ args: [Term]) throws -> Term { |
| 754 | let kept = Term.op(name, args) |
| 755 | switch (name, args.count) { |
| 756 | case ("sqrt", 1), ("exp", 1), ("ln", 1): |
| 757 | // In symbolic mode these keep a result that isn't exact, which isn't reproduced. |
| 758 | guard !symbolicMode else { throw Unsupported("function \(name) in symbolic mode") } |
| 759 | switch name { |
| 760 | case "sqrt": return try sqrtTerm(args[0]) |
| 761 | case "exp": return try expTerm(args[0]) |
| 762 | default: return try lnTerm(args[0]) |
| 763 | } |
| 764 | case ("abs", 1): return try absTerm(args[0]) |
| 765 | case ("min", _), ("max", _): |
| 766 | // `calcFunc-min` rejects an argument that isn't a real number or an infinity. |
| 767 | guard !args.isEmpty else { throw Unsupported("arguments") } |
| 768 | guard args.allSatisfy({ objectp($0) || infinitep($0) != nil }) else { return kept } |
| 769 | return try args.dropFirst().reduce(args[0]) { best, item in |
| 770 | switch try compareTerms(best, item) { |
| 771 | case 2: return nan |
| 772 | case let order: return order == (name == "max" ? -1 : 1) ? item : best |
| 773 | } |
| 774 | } |
| 775 | case ("eq", 2), ("neq", 2), ("lt", 2), ("gt", 2), ("leq", 2), ("geq", 2): |
| 776 | let (a, b) = (args[0], args[1]) |
| 777 | let order = try compareTerms(a, b) |
| 778 | if order == 2 { |
| 779 | // Both sides negative, or zero, flip. |
| 780 | if looksNeg(a) || zerop(a), looksNeg(b) || zerop(b) { |
| 781 | return .op(flipped[name]!, [try neg(a), try neg(b)]) |
| 782 | } |
| 783 | return kept |
| 784 | } |
| 785 | let holds = switch name { |
| 786 | case "eq": order == 0 |
| 787 | case "neq": order != 0 |
| 788 | case "lt": order == -1 |
| 789 | case "gt": order == 1 |
| 790 | case "leq": order != 1 |
| 791 | default: order != -1 |
| 792 | } |
| 793 | return .number(.int(holds ? 1 : 0)) |
| 794 | case ("land", 2): |
| 795 | let (a, b) = (args[0], args[1]) |
| 796 | if zerop(a) { return a } |
| 797 | if zerop(b) { return b } |
| 798 | if try isTrue(a) { return b } |
| 799 | if try isTrue(b) { return a } |
| 800 | return kept |
| 801 | case ("lor", 2): |
| 802 | let (a, b) = (args[0], args[1]) |
| 803 | if zerop(a) { return b } |
| 804 | if zerop(b) { return a } |
| 805 | if try isTrue(a) { return a } |
| 806 | if try isTrue(b) { return b } |
| 807 | return kept |
| 808 | case ("lnot", 1): |
| 809 | let a = args[0] |
| 810 | if zerop(a) { return .number(.int(1)) } |
| 811 | if try isTrue(a) { return .number(.int(0)) } |
| 812 | if case .op(let f, let xs) = a, xs.count == 2, let opposite = negated[f] { return .op(opposite, xs) } |
| 813 | return kept |
| 814 | case ("%", 2): return try modTerm(args[0], args[1]) |
| 815 | default: throw Unsupported("function \(name) of a formula") |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | /// The comparison with its sides negated, and its negation (`calc-tweak-eqn-table`). |
| 820 | static let flipped = ["eq": "eq", "neq": "neq", "lt": "gt", "gt": "lt", "leq": "geq", "geq": "leq"] |
| 821 | static let negated = ["eq": "neq", "neq": "eq", "lt": "geq", "gt": "leq", "leq": "gt", "geq": "lt"] |
| 822 | |
| 823 | /// `math-is-true`. |
| 824 | static func isTrue(_ t: Term) throws -> Bool { |
| 825 | if let v = number(t) { return !isZero(v) } |
| 826 | return [1, 4, 5, 8, 9, 12, 13].contains(try signs(t)) |
| 827 | } |
| 828 | |
| 829 | /// `math-compare`: -1, 0 or 1, or 2 when the order isn't known. |
| 830 | static func compareTerms(_ a: Term, _ b: Term) throws -> Int { |
| 831 | if a == b { |
| 832 | switch a { |
| 833 | case .number: return 0 |
| 834 | case .variable, .op("neg", _), .op("*", _), .op("/", _): return infinitep(a) != nil ? 2 : 0 |
| 835 | default: return 0 |
| 836 | } |
| 837 | } |
| 838 | if let x = number(a), let y = number(b) { return try Calc.compare(x, y) } |
| 839 | if case .number(.frac(let n, let d)) = a { return try compareTerms(.number(.int(n)), try mul(b, .number(.int(d)))) } |
| 840 | if case .number(.frac(let n, let d)) = b { return try compareTerms(try mul(a, .number(.int(d))), .number(.int(n))) } |
| 841 | let one = Term.number(.int(1)), minusOne = Term.number(.int(-1)) |
| 842 | if let infA = infinitep(a) { |
| 843 | if a == uinf || a == nan { return 2 } |
| 844 | let dirA = try infiniteDirection(a, infA) |
| 845 | if let infB = infinitep(b) { |
| 846 | if b == uinf || b == nan { return 2 } |
| 847 | let dirB = try infiniteDirection(b, infB) |
| 848 | return dirA == one && dirB == minusOne ? 1 : dirA == minusOne && dirB == one ? -1 : 2 |
| 849 | } |
| 850 | return dirA == one ? 1 : dirA == minusOne ? -1 : 2 |
| 851 | } |
| 852 | if let infB = infinitep(b) { |
| 853 | if b == uinf || b == nan { return 2 } |
| 854 | let dirB = try infiniteDirection(b, infB) |
| 855 | return dirB == one ? -1 : dirB == minusOne ? 1 : 2 |
| 856 | } |
| 857 | let e = Term.variable("e") |
| 858 | if head(a) == "exp", head(b) == "^", arg(b, 1) == e { return try compareTerms(arg(a, 1), arg(b, 2)) } |
| 859 | if head(b) == "exp", head(a) == "^", arg(a, 1) == e { return try compareTerms(arg(a, 2), arg(b, 1)) } |
| 860 | let half: [Term] = [.number(.frac(1, 2)), .number(.float(5, -1))] |
| 861 | if (head(a) == "sqrt" && head(b) == "^" && half.contains(arg(b, 2))) |
| 862 | || (head(b) == "sqrt" && head(a) == "^" && half.contains(arg(a, 2))) { |
| 863 | return try compareTerms(arg(a, 1), arg(b, 1)) |
| 864 | } |
| 865 | if case .variable = a { return 2 } |
| 866 | if case .op(let f, let xs) = a, case .op(let g, let ys) = b, f == g, xs.count == ys.count, |
| 867 | try zip(xs, ys).allSatisfy({ try compareTerms($0, $1) == 0 }) { return 0 } |
| 868 | return 2 |
| 869 | } |
| 870 | |
| 871 | /// `math-sqrt` of a formula. |
| 872 | static func sqrtTerm(_ a: Term) throws -> Term { |
| 873 | if let v = number(a) { return .number(try squareRoot(v)) } |
| 874 | // An imaginary result. |
| 875 | if [1, 2, 3].contains(try signs(a)) { throw Unsupported("complex result") } |
| 876 | let (a1, a2) = (arg(a, 1), arg(a, 2)) |
| 877 | switch head(a) { |
| 878 | case "*" where try knownNonneg(a1) || knownNonneg(a2): |
| 879 | return try mul(try sqrtTerm(a1), try sqrtTerm(a2)) |
| 880 | case "/" where try knownNonneg(a2): |
| 881 | return try div(try sqrtTerm(a1), try sqrtTerm(a2)) |
| 882 | case "/" where try knownNonneg(a1) && !equalInt(a1, 1): |
| 883 | return try mul(try sqrtTerm(a1), try sqrtTerm(try div(.number(.int(1)), a2))) |
| 884 | case "^" where try knownEven(a2) && knownReal(a1): |
| 885 | return try absTerm(try pow(a1, try div(a2, .number(.int(2))))) |
| 886 | default: break |
| 887 | } |
| 888 | if let infinity = infinitep(a) { return try mul(try sqrtTerm(try infiniteDirection(a, infinity)), infinity) } |
| 889 | return .op("sqrt", [a]) |
| 890 | } |
| 891 | |
| 892 | /// `math-abs` of a formula. |
| 893 | static func absTerm(_ a: Term) throws -> Term { |
| 894 | if let v = number(a) { return .number(try absolute(v)) } |
| 895 | if negp(a) { return try neg(a) } |
| 896 | if looksNeg(a) { return .op("abs", [try neg(a)]) } |
| 897 | let s = try signs(a) |
| 898 | if [2, 4, 6].contains(s) { return a } |
| 899 | if [1, 3].contains(s) { return try neg(a) } |
| 900 | if let infinity = infinitep(a) { return infinity == nan ? nan : inf } |
| 901 | return .op("abs", [a]) |
| 902 | } |
| 903 | |
| 904 | /// `calcFunc-exp` of a formula. |
| 905 | static func expTerm(_ a: Term) throws -> Term { |
| 906 | if objectp(a) { throw Unsupported("symbolic arithmetic") } |
| 907 | if a == inf || a == nan { return a } |
| 908 | if a == .op("neg", [inf]) { return .number(.int(0)) } |
| 909 | return .op("exp", [a]) |
| 910 | } |
| 911 | |
| 912 | /// `calcFunc-ln` of a formula. |
| 913 | static func lnTerm(_ a: Term) throws -> Term { |
| 914 | if objectp(a) { throw Unsupported("symbolic arithmetic") } |
| 915 | if a == .variable("e") { return .number(.int(1)) } |
| 916 | if head(a) == "^", arg(a, 1) == .variable("e"), try knownReal(arg(a, 2)) { return arg(a, 2) } |
| 917 | if infinitep(a) != nil { return a == nan ? nan : inf } |
| 918 | return .op("ln", [a]) |
| 919 | } |
| 920 | |
| 921 | /// `math-mod` with a formula. |
| 922 | static func modTerm(_ a: Term, _ b: Term) throws -> Term { |
| 923 | if zerop(a) { return a } |
| 924 | if objectp(a), objectp(b) { throw Unsupported("symbolic arithmetic") } |
| 925 | if b == inf { |
| 926 | if posp(a) || zerop(a) { return a } |
| 927 | if negp(a) { return b } |
| 928 | } |
| 929 | return .op("%", [a, b]) |
| 930 | } |
| 931 | |
| 650 | 932 | // MARK: Display |
| 651 | 933 | |
| 652 | 934 | /// `math-format-value`: `math-compose-expr` in the flat language. |
| @@ -659,7 +941,12 @@ extension Calc { |
| 659 | 941 | |
| 660 | 942 | /// Left and right precedence, from `math-standard-ops` with |
| 661 | 943 | /// `calc-multiplication-has-precedence`. |
| 662 | | static let operators: [String: (Int, Int)] = ["+": (180, 181), "-": (180, 181), "*": (196, 195), "/": (190, 191), "^": (201, 200)] |
| 944 | static let operators: [String: (Int, Int)] = ["+": (180, 181), "-": (180, 181), "*": (196, 195), "/": (190, 191), "^": (201, 200), |
| 945 | "%": (190, 191), "eq": (160, 161), "neq": (160, 161), "lt": (160, 161), |
| 946 | "gt": (160, 161), "leq": (160, 161), "geq": (160, 161), "land": (110, 111), |
| 947 | "lor": (100, 101)] |
| 948 | |
| 949 | static let symbols = ["eq": "=", "neq": "!=", "lt": "<", "gt": ">", "leq": "<=", "geq": ">=", "land": "&&", "lor": "||"] |
| 663 | 950 | |
| 664 | 951 | static func compose(_ t: Term, _ prec: Int, _ floatFormat: FloatFormat, div: Bool = false) throws -> String { |
| 665 | 952 | switch t { |
| @@ -668,6 +955,13 @@ extension Calc { |
| 668 | 955 | case .op("neg", let args): |
| 669 | 956 | if prec > 197 { return "(" + (try compose(t, 0, floatFormat)) + ")" } |
| 670 | 957 | return "-" + (try compose(args[0], 197, floatFormat)) |
| 958 | case .op("lnot", let args) where args.count == 1: |
| 959 | if prec > 1000 { return "(" + (try compose(t, 0, floatFormat)) + ")" } |
| 960 | return "!" + (try compose(args[0], 1000, floatFormat)) |
| 961 | case .op("if", let args) where args.count == 3: |
| 962 | if prec > 90 { return "(" + (try compose(t, 0, floatFormat)) + ")" } |
| 963 | return try compose(args[0], 91, floatFormat) + " ? " + compose(args[1], 0, floatFormat) + " : " |
| 964 | + compose(args[2], 90, floatFormat) |
| 671 | 965 | case .op(let f, let args): |
| 672 | 966 | guard let (left, right) = operators[f], args.count == 2 else { |
| 673 | 967 | return f + "(" + (try args.map { try compose($0, 0, floatFormat) }).joined(separator: ", ") + ")" |
| @@ -687,7 +981,7 @@ extension Calc { |
| 687 | 981 | } |
| 688 | 982 | if f == "^" { return lhs + "^" + rhs } |
| 689 | 983 | if f == "/", let x = number(args[0]), isWhole(x), case .number(.int) = args[1] { return lhs + "/" + rhs } |
| 690 | | return lhs + " " + f + " " + rhs |
| 984 | return lhs + " " + (symbols[f] ?? f) + " " + rhs |
| 691 | 985 | } |
| 692 | 986 | } |
| 693 | 987 | } |