Algorithm-CurveFit-Simple
view release on metacpan or search on metacpan
bin/curvefit view on Meta::CPAN
#!/usr/bin/perl
# Copyright (C) 2018 TTK Ciar
# Available for unlimited distribution and use.
# The copyright is just so someone else cannot claim ownership and sue me for use of my own code.
our $VERSION = '1.03'; # VERSION 1.02
use strict;
use warnings;
use JSON::PP;
use lib "./lib";
use Algorithm::CurveFit::Simple qw(fit);
my @DOCS;
my %OPT = (v => 1);
foreach my $arg (@ARGV) {
if ($arg =~ /^\-+(.+?)\=(.*)/) { $OPT{$1} = $2; }
elsif ($arg =~ /^\-+(v+)$/ ) { $OPT{v} = length($1) + 1; }
elsif ($arg =~ /^\-+q$/ ) { $OPT{v} = 0; }
elsif ($arg =~ /^\-+quiet$/ ) { $OPT{v} = 0; }
elsif ($arg =~ /^\-+(.+)/ ) { $OPT{$1} = -1; }
#!/bin/env perl
use strict;
use warnings;
use Test::Most;
use Time::HiRes;
use JSON::PP;
use lib "./lib";
use Algorithm::CurveFit::Simple qw(fit %STATS_H);
# Pass an argument for profiling purposes, will also dump source code string and statistics
my $N_TERMS = int($ARGV[0] // 0) || '3';
my $LANG = $ARGV[1] // 'perl';
my $tm0 = Time::HiRes::time();
my ($max_dev, $avg_dev, $src) = eval { fit(terms => $N_TERMS, xydata => [[1, 3], [2, 7], [3, 11], [4, 19], [5, 35], [6, 54], [7, 69], [8, 81], [9, 90], [10, 96]], impl_lang => $LANG); };
print "# exception: $@\n" if ($@);
my $tm_elapsed = Time::HiRes::time() - $tm0;
t/01-init-formula.t view on Meta::CPAN
#!/bin/env perl
use strict;
use warnings;
use Test::Most;
use JSON::PP;
use lib "./lib";
use Algorithm::CurveFit::Simple qw(%STATS_H);
my $f = eval { Algorithm::CurveFit::Simple::_init_formula(); };
print "# exception: $@\n" if ($@);
ok !$@, "default - no thrown exceptions";
ok defined($f), "default - returned a defined value";
$f //= '';
is ref($f), '', "default - returned string";
ok $f =~ /^k (\+ \w\s?\*\s?x\^?\d*\s*)+$/, "default - formula is well-formed" if($f);
t/02-init-data.t view on Meta::CPAN
#!/bin/env perl
use strict;
use warnings;
use Test::Most;
use JSON::PP;
use lib "./lib";
use Algorithm::CurveFit::Simple;
my ($x, $y) = eval { Algorithm::CurveFit::Simple::_init_data(); };
is $@, "must provide at least xydata or both xdata and ydata\n", "exception thrown for no parameters";
($x, $y) = eval { Algorithm::CurveFit::Simple::_init_data(xdata => [1, 2, 3]); };
is $@, "must provide at least xydata or both xdata and ydata\n", "exception thrown for lacking ydata";
($x, $y) = eval { Algorithm::CurveFit::Simple::_init_data(ydata => [1, 2, 3]); };
is $@, "must provide at least xydata or both xdata and ydata\n", "exception thrown for lacking xdata";
t/03-init-parameters.t view on Meta::CPAN
#!/bin/env perl
use strict;
use warnings;
use Test::Most;
use JSON::PP;
use lib "./lib";
use Algorithm::CurveFit::Simple;
my $p = eval { Algorithm::CurveFit::Simple::_init_parameters([3, 5, 7, 9], [1, 2, 3, 4]); };
is_deeply $p, [["k", 2.5, 1e-07], ["a", 0.5, 1e-07], ["b", 0.5, 1e-07], ["c", 0.5, 1e-07]], 'default parameters';
$p = eval { Algorithm::CurveFit::Simple::_init_parameters([3, 5, 7, 9], [1, 2, 3, 4], terms => 2); };
is_deeply $p, [["k", 2.5, 1e-07], ["a", 0.5, 1e-07], ["b", 0.5, 1e-07]], 'explicit term parameters';
if ($ARGV[0]) {
print JSON::PP::encode_json(\%Algorithm::CurveFit::Simple::STATS_H)."\n";
t/04-implement-formula.t view on Meta::CPAN
#!/bin/env perl
use strict;
use warnings;
use Test::Most;
use JSON::PP;
use lib "./lib";
use Algorithm::CurveFit::Simple;
my $parar = [["k", 10, 1], ["a", 2, 1], ["b", 3, 1], ["c", 4, 1]];
my $xdata = [8, 3, 5, 11, 9, 7, 4];
my $s = eval { Algorithm::CurveFit::Simple::_implement_formula($parar, "", "x2y", $xdata, {}); };
ok defined($s), "implementation default throws no exceptions";
ok $s =~ /^sub x2y /, "implementation default is perl";
$s = eval { Algorithm::CurveFit::Simple::_implement_formula($parar, "coderef", "x2y", $xdata, {}); };
( run in 0.269 second using v1.01-cache-2.11-cpan-87723dcf8b7 )