Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
MqCreaple committed Feb 26, 2023
1 parent 0049271 commit 03b4b86
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 209 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

extern crate num;

pub mod ops;
mod terms;

pub use self::terms::*;
Expand Down
78 changes: 0 additions & 78 deletions src/ops/add.rs

This file was deleted.

12 changes: 12 additions & 0 deletions src/terms/expression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::rc::Rc;

use num::Num;

/// Trait for every symbolic expression.
pub trait Expression<T: Num> {
}

/// Wrapper class for reference counting pointer to an expression.
pub type Expr<T> = Rc<dyn Expression<T>>;

// TODO (Add `Hash` and `Eq` trait to `Expr<T>`)
13 changes: 13 additions & 0 deletions src/terms/funcs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use num::Num;

use super::expression::{Expr, Expression};

/// Represents a function with single input argument.
pub struct SingleArgFunc<T: Num> {
name: &'static str,
arg: Expr<T>,
}

impl<T: Num> Expression<T> for SingleArgFunc<T> {
// TODO
}
130 changes: 5 additions & 125 deletions src/terms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,128 +6,8 @@

#![allow(missing_docs)]

use num::bigint::BigInt;
use num::rational::BigRational;
use std::fmt;

pub trait Expression: fmt::Debug {
fn apply(&self) -> Term;
}

#[derive(Debug)]
pub enum Term {
Integer(BigInt),
Expression(Box<dyn Expression>),
Rational(BigRational),
Symbol(String),
}

/// Pass `Expression` methods along to the underlying value.
impl Expression for Term {
fn apply(&self) -> Term {
match *self {
Term::Integer(ref val) => Term::from(val.clone()),
Term::Expression(ref expr) => expr.apply(),
Term::Rational(ref rational) => Term::from(rational.clone()),
Term::Symbol(ref symbol) => Term::from(symbol.clone()),
}
}
}

/// Construct a `Term::Integer`.
impl From<i64> for Term {
fn from(value: i64) -> Self {
Term::Integer(BigInt::from(value))
}
}

/// Construct a `Term::Integer`.
impl From<i8> for Term {
fn from(value: i8) -> Self {
Term::Integer(BigInt::from(value))
}
}

/// Construct a `Term::Integer`.
impl From<i16> for Term {
fn from(value: i16) -> Self {
Term::Integer(BigInt::from(value))
}
}

/// Construct a `Term::Integer`.
impl From<i32> for Term {
fn from(value: i32) -> Self {
Term::Integer(BigInt::from(value))
}
}

/// Construct a `Term::Integer`.
impl From<isize> for Term {
fn from(value: isize) -> Self {
Term::Integer(BigInt::from(value))
}
}

/// Construct a `Term::Integer`.
impl From<u64> for Term {
fn from(value: u64) -> Self {
Term::Integer(BigInt::from(value))
}
}

/// Construct a `Term::Integer`.
impl From<u8> for Term {
fn from(value: u8) -> Self {
Term::Integer(BigInt::from(value))
}
}

/// Construct a `Term::Integer`.
impl From<u16> for Term {
fn from(value: u16) -> Self {
Term::Integer(BigInt::from(value))
}
}

/// Construct a `Term::Integer`.
impl From<u32> for Term {
fn from(value: u32) -> Self {
Term::Integer(BigInt::from(value))
}
}

/// Construct a `Term::Integer`.
impl From<usize> for Term {
fn from(value: usize) -> Self {
Term::Integer(BigInt::from(value))
}
}

/// Construct a `Term::Integer`.
impl From<BigInt> for Term {
fn from(value: BigInt) -> Self {
Term::Integer(value)
}
}

/// Construct a `Term::Expression`.
impl From<Box<dyn Expression>> for Term {
fn from(value: Box<dyn Expression>) -> Self {
Term::Expression(value)
}
}

/// Construct a `Term::Rational`.
impl From<BigRational> for Term {
fn from(value: BigRational) -> Self {
Term::Rational(value)
}
}

/// Construct a `Term::Symbol`.
impl From<String> for Term {
fn from(value: String) -> Self {
Term::Symbol(value)
}
}
mod expression;
mod symbol;
mod number;
mod ops;
mod funcs;
10 changes: 10 additions & 0 deletions src/terms/number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::hash::Hash;

use num::Num;

use super::expression::Expression;

/// Implement the Expression trait for all numbers
impl<T: Num + Hash> Expression<T> for T {
// TODO
}
31 changes: 31 additions & 0 deletions src/terms/ops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::collections::HashMap;

use num::Num;

use super::expression::{Expr, Expression};

/// Represents the operation `coeff + c0 * x0 + c1 * x1 + c2 * x2 + ...`,
/// where `coeff`, `c0`, `c1`, `c2`, ... are number constants.
pub struct Add<T: Num> {
/// Number coefficient of the expression.
coeff: T,
/// Dictionary that maps terms (x0, x1, x2, ...) to their coefficients (c0, c1, c2, ...)
dict: HashMap<Expr<T>, T>,
}

impl<T: Num> Expression<T> for Add<T> {
// TODO
}

/// Represents the operation `coeff * x0 ^ c0 * x1 ^ c1 * x2 ^ c2 * ...`,
/// where `coeff`, `c0`, `c1`, `c2`, ... are number constants.
pub struct Mul<T: Num> {
/// Number coefficient of the expression.
coeff: T,
/// Dictionary that maps terms (x0, x1, x2, ...) to their coefficients (c0, c1, c2, ...)
dict: HashMap<Expr<T>, T>,
}

impl<T: Num> Expression<T> for Mul<T> {
// TODO
}
19 changes: 19 additions & 0 deletions src/terms/symbol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::hash::Hash;

use num::Num;

use super::expression::Expression;

pub struct Symbol {
name: &'static str,
}

impl Hash for Symbol {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
todo!()
}
}

impl<T: Num> Expression<T> for Symbol {
// TODO
}
Loading

0 comments on commit 03b4b86

Please sign in to comment.