Orbital elements: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 35:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F mulAdd(v1, x1, v2, x2)
R v1 * x1 + v2 * x2
 
Line 69:
V ps = orbitalStateVectors(1.0, 0.1, 0.0, 355.0 / (113.0 * 6.0), 0.0, 0.0)
print(‘Position : ’ps[0])
print(‘Speed : ’ps[1])</langsyntaxhighlight>
 
{{out}}
Line 79:
=={{header|Ada}}==
{{Trans|Kotlin}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Generic_Real_Arrays;
with Ada.Numerics.Generic_Elementary_Functions;
Line 184:
Put ("Position : "); Put (State.Position); New_Line;
Put ("Speed : "); Put (State.Speed); New_Line;
end Orbit;</langsyntaxhighlight>
{{out}}
<pre>
Line 193:
=={{header|ALGOL W}}==
{{Trans|C}} (which is a translation of Kotlin which is a translation of ...).
<langsyntaxhighlight lang="algolw">begin
% compute orbital elements %
% 3-element vector %
Line 277:
write( "Speed : " ); writeOnVector( speed )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 285:
=={{header|C}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
 
Line 360:
printf("Speed : %s\n", buffer);
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 370:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace OrbitalElements {
Line 455:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Position : (0.77942284339868, 0.450000034653684, 0)
Line 462:
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <tuple>
 
Line 558:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Position : (0.779423, 0.45, 0)
Line 565:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.math;
import std.stdio;
import std.typecons;
Line 653:
writeln("Position : ", res[0]);
writeln("Speed : ", res[1]);
}</langsyntaxhighlight>
{{out}}
<pre>Position : (0.7794228433986798, 0.4500000346536842, 0.0000000000000000)
Line 660:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 731:
fmt.Println("Position :", position)
fmt.Println("Speed :", speed)
}</langsyntaxhighlight>
 
{{out}}
Line 740:
 
=={{header|J}}==
{{trans|Raku}}<langsyntaxhighlight Jlang="j">NB. euler rotation matrix, left hand rule
NB. x: axis (0, 1 or 2), y: angle in radians
R=: {{ ((2 1,:1 2) o.(,-)y*_1^2|x)(,&.>/~0 1 2-.x)} =i.3 }}
Line 761:
speed=. (%:(2%ra)-%a)*norm(rp,ra,0) X ijk
position,:speed
}}</langsyntaxhighlight>
 
The true anomaly, argument of Periapsis, Longitude of the ascending node and inclination are all angles. And we use the dot product of their rotation matrices (in that order) to find the orientation of the orbit and the object's position in that orbit. Here, <code>R</code> finds the rotation matrix for a given angle around a given axis. Here's an example of what R gives us for a sixty degree angle:
 
<langsyntaxhighlight Jlang="j"> 0 1 2 R&.> 60r180p1 NB. rotate around first, second or third axis
┌────────────────────┬────────────────────┬────────────────────┐
│1 0 0│ 0.5 0 _0.866025│ 0.5 0.866025 0│
│0 0.5 0.866025│ 0 1 0│_0.866025 0.5 0│
│0 _0.866025 0.5│0.866025 0 0.5│ 0 0 1│
└────────────────────┴────────────────────┴────────────────────┘</langsyntaxhighlight>
 
Task example:<langsyntaxhighlight Jlang="j"> orbitalStateVectors 1 0.1 0 355r678 0 0
0.779423 0.45 0
_0.552771 0.957427 0</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">public class OrbitalElements {
private static class Vector {
private double x, y, z;
Line 858:
System.out.printf("Speed : %s\n", ps[1]);
}
}</langsyntaxhighlight>
 
{{out}}
Line 868:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq"># Array/vector operations
def addVectors: transpose | map(add);
 
Line 904:
| divide(abs)
| multiply( ((2 / $r) - (1 / semimajorAxis))|sqrt) as $speed
| [$position, $speed] ;</langsyntaxhighlight>
'''The Task'''
<langsyntaxhighlight lang="jq">orbitalStateVectors(1; 0.1; 0; 355 / (113 * 6); 0; 0)
| "Position : \(.[0])",
"Speed : \(.[1])"</langsyntaxhighlight>
{{out}}
<pre>
Line 917:
=={{header|Julia}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="julia">using GeometryTypes
import Base.abs, Base.print
 
Line 952:
 
testorbitalmath()
</langsyntaxhighlight>{{out}}
<pre>
Position : (0.7794228433986797, 0.45000003465368416, 0.0)
Line 960:
=={{header|Kotlin}}==
{{trans|Sidef}}
<langsyntaxhighlight lang="scala">// version 1.1.4-3
 
class Vector(val x: Double, val y: Double, val z: Double) {
Line 1,023:
println("Position : $position")
println("Speed : $speed")
}</langsyntaxhighlight>
{{out}}
<pre>Position : (0.7794228433986797, 0.45000003465368416, 0.0)
Line 1,030:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import math, strformat
 
type Vector = tuple[x, y, z: float]
Line 1,086:
trueAnomaly = 0.0)
echo "Position: ", position
echo "Speed: ", speed</langsyntaxhighlight>
 
{{out}}
Line 1,094:
=={{header|ooRexx}}==
{{trans|Java}}
<langsyntaxhighlight lang="oorexx">/* REXX */
Numeric Digits 16
ps = orbitalStateVectors(1.0, 0.1, 0.0, 355.0 / (113.0 * 6.0), 0.0, 0.0)
Line 1,200:
Return res
 
::requires 'rxmath' LIBRARY</langsyntaxhighlight>
 
{{out}}
Line 1,211:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use Math::Vector::Real;
Line 1,270:
0, # argument of periapsis
0 # true-anomaly
;</langsyntaxhighlight>
{{out}}
<pre>$VAR1 = {
Line 1,287:
=={{header|Phix}}==
{{trans|Python}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">vabs</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
Line 1,327:
<span style="color: #000000;">orbitalStateVectors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">355.0</span> <span style="color: #0000FF;">/</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">113.0</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">6.0</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">0.0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0.0</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,339:
This implementation uses the CLP/R library of swi-prolog, but doesn't have to. This removes the need for a vector divide and has limited capability to reverse the functionality (eg: given the position/speed find some orbital elements).
 
<langsyntaxhighlight Prologlang="prolog">:- use_module(library(clpr)).
 
v3_add(v(X1,Y1,Z1),v(X2,Y2,Z2),v(X,Y,Z)) :-
Line 1,397:
find_l(Ecc, SemiMajor, L) :-
dif(Ecc,1.0),
{ L = SemiMajor * (1.0 - Ecc * Ecc) }.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,408:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import math
 
class Vector:
Line 1,466:
ps = orbitalStateVectors(1.0, 0.1, 0.0, 355.0 / (113.0 * 6.0), 0.0, 0.0)
print "Position :", ps[0]
print "Speed :", ps[1]</langsyntaxhighlight>
{{out}}
<pre>Position : (0.787295801413, 0.454545489549, 0.0)
Line 1,474:
(formerly Perl 6)
We'll use the [https://github.com/grondilu/clifford Clifford geometric algebra library] but only for the vector operations.
<syntaxhighlight lang="raku" perl6line>sub orbital-state-vectors(
Real :$semimajor-axis where * >= 0,
Real :$eccentricity where * >= 0,
Line 1,517:
longitude-of-ascending-node => pi/6,
argument-of-periapsis => pi/4,
true-anomaly => 0;</langsyntaxhighlight>
{{out}}
<pre>{position => 0.237771283982207*e0+0.860960261697716*e1+0.110509023572076*e2, speed => -1.06193301748006*e0+0.27585002056925*e1+0.135747024865598*e2}</pre>
Line 1,525:
{{trans|Java}}
Vectors are represented by strings: 'x/y/z'
<langsyntaxhighlight lang="rexx">/* REXX */
Numeric Digits 16
Parse Value orbitalStateVectors(1.0,0.1,0.0,355.0/(113.0*6.0),0.0,0.0),
Line 1,660:
End
Numeric Digits prec
Return r+0</langsyntaxhighlight>
{{out}}
<pre>Position : (0.7794228433986798,0.4500000346536842,0)
Line 1,667:
===version 2===
Re-coding of REXX version 1, &nbsp; but with greater decimal digits precision.
<langsyntaxhighlight lang="rexx">/*REXX pgm converts orbital elements ──► orbital state vectors (angles are in radians).*/
numeric digits length( pi() ) - length(.) /*limited to pi len, but show 1/3 digs.*/
call orbV 1, .1, 0, 355/113/6, 0, 0 /*orbital elements taken from: Java */
Line 1,718:
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g= g *.5'e'_ % 2
do j=0 while h>9; m.j= h; h= h % 2 + 1; end
do k=j+5 to 0 by '-1'; numeric digits m.k; g= (g+x/g) * .5; end; return g</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default internal inputs:}}
<pre>
Line 1,743:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">import scala.language.existentials
 
object OrbitalElements extends App {
Line 1,785:
}
 
}</langsyntaxhighlight>
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/ac17jh2/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/2NQNgj4OQkazxZNvSzcexQ Scastie (remote JVM)].
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func orbital_state_vectors(
semimajor_axis,
eccentricity,
Line 1,846:
say "Position : #{r.position}"
say "Speed : #{r.speed}\n"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,862:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="swift">import Foundation
 
public struct Vector {
Line 1,964:
)
 
print("Position: \(position); Speed: \(speed)")</langsyntaxhighlight>
 
{{out}}
Line 1,972:
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="ecmascript">class Vector {
construct new(x, y, z) {
_x = x
Line 2,030:
var ps = orbitalStateVectors.call(1, 0.1, 0, 355 / (113 * 6), 0, 0)
System.print("Position : %(ps[0])")
System.print("Speed : %(ps[1])")</langsyntaxhighlight>
 
{{out}}
Line 2,040:
=={{header|zkl}}==
{{trans|Perl}}
<langsyntaxhighlight lang="zkl">fcn orbital_state_vectors(semimajor_axis, eccentricity, inclination,
longitude_of_ascending_node, argument_of_periapsis, true_anomaly){
i,j,k:=T(1.0, 0.0, 0.0), T(0.0, 1.0, 0.0), T(0.0, 0.0, 1.0);
Line 2,069:
return(position,speed);
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">orbital_state_vectors(
1.0, # semimajor axis
0.1, # eccentricity
Line 2,077:
0.0, # argument of periapsis
0.0 # true-anomaly
).println();</langsyntaxhighlight>
{{out}}
<pre>L(L(0.779423,0.45,0),L(-0.552771,0.957427,0))</pre>
10,333

edits