kepler has asked for the wisdom of the Perl Monks concerning the following question:

Hi

Can someone help me putting this code working on Perl 5 (I get all sort of error types which I believe come from the languages dif.)? I would apreciate it very much.

# Simple Vector implementation multi infix:<+>(@a, @b) { @a Z+ @b } multi infix:<->(@a, @b) { @a Z- @b } multi infix:<*>($r, @a) { $r X* @a } multi infix:</>(@a, $r) { @a X/ $r } sub norm { sqrt [+] @_ X** 2 } # Runge-Kutta stuff sub runge-kutta(&yp) { return -> \t, \y, \&#948;t { my $a = &#948;t * yp( t, y ); my $b = &#948;t * yp( t + &#948;t/2, y + $a/2 ); my $c = &#948;t * yp( t + &#948;t/2, y + $b/2 ); my $d = &#948;t * yp( t + &#948;t, y + $c ); ($a + 2*($b + $c) + $d) / 6; } } # gravitational constant constant G = 6.674e-11; # astronomical unit constant au = 150e9; # time constants in seconds constant year = 365.25*24*60*60; constant month = 21*24*60*60; # masses in kg constant $ma = 2e30; # Sun constant $mb = 6e24; # Earth constant $mc = 7.34e22; # Moon my &dABC = runge-kutta my &f = sub ( $t, @ABC ) { my @a = @ABC[0..2]; my @b = @ABC[3..5]; my @c = @ABC[6..8]; my $ab = norm(@a - @b); my $ac = norm(@a - @c); my $bc = norm(@b - @c); return [ flat @ABC[@(9..17)], map G * *, $mb/$ab**3 * (@b - @a) + $mc/$ac**3 * (@c - @a), $ma/$ab**3 * (@a - @b) + $mc/$bc**3 * (@c - @b), $ma/$ac**3 * (@a - @c) + $mb/$bc**3 * (@b - @c); ]; } loop ( my ($t, @ABC) = 0, 0, 0, 0, # Sun position au, 0, 0, # Earth position 0.998*au, 0, 0, # Moon position 0, 0, 0, # Sun speed 0, 2*pi*au/year, 0, # Earth speed 0, 2*pi*(au/year + 0.002*au/month), 0 # Moon speed ; $t < .2; ($t, @ABC) »+=« (.01, dABC($t, @ABC, .01)) ) { printf "t = %.02f : %s\n", $t, @ABC.fmt("%+.3e"); }

Best regards

JKepler

Replies are listed 'Best First'.
Re: (OT) Help for translation
by AnomalousMonk (Archbishop) on Aug 04, 2019 at 16:00 UTC
    constant month = 21*24*60*60;

    BTW: This constant seems a bit odd. For what planet or in what calendar is this a month?


    Give a man a fish:  <%-{-{-{-<

        Most likely. But on the other hand, the wonderful book Calendrical Calculations (3rd ed.) by Dershowitz and Reingold (is this free download kosher?) makes it clear that most likely someone, somewhere, at some time in human history has used a month of 21 and just about every other number of days. Go figure.


        Give a man a fish:  <%-{-{-{-<

Re: Help for translation
by LanX (Saint) on Aug 04, 2019 at 11:12 UTC
    Perl 6 allowes to redefine operators.

    If you want a "similar syntax" in Perl 5 you'll need vector objects with overloaded operators.

    The hyper operators X and Z could be emulated with List::Util functions like 'reduce'.

    Eg the [+] is probably just sum .

    Otherwise just use plain functions.

    But I'm pretty sure you'll already find what you need on CPAN.

    Whatever you really want ... As usual you are pretty vague.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Hi

      No, I'm not being vague at all. I have that piece of astronomy code in Perl6 on Rosetta, right? I have an entire website of professional astronomy where I have basically all made in Perl5. I want to the modules to interact, so it's not smart a Perl6 one routine only for this interaction, don't you agree? I just wanted to make the code in Perl5 for that.

      Kepler

        "...have an entire website of professional astronomy..."

        Great. I am only an amateur. But anyway, please post the link

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

        I have an entire website of professional astronomy where I have basically all made in Perl5.

        what website? open source?

        A reply falls below the community's threshold of quality. You may see it by logging in.
    A reply falls below the community's threshold of quality. You may see it by logging in.