Skip to content

Commit

Permalink
Merge branch 'features/num' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Axect committed Nov 9, 2023
2 parents c36d9b0 + 3521f1c commit 73ac209
Show file tree
Hide file tree
Showing 5 changed files with 263 additions and 2 deletions.
3 changes: 2 additions & 1 deletion peroxide-num/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "peroxide-num"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["Axect <[email protected]>"]
description = "Numerical traits for Peroxide"
Expand All @@ -9,6 +9,7 @@ license = "MIT OR Apache-2.0"
categories = ["science"]
readme = "README.md"
keywords = ["numerical", "mathematics"]
exclude = ["examples/"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
6 changes: 5 additions & 1 deletion peroxide-num/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This crate defines a set of traits for numeric operations, including basic arith
- `Float`: Define own floating point type (`f32` and `f64` are implemented as default).
- `Numeric`: A comprehensive trait that encompasses all of the above along with standard arithmetic operations.

## Example: Defining a Simple Numeric Type
## Example 1: Defining a Simple Numeric Type

Below is an example of how you can define your own simple numeric type that implements the `Numeric` trait.

Expand Down Expand Up @@ -53,6 +53,10 @@ impl Numeric<f64> for SimpleNumber {}

This `SimpleNumber` struct wraps a `f64` and implements the `Numeric` trait, making it capable of all the operations defined in the `Peroxide-num` crate.

## Example 2: `Vec3D` (3D Vector)

[examples/vec3d.rs](examples/vec3d.rs)

## Usage

To use this type in your own computations:
Expand Down
5 changes: 5 additions & 0 deletions peroxide-num/RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ver 0.1.1 (2023-11-09)

- Add constraints for `Numeric<T>`
- `T: Add<Self, Output=Self> + Sub<Self, Output=Self> + Mul<Self, Output=Self> + Div<Self, Output=Self>`
- Add example : `Vec3D`
246 changes: 246 additions & 0 deletions peroxide-num/examples/vec3d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
use peroxide_num::{PowOps, ExpLogOps, TrigOps, Numeric};
use std::ops::{Neg, Add, Sub, Mul, Div};
use std::f64::consts::PI;

fn main() {
let a = Vec3D::new(0f64, 0.5 * PI, PI);
println!("{:?}", a);
println!("{:?}", -a);
println!("{:?}", a + a);
println!("{:?}", a - a);
println!("{:?}", a * 2f64);
println!("{:?}", 2f64 * a);
println!("{:?}", a / 2f64);
println!("{:?}", 2f64 / a);
println!("{:?}", a.sin());
println!("{:?}", a.cos());
println!("{:?}", a.tan());
println!("{:?}", a.asin());
println!("{:?}", a.acos());
println!("{:?}", a.atan());
println!("{:?}", a.sinh());
println!("{:?}", a.cosh());
println!("{:?}", a.tanh());
println!("{:?}", a.asinh());
println!("{:?}", a.acosh());
println!("{:?}", a.atanh());
println!("{:?}", a.exp());
println!("{:?}", a.ln());
}

#[derive(Debug, Copy, Clone)]
struct Vec3D {
x: f64,
y: f64,
z: f64
}

impl Vec3D {
fn new(x: f64, y: f64, z: f64) -> Vec3D {
Vec3D { x, y, z }
}
}

impl Neg for Vec3D {
type Output = Vec3D;

fn neg(self) -> Vec3D {
Vec3D::new(-self.x, -self.y, -self.z)
}
}

impl Add for Vec3D {
type Output = Vec3D;

fn add(self, other: Vec3D) -> Vec3D {
Vec3D::new(self.x + other.x, self.y + other.y, self.z + other.z)
}
}

impl Sub for Vec3D {
type Output = Vec3D;

fn sub(self, other: Vec3D) -> Vec3D {
Vec3D::new(self.x - other.x, self.y - other.y, self.z - other.z)
}
}

impl Mul for Vec3D {
type Output = Vec3D;

fn mul(self, _other: Vec3D) -> Vec3D {
unimplemented!()
}
}

impl Div for Vec3D {
type Output = Vec3D;

fn div(self, _other: Vec3D) -> Vec3D {
unimplemented!()
}
}

impl Add<f64> for Vec3D {
type Output = Vec3D;

fn add(self, scalar: f64) -> Vec3D {
Vec3D::new(self.x + scalar, self.y + scalar, self.z + scalar)
}
}

impl Sub<f64> for Vec3D {
type Output = Vec3D;

fn sub(self, scalar: f64) -> Vec3D {
Vec3D::new(self.x - scalar, self.y - scalar, self.z - scalar)
}
}

impl Mul<f64> for Vec3D {
type Output = Vec3D;

fn mul(self, scalar: f64) -> Vec3D {
Vec3D::new(self.x * scalar, self.y * scalar, self.z * scalar)
}
}

impl Div<f64> for Vec3D {
type Output = Vec3D;

fn div(self, scalar: f64) -> Vec3D {
Vec3D::new(self.x / scalar, self.y / scalar, self.z / scalar)
}
}

impl Add<Vec3D> for f64 {
type Output = Vec3D;

fn add(self, other: Vec3D) -> Vec3D {
Vec3D::new(self + other.x, self + other.y, self + other.z)
}
}

impl Sub<Vec3D> for f64 {
type Output = Vec3D;

fn sub(self, other: Vec3D) -> Vec3D {
Vec3D::new(self - other.x, self - other.y, self - other.z)
}
}

impl Mul<Vec3D> for f64 {
type Output = Vec3D;

fn mul(self, other: Vec3D) -> Vec3D {
Vec3D::new(self * other.x, self * other.y, self * other.z)
}
}

impl Div<Vec3D> for f64 {
type Output = Vec3D;

fn div(self, other: Vec3D) -> Vec3D {
Vec3D::new(self / other.x, self / other.y, self / other.z)
}
}

impl PowOps for Vec3D {
type Float = f64;

fn pow(&self, _power: Self) -> Self {
unimplemented!()
}

fn powf(&self, power: Self::Float) -> Self {
Vec3D::new(self.x.powf(power), self.y.powf(power), self.z.powf(power))
}

fn powi(&self, power: i32) -> Self {
Vec3D::new(self.x.powi(power), self.y.powi(power), self.z.powi(power))
}

fn sqrt(&self) -> Self {
Vec3D::new(self.x.sqrt(), self.y.sqrt(), self.z.sqrt())
}
}

impl TrigOps for Vec3D {
fn sin(&self) -> Self {
Vec3D::new(self.x.sin(), self.y.sin(), self.z.sin())
}

fn cos(&self) -> Self {
Vec3D::new(self.x.cos(), self.y.cos(), self.z.cos())
}

fn tan(&self) -> Self {
Vec3D::new(self.x.tan(), self.y.tan(), self.z.tan())
}

fn asin(&self) -> Self {
Vec3D::new(self.x.asin(), self.y.asin(), self.z.asin())
}

fn acos(&self) -> Self {
Vec3D::new(self.x.acos(), self.y.acos(), self.z.acos())
}

fn atan(&self) -> Self {
Vec3D::new(self.x.atan(), self.y.atan(), self.z.atan())
}

fn sinh(&self) -> Self {
Vec3D::new(self.x.sinh(), self.y.sinh(), self.z.sinh())
}

fn cosh(&self) -> Self {
Vec3D::new(self.x.cosh(), self.y.cosh(), self.z.cosh())
}

fn tanh(&self) -> Self {
Vec3D::new(self.x.tanh(), self.y.tanh(), self.z.tanh())
}

fn asinh(&self) -> Self {
Vec3D::new(self.x.asinh(), self.y.asinh(), self.z.asinh())
}

fn acosh(&self) -> Self {
Vec3D::new(self.x.acosh(), self.y.acosh(), self.z.acosh())
}

fn atanh(&self) -> Self {
Vec3D::new(self.x.atanh(), self.y.atanh(), self.z.atanh())
}

fn sin_cos(&self) -> (Self, Self) {
(self.sin(), self.cos())
}
}

impl ExpLogOps for Vec3D {
type Float = f64;

fn exp(&self) -> Self {
Vec3D::new(self.x.exp(), self.y.exp(), self.z.exp())
}

fn ln(&self) -> Self {
Vec3D::new(self.x.ln(), self.y.ln(), self.z.ln())
}

fn log(&self, base: Self::Float) -> Self {
Vec3D::new(self.x.log(base), self.y.log(base), self.z.log(base))
}

fn log2(&self) -> Self {
Vec3D::new(self.x.log2(), self.y.log2(), self.z.log2())
}

fn log10(&self) -> Self {
Vec3D::new(self.x.log10(), self.y.log10(), self.z.log10())
}
}

impl Numeric<f64> for Vec3D {}
5 changes: 5 additions & 0 deletions peroxide-num/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ pub trait Numeric<T: Float>:
+ Div<T, Output = Self>
+ Sub<T, Output = Self>
+ Clone
where
T: Add<Self, Output = Self>,
T: Mul<Self, Output = Self>,
T: Div<Self, Output = Self>,
T: Sub<Self, Output = Self>,
{
}

Expand Down

0 comments on commit 73ac209

Please sign in to comment.