Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Rust build artifacts
target/
debug/
release/

# Cargo metadata / local caches
.cargo/

# Git
.git/
.gitignore

# IDE / editor
.vscode/
.idea/
*.swp
*.swo

# OS files
.DS_Store
Thumbs.db

# Environment / secrets
.env
.env.*

# Logs
*.log

# Docker / compose overrides (optional)
docker-compose.override.yml

# Test / temp outputs
*.tmp
*.bak

# Generated artifacts
*.png
*.svg
*.pdf
4 changes: 4 additions & 0 deletions src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ pub mod polynomial;
pub mod rational;

pub mod radical;

mod exponential;
mod logarithmic;
pub mod piecewise;
1 change: 1 addition & 0 deletions src/functions/exponential.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions src/functions/logarithmic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

15 changes: 15 additions & 0 deletions src/functions/piecewise.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::core::traits::Function;

pub struct Piecewise {
functions: Vec<Box<dyn Function>>,
}

impl Piecewise {
pub fn count(&self) -> usize {
self.functions.len()
}

pub fn functions(&self) -> &[Box<dyn Function>] {
&self.functions
}
}
12 changes: 6 additions & 6 deletions src/functions/polynomial.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct Polynomial {
/// Coefficients in descending degree order.
/// Example: x^2 - 4x + 3 => vec![1.0, -4.0, 3.0]
Expand Down Expand Up @@ -64,11 +64,6 @@ impl Polynomial {
Polynomial::new(result)
}

pub fn evaluate(&self, x: f64) -> f64 {
// Horner's method
self.coefficients.iter().fold(0.0, |acc, &c| acc * x + c)
}

pub fn derivative(&self) -> Self {
let degree = self.degree();

Expand All @@ -94,6 +89,11 @@ impl Polynomial {
self.evaluate(0.0)
}

pub fn evaluate(&self, x: f64) -> f64 {
// Horner's method
self.coefficients.iter().fold(0.0, |acc, &c| acc * x + c)
}

pub fn leading_coefficient(&self) -> f64 {
self.coefficients[0]
}
Expand Down
33 changes: 31 additions & 2 deletions src/functions/rational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::functions::polynomial::Polynomial;
pub struct Rational {
/// Coefficients in descending degree order.
/// Example: x^2 - 4x + 3 => vec![1.0, -4.0, 3.0]
pub numerator: Polynomial,
pub denominator: Polynomial,
numerator: Polynomial,
denominator: Polynomial,
}

impl Rational {
Expand All @@ -15,4 +15,33 @@ impl Rational {
denominator,
}
}

pub fn asymptote(&self) -> Polynomial {
let numerator = &self.numerator;
let denominator = &self.denominator;
if numerator.degree() == denominator.degree() {
let ratio = numerator.leading_coefficient() / denominator.leading_coefficient();
Polynomial::new(vec![ratio])
} else if numerator.degree() > denominator.degree() {
Polynomial::new(vec![])
} else {
Polynomial::new(vec![0.0])
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn expect_horizontal_asymptote_zero() {
let numerator = Polynomial::new(vec![1.0, 1.0]);
let denominator = Polynomial::new(vec![1.0, 1.0, 1.0]);
let rational = Rational::new(numerator, denominator);
let asymptote: Polynomial = rational.asymptote();

let horizontal_expected: Polynomial = Polynomial::new(vec![0.0]);
assert_eq!(asymptote, horizontal_expected);
}
}
Loading