File: MyOEIS.pm

package info (click to toggle)
libmath-planepath-perl 117-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,988 kB
  • ctags: 5,587
  • sloc: perl: 99,131; ansic: 299; sh: 233; lisp: 73; makefile: 4
file content (793 lines) | stat: -rw-r--r-- 21,620 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
# Copyright 2010, 2011, 2012, 2013, 2014 Kevin Ryde

# MyOEIS.pm is shared by several distributions.
#
# MyOEIS.pm is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# MyOEIS.pm is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this file.  If not, see <http://www.gnu.org/licenses/>.

package MyOEIS;
use strict;
use Carp 'croak';
use File::Spec;
use List::Util 'sum';

# uncomment this to run the ### lines
# use Smart::Comments;

my $without;

sub import {
  shift;
  foreach (@_) {
    if ($_ eq '-without') {
      $without = 1;
    } else {
      die __PACKAGE__." unknown option $_";
    }
  }
}

sub read_values {
  my ($anum, %option) = @_;
  ### read_values() ...

  if ($without) {
    return;
  }

  my $i_start;
  my $filename;
  my $next;
  if (my $seq = eval { require Math::NumSeq::OEIS::File;
                       Math::NumSeq::OEIS::File->new (anum => $anum) }) {
    ### from seq ...
    $next = sub {
      my ($i, $value) = $seq->next;
      return $value;
    };
    $filename = $seq->{'filename'};
    $i_start = $seq->i_start;
  } else {
    require Math::OEIS::Stripped;
    my @values = Math::OEIS::Stripped->anum_to_values($anum);
    if (! @values) {
      MyTestHelpers::diag ("$anum not available");
      return;
    }
    ### from stripped ...
    $next = sub {
      return shift @values;
    };
    $filename = Math::OEIS::Stripped->filename;
  }

  my $desc = $anum; # has ".scalar(@bvalues)." values";
  my @bvalues;
  for (;;) {
    my $value = &$next();
    if (! defined $value) {
      $desc .= " has ".scalar(@bvalues)." values";
      last;
    }
    if ((defined $option{'max_count'} && @bvalues >= $option{'max_count'})
       || (defined $option{'max_value'} && $value > $option{'max_value'})) {
      $desc .= " shortened to ".scalar(@bvalues)." values";
      last;
    }
    push @bvalues, $value;
  }
  if (@bvalues) {
    $desc .= " to $bvalues[-1]";
  }

  MyTestHelpers::diag ($desc);
  return (\@bvalues, $i_start, $filename);
}

# with Y reckoned increasing downwards
sub dxdy_to_direction {
  my ($dx, $dy) = @_;
  if ($dx > 0) { return 0; }  # east
  if ($dx < 0) { return 2; }  # west
  if ($dy > 0) { return 1; }  # south
  if ($dy < 0) { return 3; }  # north
}


sub compare_values {
  my %option = @_;
  require MyTestHelpers;
  my $anum = $option{'anum'} || croak "Missing anum parameter";
  my $func = $option{'func'} || croak "Missing func parameter";
  my ($bvalues, $lo, $filename) = MyOEIS::read_values
    ($anum,
     max_count => $option{'max_count'},
     max_value => $option{'max_value'});
  my $diff;
  if ($bvalues) {
    if (my $fixup = $option{'fixup'}) {
      &$fixup($bvalues);
    }
    my ($got,@rest) = &$func(scalar(@$bvalues));
    if (@rest) {
      croak "Oops, func return more than just an arrayref";
    }
    if (ref $got ne 'ARRAY') {
      croak "Oops, func return not an arrayref";
    }
    $diff = diff_nums($got, $bvalues);
    if ($diff) {
      MyTestHelpers::diag ("bvalues: ",join_values($bvalues));
      MyTestHelpers::diag ("got:     ",join_values($got));
    }
  }
  if (defined $Test::TestLevel) {
    require Test;
    local $Test::TestLevel = $Test::TestLevel + 1;
    Test::skip (! $bvalues, $diff, undef, "$anum");
  } elsif (defined $diff) {
    print "$diff\n";
  }
}

sub join_values {
  my ($aref) = @_;
  if (! @$aref) { return ''; }
  my $str = $aref->[0];
  foreach my $i (1 .. $#$aref) {
    my $value = $aref->[$i];
    if (! defined $value) { $value = 'undef'; }
    last if length($str)+1+length($value) >= 275;
    $str .= ',';
    $str .= $value;
  }
  return $str;
}

sub diff_nums {
  my ($gotaref, $wantaref) = @_;
  my $diff;
  for (my $i = 0; $i < @$gotaref; $i++) {
    if ($i > @$wantaref) {
      return "want ends prematurely pos=$i";
    }
    my $got = $gotaref->[$i];
    my $want = $wantaref->[$i];
    if (! defined $got && ! defined $want) {
      next;
    }
    if (defined $got != defined $want) {
      if (defined $diff) {
        return "$diff, and more diff";
      }
      $diff = "different pos=$i got=".(defined $got ? $got : '[undef]')
        ." want=".(defined $want ? $want : '[undef]');
    }
    unless ($got =~ /^[0-9.-]+$/) {
      if (defined $diff) {
        return "$diff, and more diff";
      }
      $diff = "not a number pos=$i got='$got'";
    }
    unless ($want =~ /^[0-9.-]+$/) {
      if (defined $diff) {
        return "$diff, and more diff";
      }
      $diff = "not a number pos=$i want='$want'";
    }
    if ($got != $want) {
      if (defined $diff) {
        return "$diff, and more diff";
      }
      $diff = "different pos=$i numbers got=$got want=$want";
    }
  }
  return $diff;
}

# counting from 1 for prime=2
sub ith_prime {
  my ($i) = @_;
  if ($i < 1) {
    croak "Oops, ith_prime() i=$i";
  }
  require Math::Prime::XS;
  my $to = 100;
  for (;;) {
    my @primes = Math::Prime::XS::primes($to);
    if (@primes >= $i) {
      return $primes[$i-1];
    }
    $to *= 2;
  }
}

#------------------------------------------------------------------------------

sub first_differences {
  my $prev = shift;
  return map { my $diff = $_-$prev; $prev = $_; $diff } @_;
}

#------------------------------------------------------------------------------
# unit square boundary

{
  my %lattice_type_to_dfunc = (square => \&path_n_to_dboundary,
                               triangular => \&path_n_to_dhexboundary);
  sub path_n_to_figure_boundary {
    my ($path, $n_end, %options) = @_;
    my $boundary = 0;
    my $dfunc = $lattice_type_to_dfunc{$options{'lattice_type'} || 'square'};
    foreach my $n ($path->n_start .. $n_end) {
      # print "$n  ",&$dfunc($path, $n),"\n";
      $boundary += &$dfunc($path, $n);
    }
    return $boundary;
  }
}

BEGIN {
  my @dir4_to_dx = (1,0,-1,0);
  my @dir4_to_dy = (0,1,0,-1);

  sub path_n_to_dboundary {
    my ($path, $n) = @_;
    my ($x,$y) = $path->n_to_xy($n) or return 0;
    {
      my @n_list = $path->xy_to_n_list($x,$y);
      if ($n > $n_list[0]) {
        return 0;
      }
    }
    my $dboundary = 4;
    foreach my $i (0 .. $#dir4_to_dx) {
      my $an = $path->xy_to_n($x+$dir4_to_dx[$i], $y+$dir4_to_dy[$i]);
      $dboundary -= 2*(defined $an && $an < $n);
    }
    return $dboundary;
  }
  sub path_n_to_dsticks {
    my ($path, $n) = @_;
    my ($x,$y) = $path->n_to_xy($n) or return 0;
    my $dsticks = 4;
    foreach my $i (0 .. $#dir4_to_dx) {
      my $an = $path->xy_to_n($x+$dir4_to_dx[$i], $y+$dir4_to_dy[$i]);
      $dsticks -= (defined $an && $an < $n);
    }
    return $dsticks;
  }
}


#------------------------------------------------------------------------------

# Return the area enclosed by the curve N=n_start() to N <= $n_limit.
#
# lattice_type => 'triangular'
#    Means take the six-way triangular lattice points as adjacent and
#    measure in X/2 and Y*sqrt(3)/2 so that the points are unit steps.
#
sub path_enclosed_area {
  my ($path, $n_limit, %options) = @_;
  ### path_enclosed_area() ...
  my $points = path_boundary_points($path, $n_limit, %options);
  ### $points
  if (@$points <= 2) {
    return 0;
  }
  require Math::Geometry::Planar;
  my $polygon = Math::Geometry::Planar->new;
  $polygon->points($points);
  return $polygon->area;
}

{
  my %lattice_type_to_divisor = (square => 1,
                                 triangular => 4);

  # Return the length of the boundary of the curve N=n_start() to N <= $n_limit.
  #
  # lattice_type => 'triangular'
  #    Means take the six-way triangular lattice points as adjacent and
  #    measure in X/2 and Y*sqrt(3)/2 so that the points are unit steps.
  #
  sub path_boundary_length {
    my ($path, $n_limit, %options) = @_;
    ### path_boundary_length(): "n_limit=$n_limit"

    my $points = path_boundary_points($path, $n_limit, %options);
    ### $points

    my $lattice_type = ($options{'lattice_type'} || 'square');
    my $triangular_mult = ($lattice_type eq 'triangular' ? 3 : 1);
    my $divisor = ($options{'divisor'} || $lattice_type_to_divisor{$lattice_type});
    my $side = ($options{'side'} || 'all');
    ### $divisor

    my $boundary = 0;
    foreach my $i (($side eq 'all' ? 0 : 1)
                   ..
                   $#$points) {
      ### hypot: ($points->[$i]->[0] - $points->[$i-1]->[0])**2 + $triangular_mult*($points->[$i]->[1] - $points->[$i-1]->[1])**2

      $boundary += sqrt(((  $points->[$i]->[0] - $points->[$i-1]->[0])**2
                         + $triangular_mult
                         * ($points->[$i]->[1] - $points->[$i-1]->[1])**2)
                        / $divisor);
    }
    ### $boundary
    return $boundary;
  }
}
{
  my @dir4_to_dxdy = ([1,0], [0,1], [-1,0], [0,-1]);
  my @dir6_to_dxdy = ([2,0], [1,1], [-1,1], [-2,0], [-1,-1], [1,-1]);
  my %lattice_type_to_dirtable = (square => \@dir4_to_dxdy,
                                  triangular => \@dir6_to_dxdy);

  # Return arrayref of points [ [$x,$y], ..., [$to_x,$to_y]]
  # which are the points on the boundary of the curve from $x,$y to
  # $to_x,$to_y inclusive.
  #
  # lattice_type => 'triangular'
  #    Means take the six-way triangular lattice points as adjacent.
  #
  sub path_boundary_points_ft {
    my ($path, $n_limit, $x,$y, $to_x,$to_y, %options) = @_;
    ### path_boundary_points_ft(): "$x,$y to $to_x,$to_y"
    ### $n_limit

    # my @dirtable = $path->_UNDOCUMENTED__dxdy_list; # $lattice_type_to_dirtable{$lattice_type};
    my $lattice_type = ($options{'lattice_type'} || 'square');
    my @dirtable = @{$lattice_type_to_dirtable{$lattice_type}};
    my $dirmod = scalar(@dirtable);
    my $dirrev = $dirmod / 2 - 1;
    ### @dirtable
    ### $dirmod
    ### $dirrev

    my $arms = $path->arms_count;
    my @points;
    my $dir = $options{'dir'} // 1;
    my @n_list;

    # FIXME: can be on boundary without having untraversed edge
    if (! defined $dir) {
      foreach my $i (0 .. $dirmod) {
        my ($dx,$dy) = @{$dirtable[$i]};
        if (! path_xyxy_is_traversed($path, $x,$y, $x+$dx,$y+$dy)) {
          $dir = $i;
          last;
        }
      }
      if (! defined $dir) {
        die "Oops, $x,$y apparently not on boundary";
      }
    }

  TOBOUNDARY: for (;;) {
      @n_list = $path->xy_to_n_list($x,$y)
        or die "Oops, no n_list at $x,$y";
      foreach my $i (1 .. $dirmod) {
        my $test_dir = ($dir + $i) % $dirmod;
        my ($dx,$dy) = @{$dirtable[$test_dir]};
        my @next_n_list = $path->xy_to_n_list($x+$dx,$y+$dy);
        if (! any_consecutive(\@n_list, \@next_n_list, $n_limit, $arms)) {
          ### is boundary: "dxdy = $dx,$dy  test_dir=$test_dir"
          $dir = ($test_dir + 1) % $dirmod;
          last TOBOUNDARY;
        }
      }
      my ($dx,$dy) = @{$dirtable[$dir]};
      if ($x == $to_x && $y == $to_y) {
        $to_x -= $dx;
        $to_y -= $dy;
      }
      $x -= $dx;
      $y -= $dy;
      ### towards boundary: "$x, $y"
    }

    ### initial: "dir=$dir  n_list=".join(',',@n_list)." seeking to_xy=$to_x,$to_y"

    for (;;) {
      ### at: "xy=$x,$y  n_list=".join(',',@n_list)
      push @points, [$x,$y];
      $dir = ($dir - $dirrev) % $dirmod;
      my $found = 0;
      foreach (1 .. $dirmod) {
        my ($dx,$dy) = @{$dirtable[$dir]};
        my @next_n_list = $path->xy_to_n_list($x+$dx,$y+$dy);
        ### consider: "dir=$dir  next_n_list=".join(',',@next_n_list)
        if (any_consecutive(\@n_list, \@next_n_list, $n_limit, $arms)) {
          ### yes, consecutive, go: "dir=$dir  dx=$dx,dy=$dy"
          @n_list = @next_n_list;
          $x += $dx;
          $y += $dy;
          $found = 1;
          last;
        }
        $dir = ($dir+1) % $dirmod;
      }
      if (! $found) {
        die "oops, direction of next boundary step not found";
      }

      if ($x == $to_x && $y == $to_y) {
        ### stop at: "$x,$y"
        unless ($x == $points[0][0] && $y == $points[0][1]) {
          push @points, [$x,$y];
        }
        last;
      }
    }
    return \@points;
  }
}

# Return arrayref of points [ [$x1,$y1], [$x2,$y2], ... ]
# which are the points on the boundary of the curve N=n_start() to N <= $n_limit
# The final point should be taken to return to the initial $x1,$y1.
#
# lattice_type => 'triangular'
#    Means take the six-way triangular lattice points as adjacent.
#
sub path_boundary_points {
  my ($path, $n_limit, %options) = @_;
  ### path_boundary_points(): "n_limit=$n_limit"
  ### %options

  my $x = 0;
  my $y = 0;
  my $to_x = $x;
  my $to_y = $y;
  if ($options{'side'} && $options{'side'} eq 'right') {
    ($to_x,$to_y) = $path->n_to_xy($n_limit);

  } elsif ($options{'side'} && $options{'side'} eq 'left') {
    ($x,$y) = $path->n_to_xy($n_limit);
  }
  return path_boundary_points_ft($path, $n_limit, $x,$y, $to_x,$to_y, %options);
}

# return true if line segment $x1,$y1 to $x2,$y2 is traversed,
# ie. consecutive N goes from $x1,$y1 to $x2,$y2, in either direction.
sub path_xyxy_is_traversed {
  my ($path, $x1,$y1, $x2,$y2) = @_;
  ### path_xyxy_is_traversed(): "$x1,$y1, $x2,$y2"
  my $arms = $path->arms_count;
  foreach my $n1 ($path->xy_to_n_list($x1,$y1)) {
    foreach my $n2 ($path->xy_to_n_list($x2,$y2)) {
      if (abs($n1-$n2) == $arms) {
        ### yes: "$n1 to $n2"
        return 1;
      }
    }
  }
  ### no ...
  return 0;
}

# $aref and $bref are arrayrefs of N values.
# Return true if any pair of values $aref->[a], $bref->[b] are consecutive.
# Values in the arrays which are > $n_limit are ignored.
sub any_consecutive {
  my ($aref, $bref, $n_limit, $arms) = @_;
  foreach my $a (@$aref) {
    next if $a > $n_limit;
    foreach my $b (@$bref) {
      next if $b > $n_limit;
      if (abs($a-$b) == $arms) {
        return 1;
      }
    }
  }
  return 0;
}

# Return the count of single points in the path from N=Nstart to N=$n_end
# inclusive.  Anything which happends beyond $n_end does not count, so a
# point which is doubled somewhere beyond $n_end is still reckoned as single.
#
sub path_n_to_singles {
  my ($path, $n_end) = @_;
  my $ret = 0;
  foreach my $n ($path->n_start .. $n_end) {
    my ($x,$y) = $path->n_to_xy($n) or next;
    my @n_list = $path->xy_to_n_list($x,$y);
    if (@n_list == 1
        || (@n_list == 2
            && $n == $n_list[0]
            && $n_list[1] > $n_end)) {
      $ret++;
    }
  }
  return $ret;
}

# Return the count of doubled points in the path from N=Nstart to N=$n_end
# inclusive.  Anything which happends beyond $n_end does not count, so a
# point which is doubled somewhere beyond $n_end is not reckoned as doubled
# here.
#
sub path_n_to_doubles {
  my ($path, $n_end) = @_;
  my $ret = 0;
  foreach my $n ($path->n_start .. $n_end) {
    my ($x,$y) = $path->n_to_xy($n) or next;
    my @n_list = $path->xy_to_n_list($x,$y);
    if (@n_list == 2
        && $n == $n_list[0]
        && $n_list[1] <= $n_end) {
      $ret++;
    }
  }
  return $ret;
}

# # Return true if the X,Y point at $n is visited only once.
# sub path_n_is_single {
#   my ($path, $n) = @_;
#   my ($x,$y) = $path->n_to_xy($n) or return 0;
#   my @n_list = $path->xy_to_n_list($x,$y);
#   return scalar(@n_list) == 1;
# }

# Return the count of distinct visited points in the path from N=Nstart to
# N=$n_end inclusive.
#
sub path_n_to_visited {
  my ($path, $n_end) = @_;
  my $ret = 0;
  foreach my $n ($path->n_start .. $n_end) {
    my ($x,$y) = $path->n_to_xy($n) or next;
    my @n_list = $path->xy_to_n_list($x,$y);
    if ($n_list[0] == $n) {  # relying on sorted @n_list
      $ret++;
    }
  }
  return $ret;
}

#------------------------------------------------------------------------------

sub gf_term {
  my ($gf_str, $i) = @_;
  my ($num,$den) = ($gf_str =~ m{(.*)/(.*)}) or die $gf_str;
  $num = Math::Polynomial->new(poly_parse($num));
  $den = Math::Polynomial->new(poly_parse($den));
  my $q;
  foreach (0 .. $i) {
    $q = $num->coeff(0) / $den->coeff(0);
    $num -= $q * $den;
    $num->coeff(0) == 0 or die;
  }
  return $q;
}
sub poly_parse {
  my ($str) = @_;
  ### poly_parse(): $str
  unless ($str =~ /^\s*[+-]/) {
    $str = "+ $str";
  }
  my @coeffs;
  my $end = 0;
  ### $str
  while ($str =~ m{\s*([+-])     # +/- between terms
                   (\s*(-?\d+))? # coefficient
                   ((\s*\*)?     # optional * multiplier
                     \s*x        # variable
                     \s*(\^\s*(\d+))?)?  # optional exponent
                   \s*
                }xg) {
    ### between: $1
    ### coeff  : $2
    ### x      : $4
    $end = pos($str);
    last if ! defined $2 && ! defined $4;
    my $coeff = (defined $2 ? $2 : 1);
    my $power = (defined $7 ? $7
                 : defined $4 ? 1
                 : 0);
    if ($1 eq '-') { $coeff = -$coeff; }
    $coeffs[$power] += $coeff;
    ### $coeff
    ### $power
    ### $end
  }
  ### final coeffs: @coeffs
  $end == length($str)
    or die "parse $str fail at pos=$end";
  foreach (@coeffs) { $_ ||= 0 }
  require Math::Polynomial;
  return Math::Polynomial->new(@coeffs);
}

#------------------------------------------------------------------------------
# boundary iterator

sub path_make_boundary_iterator {
  my ($path, %option) = @_;
  my $x = $option{'x'};
  my $y = $option{'y'};
  if (! defined $x) {
    ($x,$y) = $path->n_to_xy($path->n_start);
  }
  my $dir = $option{'dir'};
  if (! defined $dir) { $dir = 1; }
  my @n_list = $path->xy_to_n_list($x,$y);

  # my $dirmod = scalar(@$dirtable);
  # my $dirrev = $dirmod / 2 - 1;
  # ### $dirmod
  # ### $dirrev
  #
  # my $arms = $path->arms_count;
  # my @points;
  # my $dir = $options{'dir'} // 1;

  return sub {
    my $ret_x = $x;
    my $ret_y = $y;

    return ($ret_x,$ret_y);
  };
}


#------------------------------------------------------------------------------
# recurrence guess

# sub guess_recurrence {
#   my @values = @_;
#
#   require Math::Matrix;
# }

#------------------------------------------------------------------------------
# polynomial partial fractions
#

# $numerator / product(@denominators) is a polynomial fraction.
# Return a list of polynomials p1,p2,... which are numerators of partial
# fractions so
#
#      p1   p2                $numerator
#      -- + -- + ... =  ----------------------
#      d1   d2          product(@denominators)
#
sub polynomial_partial_fractions {
  my ($numerator, @denominators) = @_;
  ### denominators: "@denominators"

  my $total_degree = sum(map {$_->degree} @denominators);
  ### $total_degree
  ### numerator degree: $numerator->degree
  if ($numerator->degree >= $total_degree) {
    croak "Numerator degree should be less than total denominators";
  }

  require Math::Matrix;
  my $m = math_matrix_new_zero($total_degree);
  my @prods;

  {
    my $r = 0;
    foreach my $i (0 .. $#denominators) {
      my $degree = $denominators[$i]->degree;
      if ($degree < 0) {
        croak "Zero denominator";
      }

      # product of denominators excluding this $denominators[$i]
      my $prod = Math::Polynomial->new(1);
      foreach my $j (0 .. $#denominators) {
        if ($i != $j) {
          $prod *= $denominators[$j]
        }
      }
      push @prods, $prod;
      my $prod_degree = $prod->degree;
      ### prod: "$prod"
      ### $prod_degree

      foreach my $c (0 .. $degree-1) {
        foreach my $j (0 .. $prod_degree) {
          $m->[$r][$c+$j] += $prod->coeff($j);
        }
        $r++;
      }
    }
  }
  ### m: "\n$m"

  $m = $m->transpose;
  ### transposed: "\n$m"

  ### det: $m->determinant
  if ($m->determinant == 0) {
    die "Oops, matrix not invertible";
  }

  my $v = Math::Matrix->new(map {[$numerator->coeff($_)]} 0 .. $total_degree-1);
  ### vector: "\n$v"

  $m = $m->concat($v);
  ### concat: "\n$m"

  my $s = $m->solve;
  ### solve: "\n$s"

  my @ret;
  {
    my $check = Math::Polynomial->new(0);
    my $r = 0;
    foreach my $i (0 .. $#denominators) {
      if ($denominators[$i]->degree < 0) {
        croak "Zero denominator";
      }
      my @coeffs;
      foreach my $j (1 .. $denominators[$i]->degree) {
        push @coeffs, $s->[$r][0];
        $r++;
      }
      my $ret = Math::Polynomial->new(@coeffs);
      push @ret, $ret;

      $check += $ret * $prods[$i];
    }

    unless ($check == $numerator) {
      die "Oops, multiply back as check not equal to original numerator, got $check want $numerator";
    }
  }

  return @ret;
}

# Return a Math::Matrix which is $rows x $columns of zeros.
# If $columns is omitted then square $rows x $rows.
sub math_matrix_new_zero {
  my ($rows, $columns) = @_;
  if (! defined $columns) {
    $columns = $rows;
  }
  return Math::Matrix->new(map { [ (0) x $columns ]
                               } 0 .. $rows-1);
}

# a + b*x + c*x^2     d         2 + 2*x^2
# ---------------- + --- = ---------------------
#  1 - x - 2*x^3     1-x   (1 - x - 2*x^3)*(1-x)
#
# (a + b*x + c*x^2)*(1-x) + d*(1 - x - 2*x^3) = 2 + 2*x^2
#
#    a - a*x
#        b*x - b*x^2
#              c*x^2 - c*x^3
#    d  -d*x         -2d*x^3
# =  2       + 2*x^2
#  m = [1,0,0,1; -1,1,0,-1; 0,-1,1,0; 0,0,-1,-2]
#   v = [2;0;2;0]
#  matsolve(m,v)
#
# a = -2    4
# b =  2    2
# c =  4    4
# d =  4   -2
#
# (-2 + 2*x + 4*x^2)/(1 - x - 2*x^3)   + 4 /(1-x) ==  (2 + 2*x^2)/(1 - x - 2*x^3)*(1-x)

1;
__END__