Skip to content
Browse files

get topological sorting and dependant variables going

  • Loading branch information...
1 parent f70421e commit 3a08e22bbd0ed431b5917acc664264df0f53c9c9 @moritz committed Jun 19, 2010
Showing with 13 additions and 12 deletions.
  1. +9 −10 lib/Math/Model.pm
  2. +4 −2 scratch.pl
View
19 lib/Math/Model.pm
@@ -36,18 +36,19 @@ method !params-for(&c) {
param-names(&c).map( {; $_ => %!current-values{$_} } ).hash;
}
-method topo-sort(*@a) {
+method topo-sort(*@vars) {
my %seen;
my @order;
sub topo(*@a) {
for @a {
- die "Circular dependency involving $_" if %seen{$_};
- topo(param-names(%.variables{$_})) unless %.derivatives.exists($_);
- @order.push: $_ unless %seen{$_};
- ++%seen{$_};
+ next if %!inv.exists($_) or %seen{$_};
+ topo(param-names(%.variables{$_}));
+ @order.push: $_;
+ %seen{$_}++;
}
}
- topo(@a);
+ topo(@vars);
+# say @order.perl;
@order;
}
@@ -60,17 +61,15 @@ method integrate($from = 0, $to = 10, $min-resolution = ($to - $from) / 20) {
unless %.initials.exists($d.value);
}
- my %vars = %.variables.pairs.grep: { ! %!inv.exists(.key) };
-
%!current-values = %.initials;
%!current-values<time> = $from;
- my @vars-topo = @.topo-sort(%vars.keys);
+ my @vars-topo = self.topo-sort(%.variables.keys);
sub update-current-values($time, @values) {
%!current-values<time> = $time;
%!current-values{@!deriv-names} = @values;
for @vars-topo {
- my $c = %vars{$_};
+ my $c = %.variables{$_};
%!current-values{$_} = $c.(|self!params-for($c));
}
}
View
6 scratch.pl
@@ -10,7 +10,9 @@
acceleration => 'velocity',
},
variables => {
- acceleration => { -$:height },
+ acceleration => { $:force / $:mass },
+ mass => { 1 },
+ force => { - $:height }
},
initials => {
height => 1,
@@ -19,5 +21,5 @@
captures => <height velocity acceleration>,
);
-$m.integrate(:to(5), :min-resolution(0.5));
+$m.integrate(:to(10), :min-resolution(0.5));
$m.render-svg('spring.svg');

0 comments on commit 3a08e22

Please sign in to comment.
Something went wrong with that request. Please try again.