-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmath2matrix.pl
More file actions
executable file
·113 lines (98 loc) · 2.8 KB
/
Copy pathmath2matrix.pl
File metadata and controls
executable file
·113 lines (98 loc) · 2.8 KB
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
#!/usr/bin/perl
# Read from standard input an array in Mathematica's CForm, and output
# (to standard output) C code to fill an ARRAY (defined in defs.h)
# with its elements.
use strict;
use warnings;
my $line = "";
my @output = (0,0,"","");
my $i = 0;
my $j = 0;
while (<STDIN>)
{
# Add next line of input to line, first removing whitespace and
# trailing backslashes.
s/\s*|\\\\$//g;
$line .= $_;
}
# Convert Mathematica'isms to proper C code (some from GSL).
$line =~ s/Power/pow/g;
$line =~ s/Sqrt/sqrt/g;
$line =~ s/\.([^\d])/.0$1/g;
$line =~ s/Erfc/gsl_sf_erfc/g;
$line =~ s/Erf/gsl_sf_erf/g;
$line =~ s/Pi/M_PI/g;
$line =~ s/\bE\b/M_E/g;
while($line ne "")
{
# Strip leading List( directives. Unless this is the beginning of
# input, increment row, reset column, and remove trailing
# close-paren from output.
while($line =~ /^List\((.*)$/)
{
$line = $1;
if($output[2] ne "")
{
if($output[2] =~ /(.*)\)/) { $output[2] = $1; }
if($output[3] =~ /(.*)\)/) { $output[3] = $1; }
$i++;
$j=0;
}
}
# Format and print output.
if($output[2] ne "")
{
print "gsl_matrix_complex_set(output, $output[0], $output[1], "
. "gsl_complex_rect($output[2], $output[3]));\n";
}
# Reset output.
@output = ($i,$j,"0","0");
# Continue parsing until we run out of commas, at which point we
# have one remaining entry.
if(!($line =~ /^[^,]*((Complex\([^\)]*\)|pow(\(((?:(?>[^()]+)|(?3))*)\)))[^,]*)*$/))
{
# If Complex is used, separate the real and imaginary parts, and
# consume everything up to and including the complex number.
if($line =~ /^([^,]*)Complex\(([^,\)]*),([^\)]*)\)(.*)$/)
{
$output[2] = $1.$2;
$output[3] = $1.$3;
$line = $4;
}
# Otherwise blank the real part so it isn't assumed to be 0.
else { $output[2] = ""; }
# Check whether either output is 0 so we can avoid further
# processing on it.
if($output[2] =~ /^\(*0$/) { $output[2] = "0"; }
if($output[3] =~ /^\(*0$/) { $output[3] = "0"; }
# If Power is used, consume and add to output so we don't
# parse the internal comma.
while($line =~ /^([^,]*?pow(\(((?:(?>[^()]+)|(?2))*)\)))(?'rem'.*)$/)
{
if($output[2] ne "0") { $output[2] .= $1; }
if($output[3] ne "0") { $output[3] .= $1; }
$line = $+{rem};
}
# Add everything up to first comma to output and consume from
# input.
if($line =~ /^([^,]*),(.*)$/)
{
$line = $2;
if($output[2] ne "0") { $output[2] .= $1; }
if($output[3] ne "0") { $output[3] .= $1; }
}
# Increment j.
$j++;
}
# Finally, parse the last element. This must be real due to
# hermiticity, but we need to clear out two closing parentheses.
else
{
if($line =~ /(.*)\)\)$/)
{
print "gsl_matrix_complex_set(output, $i, $j, "
. "gsl_complex_rect($1, 0));\n";
}
$line = "";
}
}