Skip to main content

RationalFunction

Struct RationalFunction 

Source
pub struct RationalFunction { /* private fields */ }
Expand description

A rational function represented as numerator / denominator.

Invariants:

  • The denominator is never the zero polynomial
  • The representation is kept in reduced form (gcd of numerator and denominator is 1)

Implementations§

Source§

impl RationalFunction

Source

pub fn new(numerator: Polynomial, denominator: Polynomial) -> Self

Create a new rational function from numerator and denominator.

§Arguments
  • numerator - The numerator polynomial
  • denominator - The denominator polynomial
§Panics

Panics if the denominator is the zero polynomial.

§Examples
use oxiz_math::polynomial::Polynomial;
use oxiz_math::rational_function::RationalFunction;

let num = Polynomial::from_coeffs_int(&[(1, &[(0, 1)])]); // x
let den = Polynomial::from_coeffs_int(&[(1, &[(0, 1)]), (1, &[])]); // x + 1
let rf = RationalFunction::new(num, den);
Source

pub fn from_polynomial(p: Polynomial) -> Self

Create a rational function from a polynomial (denominator = 1).

§Examples
use oxiz_math::polynomial::Polynomial;
use oxiz_math::rational_function::RationalFunction;

let p = Polynomial::from_coeffs_int(&[(1, &[(0, 2)])]); // x^2
let rf = RationalFunction::from_polynomial(p);
Source

pub fn from_constant(c: BigRational) -> Self

Create a rational function from a constant.

§Examples
use num_bigint::BigInt;
use num_rational::BigRational;
use oxiz_math::rational_function::RationalFunction;

let rf = RationalFunction::from_constant(BigRational::from_integer(BigInt::from(5)));
Source

pub fn numerator(&self) -> &Polynomial

Get the numerator.

Source

pub fn denominator(&self) -> &Polynomial

Get the denominator.

Source

pub fn is_zero(&self) -> bool

Check if the rational function is zero.

Source

pub fn is_constant(&self) -> bool

Check if the rational function is constant.

Source

pub fn eval( &self, assignment: &FxHashMap<Var, BigRational>, ) -> Option<BigRational>

Evaluate the rational function at a point.

§Arguments
  • assignment - Variable assignments
§Returns

The value of the rational function, or None if the denominator evaluates to zero.

§Examples
use num_bigint::BigInt;
use num_rational::BigRational;
use oxiz_math::polynomial::Polynomial;
use oxiz_math::rational_function::RationalFunction;
use rustc_hash::FxHashMap;

let num = Polynomial::from_coeffs_int(&[(1, &[(0, 1)])]); // x
let den = Polynomial::from_coeffs_int(&[(1, &[(0, 1)]), (1, &[])]); // x + 1
let rf = RationalFunction::new(num, den);

let mut assignment = FxHashMap::default();
assignment.insert(0, BigRational::from_integer(BigInt::from(2)));
let val = rf.eval(&assignment).unwrap();
assert_eq!(val, BigRational::new(BigInt::from(2), BigInt::from(3))); // 2/3
Source

pub fn derivative(&self, var: Var) -> RationalFunction

Compute the derivative of the rational function.

Uses the quotient rule: (p/q)’ = (p’q - pq’) / q^2

§Examples
use oxiz_math::polynomial::Polynomial;
use oxiz_math::rational_function::RationalFunction;

let num = Polynomial::from_coeffs_int(&[(1, &[(0, 1)])]); // x
let den = Polynomial::from_coeffs_int(&[(1, &[(0, 1)]), (1, &[])]); // x + 1
let rf = RationalFunction::new(num, den);
let derivative = rf.derivative(0);
Source

pub fn simplify(&mut self)

Simplify the rational function.

This is an alias for reducing to lowest terms.

Source

pub fn is_proper(&self) -> bool

Check if this is a proper rational function (degree of numerator < degree of denominator).

§Examples
use oxiz_math::polynomial::Polynomial;
use oxiz_math::rational_function::RationalFunction;

let num = Polynomial::from_coeffs_int(&[(1, &[(0, 1)])]); // x
let den = Polynomial::from_coeffs_int(&[(1, &[(0, 2)])]); // x^2
let rf = RationalFunction::new(num, den);
assert!(rf.is_proper());
Source

pub fn polynomial_division( &self, _var: Var, ) -> Option<(Polynomial, RationalFunction)>

Perform polynomial long division to separate improper rational functions.

For an improper rational function N(x)/D(x) where deg(N) >= deg(D), returns (Q(x), R(x)/D(x)) where N(x)/D(x) = Q(x) + R(x)/D(x) and R(x)/D(x) is proper.

§Arguments
  • var - The variable for univariate division
§Returns
  • (quotient_polynomial, proper_remainder) if the function is improper and univariate
  • None if the function is proper or not univariate
§Examples
use oxiz_math::polynomial::Polynomial;
use oxiz_math::rational_function::RationalFunction;

// (x^2 + 1) / x = x + 1/x
let num = Polynomial::from_coeffs_int(&[(1, &[(0, 2)]), (1, &[])]);
let den = Polynomial::from_coeffs_int(&[(1, &[(0, 1)])]);
let rf = RationalFunction::new(num, den);

let (quotient, remainder) = rf.polynomial_division(0).unwrap();
// quotient should be x, remainder should be 1/x
Source

pub fn partial_fraction_decomposition( &self, _var: Var, ) -> Option<Vec<RationalFunction>>

Compute partial fraction decomposition for simple cases.

For a proper rational function with a factored denominator of distinct linear factors, decomposes into a sum of simpler fractions.

Note: This is a simplified implementation that works for:

  • Proper rational functions (deg(numerator) < deg(denominator))
  • Univariate polynomials
  • Denominators that are products of distinct linear factors

For more complex cases (repeated factors, irreducible quadratics), use specialized computer algebra systems.

§Arguments
  • var - The variable
§Returns

Vector of simpler rational functions that sum to the original, or None if decomposition is not applicable

§Examples
use oxiz_math::polynomial::Polynomial;
use oxiz_math::rational_function::RationalFunction;

// Example: 1 / (x(x-1)) = A/x + B/(x-1)
// where A = -1, B = 1
let num = Polynomial::from_coeffs_int(&[(1, &[])]);  // 1
let den = Polynomial::from_coeffs_int(&[             // x^2 - x
    (1, &[(0, 2)]),   // x^2
    (-1, &[(0, 1)]),  // -x
]);
let rf = RationalFunction::new(num, den);

// Partial fraction decomposition (if factors are available)
// This is a placeholder - full implementation would require factorization

Trait Implementations§

Source§

impl Add for &RationalFunction

Source§

type Output = RationalFunction

The resulting type after applying the + operator.
Source§

fn add(self, other: &RationalFunction) -> RationalFunction

Performs the + operation. Read more
Source§

impl Add for RationalFunction

Source§

fn add(self, other: RationalFunction) -> RationalFunction

Add two rational functions: p/q + r/s = (ps + qr) / (qs)

Source§

type Output = RationalFunction

The resulting type after applying the + operator.
Source§

impl Clone for RationalFunction

Source§

fn clone(&self) -> RationalFunction

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RationalFunction

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div for &RationalFunction

Source§

type Output = RationalFunction

The resulting type after applying the / operator.
Source§

fn div(self, other: &RationalFunction) -> RationalFunction

Performs the / operation. Read more
Source§

impl Div for RationalFunction

Source§

fn div(self, other: RationalFunction) -> RationalFunction

Divide two rational functions: (p/q) / (r/s) = (ps) / (qr)

Source§

type Output = RationalFunction

The resulting type after applying the / operator.
Source§

impl Mul for &RationalFunction

Source§

type Output = RationalFunction

The resulting type after applying the * operator.
Source§

fn mul(self, other: &RationalFunction) -> RationalFunction

Performs the * operation. Read more
Source§

impl Mul for RationalFunction

Source§

fn mul(self, other: RationalFunction) -> RationalFunction

Multiply two rational functions: (p/q) * (r/s) = (pr) / (qs)

Source§

type Output = RationalFunction

The resulting type after applying the * operator.
Source§

impl Neg for &RationalFunction

Source§

type Output = RationalFunction

The resulting type after applying the - operator.
Source§

fn neg(self) -> RationalFunction

Performs the unary - operation. Read more
Source§

impl Neg for RationalFunction

Source§

fn neg(self) -> RationalFunction

Negate a rational function: -(p/q) = (-p)/q

Source§

type Output = RationalFunction

The resulting type after applying the - operator.
Source§

impl PartialEq for RationalFunction

Source§

fn eq(&self, other: &RationalFunction) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Sub for &RationalFunction

Source§

type Output = RationalFunction

The resulting type after applying the - operator.
Source§

fn sub(self, other: &RationalFunction) -> RationalFunction

Performs the - operation. Read more
Source§

impl Sub for RationalFunction

Source§

fn sub(self, other: RationalFunction) -> RationalFunction

Subtract two rational functions: p/q - r/s = (ps - qr) / (qs)

Source§

type Output = RationalFunction

The resulting type after applying the - operator.
Source§

impl Eq for RationalFunction

Source§

impl StructuralPartialEq for RationalFunction

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V