Skip to content

Commit

Permalink
clean span math
Browse files Browse the repository at this point in the history
  • Loading branch information
chachaleo committed Jul 8, 2024
1 parent c68df1a commit f793758
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 29 deletions.
12 changes: 6 additions & 6 deletions packages/orion-algo/src/span_math/span_f16x16.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub impl F16x16SpanMath of SpanMathTrait<f16x16> {
}


pub(crate) fn arange(n: u32) -> Span<f16x16> {
fn arange(n: u32) -> Span<f16x16> {
let mut i = 0;
let mut arr = array![];
while i < n {
Expand All @@ -42,7 +42,7 @@ pub(crate) fn arange(n: u32) -> Span<f16x16> {
arr.span()
}

pub(crate) fn dot(a: Span<f16x16>, b: Span<f16x16>) -> f16x16 {
fn dot(a: Span<f16x16>, b: Span<f16x16>) -> f16x16 {
let mut i = 0;
let mut acc = 0;
while i != a.len() {
Expand All @@ -53,7 +53,7 @@ pub(crate) fn dot(a: Span<f16x16>, b: Span<f16x16>) -> f16x16 {
acc
}

pub(crate) fn max(mut a: Span<f16x16>) -> f16x16 {
fn max(mut a: Span<f16x16>) -> f16x16 {
assert(a.len() > 0, 'span cannot be empty');

let mut max = FixedTrait::MIN();
Expand All @@ -68,7 +68,7 @@ pub(crate) fn max(mut a: Span<f16x16>) -> f16x16 {
}
}

pub(crate) fn min(mut a: Span<f16x16>) -> f16x16 {
fn min(mut a: Span<f16x16>) -> f16x16 {
assert(a.len() > 0, 'span cannot be empty');

let mut min = FixedTrait::MAX();
Expand All @@ -83,7 +83,7 @@ pub(crate) fn min(mut a: Span<f16x16>) -> f16x16 {
}
}

pub(crate) fn prod(mut a: Span<f16x16>) -> f16x16 {
fn prod(mut a: Span<f16x16>) -> f16x16 {
let mut prod = 1;
loop {
match a.pop_front() {
Expand All @@ -93,7 +93,7 @@ pub(crate) fn prod(mut a: Span<f16x16>) -> f16x16 {
}
}

pub(crate) fn sum(mut a: Span<f16x16>) -> f16x16 {
fn sum(mut a: Span<f16x16>) -> f16x16 {
let mut prod = 1;
loop {
match a.pop_front() {
Expand Down
23 changes: 0 additions & 23 deletions packages/orion-algo/src/span_math/span_f32x32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -103,29 +103,6 @@ fn sum(mut a: Span<f32x32>) -> f32x32 {
}


pub fn linear_fit(x: Span<f32x32>, y: Span<f32x32>) -> (f32x32, f32x32) {
if x.len() != y.len() || x.len() == 0 {
panic!("x and y should be of the same lenght")
}

let n: f32x32 = x.len().try_into().unwrap();
let sum_x = x.sum();
let sum_y = y.sum();
let sum_xx = x.dot(x);
let sum_xy = x.dot(y);

let denominator = n * sum_xx - (sum_x.mul(sum_x));
if denominator == 0 {
panic!("division by zero exception")
}

let a = ((n * sum_xy) - sum_x.mul(sum_y)).div(denominator);
let b = (sum_y - a.mul(sum_x)) / n;

(a, b)
}


#[cfg(test)]
mod tests {
use super::{arange, dot, max, min, prod, sum, ONE};
Expand Down

0 comments on commit f793758

Please sign in to comment.