From 642d8bdbdb28ce746349b534bc460d4197df4578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Wed, 20 Sep 2023 16:51:28 +0200 Subject: [PATCH] Replaces Scale trait by impl Mul. --- codegen/src/compile.rs | 10 ++-------- codegen/src/main.rs | 6 ++++-- src/lib.rs | 26 ++++++-------------------- src/polynomial.rs | 20 +++++++++----------- 4 files changed, 21 insertions(+), 41 deletions(-) diff --git a/codegen/src/compile.rs b/codegen/src/compile.rs index c7db1cd..0fd2edf 100644 --- a/codegen/src/compile.rs +++ b/codegen/src/compile.rs @@ -657,13 +657,7 @@ impl MultiVectorClass { name, data_type: geometric_product_result.data_type.clone(), }, - parameters: vec![ - parameter_a.clone(), - Parameter { - name: "other", - data_type: DataType::SimdVector(1), - }, - ], + parameters: vec![parameter_a.clone(), parameter_b.clone()], body: vec![AstNode::ReturnStatement { expression: Box::new(Expression { size: 1, @@ -676,7 +670,7 @@ impl MultiVectorClass { geometric_product_result.name, geometric_product_result.data_type.clone(), vec![( - DataType::MultiVector(parameter_b.multi_vector_class()), + parameter_b.data_type.clone(), Expression { size: 1, content: ExpressionContent::InvokeClassMethod( diff --git a/codegen/src/main.rs b/codegen/src/main.rs index 7d76bd2..9ec7c1e 100644 --- a/codegen/src/main.rs +++ b/codegen/src/main.rs @@ -142,8 +142,10 @@ fn main() { for (parameter_b, pair_trait_implementations) in pair_trait_implementations.values() { if let Some(geometric_product) = pair_trait_implementations.get("GeometricProduct") { if parameter_b.data_type.is_scalar() { - let scale = MultiVectorClass::derive_scale("Scale", geometric_product, ¶meter_a, parameter_b); - emitter.emit(&scale).unwrap(); + if !parameter_a.data_type.is_scalar() { + let scale = MultiVectorClass::derive_scale("Mul", geometric_product, ¶meter_a, parameter_b); + emitter.emit(&scale).unwrap(); + } if let Some(magnitude) = single_trait_implementations.get("Magnitude") { let signum = MultiVectorClass::derive_signum("Signum", geometric_product, magnitude, ¶meter_a); emitter.emit(&signum).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index e325eda..034ad2a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -110,14 +110,6 @@ impl Magnitude for f32 { } } -impl Scale for f32 { - type Output = f32; - - fn scale(self, other: f32) -> f32 { - self.geometric_product(other) - } -} - impl Signum for f32 { type Output = f32; @@ -206,7 +198,7 @@ impl Ln for ppga2d::Translator { fn ln(self) -> ppga2d::IdealPoint { let result: ppga2d::IdealPoint = self.into(); - result.scale(1.0 / self[0]) + result * (1.0 / self[0]) } } @@ -214,7 +206,7 @@ impl Powf for ppga2d::Translator { type Output = Self; fn powf(self, exponent: f32) -> Self { - self.ln().scale(exponent).exp() + (self.ln() * exponent).exp() } } @@ -253,7 +245,7 @@ impl Powf for ppga2d::Motor { type Output = Self; fn powf(self, exponent: f32) -> Self { - self.ln().scale(exponent).exp() + (self.ln() * exponent).exp() } } @@ -270,7 +262,7 @@ impl Ln for ppga3d::Translator { fn ln(self) -> ppga3d::IdealPoint { let result: ppga3d::IdealPoint = self.into(); - result.scale(1.0 / self[0]) + result * (1.0 / self[0]) } } @@ -278,7 +270,7 @@ impl Powf for ppga3d::Translator { type Output = Self; fn powf(self, exponent: f32) -> Self { - self.ln().scale(exponent).exp() + (self.ln() * exponent).exp() } } @@ -322,7 +314,7 @@ impl Powf for ppga3d::Motor { type Output = Self; fn powf(self, exponent: f32) -> Self { - self.ln().scale(exponent).exp() + (self.ln() * exponent).exp() } } @@ -426,12 +418,6 @@ pub trait Transformation { fn transformation(self, other: T) -> Self::Output; } -/// Geometric product with a scalar -pub trait Scale { - type Output; - fn scale(self, other: f32) -> Self::Output; -} - /// Square of the magnitude pub trait SquaredMagnitude { type Output; diff --git a/src/polynomial.rs b/src/polynomial.rs index 3188bc0..bf36f21 100644 --- a/src/polynomial.rs +++ b/src/polynomial.rs @@ -1,9 +1,7 @@ //! Solves polynomials with real valued coefficients up to degree 4 #![allow(clippy::many_single_char_names)] -use crate::{ - epga1d::*, GeometricProduct, GeometricQuotient, Powf, Reversal, Scale, SquaredMagnitude, -}; +use crate::{epga1d::*, GeometricProduct, GeometricQuotient, Powf, Reversal, SquaredMagnitude}; /// Represents a complex root as homogeneous coordinates #[derive(Debug, Clone, Copy)] @@ -94,13 +92,13 @@ pub fn solve_cubic(coefficients: [f32; 4], error_margin: f32) -> (f32, Vec let mut solutions = Vec::with_capacity(3); let discriminant = d[1].powi(2) - 4.0 * d[0].powi(3); let c = discriminant.sqrt(); - let c = ((c + ComplexNumber::new(if c + d[1] == 0.0 { -d[1] } else { d[1] }, 0.0)).scale(0.5)) + let c = ((c + ComplexNumber::new(if c + d[1] == 0.0 { -d[1] } else { d[1] }, 0.0)) * 0.5) .powf(1.0 / 3.0); for root_of_unity in &ROOTS_OF_UNITY_3 { let ci = c.geometric_product(*root_of_unity); - let denominator = ci.scale(3.0 * coefficients[3]); + let denominator = ci * (3.0 * coefficients[3]); let numerator = - (ci.scale(-coefficients[2]) - ci.geometric_product(ci) - ComplexNumber::new(d[0], 0.0)) + (ci * -coefficients[2] - ci.geometric_product(ci) - ComplexNumber::new(d[0], 0.0)) .geometric_product(denominator.reversal()); solutions.push(Root { numerator, @@ -144,19 +142,19 @@ pub fn solve_quartic(coefficients: [f32; 5], error_margin: f32) -> (f32, Vec