Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Calc9// File : Packages/ORIXNumerics/Tests/ORIXNumericsTests/NumericsTests.swift10// Description : Reference tests for root finding, quadrature and11// differentiation.12// Module : ORIXNumericsTests13// 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 Testing92@testable import ORIXNumerics9394@Suite struct RootFindingTests {95 @Test func brentCubic() throws {96 let r = try RootFinder.brent({ $0 * $0 * $0 - 2 * $0 - 5 }, 2, 3)97 #expect(abs(r.root - 2.0945514815423265) < 1e-13)98 }99 @Test func brentRejectsUnbracketed() {100 #expect(throws: RootFindingError.self) { try RootFinder.brent({ $0 * $0 + 1 }, -1, 1) }101 }102 @Test func allRootsOfSine() {103 let r = RootFinder.allRoots({ sin($0) }, in: -7, 7)104 #expect(r.count == 5)105 for (a, b) in zip(r, [-2 * Double.pi, -Double.pi, 0, Double.pi, 2 * Double.pi]) { #expect(abs(a - b) < 1e-10) }106 }107 @Test func polesAreNotRoots() {108 #expect(RootFinder.allRoots({ 1 / $0 }, in: -1, 1.3).isEmpty)109 let r = RootFinder.allRoots({ tan($0) }, in: -2, 2)110 #expect(r.count == 1 && abs(r[0]) < 1e-10)111 }112 @Test func touchingRoot() {113 let r = RootFinder.allRoots({ ($0 - 1) * ($0 - 1) }, in: -3, 4.1)114 #expect(r.count == 1 && abs(r[0] - 1) < 1e-5)115 }116 @Test func newton() throws {117 let r = try RootFinder.newton({ exp($0) - 3 }, guess: 0)118 #expect(abs(r.root - log(3.0)) < 1e-12)119 }120}121122@Suite struct CalculusTests {123 @Test func gaussian() {124 let r = Quadrature.integrate({ exp(-$0 * $0) }, from: -.infinity, to: .infinity)125 #expect(abs(r.value - sqrt(Double.pi)) < 1e-10 && r.converged)126 }127 @Test func polynomialAndReversed() {128 #expect(abs(Quadrature.integrate({ $0 * $0 }, from: 0, to: 3).value - 9) < 1e-12)129 #expect(abs(Quadrature.integrate({ $0 * $0 }, from: 3, to: 0).value + 9) < 1e-12)130 }131 @Test func oscillatory() {132 let r = Quadrature.integrate({ sin(50 * $0) }, from: 0, to: 1)133 #expect(abs(r.value - (1 - cos(50.0)) / 50) < 1e-11)134 }135 @Test func halfInfinite() {136 let r = Quadrature.integrate({ 1 / (1 + $0 * $0) }, from: 0, to: .infinity)137 #expect(abs(r.value - Double.pi / 2) < 1e-9)138 }139 @Test func endpointSingularities() {140 #expect(abs(Quadrature.tanhSinh({ log($0) }, from: 0, to: 1).value + 1) < 1e-10)141 #expect(abs(Quadrature.tanhSinh({ 1 / sqrt($0) }, from: 0, to: 1).value - 2) < 1e-8)142 }143 @Test func derivatives() {144 #expect(abs(Differentiation.derivative({ sin($0) }, at: 1).value - cos(1.0)) < 1e-10)145 #expect(abs(Differentiation.derivative({ exp($0) }, at: 0).value - 1) < 1e-10)146 #expect(abs(Differentiation.secondDerivative({ $0 * $0 * $0 }, at: 2).value - 12) < 1e-5)147 }148}149