initial commit

This commit is contained in:
Robot 2024-05-03 14:53:50 -05:00
commit 8823021ef7
8 changed files with 200 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
Cargo.lock generated Normal file
View File

@ -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"

8
Cargo.toml Normal file
View File

@ -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]

72
src/function.rs Normal file
View File

@ -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<T: Number> {
/// // some exponential internals
/// }
/// // could be done by using Into<Complex> instead
/// impl Functiton<f64, f64, 1, 1> for Exponential<f64> {
/// fn call(&self, arg: f64) -> f64 {
/// todo!();
/// }
/// }
/// impl Functiton<Complex, Complex, 1, 1> for Exponential<Complex> {
/// fn call(&self, arg: Complex) -> Complex {
/// todo!();
/// }
/// }
/// ```
pub trait Function<T, U> {
fn call(&self, arg: T) -> U;
}
/// ```
/// struct Polonomial {
/// // polonomial internals
/// }
/// impl<T, U> Function<T, U> for Polonomial {
/// fn call(&self, arg: T)
/// }
/// impl<T, U, O> Differentiable<T, U, O> for Polonomial {
/// type Output = Polonomial
///
/// }
/// ```
pub trait Differentiable<T, U, O>: Function<T, U> {
type Output: Function<T, O>;
fn derivitave(self) -> Self::Output;
}
pub trait Smooth<T, U, O>: Differentiable<T, U, O> {}
#[derive(Debug, Default)]
pub struct Exponential<T: Number>{
phantom: PhantomData<T>,
}
impl Function<f64, f64>for Exponential<f64> {
fn call(&self, arg: f64) -> f64 {
std::f64::consts::E.powf(arg)
}
}
impl Differentiable<f64, f64, f64> for Exponential<f64> {
type Output = Self;
fn derivitave(self) -> Self::Output {
self
}
}
/* impl<T, U, O, A, X> Smooth<T, U, O> for A
where
X: Smooth<T, U, O>,
A: Differentiable<T, U, O, Output = X>,
{
} */
impl<T, U, O, A> Smooth<T, U, O> for A
where
A: Differentiable<T, U, O, Output = A>,
{
}

5
src/lib.rs Normal file
View File

@ -0,0 +1,5 @@
pub mod polonomial;
pub mod numbers;
pub mod linear;
pub mod function;

5
src/linear.rs Normal file
View File

@ -0,0 +1,5 @@
use crate::numbers::Number;
/// A mathmatical Vector
pub struct Vector<T:Number, const D: usize>([T; D]);
// impl<T,

83
src/numbers.rs Normal file
View File

@ -0,0 +1,83 @@
use std::ops::*;
pub trait Number: Add + Sub + Mul + Div + Neg + Clone + Sized { }
impl<T: Add + Sub + Mul + Div + Neg + Clone + Sized> 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<f64> 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,
}
}
}

19
src/polonomial.rs Normal file
View File

@ -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<T: Number = f64> {
power: i32,
coeficient: T,
offset: T,
}
#[derive(Default, Debug, Clone)]
pub struct Equation<T: Number = f64> {
fields: Vec<Option<T>>,
// fields: HashMap<usize, T>,
}
impl<T:Number> Equation<T> {
}