Skip to content

Instantly share code, notes, and snippets.

@zmughal
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmughal/6af59541692250123d99 to your computer and use it in GitHub Desktop.
Save zmughal/6af59541692250123d99 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use warnings;
use strict;
use PDL;
use PDL::Complex;
use Math::Symbolic;
my $z = 2 + 6*i;
print "$z\n";
my ($x,$y);
my $line = "Z = 5 + 6j + a";
my $vars;
$vars->{j} = i;
$vars->{a} = sequence(10);
if ($line =~ m/=/) {
$line =~ s/^(?<varname>\w+)\s*=//;
my $variable_name = $+{varname};
# change <number>j into <number>*j
$line =~ s/(?<number>\d+)j\b/$+{number}*j/;
my $tree = Math::Symbolic->parse_from_string($line);
print "$line\n";
print "$tree\n";
my @expr_vars = $tree->explicit_signature;
use DDP; p @expr_vars;
#my ($code) = Math::Symbolic::Compiler->compile_to_code($tree);
#use DDP; p $code;
my ($sub) = Math::Symbolic::Compiler->compile_to_sub($tree);
my $result = $sub->( @$vars{ @expr_vars } );
$vars->{ $variable_name } = $result;
use DDP; p $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment