From 8823021ef729ff614f0f973a4fbaa4cd35419122 Mon Sep 17 00:00:00 2001 From: Robot Date: Fri, 3 May 2024 14:53:50 -0500 Subject: [PATCH] initial commit --- .gitignore | 1 + Cargo.lock | 7 ++++ Cargo.toml | 8 +++++ src/function.rs | 72 ++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 5 +++ src/linear.rs | 5 +++ src/numbers.rs | 83 +++++++++++++++++++++++++++++++++++++++++++++++ src/polonomial.rs | 19 +++++++++++ 8 files changed, 200 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/function.rs create mode 100644 src/lib.rs create mode 100644 src/linear.rs create mode 100644 src/numbers.rs create mode 100644 src/polonomial.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..02755a5 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "calculator-rs" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..57fe6dc --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "calculator-rs" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/function.rs b/src/function.rs new file mode 100644 index 0000000..3d9d19f --- /dev/null +++ b/src/function.rs @@ -0,0 +1,72 @@ +use std::marker::PhantomData; + +use crate::numbers::Number; + + +/// a function that represents specificaly a mathmatical function, with a fixed number of inputs +/// and outputs. it is generic over inputs and outputs, so you can have something like this +/// ``` +/// /// the function defined as being its own derivitave. +/// struct Exponential { +/// // some exponential internals +/// } +/// // could be done by using Into instead +/// impl Functiton for Exponential { +/// fn call(&self, arg: f64) -> f64 { +/// todo!(); +/// } +/// } +/// impl Functiton for Exponential { +/// fn call(&self, arg: Complex) -> Complex { +/// todo!(); +/// } +/// } +/// ``` +pub trait Function { + fn call(&self, arg: T) -> U; +} +/// ``` +/// struct Polonomial { +/// // polonomial internals +/// } +/// impl Function for Polonomial { +/// fn call(&self, arg: T) +/// } +/// impl Differentiable for Polonomial { +/// type Output = Polonomial +/// +/// } +/// ``` +pub trait Differentiable: Function { + type Output: Function; + fn derivitave(self) -> Self::Output; +} +pub trait Smooth: Differentiable {} +#[derive(Debug, Default)] +pub struct Exponential{ + phantom: PhantomData, +} +impl Functionfor Exponential { + fn call(&self, arg: f64) -> f64 { + std::f64::consts::E.powf(arg) + } +} +impl Differentiable for Exponential { + type Output = Self; + fn derivitave(self) -> Self::Output { + self + } +} +/* impl Smooth for A +where + X: Smooth, + A: Differentiable, +{ + +} */ +impl Smooth for A +where + A: Differentiable, +{ + +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..55a288f --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,5 @@ + +pub mod polonomial; +pub mod numbers; +pub mod linear; +pub mod function; diff --git a/src/linear.rs b/src/linear.rs new file mode 100644 index 0000000..7ba0b13 --- /dev/null +++ b/src/linear.rs @@ -0,0 +1,5 @@ +use crate::numbers::Number; + +/// A mathmatical Vector +pub struct Vector([T; D]); +// impl Number for T {} + +/// angles are assumed to always be in radians +pub type Angle = f64; +#[derive(Debug, Clone, PartialEq)] +pub struct Complex{ + real: f64, + imaginary: f64, +} +impl Complex { + pub const IMAGINARY_UNIT: Self = Self { real:0.0, imaginary: 0.0}; + /// computes the absolute value of a complex number + pub fn abs(&self) -> f64 { + (self.real.powi(2) + self.imaginary.powi(2)).sqrt() + } + /// returns a unit vector that is angle a away from the positive x axis + pub fn unit_angle(a: Angle) -> Self { + Self { + real: a.cos(), + imaginary: a.sin(), + } + + } + +} +impl From for Complex { + fn from(value: f64) -> Self { + Self { real: value, imaginary: 0.0 } + } +} +impl Add for Complex { + type Output = Self; + fn add(self, rhs: Self) -> Self::Output { + Self { + real: self.real + rhs.real, + imaginary: self.imaginary + rhs.imaginary + } + } +} +impl AddAssign for Complex { + fn add_assign(&mut self, rhs: Self) { + *self = self.clone() + rhs; + } + +} +impl Sub for Complex { + type Output = Self; + fn sub(self, rhs: Self) -> Self::Output { + Self { + real: self.real - rhs.real, + imaginary: self.imaginary - rhs.imaginary + } + } +} +impl SubAssign for Complex { + fn sub_assign(&mut self, rhs: Self) { + *self = self.clone() - rhs; + } + +} +impl Mul for Complex { + type Output = Self; + fn mul(self, rhs: Self) -> Self::Output { + Self { + real: self.real * rhs.real - self.imaginary * rhs.imaginary, + imaginary: self.real * rhs.imaginary + self.imaginary * rhs.real, + } + + } +} +impl Div for Complex { + type Output = Self; + fn div(self, rhs: Self) -> Self::Output { + Self { + real: self.real / rhs.real - self.imaginary / rhs.imaginary, + imaginary: self.real / rhs.imaginary + self.imaginary / rhs.real, + } + + } +} diff --git a/src/polonomial.rs b/src/polonomial.rs new file mode 100644 index 0000000..aa21ae6 --- /dev/null +++ b/src/polonomial.rs @@ -0,0 +1,19 @@ +use super::numbers::Number; +use std::collections::HashMap; + +/// a factor of a polinomial +// this is of the form (coeficient*x+offset)^power +pub struct Factor { + power: i32, + coeficient: T, + offset: T, +} +#[derive(Default, Debug, Clone)] +pub struct Equation { + fields: Vec>, + // fields: HashMap, +} +impl Equation { + + +}