Orbital elements: Difference between revisions

Added FreeBASIC
(→‎{{header|ooRexx}}: Numeric Digits 16 + added Perl6 alternate test, courtesy GS)
(Added FreeBASIC)
 
(41 intermediate revisions by 18 users not shown)
Line 1:
{{draft task}}
When neglecting the influence of other objects, two celestial bodies orbit one another along a [[wp:conic section|conic]] trajectory. In the orbital plane, the radial[[w:Polar coordinate system|polar equation]] is thus:
 
&nbsp;<big> r = L/(1 + e cos(angle)) </big>
Line 31:
TODO: pick an example from a reputable source, and bring the algorithm description onto this site. (Restating those pages in concise a fashion comprehensible to the coders and readers of this site will be a good exercise.)
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F mulAdd(v1, x1, v2, x2)
R v1 * x1 + v2 * x2
 
F rotate(i, j, alpha)
R [mulAdd(i, cos(alpha), j, sin(alpha)), mulAdd(i, -sin(alpha), j, cos(alpha))]
 
F orbitalStateVectors(semimajorAxis, eccentricity, inclination, longitudeOfAscendingNode, argumentOfPeriapsis, trueAnomaly)
V i = (1.0, 0.0, 0.0)
V j = (0.0, 1.0, 0.0)
V k = (0.0, 0.0, 1.0)
 
V p = rotate(i, j, longitudeOfAscendingNode)
i = p[0]
j = p[1]
p = rotate(j, k, inclination)
j = p[0]
p = rotate(i, j, argumentOfPeriapsis)
i = p[0]
j = p[1]
 
V l = I (eccentricity == 1.0) {2.0} E 1.0 - eccentricity * eccentricity
l *= semimajorAxis
V c = cos(trueAnomaly)
V s = sin(trueAnomaly)
V r = 1 / (1.0 + eccentricity * c)
V rprime = s * r * r / l
V position = mulAdd(i, c, j, s) * r
V speed = mulAdd(i, rprime * c - r * s, j, rprime * s + r * c)
speed = normalize(speed) * sqrt(2.0 / r - 1.0 / semimajorAxis)
 
R [position, speed]
 
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])</syntaxhighlight>
 
{{out}}
<pre>
Position : (0.787295801, 0.45454549, 0)
Speed : (-0.5477226, 0.948683274, 0)
</pre>
 
=={{header|Ada}}==
{{Trans|Kotlin}}
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Generic_Real_Arrays;
with Ada.Numerics.Generic_Elementary_Functions;
 
procedure Orbit is
 
type Real is new Long_Float;
 
package Real_Arrays is
new Ada.Numerics.Generic_Real_Arrays (Real => Real);
use Real_Arrays;
 
package Math is
new Ada.Numerics.Generic_Elementary_Functions (Float_Type => Real);
 
subtype Vector_3D is Real_Vector (1 .. 3);
 
procedure Put (V : Vector_3D) is
package Real_IO is
new Ada.Text_Io.Float_IO (Num => Real);
begin
Put ("(");
Real_IO.Put (V (1), Exp => 0, Aft => 6); Put (",");
Real_IO.Put (V (2), Exp => 0, Aft => 6); Put (",");
Real_IO.Put (V (3), Exp => 0, Aft => 6); Put (")");
end Put;
 
function Mul_Add (V1 : Vector_3D;
X1 : Real;
V2 : Vector_3D;
X2 : Real)
return Vector_3D is
begin
return V1 * X1 + V2 * X2;
end Mul_Add;
 
procedure Rotate (R1 : out Vector_3D;
R2 : out Vector_3D;
I : Vector_3D;
J : Vector_3D;
Alpha : Real) is
begin
R1 := Mul_Add (I, +Math.Cos (Alpha), J, Math.Sin (Alpha));
R2 := Mul_Add (I, -Math.Sin (Alpha), J, Math.Cos (Alpha));
end Rotate;
 
type Orbital_State_Vectors is record
Position : Vector_3D;
Speed : Vector_3D;
end record;
 
function Calculate_Orbital_State
(Semimajor_Axis : Real;
Eccentricity : Real;
Inclination : Real;
Longitude_Of_Ascending_Node : Real;
Argument_Of_Periapsis : Real;
True_Anomaly : Real)
return Orbital_State_Vectors
is
I : Vector_3D := (1.0, 0.0, 0.0);
J : Vector_3D := (0.0, 1.0, 0.0);
K : constant Vector_3D := (0.0, 0.0, 1.0);
P_R1, P_R2 : Vector_3D;
State : Orbital_State_Vectors;
Position : Vector_3D renames State.Position;
Speed : Vector_3D renames State.Speed;
begin
Rotate (P_R1, P_R2, I, J, Longitude_Of_Ascending_Node);
I := P_R1; J := P_R2;
Rotate (P_R1, P_R2, J, K, Inclination);
J := P_R1;
Rotate (P_R1, P_R2, I, J, Argument_Of_Periapsis);
I := P_R1; J := P_R2;
declare
L : constant Real :=
Semimajor_Axis * (if (Eccentricity = 1.0) then 2.0
else (1.0 - Eccentricity * Eccentricity));
C : constant Real := Math.Cos (True_Anomaly);
S : constant Real := Math.Sin (True_Anomaly);
R : constant Real := L / (1.0 + Eccentricity * C);
Rprime : constant Real := S * R * R / L;
begin
Position := Mul_Add (I, C, J, S) * R;
Speed := Mul_Add (I, Rprime * C - R * S,
J, Rprime * S + R * C);
Speed := Speed / abs (Speed);
Speed := Speed * Math.Sqrt (2.0 / R - 1.0 / Semimajor_Axis);
end;
return State;
end Calculate_Orbital_State;
 
Longitude : constant Real := 355.000 / (113.000 * 6.000);
State : constant Orbital_State_Vectors :=
 
Calculate_Orbital_State
(Semimajor_Axis => 1.000,
Eccentricity => 0.100,
Inclination => 0.000,
Longitude_Of_Ascending_Node => Longitude,
Argument_Of_Periapsis => 0.000,
True_Anomaly => 0.000);
begin
Put ("Position : "); Put (State.Position); New_Line;
Put ("Speed : "); Put (State.Speed); New_Line;
end Orbit;</syntaxhighlight>
{{out}}
<pre>
Position : ( 0.779423, 0.450000, 0.000000)
Speed : (-0.552771, 0.957427, 0.000000)
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|Lua}} (which is a translation of C which is...)
<syntaxhighlight lang="algol68">
BEGIN # orbital elements #
 
MODE VECTOR = STRUCT( REAL x, y, z );
 
OP + = ( VECTOR v, w )VECTOR: ( x OF v + x OF w, y OF v + y OF w, z OF v + z OF w );
OP * = ( VECTOR v, REAL m )VECTOR: ( x OF v * m, y OF v * m, z OF v * m );
OP / = ( VECTOR v, REAL d )VECTOR: v * ( 1 / d );
OP ABS = ( VECTOR v )REAL: sqrt( x OF v * x OF v + y OF v * y OF v + z OF v * z OF v );
 
PROC muladd = ( VECTOR v1, v2, REAL x1, x2 )VECTOR: ( v1 * x1 ) + ( v2 * x2 );
PROC set = ( REF VECTOR v, w, []VECTOR ps )VOID: BEGIN v := ps[ LWB ps ]; w := ps[ UPB ps ] END;
PROC rotate = ( VECTOR i, j, REAL alpha )[]VECTOR:
( muladd( i, j, cos( alpha ), sin( alpha ) ), muladd( i, j, -sin( alpha ), cos( alpha ) ) );
PROC orbital state vectors = ( REAL semimajor axis, eccentricity, inclination
, longitude of ascending node, argument of periapsis
, true anomaly
, REF VECTOR position, speed
) VOID:
BEGIN
VECTOR i := ( 1.0, 0.0, 0.0 ), j := ( 0.0, 1.0, 0.0 ), k := ( 0.0, 0.0, 1.0 );
set( i, j, rotate( i, j, longitude of ascending node ) );
set( j, LOC VECTOR, rotate( j, k, inclination ) );
set( i, j, rotate( i, j, argument of periapsis ) );
REAL l = IF eccentricity /= 1 THEN 1 - eccentricity * eccentricity ELSE 2 FI
* semimajor axis;
REAL c = cos( true anomaly ), s = sin( true anomaly );
REAL r = l / ( 1.0 + eccentricity * c );
REAL rprime = s * r * r / l;
position := muladd( i, j, c, s ) * r;
speed := muladd( i, j, rprime * c - r * s, rprime * s + r * c );
speed := speed / ABS speed;
speed := speed * sqrt( 2 / r - 1 / semimajor axis )
END # orbital state vectors # ;
 
OP FMT = ( REAL v )STRING:
BEGIN
STRING result := fixed( ABS v, 0, 15 );
IF result[ LWB result ] = "." THEN "0" +=: result FI;
WHILE result[ UPB result ] = "0" DO result := result[ : UPB result - 1 ] OD;
IF result[ UPB result ] = "." THEN result := result[ : UPB result - 1 ] FI;
IF v < 0 THEN "-" + result ELSE result FI
END # FMT # ;
OP TOSTRING = ( VECTOR v )STRING: "(" + FMT x OF v + ", " + FMT y OF v + ", " + FMT z OF v + ")";
 
REAL longitude = 355 / ( 113 * 6 );
VECTOR position, speed;
orbital state vectors( 1.0, 0.1, 0.0, longitude, 0.0, 0.0, position, speed );
print( ( "Position : ", TOSTRING position, newline ) );
print( ( "Speed : ", TOSTRING speed ) )
END
</syntaxhighlight>
{{out}}
<pre>
Position : (0.77942284339868, 0.450000034653684, 0)
Speed : (-0.552770840960444, 0.957427083179762, 0)
</pre>
 
=={{header|ALGOL W}}==
{{Trans|C}} (which is a translation of Kotlin which is a translation of ...).
<syntaxhighlight lang="algolw">begin
% compute orbital elements %
% 3-element vector %
record Vector( real x, y, z );
% prints the components of the vector v %
procedure writeOnVector( reference(Vector) value v ) ;
writeon( r_format := "A", r_w := 10, r_d := 6, s_w := 0, "( ", x(v), ", ", y(v), ", ", z(v), " )" );
% returns a vector whose elements are the sum of the elements of v & w %
reference(Vector) procedure add( reference(Vector) value v, w ) ;
Vector( x(v) + x(w), y(v) + y(w), z(v) + z(w) );
% returns a vector whose elements are those of v multiplied by m %
reference(Vector) procedure mul( reference(Vector) value v
; real value m
) ;
Vector( x(v) * m, y(v) * m, z(v) * m );
% returns a vector whose elements are those of v divided by d %
reference(Vector) procedure divVR( reference(Vector) value v
; real value d
) ;
mul( v, 1 / d );
% returns the norm of the vector v %
real procedure vabs( reference(Vector) value v ) ;
sqrt( ( x(v) * x(v) ) + y(v) * y(v) + z(v) * z(v) );
% returns the sum of v1 * x1 and v2 * x2 %
reference(Vector) procedure mulAdd( reference(Vector) value v1, v2
; real value x1, x2
) ;
add( mul( v1, x1 ), mul( v2, x2 ) );
% sets ps to rotations of i and j by alpha %
procedure rotate( reference(Vector) value i, j
; real value alpha
; reference(Vector) array ps ( * )
) ;
begin
ps( 0 ) := mulAdd( i, j, cos( alpha ), sin( alpha ) );
ps( 1 ) := mulAdd( i, j, -sin( alpha ), cos( alpha ) )
end rotate ;
% sets position and speed vectors from the supplied elements %
procedure orbitalStateVectors( real value semimajorAxis
, eccentricity
, inclination
, longitudeOfAscendingNode
, argumentOfPeriapsis
, trueAnomaly
; reference(Vector) result position
, speed
) ;
begin
reference(Vector) i, j, k;
reference(Vector) array qs ( 0 :: 1 );
real L, c, s, r, rprime;
i := Vector( 1.0, 0.0, 0.0 );
j := Vector( 0.0, 1.0, 0.0 );
k := Vector( 0.0, 0.0, 1.0 );
L := 2.0;
rotate( i, j, longitudeOfAscendingNode, qs );
i := qs( 0 );
j := qs( 1 );
rotate( j, k, inclination, qs );
j := qs( 0 );
rotate( i, j, argumentOfPeriapsis, qs );
i := qs( 0 );
j := qs( 1 );
if eccentricity not = 1 then L := 1 - eccentricity * eccentricity;
l := L * semimajorAxis;
c := cos( trueAnomaly );
s := sin( trueAnomaly );
r := L / ( 1.0 + eccentricity * c );
rprime := s * r * r / L;
position := mulAdd( i, j, c, s );
position := mul( position, r ) ;
speed := mulAdd( i, j, rprime * c - r * s, rprime * s + r * c );
speed := divVR( speed, vabs( speed ) );
speed := mul( speed, sqrt( 2 / r - 1 / semimajorAxis ) );
end orbitalStateVectors ;
% test the orbitalStateVectors routine %
begin
real longitude;
reference(Vector) position, speed;
longitude := 355.0 / ( 113.0 * 6.0 );
orbitalStateVectors( 1.0, 0.1, 0.0, longitude, 0.0, 0.0, position, speed );
write( "Position : " ); writeOnVector( position );
write( "Speed : " ); writeOnVector( speed )
end
end.</syntaxhighlight>
{{out}}
<pre>
Position : ( 0.779422, 0.450000, 0.000000 )
Speed : ( -0.552770, 0.957427, 0.000000 )</pre>
 
=={{header|C}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
 
Line 109 ⟶ 420:
printf("Speed : %s\n", buffer);
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 116 ⟶ 427:
Speed : (-0.55277084096044382, 0.95742708317976177, 0)
</pre>
 
=={{header|C sharp|C#}}==
{{trans|D}}
<syntaxhighlight lang="csharp">using System;
 
namespace OrbitalElements {
class Vector {
public Vector(double x, double y, double z) {
X = x;
Y = y;
Z = z;
}
 
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
 
public double Abs() {
return Math.Sqrt(X * X + Y * Y + Z * Z);
}
 
public static Vector operator +(Vector lhs, Vector rhs) {
return new Vector(lhs.X + rhs.X, lhs.Y + rhs.Y, lhs.Z + rhs.Z);
}
 
public static Vector operator *(Vector self, double m) {
return new Vector(self.X * m, self.Y * m, self.Z * m);
}
 
public static Vector operator /(Vector self, double m) {
return new Vector(self.X / m, self.Y / m, self.Z / m);
}
 
public override string ToString() {
return string.Format("({0}, {1}, {2})", X, Y, Z);
}
}
 
class Program {
static Tuple<Vector, Vector> OrbitalStateVectors(
double semiMajorAxis,
double eccentricity,
double inclination,
double longitudeOfAscendingNode,
double argumentOfPeriapsis,
double trueAnomaly
) {
Vector mulAdd(Vector v1, double x1, Vector v2, double x2) {
return v1 * x1 + v2 * x2;
}
 
Tuple<Vector, Vector> rotate(Vector iv, Vector jv, double alpha) {
return new Tuple<Vector, Vector>(
mulAdd(iv, +Math.Cos(alpha), jv, Math.Sin(alpha)),
mulAdd(iv, -Math.Sin(alpha), jv, Math.Cos(alpha))
);
}
 
var i = new Vector(1, 0, 0);
var j = new Vector(0, 1, 0);
var k = new Vector(0, 0, 1);
 
var p = rotate(i, j, longitudeOfAscendingNode);
i = p.Item1; j = p.Item2;
p = rotate(j, k, inclination);
j = p.Item1;
p = rotate(i, j, argumentOfPeriapsis);
i = p.Item1; j = p.Item2;
 
var l = semiMajorAxis * ((eccentricity == 1.0) ? 2.0 : (1.0 - eccentricity * eccentricity));
var c = Math.Cos(trueAnomaly);
var s = Math.Sin(trueAnomaly);
var r = l / (1.0 + eccentricity * c);
var rprime = s * r * r / l;
var position = mulAdd(i, c, j, s) * r;
var speed = mulAdd(i, rprime * c - r * s, j, rprime * s + r * c);
speed /= speed.Abs();
speed *= Math.Sqrt(2.0 / r - 1.0 / semiMajorAxis);
 
return new Tuple<Vector, Vector>(position, speed);
}
 
static void Main(string[] args) {
var res = OrbitalStateVectors(1.0, 0.1, 0.0, 355.0 / (113.0 * 6.0), 0.0, 0.0);
Console.WriteLine("Position : {0}", res.Item1);
Console.WriteLine("Speed : {0}", res.Item2);
}
}
}</syntaxhighlight>
{{out}}
<pre>Position : (0.77942284339868, 0.450000034653684, 0)
Speed : (-0.552770840960444, 0.957427083179762, 0)</pre>
 
=={{header|C++}}==
{{trans|C#}}
<syntaxhighlight lang="cpp">#include <iostream>
#include <tuple>
 
class Vector {
private:
double _x, _y, _z;
 
public:
Vector(double x, double y, double z) : _x(x), _y(y), _z(z) {
// empty
}
 
double getX() {
return _x;
}
 
double getY() {
return _y;
}
 
double getZ() {
return _z;
}
 
double abs() {
return sqrt(_x * _x + _y * _y + _z * _z);
}
 
Vector operator+(const Vector& rhs) const {
return Vector(_x + rhs._x, _y + rhs._y, _z + rhs._z);
}
 
Vector operator*(double m) const {
return Vector(_x * m, _y * m, _z * m);
}
 
Vector operator/(double m) const {
return Vector(_x / m, _y / m, _z / m);
}
 
friend std::ostream& operator<<(std::ostream& os, const Vector& v);
};
 
std::ostream& operator<<(std::ostream& os, const Vector& v) {
return os << '(' << v._x << ", " << v._y << ", " << v._z << ')';
}
 
std::pair<Vector, Vector> orbitalStateVectors(
double semiMajorAxis,
double eccentricity,
double inclination,
double longitudeOfAscendingNode,
double argumentOfPeriapsis,
double trueAnomaly
) {
auto mulAdd = [](const Vector& v1, double x1, const Vector& v2, double x2) {
return v1 * x1 + v2 * x2;
};
 
auto rotate = [mulAdd](const Vector& iv, const Vector& jv, double alpha) {
return std::make_pair(
mulAdd(iv, +cos(alpha), jv, sin(alpha)),
mulAdd(iv, -sin(alpha), jv, cos(alpha))
);
};
 
Vector i(1, 0, 0);
Vector j(0, 1, 0);
Vector k(0, 0, 1);
 
auto p = rotate(i, j, longitudeOfAscendingNode);
i = p.first; j = p.second;
p = rotate(j, k, inclination);
j = p.first;
p = rotate(i, j, argumentOfPeriapsis);
i = p.first; j = p.second;
 
auto l = semiMajorAxis * ((eccentricity == 1.0) ? 2.0 : (1.0 - eccentricity * eccentricity));
auto c = cos(trueAnomaly);
auto s = sin(trueAnomaly);
auto r = l / (1.0 + eccentricity * c);;
auto rprime = s * r * r / l;
auto position = mulAdd(i, c, j, s) * r;
auto speed = mulAdd(i, rprime * c - r * s, j, rprime * s + r * c);
speed = speed / speed.abs();
speed = speed * sqrt(2.0 / r - 1.0 / semiMajorAxis);
 
return std::make_pair(position, speed);
}
 
int main() {
auto res = orbitalStateVectors(1.0, 0.1, 0.0, 355.0 / (113.0 * 6.0), 0.0, 0.0);
std::cout << "Position : " << res.first << '\n';
std::cout << "Speed : " << res.second << '\n';
 
return 0;
}</syntaxhighlight>
{{out}}
<pre>Position : (0.779423, 0.45, 0)
Speed : (-0.552771, 0.957427, 0)</pre>
 
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.math;
import std.stdio;
import std.typecons;
Line 207 ⟶ 713:
writeln("Position : ", res[0]);
writeln("Speed : ", res[1]);
}</langsyntaxhighlight>
{{out}}
<pre>Position : (0.7794228433986798, 0.4500000346536842, 0.0000000000000000)
Speed : (-0.5527708409604437, 0.9574270831797614, 0.0000000000000000)</pre>
 
Edit source/app.d to start your project.</pre>
=={{header|FreeBASIC}}==
{{trans|Phix}}
<syntaxhighlight lang="vbnet">Sub vabs(v() As Double, Byref result As Double)
result = 0
For i As Integer = Lbound(v) To Ubound(v)
result += v(i) * v(i)
Next i
result = Sqr(result)
End Sub
 
Sub mulAdd(v1() As Double, x1 As Double, v2() As Double, x2 As Double, result() As Double)
For i As Integer = Lbound(v1) To Ubound(v1)
result(i) = v1(i) * x1 + v2(i) * x2
Next i
End Sub
 
Sub rotate(i() As Double, j() As Double, alfa As Double, result1() As Double, result2() As Double)
Dim As Double ca = Cos(alfa), sa = Sin(alfa)
mulAdd(i(), ca, j(), sa, result1())
mulAdd(i(), -sa, j(), ca, result2())
End Sub
 
Sub orbitalStateVectors(semimajorAxis As Double, eccentricity As Double, inclination As Double, longitudeOfAscendingNode As Double, argumentOfPeriapsis As Double, trueAnomaly As Double)
Dim As Double i(1 To 3) = {1, 0, 0}
Dim As Double j(1 To 3) = {0, 1, 0}
Dim As Double k(1 To 3) = {0, 0, 1}
Dim As Double temp1(1 To 3), temp2(1 To 3)
Dim As Integer index, t
rotate(i(), j(), longitudeOfAscendingNode, temp1(), temp2())
For index = 1 To 3
i(index) = temp1(index)
j(index) = temp2(index)
Next index
rotate(j(), k(), inclination, temp1(), temp2())
For index = 1 To 3
j(index) = temp1(index)
Next index
rotate(i(), j(), argumentOfPeriapsis, temp1(), temp2())
For index = 1 To 3
i(index) = temp1(index)
j(index) = temp2(index)
Next index
Dim As Double l = Iif(eccentricity = 1, 2, 1 - eccentricity * eccentricity) * semimajorAxis
Dim As Double c = Cos(trueAnomaly), s = Sin(trueAnomaly)
Dim As Double r = 1 / (1 + eccentricity * c)
Dim As Double rprime = s * r * r / l
Dim As Double posn(1 To 3), speed(1 To 3), vabsResult
mulAdd(i(), c, j(), s, posn())
For t = Lbound(posn) To Ubound(posn)
posn(t) *= r
Next t
mulAdd(i(), rprime * c - r * s, j(), rprime * s + r * c, speed())
vabs(speed(), vabsResult)
mulAdd(speed(), 1 / vabsResult, speed(), 0, speed())
For t = Lbound(speed) To Ubound(speed)
speed(t) *= Sqr(2 / r - 1 / semimajorAxis)
Next t
Print Using "Position : {&, &, &}"; posn(1); posn(2); posn(3)
Print Using "Speed : {&, &, &}"; speed(1); speed(2); speed(3)
End Sub
 
orbitalStateVectors(1.0, 0.1, 0.0, 355.0 / (113.0 * 6.0), 0.0, 0.0)
 
Sleep</syntaxhighlight>
{{out}}
<pre>Position : {0.7872958014128079, 0.454545489549176, 0}
Speed : {-0.5477225996842874, 0.9486832736983857, 0}</pre>
 
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 286 ⟶ 862:
fmt.Println("Position :", position)
fmt.Println("Speed :", speed)
}</langsyntaxhighlight>
 
{{out}}
Line 293 ⟶ 869:
Speed : (-0.5527708409604438, 0.9574270831797618, 0)
</pre>
 
=={{header|J}}==
{{trans|Raku}}<syntaxhighlight lang="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 }}
X=: +/ .* NB. inner product
norm=: % %:@X~
 
orbitalStateVectors=: {{ 'a e i Om w f'=. y
NB. a: semi-major axis
NB. e: eccentricity
NB. i: inclination
NB. Om: Longitude of the ascending node
NB. w: argument of Periapsis (the other "omega")
NB. f: true anomaly (varies with time)
L=. a*2:`]@.*1-*:e
'c s'=. 2{.,F=. 2 R f
ra=. L % 1+ e*c
rp=. s*ra*ra%L
ijk=. F X (2 R w)X(0 R i)X(2 R Om)
position=. ra*{.ijk
speed=. (%:(2%ra)-%a)*norm(rp,ra,0) X ijk
position,:speed
}}</syntaxhighlight>
 
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:
 
<syntaxhighlight lang="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│
└────────────────────┴────────────────────┴────────────────────┘</syntaxhighlight>
 
Task example:<syntaxhighlight lang="j"> orbitalStateVectors 1 0.1 0 355r678 0 0
0.779423 0.45 0
_0.552771 0.957427 0</syntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight Javalang="java">public class OrbitalElements {
private static class Vector {
private double x, y, z;
Line 376 ⟶ 989:
System.out.printf("Speed : %s\n", ps[1]);
}
}</langsyntaxhighlight>
 
{{out}}
<pre>Position : (0.7794228433986797, 0.4500000346536842, 0.0000000000000000)
Speed : (-0.5527708409604438, 0.9574270831797618, 0.0000000000000000)</pre>
 
=={{header|jq}}==
{{trans|Wren}}
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq"># Array/vector operations
def addVectors: transpose | map(add);
 
def multiply($m): map(. * $m);
 
def divide($d): map(1/$d);
 
def abs: map(.*.) | add | sqrt;
 
def orbitalStateVectors(semimajorAxis; eccentricity; inclination;
longitudeOfAscendingNode; argumentOfPeriapsis; trueAnomaly):
def mulAdd($v1; $x1; $v2; $x2):
[($v1|multiply($x1)), ($v2|multiply($x2))] | addVectors;
def rotate($i; $j; $alpha):
[mulAdd($i; $alpha|cos; $j; $alpha|sin),
mulAdd($i; -$alpha|sin; $j; $alpha|cos)];
 
[1, 0, 0] as $i
| [0, 1, 0] as $j
| [0, 0, 1] as $k
| rotate($i; $j; longitudeOfAscendingNode) as [$i, $j]
| rotate($j; $k; inclination) as [$j, $_]
| rotate($i; $j; argumentOfPeriapsis) as [$i, $j]
| (semimajorAxis * (if (eccentricity == 1) then 2 else (1 - eccentricity * eccentricity) end)) as $l
| (trueAnomaly|cos) as $c
| (trueAnomaly|sin) as $s
| ($l / (1 + eccentricity * $c)) as $r
| ($s * $r * $r / $l) as $rprime
 
| mulAdd($i; $c; $j; $s) | multiply($r) as $position
 
| mulAdd($i; $rprime * $c - $r * $s; $j; $rprime * $s + $r * $c)
| divide(abs)
| multiply( ((2 / $r) - (1 / semimajorAxis))|sqrt) as $speed
| [$position, $speed] ;</syntaxhighlight>
'''The Task'''
<syntaxhighlight lang="jq">orbitalStateVectors(1; 0.1; 0; 355 / (113 * 6); 0; 0)
| "Position : \(.[0])",
"Speed : \(.[1])"</syntaxhighlight>
{{out}}
<pre>
Position : [0.7794228433986797,0.45000003465368416,0]
Speed : [1.228379551983482,1.228379551983482,1.228379551983482]
</pre>
 
=={{header|Julia}}==
{{trans|Kotlin}}
<syntaxhighlight lang="julia">using GeometryTypes
import Base.abs, Base.print
 
Vect = Point3
Base.abs(p::Vect) = sqrt(sum(x -> x*x, p))
Base.print(io::IO, p::Vect) = print(io, "(", p[1], ", ", p[2], ", ", p[3], ")")
muladd(v1, x1, v2, x2) = v1 * x1 + v2 * x2
rotate(i, j, a) = Pair(muladd(i, cos(a), j, sin(a)), muladd(i, -sin(a), j, cos(a)))
 
function orbitalStateVectors(semimajorAxis, eccentricity, inclination,
longitudeOfAscendingNode, argumentOfPeriapsis, trueAnomaly)
i, j, k = Vect(1.0, 0.0, 0.0), Vect(0.0, 1.0, 0.0), Vect(0.0, 0.0, 1.0)
p = rotate(i, j, longitudeOfAscendingNode)
i, j = p
p = rotate(j, k, inclination)
p = rotate(i, p[1], argumentOfPeriapsis)
i, j = p
l = semimajorAxis * (eccentricity == 1.0 ? 2.0 : (1.0 - eccentricity * eccentricity))
c, s = cos(trueAnomaly), sin(trueAnomaly)
r = l / (1.0 + eccentricity * c)
rprime, position = s * r * r / l, muladd(i, c, j, s) * r
speed = muladd(i, rprime * c - r * s, j, rprime * s + r * c)
speed /= abs(speed)
speed *= sqrt(2.0 / r - 1.0 / semimajorAxis)
return Pair(position, speed)
end
function testorbitalmath()
(position, speed) = orbitalStateVectors(1.0, 0.1, 0.0, 355.0 / (113.0 * 6.0), 0.0, 0.0)
println("Position : $position\nSpeed : $speed")
end
 
testorbitalmath()
</syntaxhighlight>{{out}}
<pre>
Position : (0.7794228433986797, 0.45000003465368416, 0.0)
Speed : (-0.5527708409604438, 0.9574270831797618, 0.0)
</pre>
 
=={{header|Kotlin}}==
{{trans|Sidef}}
<langsyntaxhighlight lang="scala">// version 1.1.4-3
 
class Vector(val x: Double, val y: Double, val z: Double) {
Line 447 ⟶ 1,154:
println("Position : $position")
println("Speed : $speed")
}</langsyntaxhighlight>
{{out}}
<pre>Position : (0.7794228433986797, 0.45000003465368416, 0.0)
Speed : (-0.5527708409604438, 0.9574270831797618, 0.0)</pre>
 
=={{header|Lua}}==
{{Trans|C}}...which is translation of Kotlin which is ...
<syntaxhighlight lang="lua">
do -- orbital elements
 
local function Vector( x, y, z )
return { x = x, y= y, z = z }
end
 
local function add( v, w )
return Vector( v.x + w.x, v.y + w.y, v.z + w.z )
end
 
local function mul( v, m )
return Vector( v.x * m, v.y * m, v.z * m )
end
 
local function div( v, d )
return mul( v, 1.0 / d )
end
 
local function vabs( v )
return math.sqrt( v.x * v.x + v.y * v.y + v.z * v.z )
end
 
local function mulAdd( v1, v2, x1, x2 )
return add( mul( v1, x1 ), mul( v2, x2 ) )
end
 
local function vecAsStr( v )
return string.format( "(%.17g", v.x )..string.format( ", %.17g", v.y )..string.format( ", %.17g)", v.z )
end
 
local function rotate( i, j, alpha )
return mulAdd( i, j, math.cos( alpha ), math.sin( alpha ) )
, mulAdd( i, j, -math.sin( alpha ), math.cos( alpha ) )
end
local function orbitalStateVectors( semimajorAxis, eccentricity, inclination
, longitudeOfAscendingNode, argumentOfPeriapsis
, trueAnomaly
)
 
local i, j, k = Vector( 1.0, 0.0, 0.0 ), Vector( 0.0, 1.0, 0.0 ), Vector( 0.0, 0.0, 1.0 )
local L = 2.0
i, j = rotate( i, j, longitudeOfAscendingNode )
j, _ = rotate( j, k, inclination )
i, j = rotate( i, j, argumentOfPeriapsis )
if eccentricity ~= 1.0 then L = 1.0 - eccentricity * eccentricity end
L = L * semimajorAxis
local c, s = math.cos( trueAnomaly ), math.sin( trueAnomaly )
local r = L / ( 1.0 + eccentricity * c )
local rprime = s * r * r / L;
local position = mul( mulAdd( i, j, c, s ), r )
local speed = mulAdd( i, j, rprime * c - r * s, rprime * s + r * c )
speed = div( speed, vabs( speed ) )
speed = mul( speed, math.sqrt( 2.0 / r - 1.0 / semimajorAxis ) )
return position, speed
end
 
local longitude = 355.0 / ( 113.0 * 6.0 )
local position, speed = orbitalStateVectors( 1.0, 0.1, 0.0, longitude, 0.0, 0.0 )
print( "Position : "..vecAsStr( position ) )
print( "Speed : "..vecAsStr( speed ) )
end</syntaxhighlight>
{{out}}
<pre>
Position : (0.77942284339867973, 0.45000003465368416, 0)
Speed : (-0.55277084096044382, 0.95742708317976177, 0)
</pre>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<syntaxhighlight lang="nim">import math, strformat
 
type Vector = tuple[x, y, z: float]
 
func `+`(v1, v2: Vector): Vector = (v1.x + v2.x, v1.y + v2.y, v1.z + v2.z)
func `*`(v: Vector; m: float): Vector = (v.x * m, v.y * m, v.z * m)
func `*=`(v: var Vector; m: float) = v.x *= m; v.y *= m; v.z *= m
func `/=`(v: var Vector; d: float) = v.x /= d; v.y /= d; v.z /= d
func abs(v: Vector): float = sqrt(v.x * v.x + v.y * v.y + v.z * v.z)
func `$`(v: Vector): string = &"({v.x}, {v.y}, {v.z})"
 
func orbitalStateVectors(semimajorAxis: float,
eccentricity: float,
inclination: float,
longitudeOfAscendingNode: float,
argumentOfPeriapsis: float,
trueAnomaly: float): tuple[position, speed: Vector] =
 
var
i: Vector = (1.0, 0.0, 0.0)
j: Vector = (0.0, 1.0, 0.0)
k: Vector = (0.0, 0.0, 1.0)
 
 
func mulAdd(v1: Vector; x1: float; v2: Vector; x2: float): Vector = v1 * x1 + v2 * x2
 
func rotate(a, b: Vector; alpha: float): (Vector, Vector) =
(mulAdd(a, cos(alpha), b, sin(alpha)), mulAdd(a, -sin(alpha), b, cos(alpha)))
 
var p = rotate(i, j, longitudeOfAscendingNode)
(i, j) = p
p = rotate(j, k, inclination)
j = p[0]
p = rotate(i, j, argumentOfPeriapsis)
(i, j) = p
 
let
l = semimajorAxis * (if eccentricity == 1: 2.0 else: 1.0 - eccentricity * eccentricity)
c = cos(trueAnomaly)
s = sin(trueAnomaly)
r = l / (1.0 + eccentricity * c)
rprime = s * r * r / l
 
result.position = mulAdd(i, c, j, s) * r
result.speed = mulAdd(i, rprime * c - r * s, j, rprime * s + r * c)
result.speed /= abs(result.speed)
result.speed *= sqrt(2 / r - 1 / semimajorAxis)
 
 
let (position, speed) = orbitalStateVectors(semimajorAxis = 1.0,
eccentricity = 0.1,
inclination = 0.0,
longitudeOfAscendingNode = 355.0 / (113.0 * 6.0),
argumentOfPeriapsis = 0.0,
trueAnomaly = 0.0)
echo "Position: ", position
echo "Speed: ", speed</syntaxhighlight>
 
{{out}}
<pre>Position: (0.7794228433986797, 0.4500000346536842, 0.0)
Speed: (-0.5527708409604438, 0.9574270831797618, 0.0)</pre>
 
=={{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)
Say "Position :" ps~x~tostring
Say "Speed :" ps~y~tostring
Say 'Perl6Raku:'
pi=rxCalcpi(16)
ps=orbitalStateVectors(1,.1,pi/18,pi/6,pi/4,0) /*Perl6Raku*/
Say "Position :" ps~x~tostring
Say "Speed :" ps~y~tostring
Line 560 ⟶ 1,402:
Return res
 
::requires 'rxmath' LIBRARY</langsyntaxhighlight>
 
{{out}}
<pre>Position : (0.7794228433986798,0.4500000346536842,0)
Speed : (-0.5527708409604436,0.9574270831797613,0)
Raku:
Perl6:
Position : (0.2377712839822067,0.8609602616977158,0.1105090235720755)
Speed : (-1.061933017480060,0.2758500205692495,0.1357470248655981)</pre>
 
=={{header|Perl}}==
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use Math::Vector::Real;
Line 630 ⟶ 1,472:
0, # argument of periapsis
0 # true-anomaly
;</langsyntaxhighlight>
{{out}}
<pre>$VAR1 = {
Line 645 ⟶ 1,487:
};</pre>
 
=={{header|Perl 6}}==
We'll use the [https://github.com/grondilu/clifford Clifford geometric algebra library] but only for the vector operations.
<lang perl6>sub orbital-state-vectors(
Real :$semimajor-axis where * >= 0,
Real :$eccentricity where * >= 0,
Real :$inclination,
Real :$longitude-of-ascending-node,
Real :$argument-of-periapsis,
Real :$true-anomaly
) {
use Clifford;
my ($i, $j, $k) = @e[^3];
 
sub rotate($a is rw, $b is rw, Real \α) {
($a, $b) = cos(α)*$a + sin(α)*$b, -sin(α)*$a + cos(α)*$b;
}
rotate($i, $j, $longitude-of-ascending-node);
rotate($j, $k, $inclination);
rotate($i, $j, $argument-of-periapsis);
 
my \l = $eccentricity == 1 ?? # PARABOLIC CASE
2*$semimajor-axis !!
$semimajor-axis*(1 - $eccentricity**2);
 
my ($c, $s) = .cos, .sin given $true-anomaly;
 
my \r = l/(1 + $eccentricity*$c);
my \rprime = $s*r**2/l;
 
my $position = r*($c*$i + $s*$j);
 
my $speed =
(rprime*$c - r*$s)*$i + (rprime*$s + r*$c)*$j;
$speed /= sqrt($speed**2);
$speed *= sqrt(2/r - 1/$semimajor-axis);
 
{ :$position, :$speed }
}
 
say orbital-state-vectors
semimajor-axis => 1,
eccentricity => 0.1,
inclination => pi/18,
longitude-of-ascending-node => pi/6,
argument-of-periapsis => pi/4,
true-anomaly => 0;</lang>
{{out}}
<pre>{position => 0.237771283982207*e0+0.860960261697716*e1+0.110509023572076*e2, speed => -1.06193301748006*e0+0.27585002056925*e1+0.135747024865598*e2}</pre>
=={{header|Phix}}==
{{trans|Python}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function vabs(sequence v)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
return sqrt(sum(sq_power(v,2)))
<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>
end function
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)))</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function mulAdd(sequence v1, atom x1, sequence v2, atom x2)
return sq_add(sq_mul(v1,x1),sq_mul(v2,x2))
<span style="color: #008080;">function</span> <span style="color: #000000;">mulAdd</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">v1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">v2</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">x2</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">))</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function rotate(sequence i, j, atom alpha)
atom ca = cos(alpha),
<span style="color: #008080;">function</span> <span style="color: #000000;">rotate</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">alpha</span><span style="color: #0000FF;">)</span>
sa = sin(alpha)
<span style="color: #004080;">atom</span> <span style="color: #000000;">ca</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">alpha</span><span style="color: #0000FF;">),</span>
return {mulAdd(i,ca,j,sa),mulAdd(i,-sa,j,ca)}
<span style="color: #000000;">sa</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">alpha</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">mulAdd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ca</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sa</span><span style="color: #0000FF;">),</span><span style="color: #000000;">mulAdd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">sa</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ca</span><span style="color: #0000FF;">)}</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
procedure orbitalStateVectors(atom semimajorAxis, eccentricity, inclination, longitudeOfAscendingNode, argumentOfPeriapsis, trueAnomaly)
sequence i = {1, 0, 0},
<span style="color: #008080;">procedure</span> <span style="color: #000000;">orbitalStateVectors</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">semimajorAxis</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">eccentricity</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">inclination</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">longitudeOfAscendingNode</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">argumentOfPeriapsis</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">trueAnomaly</span><span style="color: #0000FF;">)</span>
j = {0, 1, 0},
<span style="color: #004080;">sequence</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">},</span>
k = {0, 0, 1}
<span style="color: #000000;">j</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
{i,j} = rotate(i, j, longitudeOfAscendingNode)
{j} = rotate(j, k, inclination)
<span style="color: #0000FF;">{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rotate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">longitudeOfAscendingNode</span><span style="color: #0000FF;">)</span>
{i,j} = rotate(i, j, argumentOfPeriapsis)
<span style="color: #0000FF;">{</span><span style="color: #000000;">j</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rotate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">inclination</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rotate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">argumentOfPeriapsis</span><span style="color: #0000FF;">)</span>
atom l = iff(eccentricity=1?2:1-eccentricity*eccentricity)*semimajorAxis,
c = cos(trueAnomaly),
<span style="color: #004080;">atom</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">eccentricity</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #000000;">2</span><span style="color: #0000FF;">:</span><span style="color: #000000;">1</span><span style="color: #0000FF;">-</span><span style="color: #000000;">eccentricity</span><span style="color: #0000FF;">*</span><span style="color: #000000;">eccentricity</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">semimajorAxis</span><span style="color: #0000FF;">,</span>
s = sin(trueAnomaly),
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">trueAnomaly</span><span style="color: #0000FF;">),</span>
r = 1 / (1+eccentricity*c),
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">trueAnomaly</span><span style="color: #0000FF;">),</span>
rprime = s * r * r / l
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">+</span><span style="color: #000000;">eccentricity</span><span style="color: #0000FF;">*</span><span style="color: #000000;">c</span><span style="color: #0000FF;">),</span>
sequence posn = sq_mul(mulAdd(i, c, j, s),r),
<span style="color: #000000;">rprime</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">l</span>
speed = mulAdd(i, rprime*c-r*s, j, rprime*s+r*c)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">posn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mulAdd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">),</span><span style="color: #000000;">r</span><span style="color: #0000FF;">),</span>
speed = sq_div(speed,vabs(speed))
<span style="color: #000000;">speed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mulAdd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rprime</span><span style="color: #0000FF;">*</span><span style="color: #000000;">c</span><span style="color: #0000FF;">-</span><span style="color: #000000;">r</span><span style="color: #0000FF;">*</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rprime</span><span style="color: #0000FF;">*</span><span style="color: #000000;">s</span><span style="color: #0000FF;">+</span><span style="color: #000000;">r</span><span style="color: #0000FF;">*</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
speed = sq_mul(speed,sqrt(2/r - 1/semimajorAxis))
<span style="color: #000000;">speed</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">speed</span><span style="color: #0000FF;">,</span><span style="color: #000000;">vabs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">speed</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">speed</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">speed</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">/</span><span style="color: #000000;">r</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">semimajorAxis</span><span style="color: #0000FF;">))</span>
puts(1,"Position :") ?posn
puts(1,"Speed :") ?speed
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Position :"</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">posn</span>
end procedure
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Speed :"</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">speed</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
orbitalStateVectors(1.0, 0.1, 0.0, 355.0 / (113.0 * 6.0), 0.0, 0.0)</lang>
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 738 ⟶ 1,535:
Speed :{-0.5477225997,0.9486832737,0}
</pre>
 
=={{header|Prolog}}==
{{trans|C#}}
 
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).
 
<syntaxhighlight lang="prolog">:- use_module(library(clpr)).
 
v3_add(v(X1,Y1,Z1),v(X2,Y2,Z2),v(X,Y,Z)) :-
{ X = X1 + X2 },
{ Y = Y1 + Y2 },
{ Z = Z1 + Z2 }.
v3_mul(v(X1,Y1,Z1),M,v(X,Y,Z)) :-
{ X = X1 * M },
{ Y = Y1 * M },
{ Z = Z1 * M }.
v3_muladd(V1,X1,V2,X2,R) :-
v3_mul(V1,X1,V1X1),
v3_mul(V2,X2,V2X2),
v3_add(V1X1,V2X2,R).
v3_rotate(IV, JV, Alpha, R1, R2) :-
{ SinA = sin(Alpha) },
{ CosA = cos(Alpha) },
{ NegSinA = 0 - SinA },
v3_muladd(IV, CosA, JV, SinA, R1),
v3_muladd(IV, NegSinA, JV, CosA, R2).
v3_abs(v(X,Y,Z), Abs) :- { Abs = (X * X + Y * Y + Z * Z) ^ 0.5 }.
orbital_state_vectors(
o(SemiMajor,Ecc,Inc,LongAscNode,ArgPer,TrueAnon),
Position,
Speed) :-
v3_rotate(v(1,0,0),v(0,1,0),LongAscNode,I1,J1),
v3_rotate(J1,v(0,0,1),Inc,J2,_),
v3_rotate(I1,J2,ArgPer,I,J),
find_l(Ecc, SemiMajor, L),
{ C = cos(TrueAnon) },
{ S = sin(TrueAnon) },
{ R = L / (1.0 + Ecc * C ) },
{ RPrime = S * R * R / L },
v3_muladd(I, C, J, S, P1),
v3_mul(P1, R, Position),
{ SpeedIr = RPrime * C - R * S },
{ SpeedJr = RPrime * S + R * C },
v3_muladd(I, SpeedIr, J, SpeedJr, SpeedA),
v3_abs(SpeedA, SpeedAbs),
v3_mul(SpeedDiv, SpeedAbs, SpeedA),
{ Sf = (2.0 / R - 1.0 / SemiMajor ) ^ 0.5 },
v3_mul(SpeedDiv, Sf, Speed).
find_l(1.0, SemiMajor, L) :-
{ L = SemiMajor * 2.0 }.
find_l(Ecc, SemiMajor, L) :-
dif(Ecc,1.0),
{ L = SemiMajor * (1.0 - Ecc * Ecc) }.</syntaxhighlight>
{{out}}
<pre>
?- { T = 355 / (113 * 6) }, orbital_state_vectors(o(1.0,0.1,0.0,T,0,0), P, S).
T = 0.523598820058997,
P = v(0.7794228433986797, 0.45000003465368416, 0.0),
S = v(-0.5527708409604438, 0.9574270831797618, 0.0) .
 
?-</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import math
 
class Vector:
Line 798 ⟶ 1,668:
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)
Speed : (-0.547722599684, 0.948683273698, 0.0)</pre>
 
=={{header|Raku}}==
(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" line>sub orbital-state-vectors(
Real :$semimajor-axis where * >= 0,
Real :$eccentricity where * >= 0,
Real :$inclination,
Real :$longitude-of-ascending-node,
Real :$argument-of-periapsis,
Real :$true-anomaly
) {
use Clifford;
my ($i, $j, $k) = @e[^3];
 
sub rotate($a is rw, $b is rw, Real \α) {
($a, $b) = cos(α)*$a + sin(α)*$b, -sin(α)*$a + cos(α)*$b;
}
rotate($i, $j, $longitude-of-ascending-node);
rotate($j, $k, $inclination);
rotate($i, $j, $argument-of-periapsis);
 
my \l = $eccentricity == 1 ?? # PARABOLIC CASE
2*$semimajor-axis !!
$semimajor-axis*(1 - $eccentricity**2);
 
my ($c, $s) = .cos, .sin given $true-anomaly;
 
my \r = l/(1 + $eccentricity*$c);
my \rprime = $s*r**2/l;
 
my $position = r*($c*$i + $s*$j);
 
my $speed =
(rprime*$c - r*$s)*$i + (rprime*$s + r*$c)*$j;
$speed /= sqrt($speed**2);
$speed *= sqrt(2/r - 1/$semimajor-axis);
 
{ :$position, :$speed }
}
 
say orbital-state-vectors
semimajor-axis => 1,
eccentricity => 0.1,
inclination => pi/18,
longitude-of-ascending-node => pi/6,
argument-of-periapsis => pi/4,
true-anomaly => 0;</syntaxhighlight>
{{out}}
<pre>{position => 0.237771283982207*e0+0.860960261697716*e1+0.110509023572076*e2, speed => -1.06193301748006*e0+0.27585002056925*e1+0.135747024865598*e2}</pre>
 
=={{header|REXX}}==
Line 807 ⟶ 1,727:
{{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 942 ⟶ 1,862:
End
Numeric Digits prec
Return r+0</langsyntaxhighlight>
{{out}}
<pre>Position : (0.7794228433986798,0.4500000346536842,0)
Line 948 ⟶ 1,868:
 
===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 964 ⟶ 1,884:
say ' true anomaly:' fmt(anomaly)
i= 1 0 0; j= 0 1 0; k= 0 0 1 /*define the I, J, K vectors.*/
parse value rot(i, j, node) with with i '~' j /*rotate ascending node longitude.*/
parse value rot(j, k, inclination) with j '~' /*rotate the inclination. */
parse value rot(i, j, periapsis) with i '~' j /*rotate the argument of periapsis*/
Line 975 ⟶ 1,895:
speed= mulV( divV( speed, absV(speed) ), sqrt(2 / r - 1 / semiMaj) )
say ' position:' show( mulV( MA(i, c, j, s), r) )
say ' speed:' show( speed); return
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
absV: procedure; parse arg x y z; return sqrt(x**2 + y**2 + z**2)
Line 987 ⟶ 1,906:
rot: procedure; parse arg i,j,$; return MA(i,cos($),j,sin($))'~'MA(i, -sin($), j, cos($))
r2r: return arg(1) // (pi() * 2) /*normalize radians ──► a unit circle*/
.sinCos: arg z 1 _,i; do k=2 by 2 until p=z; p=z; _= -_*$ /(k*(k+i)); z=z+_; end; return z
/*──────────────────────────────────────────────────────────────────────────────────────*/
cos: procedure; arg x; x= r2r(x); if x=0 then return 1; a= abs(x); Hpi= pi * .5
numeric fuzz min(6, digits() - 3); if a=pi then return '-1'
if a=Hpi | a=Hpi*3 then return 0; if a=pi / 3 then return .5
if a=pi * 2 / 3 then return '-.5'; $= x * x; return .sinCos(1, '-1')
/*──────────────────────────────────────────────────────────────────────────────────────*/
sin: procedure; arg x; x= r2r(x); numeric fuzz min(5, max(1, digits() - 3) )
if x=0 then return 0; if x=pi*.5 then return 1; if x==pi*1.5 then return '-1'
if abs(x)=pi then return 0; $= x * x; return .sinCos(x, 1)
/*──────────────────────────────────────────────────────────────────────────────────────*/
.sinCos: parse arg z 1 _,i; xx= x*x
do k=2 by 2 until p=z; p=z; _= -_ * xx / (k*(k+i)); z= z+_; end; return z
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; arg x; if x=0 then return 0; d= digits(); numeric form; m.= 9; h= d+6
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,028 ⟶ 1,945:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">import scala.language.existentials
 
object OrbitalElements extends App {
Line 1,070 ⟶ 1,987:
}
 
}</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,083 ⟶ 2,000:
true_anomaly
) {
 
static vec = frequire('Math::Vector::Real')
var (i, j, k) = (vec.V(1,0,0), vec.V(0,1,0), vec.V(0,0,1))
Vector(1, 0, 0),
Vector(0, 1, 0),
Vector(0, 0, 1),
)
 
func muladd(v1, x1, v2, x2) {
(v1.mul( * x1).add + (v2.mul( * x2))
}
 
Line 1,108 ⟶ 2,029:
var r = l/(1 + eccentricity*c)
var rprime = (s * r**2 / l)
var position = muladd(i, c, j, s).mul(*r)
 
var speed = muladd(i, rprime*c - r*s, j, rprime*s + r*c)
speed /= speed.div!(abs(speed))
speed.mul!( *= sqrt(2/r - 1/semimajor_axis))
 
struct Result { position, speed }
Result([position, speed].map {|v| [v{:module}[]].map{Num(_)} }...)
}
 
for args in ([
var r = orbital_state_vectors(
[1, 0.1, 0, 355/(113*6), 0, 0],
semimajor_axis: 1,
[1, 0.1, Num.pi/18, Num.pi/6, Num.pi/4, 0]
eccentricity: 0.1,
]) {
inclination: 0,
var r = orbital_state_vectors(args...)
longitude_of_ascending_node: 355/(113*6),
 
argument_of_periapsis: 0,
true_anomalysay "Arguments: 0,#{args}:"
say "Position : #{r.position}"
say "Speed : #{r.speed}\n"
}</syntaxhighlight>
{{out}}
<pre>
Arguments: [1, 1/10, 0, 355/678, 0, 0]:
Position : Vector(0.779422843398679832042176328223663037464703527986, 0.450000034653684237432302249506712706822033851071, 0)
Speed : Vector(-0.552770840960443759673279062314259546277084494097, 0.957427083179761535246200368614952095349966503287, 0)
 
Arguments: [1, 1/10, 0.174532925199432957692369076848861271344287188854, 0.523598775598298873077107230546583814032861566563, 0.785398163397448309615660845819875721049292349844, 0]:
Position : Vector(0.23777128398220654779107184959165027147748809404, 0.860960261697715834668966272382699039216399966872, 0.110509023572075562109405412890808505271310143909)
Speed : Vector(-1.06193301748006004757467368094494935655538772696, 0.275850020569249507846452830330085489348356659642, 0.135747024865598167166145512759280712986072818844)
</pre>
 
=={{header|Swift}}==
 
{{trans|Kotlin}}
 
<syntaxhighlight lang="swift">import Foundation
 
public struct Vector {
public var x = 0.0
public var y = 0.0
public var z = 0.0
 
public init(x: Double, y: Double, z: Double) {
(self.x, self.y, self.z) = (x, y, z)
}
 
public func mod() -> Double {
(x * x + y * y + z * z).squareRoot()
}
 
public static func + (lhs: Vector, rhs: Vector) -> Vector {
return Vector(
x: lhs.x + rhs.x,
y: lhs.y + rhs.y,
z: lhs.z + rhs.z
)
}
 
public static func * (lhs: Vector, rhs: Double) -> Vector {
return Vector(
x: lhs.x * rhs,
y: lhs.y * rhs,
z: lhs.z * rhs
)
}
 
public static func *= (lhs: inout Vector, rhs: Double) {
lhs.x *= rhs
lhs.y *= rhs
lhs.z *= rhs
}
 
public static func / (lhs: Vector, rhs: Double) -> Vector {
return lhs * (1 / rhs)
}
 
public static func /= (lhs: inout Vector, rhs: Double) {
lhs = lhs * (1 / rhs)
}
}
 
extension Vector: CustomStringConvertible {
public var description: String {
return String(format: "%.6f\t%.6f\t%.6f", x, y, z)
}
}
 
private func mulAdd(v1: Vector, x1: Double, v2: Vector, x2: Double) -> Vector {
return v1 * x1 + v2 * x2
}
 
private func rotate(_ i: Vector, _ j: Vector, alpha: Double) -> (Vector, Vector) {
return (
mulAdd(v1: i, x1: +cos(alpha), v2: j, x2: sin(alpha)),
mulAdd(v1: i, x1: -sin(alpha), v2: j, x2: cos(alpha))
)
}
 
public func orbitalStateVectors(
semimajorAxis: Double,
eccentricity: Double,
inclination: Double,
longitudeOfAscendingNode: Double,
argumentOfPeriapsis: Double,
trueAnomaly: Double
) -> (Vector, Vector) {
var i = Vector(x: 1.0, y: 0.0, z: 0.0)
var j = Vector(x: 0.0, y: 1.0, z: 0.0)
let k = Vector(x: 0.0, y: 0.0, z: 1.0)
 
(i, j) = rotate(i, j, alpha: longitudeOfAscendingNode)
(j, _) = rotate(j, k, alpha: inclination)
(i, j) = rotate(i, j, alpha: argumentOfPeriapsis)
 
let l = eccentricity == 1.0 ? 2.0 : 1.0 - eccentricity * eccentricity
let c = cos(trueAnomaly)
let s = sin(trueAnomaly)
let r = l / (1.0 + eccentricity * c)
let rPrime = s * r * r / l
let position = mulAdd(v1: i, x1: c, v2: j, x2: s) * r
var speed = mulAdd(v1: i, x1: rPrime * c - r * s, v2: j, x2: rPrime * s + r * c)
 
speed /= speed.mod()
speed *= (2.0 / r - 1.0 / semimajorAxis).squareRoot()
 
return (position, speed)
}
 
let (position, speed) = orbitalStateVectors(
semimajorAxis: 1.0,
eccentricity: 0.1,
inclination: 0.0,
longitudeOfAscendingNode: 355.0 / (113.0 * 6.0),
argumentOfPeriapsis: 0.0,
trueAnomaly: 0.0
)
 
print("Position: \(position); Speed: \(speed)")</syntaxhighlight>
say '['+r.position.join(', ')+']'
 
say '['+r.speed.join(', ')+']'</lang>
{{out}}
 
<pre>[0.77942284339868, 0.450000034653684, 0]
<pre>Position: 0.779423 0.450000 0.000000; Speed: -0.552771 0.957427 0.000000</pre>
[-0.552770840960444, 0.957427083179761, 0]</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-vector}}
<syntaxhighlight lang="wren">import "./vector" for Vector3
 
var orbitalStateVectors = Fn.new { |semimajorAxis, eccentricity, inclination,
longitudeOfAscendingNode, argumentOfPeriapsis, trueAnomaly|
var i = Vector3.new(1, 0, 0)
var j = Vector3.new(0, 1, 0)
var k = Vector3.new(0, 0, 1)
 
var mulAdd = Fn.new { |v1, x1, v2, x2| v1 * x1 + v2 * x2 }
 
var rotate = Fn.new { |i, j, alpha|
return [mulAdd.call(i, alpha.cos, j, alpha.sin),
mulAdd.call(i, -alpha.sin, j, alpha.cos)]
}
 
var p = rotate.call(i, j, longitudeOfAscendingNode)
i = p[0]
j = p[1]
p = rotate.call(j, k, inclination)
j = p[0]
p = rotate.call(i, j, argumentOfPeriapsis)
i = p[0]
j = p[1]
 
var l = semimajorAxis * ((eccentricity == 1) ? 2 : (1 - eccentricity * eccentricity))
var c = trueAnomaly.cos
var s = trueAnomaly.sin
var r = l / (1 + eccentricity * c)
var rprime = s * r * r / l
var position = mulAdd.call(i, c, j, s) * r
var speed = mulAdd.call(i, rprime * c - r * s, j, rprime * s + r * c)
speed = speed / speed.length
speed = speed * (2 / r - 1 / semimajorAxis).sqrt
return [position, speed]
}
 
var ps = orbitalStateVectors.call(1, 0.1, 0, 355 / (113 * 6), 0, 0)
System.print("Position : %(ps[0])")
System.print("Speed : %(ps[1])")</syntaxhighlight>
 
{{out}}
<pre>
Position : (0.77942284339868, 0.45000003465368, 0)
Speed : (-0.55277084096044, 0.95742708317976, 0)
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">include xpllib; \for VCopy, VAdd, VMul, VDiv, VMag and Print
 
proc VShow(A);
real A;
Print("(%1.6f, %1.6f, %1.6f)\n", A(0), A(1), A(2));
 
func real VMulAdd(V0, V1, V2, X1, X2);
real V0, V1, V2, X1, X2, V3(3), V4(3);
return VAdd(V0, VMul(V3, V1, X1), VMul(V4, V2, X2));
 
proc Rotate(I, J, Alpha, PS);
real I, J, Alpha, PS;
[VMulAdd(PS(0), I, J, Cos(Alpha), Sin(Alpha));
VMulAdd(PS(1), I, J, -Sin(Alpha), Cos(Alpha));
];
 
proc OrbitalStateVectors; real SemiMajorAxis, Eccentricity, Inclination,
LongitudeOfAscendingNode, ArgumentOfPeriapsis, TrueAnomaly, PS;
real I, J, K, L, QS(2,3), C, S, R, RPrime;
[I:= [1.0, 0.0, 0.0];
J:= [0.0, 1.0, 0.0];
K:= [0.0, 0.0, 1.0];
L:= 2.0;
Rotate(I, J, LongitudeOfAscendingNode, QS);
VCopy(I, QS(0)); VCopy(J, QS(1));
Rotate(J, K, Inclination, QS);
VCopy(J, QS(0));
Rotate(I, J, ArgumentOfPeriapsis, QS);
VCopy(I, QS(0)); VCopy(J, QS(1));
if Eccentricity # 1.0 then L:= 1.0 - Eccentricity*Eccentricity;
L:= L * SemiMajorAxis;
C:= Cos(TrueAnomaly);
S:= Sin(TrueAnomaly);
R:= L / (1.0 + Eccentricity*C);
RPrime:= S * R * R / L;
VMulAdd(PS(0), I, J, C, S);
VMul(PS(0), PS(0), R);
VMulAdd(PS(1), I, J, RPrime*C - R*S, RPrime*S + R*C);
VDiv(PS(1), PS(1), VMag(PS(1)));
VMul(PS(1), PS(1), sqrt(2.0/R - 1.0/SemiMajorAxis));
];
 
def Longitude = 355.0 / (113.0 * 6.0);
real PS(2,3);
[OrbitalStateVectors(1.0, 0.1, 0.0, Longitude, 0.0, 0.0, PS);
Print("Position : "); VShow(PS(0));
Print("Speed : "); VShow(PS(1));
]</syntaxhighlight>
{{out}}
<pre>
Position : (0.779423, 0.450000, 0.000000)
Speed : (-0.552771, 0.957427, 0.000000)
</pre>
 
=={{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 1,164 ⟶ 2,308:
return(position,speed);
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">orbital_state_vectors(
1.0, # semimajor axis
0.1, # eccentricity
Line 1,172 ⟶ 2,316:
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>
2,122

edits