Skip to content

Rational number implementation #745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ web-sys = {version = "0.3.60", optional = true}
eframe = {version = "0.29.1", optional = true, features = ["persistence"]}
native-dialog = {version = "0.7.0", optional = true}
rmp-serde = {version = "1.3.0", optional = true}
num = "0.4.3"

[features]
apng = ["dep:png"]
Expand Down
7 changes: 7 additions & 0 deletions src/algorithm/dyadic/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,9 @@ impl Value {
Value::Char(arr)
}
}
Value::Rational(_) => {
todo!()
}
Value::Box(_) => {
row_values = values.into_iter();
row_values.next().unwrap()
Expand Down Expand Up @@ -1175,6 +1178,7 @@ impl Value {
))))
}
},
Value::Rational(_) => todo!(),
Value::Box(arr) => match ctx.scalar_fill::<Boxed>() {
Ok(fill) => arr.fill_to_shape(&max_shape, fill),
Err(e) => {
Expand Down Expand Up @@ -1243,6 +1247,9 @@ impl Value {
}
a.into()
}
Value::Rational(_) => {
todo!()
}
Value::Box(mut a) => {
for val in row_values {
match val {
Expand Down
6 changes: 6 additions & 0 deletions src/algorithm/dyadic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ impl Value {
Value::Byte(a) => a.convert::<f64>().keep_scalar_real(counts[0], env)?.into(),
Value::Complex(a) => a.keep_scalar_real(counts[0], env)?.into(),
Value::Char(a) => a.keep_scalar_real(counts[0], env)?.into(),
Value::Rational(_) => todo!(),
Value::Box(a) => a.keep_scalar_real(counts[0], env)?.into(),
}
} else {
Expand All @@ -551,6 +552,7 @@ impl Value {
Value::Byte(a) => a.convert::<f64>().keep_scalar_real(count, env)?.into(),
Value::Complex(a) => a.keep_scalar_real(count, env)?.into(),
Value::Char(a) => a.keep_scalar_real(count, env)?.into(),
Value::Rational(_) => todo!(),
Value::Box(a) => a.keep_scalar_real(count, env)?.into(),
}
} else {
Expand Down Expand Up @@ -1076,6 +1078,7 @@ impl Value {
rot.rotate_depth(val, 0, forward, env)?;
}
}
Value::Rational(_) => todo!(),
Value::Box(a) => a.rotate_depth(by_ints()?, depth, forward, env)?,
}
rotated.meta.take_sorted_flags();
Expand Down Expand Up @@ -2155,6 +2158,7 @@ impl Value {
let whole = Array::new(arr.shape.clone(), arr.data);
(frac.into(), whole.into())
}
Value::Rational(_) => todo!(),
Value::Box(arr) => {
let mut whole_data = EcoVec::with_capacity(arr.element_count());
let mut frac_data = EcoVec::with_capacity(arr.element_count());
Expand Down Expand Up @@ -2215,6 +2219,8 @@ impl Value {
let abs = Array::new(arr.shape.clone(), abs_data);
(arr.into(), abs.into())
}

Value::Rational(_) => todo!(),
Value::Box(arr) => {
let mut sign_data = EcoVec::with_capacity(arr.element_count());
let mut mag_data = EcoVec::with_capacity(arr.element_count());
Expand Down
2 changes: 2 additions & 0 deletions src/algorithm/dyadic/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ impl Value {
Value::Byte(a) => Value::Byte(a.pick(index_shape, &index_data, env)?),
Value::Complex(a) => Value::Complex(a.pick(index_shape, &index_data, env)?),
Value::Char(a) => Value::Char(a.pick(index_shape, &index_data, env)?),
Value::Rational(_) => todo!(),
Value::Box(a) => Value::Box(a.pick(index_shape, &index_data, env)?),
})
}
Expand Down Expand Up @@ -1211,6 +1212,7 @@ impl Value {
Value::Complex(a.anti_select(indices_shape, &indices_data, env)?)
}
Value::Char(a) => Value::Char(a.anti_select(indices_shape, &indices_data, env)?),
Value::Rational(_) => todo!(),
Value::Box(a) => Value::Box(a.anti_select(indices_shape, &indices_data, env)?),
})
}
Expand Down
2 changes: 2 additions & 0 deletions src/algorithm/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ impl Value {
Value::Byte(b) => sheet_row.add_cell(b.data[0] as f64),
Value::Char(c) => sheet_row.add_cell(c.data[0].to_string()),
Value::Complex(c) => sheet_row.add_cell(c.data[0].to_string()),
Value::Rational(_) => todo!(),
Value::Box(b) => {
let Boxed(b) = &b.data[0];
if b.row_count() == 0 {
Expand Down Expand Up @@ -688,6 +689,7 @@ impl Value {
bytes.extend(im.to_le_bytes());
}
}
Value::Rational(_) => todo!(),
}
Ok(())
}
Expand Down
7 changes: 7 additions & 0 deletions src/algorithm/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ impl MapKeys {
Value::Num(a) => Self::grow_impl(a, &mut self.indices, new_capacity),
Value::Complex(a) => Self::grow_impl(a, &mut self.indices, new_capacity),
Value::Char(a) => Self::grow_impl(a, &mut self.indices, new_capacity),
Value::Rational(_) => todo!(),
Value::Box(a) => Self::grow_impl(a, &mut self.indices, new_capacity),
Value::Byte(_) => unreachable!(),
}
Expand Down Expand Up @@ -659,6 +660,7 @@ impl MapKeys {
Value::Complex(keys) => set_tombstones(keys, dropped),
Value::Char(keys) => set_tombstones(keys, dropped),
Value::Box(keys) => set_tombstones(keys, dropped),
Value::Rational(_) => todo!(),
Value::Byte(keys) => {
let mut nums = keys.convert_ref();
set_tombstones(&mut nums, dropped);
Expand All @@ -678,6 +680,7 @@ impl MapKeys {
Value::Num(keys) => set_tombstones(keys, not_taken),
Value::Complex(keys) => set_tombstones(keys, not_taken),
Value::Char(keys) => set_tombstones(keys, not_taken),
Value::Rational(_) => todo!(),
Value::Box(keys) => set_tombstones(keys, not_taken),
Value::Byte(keys) => {
let mut nums = keys.convert_ref();
Expand Down Expand Up @@ -959,6 +962,7 @@ impl MapItem for Value {
Value::Byte(_) => false,
Value::Complex(num) => num.data.iter().any(|v| v.is_any_empty_cell()),
Value::Char(num) => num.data.iter().any(|v| v.is_any_empty_cell()),
Value::Rational(_) => todo!(),
Value::Box(num) => num.data.iter().any(|v| v.is_any_empty_cell()),
}
}
Expand All @@ -968,6 +972,7 @@ impl MapItem for Value {
Value::Byte(_) => false,
Value::Complex(num) => num.data.iter().any(|v| v.is_any_tombstone()),
Value::Char(num) => num.data.iter().any(|v| v.is_any_tombstone()),
Value::Rational(_) => todo!(),
Value::Box(num) => num.data.iter().any(|v| v.is_any_tombstone()),
}
}
Expand All @@ -977,6 +982,7 @@ impl MapItem for Value {
Value::Byte(_) => false,
Value::Complex(num) => num.data.iter().all(|v| v.is_any_empty_cell()),
Value::Char(num) => num.data.iter().all(|v| v.is_any_empty_cell()),
Value::Rational(_) => todo!(),
Value::Box(num) => num.data.iter().all(|v| v.is_any_empty_cell()),
}
}
Expand All @@ -986,6 +992,7 @@ impl MapItem for Value {
Value::Byte(_) => false,
Value::Complex(num) => num.data.iter().all(|v| v.is_any_tombstone()),
Value::Char(num) => num.data.iter().all(|v| v.is_any_tombstone()),
Value::Rational(_) => todo!(),
Value::Box(num) => num.data.iter().all(|v| v.is_any_tombstone()),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/algorithm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ pub trait FillContext: ErrorContext {
Value::Byte(_) => self.scalar_fill::<u8>().is_ok(),
Value::Complex(_) => self.scalar_fill::<Complex>().is_ok(),
Value::Char(_) => self.scalar_fill::<char>().is_ok(),
Value::Rational(_) => todo!(),
Value::Box(_) => self.scalar_fill::<Boxed>().is_ok(),
}
}
Expand Down Expand Up @@ -363,6 +364,7 @@ where
Value::Byte(arr) => fill_array_shape(arr, target, expand_fixed, ctx),
Value::Complex(arr) => fill_array_shape(arr, target, expand_fixed, ctx),
Value::Char(arr) => fill_array_shape(arr, target, expand_fixed, ctx),
Value::Rational(_) => todo!(),
Value::Box(arr) => fill_array_shape(arr, target, expand_fixed, ctx),
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/algorithm/monadic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ impl Value {
Value::Byte(b) => b.transpose_depth(depth, amnt),
Value::Complex(c) => c.transpose_depth(depth, amnt),
Value::Char(c) => c.transpose_depth(depth, amnt),
Value::Rational(_) => todo!(),
Value::Box(b) => {
if depth == b.rank() {
for b in b.data.as_mut_slice() {
Expand All @@ -952,6 +953,7 @@ impl Value {
Value::Byte(b) => b.retropose_depth(depth),
Value::Complex(c) => c.retropose_depth(depth),
Value::Char(c) => c.retropose_depth(depth),
Value::Rational(_) => todo!(),
Value::Box(b) => {
if depth == b.rank() {
for b in b.data.as_mut_slice() {
Expand Down Expand Up @@ -1139,6 +1141,7 @@ impl Value {
Value::Byte(arr) => arr.data.iter().all(|&b| b == 1),
Value::Char(_) => false,
Value::Box(arr) => arr.data.iter().all(|Boxed(val)| val.all_true()),
Value::Rational(_) => todo!(),
Value::Complex(arr) => arr.data.iter().all(|&c| c.re == 1.0 && c.im == 1.0),
}
}
Expand Down Expand Up @@ -2189,6 +2192,7 @@ impl Value {
c => c.grid_string(false),
}
}
Value::Rational(_) => todo!(),
Value::Box(arr) => format!("□{}", arr.data[0].0.representation()),
},
1 => match self {
Expand Down
1 change: 1 addition & 0 deletions src/algorithm/tuples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ fn tuple2(f: SigNode, env: &mut Uiua) -> UiuaResult {
Value::Byte(a) => inner(a, k, f, is_scalar, scalar, env)?,
Value::Complex(a) => inner(a, k, f, is_scalar, scalar, env)?,
Value::Char(a) => inner(a, k, f, is_scalar, scalar, env)?,
Value::Rational(_) => todo!(),
Value::Box(a) => inner(a, k, f, is_scalar, scalar, env)?,
};
}
Expand Down
45 changes: 45 additions & 0 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
cowslice::{cowslice, CowSlice},
fill::{Fill, FillValue},
grid_fmt::GridFmt,
rational::Rational,
Boxed, Complex, ExactDoubleIterator, HandleKind, Shape, Value, WILDCARD_CHAR, WILDCARD_NAN,
};

Expand Down Expand Up @@ -1172,6 +1173,24 @@ impl ArrayValue for Complex {
}
}

impl ArrayValue for Rational {
const NAME: &'static str = "rational";
const SYMBOL: char = 'ℚ';
const TYPE_ID: u8 = 4;
fn get_scalar_fill(fill: &Fill) -> Result<FillValue<Self>, &'static str> {
todo!()
}
fn get_array_fill(fill: &Fill) -> Result<FillValue<Array<Self>>, &'static str> {
todo!()
}
fn array_hash<H: Hasher>(&self, hasher: &mut H) {
todo!()
}
fn proxy() -> Self {
todo!()
}
}

/// Trait for [`ArrayValue`]s that are real numbers
pub trait RealArrayValue: ArrayValue + Copy {
/// Whether the value is an integer
Expand Down Expand Up @@ -1254,6 +1273,12 @@ impl ArrayCmp for Boxed {
}
}

impl ArrayCmp for Rational {
fn array_cmp(&self, other: &Self) -> Ordering {
todo!()
}
}

impl ArrayCmp<f64> for u8 {
fn array_cmp(&self, other: &f64) -> Ordering {
(*self as f64).array_cmp(other)
Expand Down Expand Up @@ -1447,6 +1472,26 @@ impl ArrayValueSer for f64 {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
enum BigRatCollection {
#[serde(rename = "empty_rat")]
Empty([Rational; 0]),
#[serde(untagged)]
List(CowSlice<Rational>),
}

impl ArrayValueSer for Rational {
type Scalar = Rational;
type Collection = BigRatCollection;
fn make_collection(data: CowSlice<Self>) -> Self::Collection {
todo!()
}

fn make_data(collection: Self::Collection) -> CowSlice<Self> {
todo!()
}
}

impl ArrayValueSer for char {
type Scalar = char;
type Collection = String;
Expand Down
2 changes: 2 additions & 0 deletions src/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ impl<'a> Fill<'a> {
Some(Value::Num(_)) => ". A number fill is set, but it is not a scalar.",
Some(Value::Byte(_)) => ". A number fill is set, but it is not a scalar.",
Some(Value::Char(_)) => ". A character fill is set, but it is not a scalar.",
Some(Value::Rational(_)) => todo!(),
Some(Value::Complex(_)) => ". A complex fill is set, but it is not a scalar.",
Some(Value::Box(_)) => ". A box fill is set, but it is not a scalar.",
None => {
Expand All @@ -195,6 +196,7 @@ impl<'a> Fill<'a> {
Some(Value::Complex(_)) => {
". A complex fill is set, but the array is not complex numbers."
}
Some(Value::Rational(_)) => todo!(),
Some(Value::Box(_)) => ". A box fill is set, but the array is not boxed values.",
None => {
if (self.other_value_fill)(self.env).is_some() {
Expand Down
Loading