elementary-row-operation-verifier 0.0.1

A tool to verify the correctness of elementary row operations on matrices
Documentation
//! 数学概念类型 —— 一个概念对应一个结构体

use num_rational::Rational64;

// ---- 操作数 ----

/// 表达式中的一个操作数
#[derive(Debug, Clone, PartialEq)]
pub struct Operand {
    pub kind: OperandKind,
    /// 原始文本(用于显示和调试)
    pub raw: String,
}

/// 操作数的种类
#[derive(Debug, Clone, PartialEq)]
pub enum OperandKind {
    Number(f64),
    Fraction(Rational64),
    /// 行引用: r[1] — 0-indexed
    RowRef(usize),
    /// 列引用: c[1] — 0-indexed
    ColRef(usize),
    Variable(String),
}

impl Operand {
    pub fn number(n: f64, raw: impl Into<String>) -> Self {
        Operand {
            kind: OperandKind::Number(n),
            raw: raw.into(),
        }
    }

    pub fn fraction(num: i64, den: i64, raw: impl Into<String>) -> Self {
        Operand {
            kind: OperandKind::Fraction(Rational64::new(num, den)),
            raw: raw.into(),
        }
    }

    pub fn row_ref(idx: usize, raw: impl Into<String>) -> Self {
        Operand {
            kind: OperandKind::RowRef(idx),
            raw: raw.into(),
        }
    }

    pub fn col_ref(idx: usize, raw: impl Into<String>) -> Self {
        Operand {
            kind: OperandKind::ColRef(idx),
            raw: raw.into(),
        }
    }

    pub fn variable(name: impl Into<String>) -> Self {
        let name = name.into();
        Operand {
            kind: OperandKind::Variable(name.clone()),
            raw: name,
        }
    }

    /// 转为 f64(参数暂按 1.0 处理)
    pub fn to_f64(&self) -> f64 {
        match &self.kind {
            OperandKind::Number(n) => *n,
            OperandKind::Fraction(r) => *r.numer() as f64 / *r.denom() as f64,
            OperandKind::RowRef(_) | OperandKind::ColRef(_) | OperandKind::Variable(_) => 1.0,
        }
    }
}

// ---- 操作符 ----

/// 初等行变换操作符
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Operator {
    /// +  倍加
    Add,
    /// -  倍减
    Subtract,
    /// x  倍乘
    Multiply,
    /// <  替换
    Replace,
}

impl std::fmt::Display for Operator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Operator::Add => write!(f, "+"),
            Operator::Subtract => write!(f, "-"),
            Operator::Multiply => write!(f, "x"),
            Operator::Replace => write!(f, "<"),
        }
    }
}

// ---- 行操作 ----

/// 一次初等行变换操作
#[derive(Debug, Clone, PartialEq)]
pub struct RowOperation {
    /// 该操作应用到的目标行
    pub target: usize,
    /// 操作符
    pub operator: Operator,
    /// 操作数列表
    pub operands: Vec<Operand>,
}

impl RowOperation {
    /// 获取第一个操作数(通常是乘数)
    pub fn first_operand(&self) -> Option<&Operand> {
        self.operands.first()
    }

    /// 查找行引用(源行)
    pub fn source_row(&self) -> Option<usize> {
        self.operands.iter().find_map(|op| match &op.kind {
            OperandKind::RowRef(idx) => Some(*idx),
            _ => None,
        })
    }

    /// 查找列引用(源列)
    pub fn source_col(&self) -> Option<usize> {
        self.operands.iter().find_map(|op| match &op.kind {
            OperandKind::ColRef(idx) => Some(*idx),
            _ => None,
        })
    }
}