Vector products: Difference between revisions

Updated D code
(→‎{{header|Pascal}}: add example)
(Updated D code)
Line 388:
 
=={{header|D}}==
<lang d>import std.stdio, std.algorithm, std.conv, std.numeric;
 
immutable struct V3 {
union {
static struct { double x, y, z; }
double[3] v;
}
 
pure nothrow double dot(in V3 rhs) const pure nothrow {
return dotProduct(v, rhs.v);
}
 
pure nothrow V3 cross(in V3 rhs) const pure nothrow {
return V3(y*rhs.z - z*rhs.y,
z*rhs.x - x*rhs.z,
x*rhs.y - y*rhs.x);
}
 
string toString() const { return text(v); }
}
 
pure nothrow double scalarTriple(in V3 a, in V3 b, in V3 c) pure nothrow {
return a.dot(b.cross(c));
}
 
pure nothrow V3 vectorTriple(in V3 a, in V3 b, in V3 c) pure nothrow {
return a.cross(b.cross(c));
}
 
void main() {
constimmutable V3 a = {3, 4, 5},
b = {4, 3, 5},
c = {-5, -12, -13};
writeln("a = ", a);
writeln("b = ", b);
Anonymous user