Ancient

 view release on metacpan or  search on metacpan

t/7028-nvec-numerical-validation.t  view on Meta::CPAN

#!/usr/bin/env perl
# Test nvec numerical validation with known mathematical identities
# These tests validate correctness across precision levels
use strict;
use warnings;
use Test::More;
use lib 'blib/lib', 'blib/arch', 't/lib';

use nvec;
use TestVec qw(is_quadmath approx_eq vec_approx_eq get_tolerance within_tolerance);

my $is_quad = is_quadmath();
if ($is_quad) {
    diag("Testing with quadmath (128-bit precision)");
} else {
    diag("Testing with standard double precision (64-bit)");
}

my $tol = get_tolerance();
my $strict_tol = get_tolerance(1);
my $pi = 3.14159265358979323846;

# ============================================
# Mathematical identities
# ============================================

subtest 'identity: Pythagorean theorem' => sub {
    # a^2 + b^2 = c^2 for right triangles
    my @triangles = (
        [3, 4, 5],
        [5, 12, 13],
        [8, 15, 17],
        [7, 24, 25],
    );

    for my $t (@triangles) {
        my ($a, $b, $c) = @$t;
        my $v = nvec::new([$a, $b]);
        my $hyp = $v->norm();
        ok(approx_eq($hyp, $c, $tol), "Pythagorean: $a^2 + $b^2 = $c^2");
    }
};

subtest 'identity: sum of arithmetic series' => sub {
    # Sum of 1 to n = n(n+1)/2
    for my $n (10, 100, 1000, 10000) {
        my $v = nvec::range(1, $n + 1);
        my $sum = $v->sum();
        my $expected = $n * ($n + 1) / 2;
        ok(approx_eq($sum, $expected, $tol), "sum(1..$n) = $expected");
    }
};

subtest 'identity: sum of squares' => sub {
    # Sum of 1^2 to n^2 = n(n+1)(2n+1)/6
    for my $n (10, 100, 500) {
        my $v = nvec::range(1, $n + 1)->pow(2);
        my $sum = $v->sum();
        my $expected = $n * ($n + 1) * (2 * $n + 1) / 6;
        within_tolerance($sum, $expected, "sum of squares 1..$n");
    }
};



( run in 1.195 second using v1.01-cache-2.11-cpan-13bb782fe5a )