add contains function for monomials

This commit is contained in:
2026-04-22 10:22:32 +02:00
parent 91f196035c
commit 09a9613870
5 changed files with 80 additions and 3 deletions

View File

@@ -3,6 +3,7 @@ use std::fmt::{self, Display};
use std::ops::{Add, Sub, BitXor, Mul};
use crate::fmt::num_to_superscript;
use super::var::{StaticVar, Var};
use std::collections::HashMap;
@@ -57,6 +58,33 @@ pub struct Mono<V: Var> {
pub term: Vec<(V, u32)>,
}
impl<V:Var> Mono<V>{
pub fn contains(&self, other:&Mono<V>)->bool{
let mut self_it=self.term.iter().peekable();
let mut other_it=other.term.iter().peekable();
while let Some((o_term,o_exp))=other_it.peek(){
if let Some((s_term,s_exp))=self_it.peek(){
if s_term<o_term{
self_it.next();
continue;
}else if s_term>o_term{
return false;
}else if o_exp<=s_exp{
self_it.next();
other_it.next();
}else{
return false;
}
}else{
return false;
}
}
true
}
}
impl<V: Var, T: IntoIterator> From<T> for Mono<V>
where
Mono<V>: FromIterator<<T as IntoIterator>::Item>,
@@ -94,7 +122,7 @@ impl<V: Var> Display for Mono<V> {
.iter()
.map(|(t, p)| match p {
1 => format!("{t}"),
_ => format!("{t}^{p}"),
_ => format!("{t}{}",num_to_superscript(p.to_string())),
})
.join("")
)
@@ -194,3 +222,4 @@ impl<V: Var> Sub for Poly<V> {
self
}
}