add contains function for monomials
This commit is contained in:
@@ -7,9 +7,20 @@ fn main() {
|
|||||||
+ (3 * ((var!("x", 1, 9) ^ 5) * (var!("x", 1, 2) ^ 5) * (var!("x", 2, 5) ^ 1)));
|
+ (3 * ((var!("x", 1, 9) ^ 5) * (var!("x", 1, 2) ^ 5) * (var!("x", 2, 5) ^ 1)));
|
||||||
|
|
||||||
let x = var!("x");
|
let x = var!("x");
|
||||||
let y = var!("y");
|
let y = var!("x\u{0304}");
|
||||||
|
let z = var!("z");
|
||||||
let other = -3 * ((&x ^ 2) * (&y ^ 4));
|
let other = -3 * ((&x ^ 2) * (&y ^ 4));
|
||||||
|
|
||||||
|
let mono = (&x^2)*(&y^4);
|
||||||
|
|
||||||
|
let inside = (&x^2)*(&y^2)*(&z^1);
|
||||||
|
|
||||||
|
if mono.contains(&inside){
|
||||||
|
println!("{inside}\u{2286}{mono}");
|
||||||
|
}else{
|
||||||
|
println!("{inside}\u{2284}{mono}");
|
||||||
|
}
|
||||||
|
|
||||||
println!("{poly}");
|
println!("{poly}");
|
||||||
|
|
||||||
let z = poly - other;
|
let z = poly - other;
|
||||||
|
|||||||
34
src/fmt.rs
Normal file
34
src/fmt.rs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
|
||||||
|
pub fn num_to_subscript(s:String)->String{
|
||||||
|
s.chars().map(|c| match c{
|
||||||
|
'0'=>'\u{2080}',
|
||||||
|
'1'=>'\u{2081}',
|
||||||
|
'2'=>'\u{2082}',
|
||||||
|
'3'=>'\u{2083}',
|
||||||
|
'4'=>'\u{2084}',
|
||||||
|
'5'=>'\u{2085}',
|
||||||
|
'6'=>'\u{2086}',
|
||||||
|
'7'=>'\u{2087}',
|
||||||
|
'8'=>'\u{2088}',
|
||||||
|
'9'=>'\u{2089}',
|
||||||
|
_=>c
|
||||||
|
}).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn num_to_superscript(s:String)->String{
|
||||||
|
s.chars().map(|c| match c{
|
||||||
|
'0'=>'\u{2070}',
|
||||||
|
'1'=>'\u{20B9}',
|
||||||
|
'2'=>'\u{00B2}',
|
||||||
|
'3'=>'\u{00B3}',
|
||||||
|
'4'=>'\u{2074}',
|
||||||
|
'5'=>'\u{2075}',
|
||||||
|
'6'=>'\u{2076}',
|
||||||
|
'7'=>'\u{2077}',
|
||||||
|
'8'=>'\u{2078}',
|
||||||
|
'9'=>'\u{2079}',
|
||||||
|
_=>c
|
||||||
|
}).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1 +1,2 @@
|
|||||||
pub mod poly;
|
pub mod poly;
|
||||||
|
pub mod fmt;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use std::fmt::{self, Display};
|
|||||||
|
|
||||||
use std::ops::{Add, Sub, BitXor, Mul};
|
use std::ops::{Add, Sub, BitXor, Mul};
|
||||||
|
|
||||||
|
use crate::fmt::num_to_superscript;
|
||||||
use super::var::{StaticVar, Var};
|
use super::var::{StaticVar, Var};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
@@ -57,6 +58,33 @@ pub struct Mono<V: Var> {
|
|||||||
pub term: Vec<(V, u32)>,
|
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>
|
impl<V: Var, T: IntoIterator> From<T> for Mono<V>
|
||||||
where
|
where
|
||||||
Mono<V>: FromIterator<<T as IntoIterator>::Item>,
|
Mono<V>: FromIterator<<T as IntoIterator>::Item>,
|
||||||
@@ -94,7 +122,7 @@ impl<V: Var> Display for Mono<V> {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|(t, p)| match p {
|
.map(|(t, p)| match p {
|
||||||
1 => format!("{t}"),
|
1 => format!("{t}"),
|
||||||
_ => format!("{t}^{p}"),
|
_ => format!("{t}{}",num_to_superscript(p.to_string())),
|
||||||
})
|
})
|
||||||
.join("")
|
.join("")
|
||||||
)
|
)
|
||||||
@@ -194,3 +222,4 @@ impl<V: Var> Sub for Poly<V> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ use itertools::Itertools;
|
|||||||
use std::fmt::{self, Debug, Display, Formatter};
|
use std::fmt::{self, Debug, Display, Formatter};
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
|
||||||
|
use crate::fmt::num_to_subscript;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
pub struct StaticVar {
|
pub struct StaticVar {
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
@@ -15,7 +17,7 @@ impl Display for StaticVar {
|
|||||||
let num_indices = self.indices.len();
|
let num_indices = self.indices.len();
|
||||||
match num_indices {
|
match num_indices {
|
||||||
0 => write!(fmt, "{}", self.name),
|
0 => write!(fmt, "{}", self.name),
|
||||||
_ => write!(fmt, "{}_{{{}}}", self.name, self.indices.iter().join(",")),
|
_ => write!(fmt, "{}{}", self.name, self.indices.iter().map(|x| num_to_subscript(x.to_string())).join(",")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user