<commit_before>use Test::More;

require_ok 'Math::MatrixReal';
require_ok 'Algorithm::QuasiNewton';

my $f = sub {
   my $vector = shift;
   my $x = $vector->element(1,1);
   my $y = $vector->element(2,1);
   my $z = $vector->element(3,1);

   return ($x - 1) * ($x - 1) + ($y - 2) * ($y - 2) + ($z - 3) * ($z - 3);

};

my $df = sub {
   my $vector = shift;
   my $x = $vector->element(1,1);
   my $y = $vector->element(2,1);
   my $z = $vector->element(3,1);

   return Math::MatrixReal->new_from_cols([ [2 * $x - 2, 2 * $y - 4, 2 * $z - 6] ]);

};

my $init_param = Math::MatrixReal->new_from_cols([ [0,0,0] ]);
my $newton = Algorithm::QuasiNewton->new(f => $f, df => $df, x => $init_param);
my $result = $newton->run();

is(sprintf(".6f",$result->element(1,1)),sprintf(".6f",1.0));
is(sprintf(".6f",$result->element(2,1)),sprintf(".6f",2.0));
is(sprintf(".6f",$result->element(3,1)),sprintf(".6f",3.0));

done_testing();
<commit_msg>Add a test for BFGS<commit_after>use Test::More;

require_ok 'Math::MatrixReal';
require_ok 'Algorithm::QuasiNewton';

my $f = sub {
   my $vector = shift;
   my $x = $vector->element(1,1);
   my $y = $vector->element(2,1);
   my $z = $vector->element(3,1);

   return ($x - 1) * ($x - 1) + ($y - 2) * ($y - 2) + ($z - 3) * ($z - 3);

};

my $df = sub {
   my $vector = shift;
   my $x = $vector->element(1,1);
   my $y = $vector->element(2,1);
   my $z = $vector->element(3,1);

   return Math::MatrixReal->new_from_cols([ [2 * $x - 2, 2 * $y - 4, 2 * $z - 6] ]);

};

my $init_param = Math::MatrixReal->new_from_cols([ [0,0,0] ]);
my $newton = Algorithm::QuasiNewton->new(f => $f, df => $df, x => $init_param, mode => 'lbfgs');
my $result = $newton->run();

is(sprintf(".6f",$result->element(1,1)),sprintf(".6f",1.0));
is(sprintf(".6f",$result->element(2,1)),sprintf(".6f",2.0));
is(sprintf(".6f",$result->element(3,1)),sprintf(".6f",3.0));

my $init_param = Math::MatrixReal->new_from_cols([ [0,0,0] ]);
my $newton = Algorithm::QuasiNewton->new(f => $f, df => $df, x => $init_param, mode => 'bfgs');
my $result = $newton->run();

is(sprintf(".6f",$result->element(1,1)),sprintf(".6f",1.0));
is(sprintf(".6f",$result->element(2,1)),sprintf(".6f",2.0));
is(sprintf(".6f",$result->element(3,1)),sprintf(".6f",3.0));

done_testing();

{'fraction_non_alphanumeric': 0.27665198237885463, 'fraction_numerical': 0.04889867841409692, 'mean_word_length': 4.269141531322505, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4748624', 'n_tokens_mistral': 993, 'n_tokens_neox': 975, 'n_words': 184}