用Perl从LaTeX代码中提取数字



我有一大堆关于幂级数系数的大LaTeX表,我正试图编写一个Perl脚本将它们转换为实际的幂级数(以Mathematica符号表示)。我快弄好了,但是有个奇怪的bug,我完全不明白。

下面是我的代码:
#!usr/bin/perl
# split the input into lines (note that '\\\\' matches a double backslash)
my @lines = split('\\\\',$ARGV[0]);
my @array = ();
foreach my $i ( 0 .. $#lines ) {
    # split each line into a list of coefficients, which are separated by a &
    # I think this is the part that is broken
    my @line = split('&',$lines[$i]);
    # create a two-dimensional array of coefficients
    foreach my $j ( 0 .. $#line ) {
        push @{ $array[$i] }, $line[$j];
    }
}
# output array as power series, this part works fine
foreach my $i (0 .. length(@array[0])){
    print "nPower series of ",$array[0][$i],":n";
    foreach my $j (2 .. $#array){
        print $array[$j][$i],"q^",$j-2,"+";
    }
}
print "n";

下面是一个示例输入字符串(LaTeX代码):

[不需要读这个,它不是那么重要;我只是把它包括在内[/p]。

'$[g]$  &1A &2A &4A &2B &2C &3A &6A &3B &6B &4B &4C &5A &10A    &12A    &6C &6D &8AB    &8CD    &20AB   &11AB   &22AB   \
$G_g$&$1$&$1|4$&${2|8}$&$2$&$2|2$&$3$&$3|4$&${3|3}$&${3|12}$&$4|2$&$4$&$5$&$5|4$&$6|24$&$6$&$6|2$&$8|4$&$8$&${10|8}$&$11$&$11|4$\
8   &20 &-20    &0  &-4 &4  &2  &-2 &-4 &4  &0  &0  &0  &0  &0  &2  &-2 &0  &0  &0  &-2 &2  \
20  &88 &-88    &0  &8  &-8 &-2 &2  &4  &-4 &0  &0  &-2 &2  &0  &2  &-2 &0  &0  &0  &0  &0  \
32  &220    &-220   &0  &-12    &12 &4  &-4 &4  &-4 &0  &0  &0  &0  &0  &0  &0  &0  &0  &0  &0  &0  \
44  &560    &-560   &0  &16 &-16    &2  &-2 &-4 &4  &0  &0  &0  &0  &0  &-2 &2  &0  &0  &0  &-1 &1  \
56  &1144   &-1144  &0  &-24    &24 &-8 &8  &4  &-4 &0  &0  &4  &-4 &0  &0  &0  &0  &0  &0  &0  &0  \
68  &2400   &-2400  &0  &32 &-32    &6  &-6 &0  &0  &0  &0  &0  &0  &0  &2  &-2 &0  &0  &0  &2  &-2 \
80  &4488   &-4488  &0  &-40    &40 &6  &-6 &-12    &12 &0  &0  &-2 &2  &0  &2  &-2 &0  &0  &0  &0  &0  \
92  &8360   &-8360  &0  &56 &-56    &-10    &10 &8  &-8 &0  &0  &0  &0  &0  &2  &-2 &0  &0  &0  &0  &0  \
104 &14696  &-14696 &0  &-72    &72 &8  &-8 &8  &-8 &0  &0  &-4 &4  &0  &0  &0  &0  &0  &0  &0  &0  \
116 &25544  &-25544 &0  &88 &-88    &2  &-2 &-16    &16 &0  &0  &4  &-4 &0  &-2 &2  &0  &0  &0  &2  &-2 \
128 &42660  &-42660 &0  &-116   &116    &-18    &18 &12 &-12    &0  &0  &0  &0  &0  &-2 &2  &0  &0  &0  &2  &-2 \
140 &70576  &-70576 &0  &144    &-144   &16 &-16    &4  &-4 &0  &0  &-4 &4  &0  &0  &0  &0  &0  &0  &0  &0  \
152 &113520 &-113520    &0  &-176   &176    &12 &-12    &-24    &24 &0  &0  &0  &0  &0  &4  &-4 &0  &0  &0  &0  &0  \
164 &180640 &-180640    &0  &224    &-224   &-26    &26 &16 &-16    &0  &0  &0  &0  &0  &2  &-2 &0  &0  &0  &-2 &2  \
176 &281808 &-281808    &0  &-272   &272    &18 &-18    &12 &-12    &0  &0  &8  &-8 &0  &-2 &2  &0  &0  &0  &-1 &1  \
188 &435160 &-435160    &0  &328    &-328   &10 &-10    &-32    &32 &0  &0  &0  &0  &0  &-2 &2  &0  &0  &0  &0  &0  \
200 &661476 &-661476    &0  &-404   &404    &-42    &42 &24 &-24    &0  &0  &-4 &4  &0  &-2 &2  &0  &0  &0  &2  &-2 \
212 &996600 &-996600    &0  &488    &-488   &30 &-30    &12 &-12    &0  &0  &0  &0  &0  &2  &-2 &0  &0  &0  &0  &0  \
224 &1482536    &-1482536   &0  &-584   &584    &20 &-20    &-52    &52 &0  &0  &-4 &4  &0  &4  &-4 &0  &0  &0  &0  &0  \
236 &2187328    &-2187328   &0  &704    &-704   &-50    &50 &40 &-40    &0  &0  &8  &-8 &0  &2  &-2 &0  &0  &0  &0  &0  \
248 &3193960    &-3193960   &0  &-840   &840    &40 &-40    &28 &-28    &0  &0  &0  &0  &0  &0  &0  &0  &0  &0  &0  &0  \
260 &4629152    &-4629152   &0  &992    &-992   &20 &-20    &-64    &64 &0  &0  &-8 &8  &0  &-4 &4  &0  &0  &0  &0  &0  \
272 &6650400    &-6650400   &0  &-1184  &1184   &-78    &78 &48 &-48    &0  &0  &0  &0  &0  &-2 &2  &0  &0  &0  &-2 &2  \
284 &9490536    &-9490536   &0  &1400   &-1400  &54 &-54    &24 &-24    &0  &0  &-4 &4  &0  &2  &-2 &0  &0  &0  &0  &0  \
296 &13441032   &-13441032  &0  &-1640  &1640   &36 &-36    &-96    &96 &0  &0  &12 &-12    &0  &4  &-4 &0  &0  &0  &0  &0  \
308 &18920240   &-18920240  &0  &1936   &-1936  &-100   &100    &68 &-68    &0  &0  &0  &0  &0  &4  &-4 &0  &0  &0  &-2 &2  \
320 &26457464   &-26457464  &0  &-2264  &2264   &74 &-74    &44 &-44    &0  &0  &-6 &6  &0  &-2 &2  &0  &0  &0  &0  &0  \
332 &36792560   &-36792560  &0  &2640   &-2640  &38 &-38    &-124   &124    &0  &0  &0  &0  &0  &-6 &6  &0  &0  &0  &2  &-2 \
344 &50865232   &-50865232  &0  &-3088  &3088   &-140   &140    &88 &-88    &0  &0  &-8 &8  &0  &-4 &4  &0  &0  &0  &0  &0  \
356 &69966336   &-69966336  &0  &3584   &-3584  &102    &-102   &48 &-48    &0  &0  &16 &-16    &0  &2  &-2 &0  &0  &0  &0  &0'

下面是它产生的输出:

[也不需要读这个].

Power series of 1A:
20q^0+88q^1+220q^2+560q^3+1144q^4+2400q^5+4488q^6+8360q^7+14696q^8+25544q^9+42660q^10+70576q^11+113520q^12+180640q^13+281808q^14+435160q^15+661476q^16+996600q^17+1482536q^18+2187328q^19+3193960q^20+4629152q^21+6650400q^22+9490536q^23+13441032q^24+18920240q^25+26457464q^26+36792560q^27+50865232q^28+69966336q^29+
Power series of 2A:
-20q^0+-88q^1+-220q^2+-560q^3+-1144q^4+-2400q^5+-4488q^6+-8360q^7+-14696q^8+-25544q^9+-42660q^10+-70576q^11+-113520q^12+-180640q^13+-281808q^14+-435160q^15+-661476q^16+-996600q^17+-1482536q^18+-2187328q^19+-3193960q^20+-4629152q^21+-6650400q^22+-9490536q^23+-13441032q^24+-18920240q^25+-26457464q^26+-36792560q^27+-50865232q^28+-69966336q^29+
Power series of 4A:
0q^0+0q^1+0q^2+0q^3+0q^4+0q^5+0q^6+0q^7+0q^8+0q^9+0q^10+0q^11+0q^12+0q^13+0q^14+0q^15+0q^16+0q^17+0q^18+0q^19+0q^20+0q^21+0q^22+0q^23+0q^24+0q^25+0q^26+0q^27+0q^28+0q^29+
Power series of 2B:
-4q^0+8q^1+-12q^2+16q^3+-24q^4+32q^5+-40q^6+56q^7+-72q^8+88q^9+-116q^10+144q^11+-176q^12+224q^13+-272q^14+328q^15+-404q^16+488q^17+-584q^18+704q^19+-840q^20+992q^21+-1184q^22+1400q^23+-1640q^24+1936q^25+-2264q^26+2640q^27+-3088q^28+3584q^29+
Power series of 2C:
4q^0+-8q^1+12q^2+-16q^3+24q^4+-32q^5+40q^6+-56q^7+72q^8+-88q^9+116q^10+-144q^11+176q^12+-224q^13+272q^14+-328q^15+404q^16+-488q^17+584q^18+-704q^19+840q^20+-992q^21+1184q^22+-1400q^23+1640q^24+-1936q^25+2264q^26+-2640q^27+3088q^28+-3584q^29+
Power series of 3A:
2to3q^0+-2q^1+4q^2+2to3q^3+-8q^4+6q^5+6q^6+-10q^7+8q^8+2to3q^9+-18q^10+16q^11+12q^12+-26q^13+18q^14+10q^15+-42q^16+30q^17+20q^18+-50q^19+40q^20+20q^21+-78q^22+54q^23+36q^24+-100q^25+74q^26+38q^27+-140q^28+102q^29+
Power series of 6A:
-2q^0+2to3q^1+-4q^2+-2q^3+8q^4+-6q^5+-6q^6+10q^7+-8q^8+-2q^9+18q^10+-16q^11+-12q^12+26q^13+-18q^14+-10q^15+42q^16+-30q^17+-20q^18+50q^19+-40q^20+-20q^21+78q^22+-54q^23+-36q^24+100q^25+-74q^26+-38q^27+140q^28+-102q^29+
Power series of 3B:
-4q^0+4q^1+4q^2+-4q^3+4q^4+0q^5+-12q^6+8q^7+8q^8+-16q^9+12q^10+4q^11+-24q^12+16q^13+12q^14+-32q^15+24q^16+12q^17+-52q^18+40q^19+28q^20+-64q^21+48q^22+24q^23+-96q^24+68q^25+44q^26+-124q^27+88q^28+48q^29+
Power series of 6B:
4q^0+-4q^1+-4q^2+4q^3+-4q^4+0q^5+12q^6+-8q^7+-8q^8+16q^9+-12q^10+-4q^11+24q^12+-16q^13+-12q^14+32q^15+-24q^16+-12q^17+52q^18+-40q^19+-28q^20+64q^21+-48q^22+-24q^23+96q^24+-68q^25+-44q^26+124q^27+-88q^28+-48q^29+
Power series of 4B:
0q^0+0q^1+0q^2+0q^3+0q^4+0q^5+0q^6+0q^7+0q^8+0q^9+0q^10+0q^11+0q^12+0q^13+0q^14+0q^15+0q^16+0q^17+0q^18+0q^19+0q^20+0q^21+0q^22+0q^23+0q^24+0q^25+0q^26+0q^27+0q^28+0q^29+
Power series of 4C:
0q^0+0q^1+0q^2+0q^3+0q^4+0q^5+0q^6+0q^7+0q^8+0q^9+0q^10+0q^11+0q^12+0q^13+0q^14+0q^15+0q^16+0q^17+0q^18+0q^19+0q^20+0q^21+0q^22+0q^23+0q^24+0q^25+0q^26+0q^27+0q^28+0q^29+
Power series of 5A:
0q^0+-2q^1+0q^2+0q^3+4q^4+0q^5+-2q^6+0q^7+-4q^8+4q^9+0q^10+-4q^11+0q^12+0q^13+8q^14+0q^15+-4q^16+0q^17+-4q^18+8q^19+0q^20+-8q^21+0q^22+-4q^23+12q^24+0q^25+-6q^26+0q^27+-8q^28+16q^29+
Power series of 10A:
0q^0+2to3q^1+0q^2+0q^3+-4q^4+0q^5+2to3q^6+0q^7+4q^8+-4q^9+0q^10+4q^11+0q^12+0q^13+-8q^14+0q^15+4q^16+0q^17+4q^18+-8q^19+0q^20+8q^21+0q^22+4q^23+-12q^24+0q^25+6q^26+0q^27+8q^28+-16q^29+
Power series of 12A:
0q^0+0q^1+0q^2+0q^3+0q^4+0q^5+0q^6+0q^7+0q^8+0q^9+0q^10+0q^11+0q^12+0q^13+0q^14+0q^15+0q^16+0q^17+0q^18+0q^19+0q^20+0q^21+0q^22+0q^23+0q^24+0q^25+0q^26+0q^27+0q^28+0q^29+
Power series of 6C:
2to3q^0+2to3q^1+0q^2+-2q^3+0q^4+2to3q^5+2to3q^6+2to3q^7+0q^8+-2q^9+-2q^10+0q^11+4q^12+2to3q^13+-2q^14+-2q^15+-2q^16+2to3q^17+4q^18+2to3q^19+0q^20+-4q^21+-2q^22+2to3q^23+4q^24+4q^25+-2q^26+-6q^27+-4q^28+2to3q^29+
Power series of 6D:
-2q^0+-2q^1+0q^2+2to3q^3+0q^4+-2q^5+-2q^6+-2q^7+0q^8+2to3q^9+2to3q^10+0q^11+-4q^12+-2q^13+2to3q^14+2to3q^15+2to3q^16+-2q^17+-4q^18+-2q^19+0q^20+4q^21+2to3q^22+-2q^23+-4q^24+-4q^25+2to3q^26+6q^27+4q^28+-2q^29+
Power series of 8AB:
0q^0+0q^1+0q^2+0q^3+0q^4+0q^5+0q^6+0q^7+0q^8+0q^9+0q^10+0q^11+0q^12+0q^13+0q^14+0q^15+0q^16+0q^17+0q^18+0q^19+0q^20+0q^21+0q^22+0q^23+0q^24+0q^25+0q^26+0q^27+0q^28+0q^29+
Power series of 8CD:
0q^0+0q^1+0q^2+0q^3+0q^4+0q^5+0q^6+0q^7+0q^8+0q^9+0q^10+0q^11+0q^12+0q^13+0q^14+0q^15+0q^16+0q^17+0q^18+0q^19+0q^20+0q^21+0q^22+0q^23+0q^24+0q^25+0q^26+0q^27+0q^28+0q^29+
Power series of 20AB:
0q^0+0q^1+0q^2+0q^3+0q^4+0q^5+0q^6+0q^7+0q^8+0q^9+0q^10+0q^11+0q^12+0q^13+0q^14+0q^15+0q^16+0q^17+0q^18+0q^19+0q^20+0q^21+0q^22+0q^23+0q^24+0q^25+0q^26+0q^27+0q^28+0q^29+
Power series of 11AB:
-2q^0+0q^1+0q^2+-1q^3+0q^4+2to3q^5+0q^6+0q^7+0q^8+2to3q^9+2to3q^10+0q^11+0q^12+-2q^13+-1q^14+0q^15+2to3q^16+0q^17+0q^18+0q^19+0q^20+0q^21+-2q^22+0q^23+0q^24+-2q^25+0q^26+2to3q^27+0q^28+0q^29+
Power series of 22AB:
2to3q^0+0q^1+0q^2+1q^3+0q^4+-2q^5+0q^6+0q^7+0q^8+-2q^9+-2q^10+0q^11+0q^12+2to3q^13+1q^14+0q^15+-2q^16+0q^17+0q^18+0q^19+0q^20+0q^21+2to3q^22+0q^23+0q^24+2to3q^25+0q^26+-2q^27+0q^28+0q^29+

这几乎就是我想要的,除了:

表中部分系数为2。当我的程序遇到这种情况时,它产生的系数是2到3。例如,在上面的输出中,它为10A产生的功率级数为

0问^ 0 + 2 to3q ^ 1 + 0问^ 2 + 0 ^ 3 + 4 q q ^ ^ 4 + 0 5 + 2 to3q ^ 6 + 0问^ 7 + 4问^ 8 + 4 q q ^ 0 ^ 9 + 10 + 4问^ 11 + 0问^ 12 + 0问^ 13 + 8问^ 14 + 0 ^ 15 + 4 q q ^ ^ 16 + 0 17 + 4问^ 18 + 8 q 19 20 + 8 + 0问^ ^ ^ 21 + 0 q 22 + 4 ^ ^ 23 + -12 q ^ 24 + 25 + 6 0问^ ^ 26 + 0 q 27 + 8 q ^ ^ 28 + -16 q ^ 29

中加粗的系数显然是无意义的,而应该是2q^1和2q^6。为什么会发生这种情况,我该如何解决?

我怀疑它与&2表示特殊的东西有关。

我不太明白你的代码是如何导致那个输出的…似乎还有更多的事情要做。下面是重写的代码,它应该会给出预期的输出。

# Always use strict and warnings to catch more errors
use strict; use warnings;
my @array;
# loop while we can read a line from input
while (<>) {
    # remove trailing backslashes and space
    s/s*[\][\]s*z//;
    # split on `&`, remove surrounding space, discard first col
    my (undef, @fields) = split /s*&s*/;
    # push fields into appropriate column
    push @{ $array[$_] }, $fields[$_] for 0 .. $#fields;
}
for my $col (@array) {
    # decompose the column, discard row № 2
    my ($name, undef, @vals) = @$col;
    print "Power series of $name:n";
    # join the coefficients.
    # The `map` transforms each index to $value q^$index
    print join('+', map "$vals[$_]q^$_", 0 .. $#vals), "n"; 
}

它可以从STDIN或作为命令行参数给出的文件中获取输入。

指出:

  • length只作用于字符串。warnings会抱怨,如果它被激活了。要获得@array的长度,请在标量上下文中使用它,例如my $length = @array
  • split接受一个正则表达式或字符串' '作为第一个参数。所有其他字符串首先被转换为正则表达式。为了避免双重转义问题,最好直接使用regex。我没有必要在转义反斜杠周围使用字符类,我这样做是为了使计数更容易。
  • 我没有在每个系数序列上发出尾随的+,我认为这是可取的。

最新更新