SPB Git

Repository Public MIT

Support.swift 7.3 KB 156 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/Support.swift10// Description      : Test helpers — parsing, numerical evaluation of results,11//                    and the sympy / mpmath reference file.12// Module           : ORIXSymbolicTests13// Repository       : https://git.spboucher.ai/orix-calc14// Created          : 2026-11-2015// Last Modified    : 2026-11-2016//17// =============================================================================18// AUTHOR & PROFESSIONAL AFFILIATION19// =============================================================================20//21// Author           : Simon-Pierre Boucher22// Position         : Professor of Finance23// Institution      : Université du Québec en Outaouais (UQO)24// Department       : Département des sciences administratives25// Organization     : SP Boucher26//27// Website          : https://spboucher.ai28// Git              : https://git.spboucher.ai29//30// =============================================================================31// OFFICIAL CONTACT DIRECTORY32// =============================================================================33//34// General          : info@spboucher.ai35// Professional     : contact@spboucher.ai36// Direct           : simon@spboucher.ai37// Research         : research@spboucher.ai38// Development      : dev@spboucher.ai39// Support          : support@spboucher.ai40// Security/Admin   : admin@spboucher.ai41// Notifications    : notifications@spboucher.ai42//43// Preferred Contact: support@spboucher.ai44//45// =============================================================================46// COPYRIGHT & INTELLECTUAL PROPERTY47// =============================================================================48//49// Copyright (c) 202650// Simon-Pierre Boucher51//52// All rights reserved where applicable.53//54// This file is governed by the applicable project license,55// copyright notices, institutional agreements and relevant law.56//57// Unless expressly authorized by the applicable license or58// rights holder, copying, redistribution, sublicensing,59// publication and commercial exploitation are not permitted.60//61// Existing third-party rights and notices remain unaffected.62//63// =============================================================================64// CONFIDENTIALITY & SECURITY65// =============================================================================66//67// Classification   : Public68// Distribution     : Public repository; use is governed by the project license69// Security Contact : admin@spboucher.ai70//71// Access to this source does not independently grant permission72// to redistribute or disclose confidential materials.73//74// Do not embed credentials, private keys, access tokens,75// certificates or confidential personal information.76//77// =============================================================================78// LICENSE & MAINTENANCE79// =============================================================================80//81// License          : Proprietary, all rights reserved (see license file)82// License File     : LICENSE83// Maintainer       : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Foundation91import BigInt92import ORIXExpression93@testable import ORIXSymbolic9495func P(_ s: String) -> Expr {96    do { return try Parser.parse(s) } catch { fatalError("cannot parse \(s): \(error)") }97}98func pretty(_ e: Expr?) -> String { e.map { Printer.string($0) } ?? "nil" }99100/// Value of an expression at a point (radians). NaN when it cannot be evaluated.101func value(_ e: Expr, _ point: [String: Double] = [:]) -> Double {102    let names = point.keys.sorted()103    guard let c = try? CompiledExpression(e, variables: names) else { return .nan }104    return c.evaluate(names.map { point[$0]! })105}106func close(_ a: Double, _ b: Double, _ tolerance: Double = 1e-9) -> Bool {107    a.isFinite && b.isFinite && abs(a - b) <= tolerance * max(1, abs(a), abs(b))108}109func rational(_ text: String) -> BigRational {110    let parts = text.split(separator: "/")111    return parts.count == 2 ? BigRational(BigInt(parts[0])!, BigInt(parts[1])!) : BigRational(BigInt(parts[0])!)112}113114struct Reference: Decodable {115    struct Sample: Decodable { var point: [String: Double]; var value: Double }116    struct Derivative: Decodable { var expr: String; var variable: String; var order: Int; var sympy: String; var samples: [Sample] }117    struct IntegralSample: Decodable { var x: Double; var value: Double }118    struct Integral: Decodable { var expr: String; var variable: String; var parameters: [String: Double]; var base: Double; var sympy: String; var samples: [IntegralSample] }119    struct Definite: Decodable { var expr: String; var from: String; var to: String; var value: Double; var sympy: String }120    struct Simplify: Decodable { var expr: String; var variables: [String]; var samples: [Sample] }121    struct Limit: Decodable { var expr: String; var point: String; var direction: String; var kind: String; var value: Double?; var sympy: String }122    struct Series: Decodable { var expr: String; var point: String; var order: Int; var coefficients: [Double?]; var sympy: [String] }123    struct Equation: Decodable { var equation: String; var real_roots: [Double]; var complex_count: Int; var degree: Int }124    struct Division: Decodable { var a: String; var b: String; var quotient: [String]; var remainder: [String]; var gcd: [String] }125    struct FactorCase: Decodable {126        struct Part: Decodable {127            var coefficients: [String]; var multiplicity: Int128            init(from decoder: Decoder) throws {129                var c = try decoder.unkeyedContainer()130                coefficients = try c.decode([String].self); multiplicity = try c.decode(Int.self)131            }132        }133        var expr: String; var content: String; var factors: [Part]; var count: Int; var sympy: String134    }135    struct Polynomials: Decodable { var division: [Division]; var factor: [FactorCase] }136    struct SumSample: Decodable { var n: Int; var value: Double? }137    struct Sum: Decodable { var term: String; var from: String; var to: String; var sympy: String; var samples: [SumSample] }138139    var generator: String140    var derivatives: [Derivative]141    var integrals: [Integral]142    var definite: [Definite]143    var simplify: [Simplify]144    var limits: [Limit]145    var series: [Series]146    var equations: [Equation]147    var polynomials: Polynomials148    var sums: [Sum]149150    static let shared: Reference = {151        guard let url = Bundle.module.url(forResource: "reference", withExtension: "json", subdirectory: "Resources"),152              let data = try? Data(contentsOf: url) else { fatalError("reference.json is missing from the test bundle") }153        do { return try JSONDecoder().decode(Reference.self, from: data) } catch { fatalError("reference.json: \(error)") }154    }()155}156
Files main
Menu

Theme