| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Math::Giac; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 137671 | use 5.006; | 
|  | 2 |  |  |  |  | 17 |  | 
| 4 | 2 |  |  | 2 |  | 11 | use strict; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 51 |  | 
| 5 | 2 |  |  | 2 |  | 13 | use warnings; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 66 |  | 
| 6 | 2 |  |  | 2 |  | 1563 | use File::Temp; | 
|  | 2 |  |  |  |  | 44111 |  | 
|  | 2 |  |  |  |  | 1302 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | =head1 NAME | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | Math::Giac - A perl interface to giac, a CAS(Computer Algebra System) | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | =head1 VERSION | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | Version 0.0.1 | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =cut | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | our $VERSION = '0.0.1'; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | use Math::Giac; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | my $giac; | 
| 26 |  |  |  |  |  |  | eval( { | 
| 27 |  |  |  |  |  |  | $giac=$Math::Giac->new; | 
| 28 |  |  |  |  |  |  | } ); | 
| 29 |  |  |  |  |  |  | if ( $@ ){ | 
| 30 |  |  |  |  |  |  | die("Failed to locate the giac binary"); | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | my $results=$giac->run('sin(x)+cos(pi)-3'); | 
| 34 |  |  |  |  |  |  | print $results."\n"; | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | $results=$giac->run('mathml(sin(x)+cos(pi)-3)'); | 
| 37 |  |  |  |  |  |  | print $results."\n"; | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | $giac->set_vars({ A=>2 }); | 
| 40 |  |  |  |  |  |  | my $results=$giac->run('sin(A)+cos(pi)-3'); | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | =head1 METHODS | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | =head2 new | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | This initiates the object. | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | This also checks to make sure that giac is in the path. If | 
| 49 |  |  |  |  |  |  | that check fails, it will die. | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | my $giac; | 
| 52 |  |  |  |  |  |  | eval( { | 
| 53 |  |  |  |  |  |  | $giac=$Math::Giac->new; | 
| 54 |  |  |  |  |  |  | } ); | 
| 55 |  |  |  |  |  |  | if ( $@ ){ | 
| 56 |  |  |  |  |  |  | die("Failed to locate the giac binary"); | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =cut | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | sub new { | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | # make sure we can locate giac binary | 
| 64 | 0 |  |  | 0 | 1 |  | my $the_bin = `/bin/sh -c 'which giac 2> /dev/null'`; | 
| 65 | 0 | 0 |  |  |  |  | if ( $? != 0 ) { | 
| 66 | 0 |  |  |  |  |  | die("Can't locate the giac binary"); | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 0 |  |  |  |  |  | my $self = { vars => {}, }; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 0 |  |  |  |  |  | bless $self; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 0 |  |  |  |  |  | return $self; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | =head2 run | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | This returns the respected string after putting together a variable list. | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | The final output is what is returned, or $output[ $#output - 1 ], assuming it is | 
| 81 |  |  |  |  |  |  | a single line one. Otherwise it works backwards to find a match. | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | This will remove the " from the start and return of the return, which gets added for | 
| 84 |  |  |  |  |  |  | with latex or mathml. | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | This will die on a non-zero exit or no string being specified. | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | my $results=$giac->run('sin(x)+cos(pi)-3'); | 
| 89 |  |  |  |  |  |  | print $results."\n"; | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | $results=$giac->run('mathml(sin(x)+cos(pi)-3)'); | 
| 92 |  |  |  |  |  |  | print $results."\n"; | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =cut | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | sub run { | 
| 97 | 0 |  |  | 0 | 1 |  | my $self   = $_[0]; | 
| 98 | 0 |  |  |  |  |  | my $to_run = $_[1]; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 0 | 0 |  |  |  |  | if ( !defined($to_run) ) { | 
| 101 | 0 |  |  |  |  |  | die('No string specified to run'); | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 0 |  |  |  |  |  | $to_run =~ s/\n+$//; | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 0 |  |  |  |  |  | my @var_list   = keys( %{ $self->{vars} } ); | 
|  | 0 |  |  |  |  |  |  | 
| 107 | 0 |  |  |  |  |  | my $vars_addon = ''; | 
| 108 | 0 |  |  |  |  |  | foreach my $cur_var (@var_list) { | 
| 109 | 0 |  |  |  |  |  | $vars_addon = $vars_addon . $cur_var . ':=' . $self->{vars}{$cur_var} . "\n"; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 0 |  |  |  |  |  | $to_run = $vars_addon . $to_run; | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 0 |  |  |  |  |  | my $tmp = File::Temp->new; | 
| 115 | 0 |  |  |  |  |  | print $tmp $to_run; | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | # run giac | 
| 118 | 0 |  |  |  |  |  | my $returned = `giac < $tmp 2> /dev/null`; | 
| 119 | 0 | 0 |  |  |  |  | if ( $? != 0 ) { | 
| 120 | 0 |  |  |  |  |  | die("giac exited with a non-zero... ".$?."... ".$@); | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 0 |  |  |  |  |  | my @split_returned = split( /\n/, $returned ); | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | # if second to last is /^[0-9]+\>\>\ / thn return | 
| 126 | 0 | 0 |  |  |  |  | if ( $split_returned[ $#split_returned - 2 ] =~ /^[0-9]+\>\>\ / ) { | 
| 127 |  |  |  |  |  |  | # removes ", which get added for latex | 
| 128 | 0 |  |  |  |  |  | $split_returned[ $#split_returned - 1 ] =~ s/^\"//; | 
| 129 | 0 |  |  |  |  |  | $split_returned[ $#split_returned - 1 ] =~ s/\"$//; | 
| 130 | 0 |  |  |  |  |  | return $split_returned[ $#split_returned - 1 ]; | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | # if we are here, go through it and put it toghether | 
| 134 | 0 |  |  |  |  |  | my $to_return = $split_returned[ $#split_returned - 1 ]; | 
| 135 | 0 |  |  |  |  |  | my $line      = 3; | 
| 136 | 0 |  |  |  |  |  | my $loop      = 1; | 
| 137 | 0 |  |  |  |  |  | while ($loop) { | 
| 138 | 0 |  |  |  |  |  | $to_return = $split_returned[ $#split_returned - $line ] . "\n" . $to_return; | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | # goto the previous line and check if it matches the end condition | 
| 141 | 0 |  |  |  |  |  | $line++; | 
| 142 | 0 | 0 |  |  |  |  | if ( $split_returned[ $#split_returned - $line ] =~ /^[0-9]+\>\>\ / ) { | 
| 143 | 0 |  |  |  |  |  | $loop = 0; | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | # remove the "s that get added for multi line... | 
| 148 | 0 |  |  |  |  |  | $to_return =~ s/^\"//; | 
| 149 | 0 |  |  |  |  |  | $to_return =~ s/\"$//; | 
| 150 |  |  |  |  |  |  |  | 
| 151 | 0 |  |  |  |  |  | return $to_return; | 
| 152 |  |  |  |  |  |  | } | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =head2 vars_clear | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | Removes any set variables. | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | As long as new was successfully called, this won't error. | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | $giac->vars_clear; | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | =cut | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | sub vars_clear { | 
| 165 | 0 |  |  | 0 | 1 |  | my $self = $_[0]; | 
| 166 |  |  |  |  |  |  |  | 
| 167 | 0 |  |  |  |  |  | delete( $self->{vars} ); | 
| 168 |  |  |  |  |  |  |  | 
| 169 | 0 |  |  |  |  |  | $self->{vars} = {}; | 
| 170 |  |  |  |  |  |  |  | 
| 171 | 0 |  |  |  |  |  | return 1; | 
| 172 |  |  |  |  |  |  | } | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | =head2 vars_set | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | Sets the variables. | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | xsThis requires one argument, which is a hash reference. | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | =cut | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | sub vars_set { | 
| 183 | 0 |  |  | 0 | 1 |  | my $self = $_[0]; | 
| 184 | 0 | 0 |  |  |  |  | if ( ref( $_[1] ne 'HASH' ) ) { | 
| 185 | 0 |  |  |  |  |  | die('$_[1] is not a hash'); | 
| 186 |  |  |  |  |  |  | } | 
| 187 | 0 |  |  |  |  |  | my $vars = $_[1]; | 
| 188 |  |  |  |  |  |  |  | 
| 189 | 0 |  |  |  |  |  | $self->{vars} = $vars; | 
| 190 |  |  |  |  |  |  |  | 
| 191 | 0 |  |  |  |  |  | return 1; | 
| 192 |  |  |  |  |  |  | } | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | =head1 VARIABLE HANDLING | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | Lets say the variable hash below is passed. | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | { | 
| 199 |  |  |  |  |  |  | A=>1, | 
| 200 |  |  |  |  |  |  | B=>3, | 
| 201 |  |  |  |  |  |  | } | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | Then the resulting code will be as below. | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | A:=1 | 
| 206 |  |  |  |  |  |  | B:=3 | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | Then requested item to run is then added to the end. | 
| 209 |  |  |  |  |  |  | So if we are running 'sin(pi)+A' then we will have | 
| 210 |  |  |  |  |  |  | the item below. | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | A:=1 | 
| 213 |  |  |  |  |  |  | B:=3 | 
| 214 |  |  |  |  |  |  | sin(pi)+A | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | =head1 AUTHOR | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | Zane C. Bowers-HAdley, C<<  >> | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | =head1 BUGS | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | Please report any bugs or feature requests to C, or through | 
| 223 |  |  |  |  |  |  | the web interface at L.  I will be notified, and then you'll | 
| 224 |  |  |  |  |  |  | automatically be notified of progress on your bug as I make changes. | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | =head1 SUPPORT | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | perldoc Math::Giac | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  |  | 
| 236 |  |  |  |  |  |  | You can also look for information at: | 
| 237 |  |  |  |  |  |  |  | 
| 238 |  |  |  |  |  |  | =over 4 | 
| 239 |  |  |  |  |  |  |  | 
| 240 |  |  |  |  |  |  | =item * RT: CPAN's request tracker (report bugs here) | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  | L | 
| 243 |  |  |  |  |  |  |  | 
| 244 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 245 |  |  |  |  |  |  |  | 
| 246 |  |  |  |  |  |  | L | 
| 247 |  |  |  |  |  |  |  | 
| 248 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 249 |  |  |  |  |  |  |  | 
| 250 |  |  |  |  |  |  | L | 
| 251 |  |  |  |  |  |  |  | 
| 252 |  |  |  |  |  |  | =item * Search CPAN | 
| 253 |  |  |  |  |  |  |  | 
| 254 |  |  |  |  |  |  | L | 
| 255 |  |  |  |  |  |  |  | 
| 256 |  |  |  |  |  |  | =item * Repository | 
| 257 |  |  |  |  |  |  |  | 
| 258 |  |  |  |  |  |  | L | 
| 259 |  |  |  |  |  |  |  | 
| 260 |  |  |  |  |  |  | =back | 
| 261 |  |  |  |  |  |  |  | 
| 262 |  |  |  |  |  |  |  | 
| 263 |  |  |  |  |  |  | =head1 ACKNOWLEDGEMENTS | 
| 264 |  |  |  |  |  |  |  | 
| 265 |  |  |  |  |  |  |  | 
| 266 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 267 |  |  |  |  |  |  |  | 
| 268 |  |  |  |  |  |  | This software is Copyright (c) 2020 by Zane C. Bowers-HAdley. | 
| 269 |  |  |  |  |  |  |  | 
| 270 |  |  |  |  |  |  | This is free software, licensed under: | 
| 271 |  |  |  |  |  |  |  | 
| 272 |  |  |  |  |  |  | The Artistic License 2.0 (GPL Compatible) | 
| 273 |  |  |  |  |  |  |  | 
| 274 |  |  |  |  |  |  |  | 
| 275 |  |  |  |  |  |  | =cut | 
| 276 |  |  |  |  |  |  |  | 
| 277 |  |  |  |  |  |  | 1; # End of Math::Giac |