SPB Git

Repository Public MIT

NumericalTests.swift 21.0 KB 355 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Calc9// File             : Packages/ORIXSymbolic/Tests/ORIXSymbolicTests/NumericalTests.swift10// Description      : Systems of equations, optimisation and ODE solvers against11//                    closed forms with explicit error bounds; randomised12//                    simplifier checks.13// Module           : ORIXSymbolicTests14// Repository       : https://git.spboucher.ai/orix-calc15// Created          : 2026-11-2016// Last Modified    : 2026-11-2017//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author           : Simon-Pierre Boucher23// Position         : Professor of Finance24// Institution      : Université du Québec en Outaouais (UQO)25// Department       : Département des sciences administratives26// Organization     : SP Boucher27//28// Website          : https://spboucher.ai29// Git              : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General          : info@spboucher.ai36// Professional     : contact@spboucher.ai37// Direct           : simon@spboucher.ai38// Research         : research@spboucher.ai39// Development      : dev@spboucher.ai40// Support          : support@spboucher.ai41// Security/Admin   : admin@spboucher.ai42// Notifications    : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification   : Public69// Distribution     : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License          : Proprietary, all rights reserved (see license file)83// License File     : LICENSE84// Maintainer       : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import Foundation92import Testing93import BigInt94import ORIXExpression95import ORIXNumerics96@testable import ORIXSymbolic9798@Suite struct Systems {99    @Test func uniqueSolutionIsExact() {100        let r = Symbolic.solveLinearSystem([P("2x+y=5"), P("x-y=1")], variables: ["x", "y"])101        #expect(r.kind == .exact && r.classification == .unique && r.rank == 2)102        #expect(r.values == [Value(2), Value(1)])103        #expect(r.verification.isVerified && r.explanation.plainText.contains("Row operation"))104        let fractions = Symbolic.solveLinearSystem([P("x/3 + y/7 = 1"), P("2x - y/2 = 1/5"), P("x + y + z = 0")], variables: ["x", "y", "z"])105        #expect(fractions.classification == .unique)106        #expect(pretty(fractions.solution["x"]) == "111/95" && pretty(fractions.solution["y"]) == "406/95" && pretty(fractions.solution["z"]) == "−517/95")107    }108    @Test func largeSystemStaysExact() {109        // Hilbert matrix 8×8: catastrophically ill-conditioned in floating point, exact in rational arithmetic110        let n = 8111        let names = (1...n).map { "x\($0)" }112        var equations: [Expr] = []113        for i in 1...n {114            let lhs = (1...n).map { j in "x\(j)/\(i + j - 1)" }.joined(separator: " + ")115            let rhs = (1...n).reduce(BigRational.zero) { $0 + BigRational(1, BigInt(i + $1 - 1)) }116            equations.append(P("\(lhs) = \(rhs.num)/\(rhs.den)"))117        }118        let r = Symbolic.solveLinearSystem(equations, variables: names)119        #expect(r.classification == .unique && r.values == Array(repeating: Value(1), count: n))120    }121    @Test func inconsistentSystem() {122        let r = Symbolic.solveLinearSystem([P("x+y=1"), P("x+y=2")], variables: ["x", "y"])123        #expect(r.kind == .exact && r.classification == LinearSystemResult.Classification.none && r.solution.isEmpty && r.rank == 1)124        #expect(r.note.contains("no solution"))125        let three = Symbolic.solveLinearSystem([P("x+y+z=1"), P("x-y=0"), P("2x+z=5")], variables: ["x", "y", "z"])126        #expect(three.classification == LinearSystemResult.Classification.none)127    }128    @Test func underdeterminedSystemGivesTheParametricForm() {129        let r = Symbolic.solveLinearSystem([P("x+y+z=6"), P("x-y=0")], variables: ["x", "y", "z"])130        #expect(r.classification == .infinitelyMany && r.rank == 2 && r.freeVariables == ["z"])131        #expect(pretty(r.solution["x"]) == "−z/2 + 3" && pretty(r.solution["y"]) == "−z/2 + 3" && pretty(r.solution["z"]) == "z")132        #expect(r.verification.isVerified)133        let dependent = Symbolic.solveLinearSystem([P("x+y=2"), P("2x+2y=4")], variables: ["x", "y"])134        #expect(dependent.classification == .infinitelyMany && dependent.rank == 1 && dependent.freeVariables == ["y"])135    }136    @Test func nonLinearInputIsRefusedByTheLinearSolver() {137        #expect(Symbolic.solveLinearSystem([P("x*y=1"), P("x+y=2")], variables: ["x", "y"]).kind == .unsupported)138        #expect(Symbolic.solveLinearSystem([P("a*x+y=1"), P("x+y=2")], variables: ["x", "y"]).kind == .unsupported)139        #expect(Symbolic.solveLinearSystem([], variables: ["x"]).kind == .unsupported)140    }141    @Test func nonlinearSystemsAreNumericalWithResiduals() {142        let r = Symbolic.solveSystem([P("x^2+y^2=4"), P("x*y=1")], variables: ["x", "y"])143        #expect(r.kind == .numerical && r.solutions.count == 4)144        for s in r.solutions {145            #expect(s.residual < 1e-10)146            #expect(abs(s.values[0] * s.values[0] + s.values[1] * s.values[1] - 4) < 1e-10 && abs(s.values[0] * s.values[1] - 1) < 1e-10)147        }148        #expect(r.note.contains("Numerical"))149        // elementary symmetric functions 3, 2, 0: the solutions are the six permutations of (0, 1, 2)150        let three = Symbolic.solveSystem([P("x+y+z=3"), P("x^2+y^2+z^2=5"), P("x*y*z=0")], variables: ["x", "y", "z"], searchBox: -4...4)151        #expect(three.kind == .numerical && three.solutions.count == 6 && three.solutions.allSatisfy { $0.residual < 1e-9 })152        for s in three.solutions { #expect(s.values.map { ($0 * 1e6).rounded() / 1e6 }.sorted() == [0, 1, 2]) }153        let guided = Symbolic.solveSystem([P("e^x + y = 2"), P("x + sin(y) = 1")], variables: ["x", "y"], guesses: [[0.5, 0.5]])154        #expect(guided.kind == .numerical && guided.solutions.count >= 1)155        let none = Symbolic.solveSystem([P("x^2+y^2=-1"), P("x-y=0")], variables: ["x", "y"])156        #expect(none.kind == .failedConvergence && none.solutions.isEmpty)157        let linear = Symbolic.solveSystem([P("x+y=3"), P("x-y=1")], variables: ["x", "y"])158        #expect(linear.kind == .exact && linear.solutions[0].values == [2, 1] && linear.linear?.classification == .unique)159        #expect(Symbolic.solveSystem([P("x^2+y=1")], variables: ["x", "y"]).kind == .unsupported)160    }161}162163@Suite struct Optimisation {164    @Test func cubicOnAClosedInterval() {165        let r = Symbolic.optimize(P("x^3-3x"), variable: "x", from: P("-3"), to: P("3"))166        #expect(r.kind == .exact && r.criticalPoints.count == 2)167        #expect(r.criticalPoints[0].classification == .localMaximum && pretty(r.criticalPoints[0].location) == "−1" && pretty(r.criticalPoints[0].valueExpression) == "2")168        #expect(r.criticalPoints[1].classification == .localMinimum && r.criticalPoints[1].value == -2)169        #expect(r.globalMinimum?.classification == .endpoint && r.globalMinimum?.value == -18 && r.globalMaximum?.value == 18)170        #expect(r.verification.isVerified)171        let inner = Symbolic.optimize(P("x^3-3x"), variable: "x", from: P("-3/2"), to: P("3/2"))172        #expect(inner.globalMinimum?.value == -2 && inner.globalMaximum?.value == 2 && inner.globalMaximum?.classification == .localMaximum)173    }174    @Test func exactSurdsAndClassification() {175        let r = Symbolic.optimize(P("x^4-4x^2"), variable: "x", from: P("-3"), to: P("3"))176        #expect(r.kind == .exact && r.criticalPoints.map { pretty($0.location) } == ["−√(2)", "0", "√(2)"])177        #expect(r.criticalPoints.map(\.classification) == [.localMinimum, .localMaximum, .localMinimum])178        #expect(pretty(r.globalMinimum?.valueExpression) == "−4" && r.globalMaximum?.value == 45)179        let flat = Symbolic.optimize(P("x^3"), variable: "x", from: P("-1"), to: P("1"))180        #expect(flat.criticalPoints.count == 1 && flat.criticalPoints[0].classification == .inflection)181        #expect(flat.globalMinimum?.value == -1 && flat.globalMaximum?.value == 1)182    }183    @Test func numericalCriticalPointsAreLabelled() {184        let r = Symbolic.optimize(P("x*e^(-x) + sin(3x)/5"), variable: "x", from: P("0"), to: P("4"))185        #expect(r.kind == .numerical && r.criticalPoints.count == 4 && r.criticalPoints.allSatisfy { $0.kind == .numerical && $0.location == nil })186        #expect(abs((r.globalMaximum?.x ?? 0) - 0.633605142933) < 1e-9)187        let periodic = Symbolic.optimize(P("sin(x)"), variable: "x", from: P("0"), to: P("2*pi"))188        #expect(periodic.criticalPoints.count == 2 && abs((periodic.globalMaximum?.value ?? 0) - 1) < 1e-12 && abs((periodic.globalMinimum?.value ?? 0) + 1) < 1e-12)189        #expect(Symbolic.optimize(P("1/x"), variable: "x", from: P("-1"), to: P("1")).kind == .unsupported)190        #expect(Symbolic.optimize(P("a*x^2"), variable: "x", from: P("-1"), to: P("1")).kind == .unsupported)191    }192    @Test func multivariateMinimisation() {193        let rosenbrock = Symbolic.minimize(P("(1-x)^2+100(y-x^2)^2"), variables: ["x", "y"], start: [-1.2, 1])194        #expect(rosenbrock.kind == .numerical && abs(rosenbrock.point[0] - 1) < 1e-6 && abs(rosenbrock.point[1] - 1) < 1e-6)195        #expect(rosenbrock.gradientNorm < 1e-6 && rosenbrock.verification.isVerified && rosenbrock.value < 1e-12)196        let quadratic = Symbolic.minimize(P("(x-1)^2 + 2(y+2)^2 + (z-3)^2 + x*y"), variables: ["x", "y", "z"], start: [0, 0, 0])197        // gradient zero: 2(x−1) + y = 0, 4(y+2) + x = 0, z = 3  ⇒  x = 16/7, y = −18/7198        #expect(abs(quadratic.point[0] - 16.0 / 7) < 1e-7 && abs(quadratic.point[1] + 18.0 / 7) < 1e-7 && abs(quadratic.point[2] - 3) < 1e-7)199        let maximum = Symbolic.maximize(P("e^(-(x-1)^2-(y+1)^2)"), variables: ["x", "y"], start: [0, 0])200        #expect(abs(maximum.value - 1) < 1e-10 && abs(maximum.point[0] - 1) < 1e-5 && abs(maximum.point[1] + 1) < 1e-5)201        #expect(Symbolic.minimize(P("x+y"), variables: ["x", "y"], start: [0, 0]).kind == .failedConvergence)        // unbounded below202        #expect(Symbolic.minimize(P("x^2"), variables: ["x", "y"], start: [0]).kind == .unsupported)203    }204}205206@Suite struct DifferentialEquations {207    @Test func exponentialDecay() {208        let r = Symbolic.solveODE([P("-2y")], states: ["y"], initial: [1], from: 0, to: 3)209        #expect(r.kind == .numerical)210        #expect(abs(r.finalState[0] - exp(-6.0)) < 1e-9)211        // the whole trajectory stays within the tolerance of the closed form212        for (t, y) in zip(r.times, r.values) { #expect(abs(y[0] - exp(-2 * t)) < 2e-8) }213        #expect(r.statistics.acceptedSteps > 5 && r.statistics.evaluations >= 6 * r.statistics.acceptedSteps)214        #expect(r.verification.isVerified && r.finalErrorEstimate[0] < 1e-8)215        #expect(abs((r.value(of: "y", at: 1.0) ?? 0) - exp(-2.0)) < 1e-3)          // linear interpolation between steps216    }217    @Test func toleranceControlsTheError() {218        var previous = Double.infinity219        for tol in [1e-4, 1e-6, 1e-8, 1e-10] {220            let r = Symbolic.solveODE([P("y*cos(t)")], states: ["y"], initial: [1], from: 0, to: 5, method: .dormandPrince(relativeTolerance: tol, absoluteTolerance: tol * 1e-2))221            let error = abs(r.finalState[0] - exp(sin(5.0)))222            #expect(error < 50 * tol, "tolerance \(tol): error \(error)")223            #expect(error <= previous)224            previous = error225        }226    }227    @Test func harmonicOscillatorEnergyDrift() {228        let r = Symbolic.solveSecondOrderODE(P("-y"), initialValue: 1, initialDerivative: 0, from: 0, to: 100)229        #expect(r.kind == .numerical && r.states == ["y", "v"])230        #expect(abs(r.finalState[0] - cos(100.0)) < 1e-6 && abs(r.finalState[1] + sin(100.0)) < 1e-6)231        var drift = 0.0232        for y in r.values { drift = max(drift, abs(0.5 * (y[0] * y[0] + y[1] * y[1]) - 0.5)) }233        #expect(drift < 1e-6, "energy drift \(drift) over about 16 periods")234    }235    @Test func rk4ConvergesWithOrderFour() {236        func error(_ n: Int) -> Double {237            let r = Symbolic.solveSecondOrderODE(P("-y"), initialValue: 1, initialDerivative: 0, from: 0, to: 10, method: .rk4(steps: n))238            return abs(r.finalState[0] - cos(10.0))239        }240        let coarse = error(200), fine = error(400)241        #expect(coarse < 1e-5 && fine < coarse)242        let order = log2(coarse / fine)243        #expect(order > 3.7 && order < 4.3, "observed order \(order)")244        let r = Symbolic.solveODE([P("-2y")], states: ["y"], initial: [1], from: 0, to: 1, method: .rk4(steps: 100))245        #expect(r.times.count == 101 && r.statistics.acceptedSteps == 100)246        // the reported error estimate is of the right size247        let actual = abs(r.finalState[0] - exp(-2.0))248        #expect(r.finalErrorEstimate[0] > actual / 3 && r.finalErrorEstimate[0] < actual * 3)249    }250    @Test func logisticGrowth() {251        // y′ = r·y·(1 − y/K), y(0) = y0  ⇒  y = K/(1 + (K/y0 − 1)·e^(−r·t))252        let r = Symbolic.solveODE([P("0.8*p*(1 - p/50)")], states: ["p"], initial: [2], from: 0, to: 12)253        for (t, y) in zip(r.times, r.values) {254            let exact = 50 / (1 + (50.0 / 2 - 1) * exp(-0.8 * t))255            #expect(abs(y[0] - exact) < 1e-6 * exact)256        }257        let backwards = Symbolic.solveODE([P("0.8*p*(1 - p/50)")], states: ["p"], initial: [r.finalState[0]], from: 12, to: 0)258        // integrating backwards from the stable equilibrium amplifies errors by e^(0.8·12) ≈ 1.5·10⁴259        #expect(abs(backwards.finalState[0] - 2) < 1e-4)260    }261    @Test func coupledSystemAndTimeDependence() {262        // rotation: x′ = −y, y′ = x and a forced equation z′ = t263        let r = Symbolic.solveODE([P("-y"), P("x"), P("t")], states: ["x", "y", "z"], initial: [1, 0, 0], from: 0, to: 2)264        #expect(abs(r.finalState[0] - cos(2.0)) < 1e-8 && abs(r.finalState[1] - sin(2.0)) < 1e-8 && abs(r.finalState[2] - 2) < 1e-8)265    }266    @Test func failuresAreReportedNotHidden() {267        let blowUp = Symbolic.solveODE([P("y^2")], states: ["y"], initial: [1], from: 0, to: 2)      // y = 1/(1 − t) blows up at t = 1268        #expect(blowUp.kind == .failedConvergence && blowUp.note.contains("step size underflowed"))269        #expect((blowUp.times.last ?? 0) < 1.0001 && (blowUp.times.last ?? 0) > 0.99 && blowUp.verification.status == .failed)270        let rk = Symbolic.solveODE([P("y^2")], states: ["y"], initial: [1], from: 0, to: 2, method: .rk4(steps: 50))271        #expect(rk.kind == .failedConvergence)272        #expect(Symbolic.solveODE([P("-k*y")], states: ["y"], initial: [1], from: 0, to: 1).kind == .unsupported)        // free parameter273        #expect(Symbolic.solveODE([P("-y")], states: ["y", "z"], initial: [1], from: 0, to: 1).kind == .unsupported)274        #expect(Symbolic.solveODE([P("1/y")], states: ["y"], initial: [0], from: 0, to: 1).kind == .failedConvergence)275    }276}277278@Suite struct RandomisedSimplification {279    /// Random expression trees: the simplifier must be idempotent and must preserve the value wherever the input is defined.280    @Test func randomExpressions() {281        var rng = SeededGenerator(seed: 4242)282        func pick(_ n: Int) -> Int { Int(rng.uniform(0, Double(n))) }283        func leaf() -> Expr {284            switch pick(7) {285            case 0, 1: return .variable("x")286            case 2: return .variable("y")287            case 3: return .rational(pick(7) - 3, pick(4) + 1)288            case 4: return .variable("pi")289            default: return .int(pick(9) - 4)290            }291        }292        func tree(_ depth: Int) -> Expr {293            if depth == 0 || pick(5) == 0 { return leaf() }294            switch pick(12) {295            case 0, 1: return .binary(.add, tree(depth - 1), tree(depth - 1))296            case 2: return .binary(.subtract, tree(depth - 1), tree(depth - 1))297            case 3, 4: return .binary(.multiply, tree(depth - 1), tree(depth - 1))298            case 5: return .binary(.divide, tree(depth - 1), tree(depth - 1))299            case 6: return .binary(.power, tree(depth - 1), .int(pick(5) - 1))300            case 7: return .binary(.power, tree(depth - 1), .rational(1, 2))301            case 8: return .unary(.negate, tree(depth - 1))302            default: return .call(["sin", "cos", "exp", "ln", "sqrt", "abs", "atan", "tan"][pick(8)], [tree(depth - 1)])303            }304        }305        var compared = 0, simplified = 0306        for _ in 0..<600 {307            let e = tree(4)308            let r = Symbolic.simplify(e)309            guard let s = r.expression else {310                #expect(r.kind == .unsupported)                       // e.g. an explicit division by zero311                continue312            }313            simplified += 1314            let again = Symbolic.simplify(s)315            #expect(again.expression == s, "not idempotent: \(Printer.string(e, style: .ascii)) → \(Printer.string(s, style: .ascii)) → \(again.expression.map { Printer.string($0, style: .ascii) } ?? "nil")")316            for _ in 0..<4 {317                let point = ["x": rng.uniform(0.2, 2.5), "y": rng.uniform(-2, 2)]318                let before = value(e, point), after = value(s, point)319                guard before.isFinite, abs(before) < 1e8 else { continue }320                compared += 1321                #expect(close(before, after, 1e-7), "value changed: \(Printer.string(e, style: .ascii)) = \(before) but \(Printer.string(s, style: .ascii)) = \(after) at \(point)")322            }323        }324        #expect(simplified > 400 && compared > 800)325    }326    @Test func derivativesOfRandomExpressionsMatchNumericalDifferentiation() {327        var rng = SeededGenerator(seed: 777)328        func pick(_ n: Int) -> Int { Int(rng.uniform(0, Double(n))) }329        func tree(_ depth: Int) -> Expr {330            if depth == 0 || pick(6) == 0 { return pick(3) == 0 ? .int(pick(5) + 1) : .variable("x") }331            switch pick(8) {332            case 0, 1: return .binary(.add, tree(depth - 1), tree(depth - 1))333            case 2, 3: return .binary(.multiply, tree(depth - 1), tree(depth - 1))334            case 4: return .binary(.divide, tree(depth - 1), .binary(.add, .int(2), .binary(.power, tree(depth - 1), .int(2))))335            case 5: return .binary(.power, tree(depth - 1), .int(pick(3) + 1))336            default: return .call(["sin", "cos", "exp", "atan", "sinh", "tanh"][pick(6)], [tree(depth - 1)])337            }338        }339        var checked = 0340        for _ in 0..<150 {341            let e = tree(3)342            let r = Symbolic.differentiate(e, variable: "x")343            guard let d = r.expression else { Issue.record("d/dx \(Printer.string(e, style: .ascii)): \(r.note)"); continue }344            let x0 = rng.uniform(-1, 1)345            guard let f = try? CompiledExpression(e, variables: ["x"]) else { continue }346            let numeric = Differentiation.derivative({ f.evaluate([$0]) }, at: x0, step: 1e-3)347            let symbolic = value(d, ["x": x0])348            guard numeric.converged, symbolic.isFinite, abs(symbolic) < 1e6 else { continue }349            checked += 1350            #expect(abs(numeric.value - symbolic) <= 1e-6 * max(1, abs(symbolic)), "d/dx \(Printer.string(e, style: .ascii)) at \(x0): symbolic \(symbolic), numerical \(numeric.value)")351        }352        #expect(checked > 100)353    }354}355
Files main
Menu

Theme