June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,87 @@
import system'math.
import extensions.
struct Quaternion :: BaseValue
{
real rprop A :: a.
real rprop B :: b.
real rprop C :: c.
real rprop D :: d.
constructor new(object a, object b, object c, object d)
<= new(a real, b real, c real, d real).
constructor new(real a, real b, real c, real d)
[
@a := a.
@b := b.
@c := c.
@d := d.
]
stacksafe explicit(Real r)
[
a := r.
b := 0.0r.
c := 0.0r.
d := 0.0r.
]
real Norm = (a*a + b*b + c*c + d*d) sqrt.
type<Quaternion> negative = Quaternion new(a negative,b negative,c negative,d negative).
type<Quaternion> Conjugate = Quaternion new(a,b negative,c negative,d negative).
type<Quaternion> add(Quaternion q)
= Quaternion new(a + q A, b + q B, c + q C, d + q D).
type<Quaternion> multiply(Quaternion q)
= Quaternion new(
a * q A - b * q B - c * q C - d * q D,
a * q B + b * q A + c * q D - d * q C,
a * q C - b * q D + c * q A + d * q B,
a * q D + b * q C - c * q B + d * q A).
type<Quaternion> add(real r)
<= add(Quaternion new(r,0,0,0)).
type<Quaternion> multiply(real r)
<= multiply(Quaternion new(r,0,0,0)).
bool equal(Quaternion q)
= (a == q A) && (b == q B) && (c == q C) && (d == q D).
literal
= String writeFormatted("Q({0}, {1}, {2}, {3})",a,b,c,d).
}
program =
[
var q := Quaternion new(1,2,3,4).
var q1 := Quaternion new(2,3,4,5).
var q2 := Quaternion new(3,4,5,6).
real r := 7.
console printLine("q = ", q).
console printLine("q1 = ", q1).
console printLine("q2 = ", q2).
console printLine("r = ", r).
console printLine("q.Norm() = ", q Norm).
console printLine("q1.Norm() = ", q1 Norm).
console printLine("q2.Norm() = ", q2 Norm).
console printLine("-q = ", q negative).
console printLine("q.Conjugate() = ", q Conjugate).
console printLine("q + r = ", q + r).
console printLine("q1 + q2 = ", q1 + q2).
console printLine("q2 + q1 = ", q2 + q1).
console printLine("q * r = ", q * r).
console printLine("q1 * q2 = ", q1 * q2).
console printLine("q2 * q1 = ", q2 * q1).
console printLineFormatted("q1*q2 {0} q2*q1", ((q1 * q2) == (q2 * q1)) iif("==","!=")).
].

View file

@ -0,0 +1,173 @@
use std::fmt::{Display, Error, Formatter};
use std::ops::{Add, Mul, Neg};
#[derive(Clone,Copy,Debug)]
struct Quaternion {
a: f64,
b: f64,
c: f64,
d: f64
}
impl Quaternion {
pub fn new(a: f64, b: f64, c: f64, d: f64) -> Quaternion {
Quaternion {
a: a,
b: b,
c: c,
d: d
}
}
pub fn norm(&self) -> f64 {
(self.a.powi(2) + self.b.powi(2) + self.c.powi(2) + self.d.powi(2)).sqrt()
}
pub fn conjugate(&self) -> Quaternion {
Quaternion {
a: self.a,
b: -self.b,
c: -self.c,
d: -self.d
}
}
}
impl Add for Quaternion {
type Output = Quaternion;
#[inline]
fn add(self, other: Quaternion) -> Self::Output {
Quaternion {
a: self.a + other.a,
b: self.b + other.b,
c: self.c + other.c,
d: self.d + other.d
}
}
}
impl Add<f64> for Quaternion {
type Output = Quaternion;
#[inline]
fn add(self, other: f64) -> Self::Output {
Quaternion {
a: self.a + other,
b: self.b,
c: self.c,
d: self.d
}
}
}
impl Add<Quaternion> for f64 {
type Output = Quaternion;
#[inline]
fn add(self, other: Quaternion) -> Self::Output {
Quaternion {
a: other.a + self,
b: other.b,
c: other.c,
d: other.d
}
}
}
impl Display for Quaternion {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "({} + {}i + {}j + {}k)", self.a, self.b, self.c, self.d)
}
}
impl Mul for Quaternion {
type Output = Quaternion;
#[inline]
fn mul(self, rhs: Quaternion) -> Self::Output {
Quaternion {
a: self.a * rhs.a - self.b * rhs.b - self.c * rhs.c - self.d * rhs.d,
b: self.a * rhs.b + self.b * rhs.a + self.c * rhs.d - self.d * rhs.c,
c: self.a * rhs.c - self.b * rhs.d + self.c * rhs.a + self.d * rhs.b,
d: self.a * rhs.d + self.b * rhs.c - self.c * rhs.b + self.d * rhs.a,
}
}
}
impl Mul<f64> for Quaternion {
type Output = Quaternion;
#[inline]
fn mul(self, other: f64) -> Self::Output {
Quaternion {
a: self.a * other,
b: self.b * other,
c: self.c * other,
d: self.d * other
}
}
}
impl Mul<Quaternion> for f64 {
type Output = Quaternion;
#[inline]
fn mul(self, other: Quaternion) -> Self::Output {
Quaternion {
a: other.a * self,
b: other.b * self,
c: other.c * self,
d: other.d * self
}
}
}
impl Neg for Quaternion {
type Output = Quaternion;
#[inline]
fn neg(self) -> Self::Output {
Quaternion {
a: -self.a,
b: -self.b,
c: -self.c,
d: -self.d
}
}
}
fn main() {
let q0 = Quaternion { a: 1., b: 2., c: 3., d: 4. };
let q1 = Quaternion::new(2., 3., 4., 5.);
let q2 = Quaternion::new(3., 4., 5., 6.);
let r: f64 = 7.;
println!("q0 = {}", q0);
println!("q1 = {}", q1);
println!("q2 = {}", q2);
println!("r = {}", r);
println!();
println!("-q0 = {}", -q0);
println!("conjugate of q0 = {}", q0.conjugate());
println!();
println!("r + q0 = {}", r + q0);
println!("q0 + r = {}", q0 + r);
println!();
println!("r * q0 = {}", r * q0);
println!("q0 * r = {}", q0 * r);
println!();
println!("q0 + q1 = {}", q0 + q1);
println!("q0 * q1 = {}", q0 * q1);
println!();
println!("q0 * (conjugate of q0) = {}", q0 * q0.conjugate());
println!();
println!(" q0 + q1 * q2 = {}", q0 + q1 * q2);
println!("(q0 + q1) * q2 = {}", (q0 + q1) * q2);
println!();
println!(" q0 * q1 * q2 = {}", q0 *q1 * q2);
println!("(q0 * q1) * q2 = {}", (q0 * q1) * q2);
println!(" q0 * (q1 * q2) = {}", q0 * (q1 * q2));
println!();
println!("normal of q0 = {}", q0.norm());
}