Vector products: Difference between revisions

Content deleted Content added
Updated D code
Line 388: Line 388:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio, std.algorithm, std.conv, std.numeric;
<lang d>import std.stdio, std.conv, std.numeric;

struct V3 {
immutable struct V3 {
union {
union {
struct { double x, y, z; }
static struct { double x, y, z; }
double[3] v;
double[3] v;
}
}

pure nothrow double dot(in V3 rhs) const {
double dot(in V3 rhs) const pure nothrow {
return dotProduct(v, rhs.v);
return dotProduct(v, rhs.v);
}
}

pure nothrow V3 cross(in V3 rhs) const {
V3 cross(in V3 rhs) const pure nothrow {
return V3(y*rhs.z - z*rhs.y,
return V3(y*rhs.z - z*rhs.y,
z*rhs.x - x*rhs.z,
z*rhs.x - x*rhs.z,
x*rhs.y - y*rhs.x);
x*rhs.y - y*rhs.x);
}
}

string toString() const { return text(v); }
string toString() const { return text(v); }
}
}

pure nothrow double scalarTriple(in V3 a, in V3 b, in V3 c) {
double scalarTriple(in V3 a, in V3 b, in V3 c) pure nothrow {
return a.dot(b.cross(c));
return a.dot(b.cross(c));
}
}

pure nothrow V3 vectorTriple(in V3 a, in V3 b, in V3 c) {
V3 vectorTriple(in V3 a, in V3 b, in V3 c) pure nothrow {
return a.cross(b.cross(c));
return a.cross(b.cross(c));
}
}

void main() {
void main() {
const V3 a = {3, 4, 5},
immutable V3 a = {3, 4, 5},
b = {4, 3, 5},
b = {4, 3, 5},
c = {-5, -12, -13};
c = {-5, -12, -13};
writeln("a = ", a);
writeln("a = ", a);
writeln("b = ", b);
writeln("b = ", b);