text
stringlengths
1
2.05k
fn div_eq(ref self: i32, other: i32) { self = Div::div(self, other); } } impl I32IntoU32 of Into<i32, u32> { fn into(self: i32) -> u32 { let number_sign: bool = self < 0; let mut self_positive: i32 = self; if number_sign { self_positive = self_positive * -1_i32 } let number_felt: felt252 = self_positive.into(); let number_u32: u32 = number_felt.try_into().unwrap(); number_u32 } } impl I64Number of NumberTrait<i64, i64> { fn new(mag: i64, sign: bool) -> i64 { if sign { return -mag; } mag } fn new_unscaled(mag: i64, sign: bool) -> i64 { mag } fn from_felt(val: felt252) -> i64 { panic(array!['not supported!']) } fn ceil(self: i64) -> i64 { panic(array!['not supported!']) } fn exp(self: i64) -> i64 { panic(array!['not supported!']) } fn exp2(self: i64) -> i64 { panic(array!['not supported!']) } fn floor(self: i64) -> i64 { panic(array!['not supported!']) } fn ln(self: i64) -> i64 { panic(array!['not supported!']) } fn log2(self: i64) -> i64 { panic(array!['not supported!']) } fn log10(self: i64) -> i64 { panic(array!['not supported!']) } fn pow(self: i64, b: i64) -> i64 { panic(array!['not supported!']) } fn round(self: i64) -> i64 { panic(array!['not supported!']) } fn sqrt(self: i64) -> i64 { panic(array!['not supported!']) } fn acos(self: i64) -> i64 { panic(array!['not supported!']) } fn asin(self: i64) -> i64 { panic(array!['not supported!']) } fn atan(self: i64) -> i64 { panic(array!['not supported!']) } fn cos(self: i64) -> i64 { panic(array!['not supported!']) } fn sin(self: i64) -> i64 { panic(array!['not supported!']) } fn tan(self: i64) -> i64 { panic(array!['not supported!'
]) } fn acosh(self: i64) -> i64 { panic(array!['not supported!']) } fn asinh(self: i64) -> i64 { panic(array!['not supported!']) } fn atanh(self: i64) -> i64 { panic(array!['not supported!']) } fn cosh(self: i64) -> i64 { panic(array!['not supported!']) } fn sinh(self: i64) -> i64 { panic(array!['not supported!']) } fn tanh(self: i64) -> i64 { panic(array!['not supported!']) } fn zero() -> i64 { 0 } fn is_zero(self: i64) -> bool { self == 0 } fn half() -> i64 { panic(array!['not supported!']) } fn one() -> i64 { 1 } fn neg_one() -> i64 { -1 } fn is_one(self: i64) -> bool { self == 1 } fn abs(self: i64) -> i64 { if self >= 0 { self } else { self * -1_i64 } } fn neg(self: i64) -> i64 { self * -1_i64 } fn min_value() -> i64 { -9223372036854775807 } fn max_value() -> i64 { 9223372036854775807 } fn min(self: i64, other: i64) -> i64 { if self < other { self } else { other } } fn max(self: i64, other: i64) -> i64 { if self > other { self } else { other } } fn mag(self: i64) -> i64 { self } fn is_neg(self: i64) -> bool { self < 0 } fn xor(lhs: i64, rhs: i64) -> bool { if (lhs == 0 || rhs == 0) && lhs != rhs { true } else { false } } fn or(lhs: i64, rhs: i64) -> bool { if lhs == 0 && rhs == 0 { false } else { true } } fn sign(self: i64) -> i64 { if self == 0 { 0_i64 } else if self > 0 { 1_i64 } else { -1_i64 } } fn and(lhs: i64, rhs: i64) -> bool { if lhs == 0 || rhs
== 0 { false } else { true } } fn where(self: i64, x: i64, y: i64) -> i64 { if self == 0 { y } else { x } } fn NaN() -> i64 { panic(array!['not supported!']) } fn is_nan(self: i64) -> bool { panic(array!['not supported!']) } fn INF() -> i64 { 9223372036854775807 } fn is_inf(self: i64) -> bool { self == 9223372036854775807 || self == -9223372036854775807 } fn is_pos_inf(self: i64) -> bool { self == 9223372036854775807 } fn is_neg_inf(self: i64) -> bool { self == -9223372036854775807 } fn bitwise_and(lhs: i64, rhs: i64) -> i64 { panic(array!['not supported!']) } fn bitwise_xor(lhs: i64, rhs: i64) -> i64 { panic(array!['not supported!']) } fn bitwise_or(lhs: i64, rhs: i64) -> i64 { panic(array!['not supported!']) } fn add(lhs: i64, rhs: i64) -> i64 { lhs + rhs } fn sub(lhs: i64, rhs: i64) -> i64 { lhs - rhs } } impl I64Div of Div<i64> { fn div(lhs: i64, rhs: i64) -> i64 { assert(rhs != 0, 'divisor cannot be 0'); let mut lhs_positive = lhs; let mut rhs_positive = rhs; if lhs < 0 { lhs_positive = lhs * -1; } if rhs < 0 { rhs_positive = rhs * -1; } let lhs_felt: felt252 = lhs_positive.into(); let rhs_felt: felt252 = rhs_positive.into(); let lhs_u128: u128 = lhs_felt.try_into().unwrap(); let rhs_u128: u128 = rhs_felt.try_into().unwrap(); let mut result = lhs_u128 / rhs_u128; let felt_result: felt252 = result.into(); let signed_int_result: i64 = felt_result.try_into().unwrap(); if lhs * rhs < 0 { signed_int_result * -1 } else { signed_int_result } } } impl I64DivEq of DivEq<i64> {
fn div_eq(ref self: i64, other: i64) { self = Div::div(self, other); } } impl I128Number of NumberTrait<i128, i128> { fn new(mag: i128, sign: bool) -> i128 { if sign { return -mag; } mag } fn new_unscaled(mag: i128, sign: bool) -> i128 { mag } fn from_felt(val: felt252) -> i128 { panic(array!['not supported!']) } fn ceil(self: i128) -> i128 { panic(array!['not supported!']) } fn exp(self: i128) -> i128 { panic(array!['not supported!']) } fn exp2(self: i128) -> i128 { panic(array!['not supported!']) } fn floor(self: i128) -> i128 { panic(array!['not supported!']) } fn ln(self: i128) -> i128 { panic(array!['not supported!']) } fn log2(self: i128) -> i128 { panic(array!['not supported!']) } fn log10(self: i128) -> i128 { panic(array!['not supported!']) } fn pow(self: i128, b: i128) -> i128 { panic(array!['not supported!']) } fn round(self: i128) -> i128 { panic(array!['not supported!']) } fn sqrt(self: i128) -> i128 { panic(array!['not supported!']) } fn acos(self: i128) -> i128 { panic(array!['not supported!']) } fn asin(self: i128) -> i128 { panic(array!['not supported!']) } fn atan(self: i128) -> i128 { panic(array!['not supported!']) } fn cos(self: i128) -> i128 { panic(array!['not supported!']) } fn sin(self: i128) -> i128 { panic(array!['not supported!']) } fn tan(self: i128) -> i128 { panic(array!['not supported!']) } fn acosh(self: i128) -> i128 { panic(array!['not supported!']) } fn asinh(self: i128) -> i128 { panic(array!['not supported!']) } fn atanh(self: i128) -> i128 { panic(array!['not supported!']) } fn cosh(self: i128) -> i128 { panic(array!['not supported!']) } fn sinh(
self: i128) -> i128 { panic(array!['not supported!']) } fn tanh(self: i128) -> i128 { panic(array!['not supported!']) } fn zero() -> i128 { 0 } fn is_zero(self: i128) -> bool { self == 0 } fn half() -> i128 { panic(array!['not supported!']) } fn one() -> i128 { 1 } fn neg_one() -> i128 { -1 } fn is_one(self: i128) -> bool { self == 1 } fn abs(self: i128) -> i128 { if self >= 0 { self } else { self * -1_i128 } } fn neg(self: i128) -> i128 { self * -1_i128 } fn min_value() -> i128 { -170141183460469231731687303715884105727 } fn max_value() -> i128 { 170141183460469231731687303715884105727 } fn min(self: i128, other: i128) -> i128 { if self < other { self } else { other } } fn max(self: i128, other: i128) -> i128 { if self > other { self } else { other } } fn mag(self: i128) -> i128 { self } fn is_neg(self: i128) -> bool { self < 0 } fn xor(lhs: i128, rhs: i128) -> bool { if (lhs == 0 || rhs == 0) && lhs != rhs { true } else { false } } fn or(lhs: i128, rhs: i128) -> bool { if lhs == 0 && rhs == 0 { false } else { true } } fn sign(self: i128) -> i128 { if self == 0 { 0_i128 } else if self > 0 { 1_i128 } else { -1_i128 } } fn and(lhs: i128, rhs: i128) -> bool { if lhs == 0 || rhs == 0 { false } else { true } } fn where(self: i128, x: i128, y: i128) -> i128 { if self == 0 { y } else { x } } fn NaN() -> i128 { panic(array!['not
supported!']) } fn is_nan(self: i128) -> bool { panic(array!['not supported!']) } fn INF() -> i128 { 170141183460469231731687303715884105727 } fn is_inf(self: i128) -> bool { self == 170141183460469231731687303715884105727 || self == -170141183460469231731687303715884105727 } fn is_pos_inf(self: i128) -> bool { self == 170141183460469231731687303715884105727 } fn is_neg_inf(self: i128) -> bool { self == -170141183460469231731687303715884105727 } fn bitwise_and(lhs: i128, rhs: i128) -> i128 { panic(array!['not supported!']) } fn bitwise_xor(lhs: i128, rhs: i128) -> i128 { panic(array!['not supported!']) } fn bitwise_or(lhs: i128, rhs: i128) -> i128 { panic(array!['not supported!']) } fn add(lhs: i128, rhs: i128) -> i128 { lhs + rhs } fn sub(lhs: i128, rhs: i128) -> i128 { lhs - rhs } } impl I128Div of Div<i128> { fn div(lhs: i128, rhs: i128) -> i128 { assert(rhs != 0, 'divisor cannot be 0'); let mut lhs_positive = lhs; let mut rhs_positive = rhs; if lhs < 0 { lhs_positive = lhs * -1; } if rhs < 0 { rhs_positive = rhs * -1; } let lhs_felt: felt252 = lhs_positive.into(); let rhs_felt: felt252 = rhs_positive.into(); let lhs_u128: u128 = lhs_felt.try_into().unwrap(); let rhs_u128: u128 = rhs_felt.try_into().unwrap(); let mut result = lhs_u128 / rhs_u128; let felt_result: felt252 = result.into(); let signed_int_result: i128 = felt_result.try_into().unwrap(); if lhs * rhs < 0 { signed_int_result * -1 } else { signed_int_result } } } impl I128DivEq of DivEq<i128> {
fn div_eq(ref self: i128, other: i128) { self = Div::div(self, other); } } impl u32Number of NumberTrait<u32, u32> { fn new(mag: u32, sign: bool) -> u32 { mag } fn new_unscaled(mag: u32, sign: bool) -> u32 { mag } fn from_felt(val: felt252) -> u32 { panic(array!['not supported!']) } fn ceil(self: u32) -> u32 { panic(array!['not supported!']) } fn exp(self: u32) -> u32 { panic(array!['not supported!']) } fn exp2(self: u32) -> u32 { panic(array!['not supported!']) } fn floor(self: u32) -> u32 { panic(array!['not supported!']) } fn ln(self: u32) -> u32 { panic(array!['not supported!']) } fn log2(self: u32) -> u32 { panic(array!['not supported!']) } fn log10(self: u32) -> u32 { panic(array!['not supported!']) } fn pow(self: u32, b: u32) -> u32 { panic(array!['not supported!']) } fn round(self: u32) -> u32 { panic(array!['not supported!']) } fn sqrt(self: u32) -> u32 { panic(array!['not supported!']) } fn acos(self: u32) -> u32 { panic(array!['not supported!']) } fn asin(self: u32) -> u32 { panic(array!['not supported!']) } fn atan(self: u32) -> u32 { panic(array!['not supported!']) } fn cos(self: u32) -> u32 { panic(array!['not supported!']) } fn sin(self: u32) -> u32 { panic(array!['not supported!']) } fn tan(self: u32) -> u32 { panic(array!['not supported!']) } fn acosh(self: u32) -> u32 { panic(array!['not supported!']) } fn asinh(self: u32) -> u32 { panic(array!['not supported!']) } fn atanh(self: u32) -> u32 { panic(array!['not supported!']) } fn cosh(self: u32) -> u32 { panic(array!['not supported!']) } fn sinh(self: u32) -> u32 { panic(array!['not supported!']) } fn tanh(self: u32) -> u32 {
panic(array!['not supported!']) } fn zero() -> u32 { 0 } fn is_zero(self: u32) -> bool { self == 0 } fn half() -> u32 { panic(array!['not supported!']) } fn one() -> u32 { 1 } fn neg_one() -> u32 { panic(array!['not supported']) } fn is_one(self: u32) -> bool { self == 1 } fn abs(self: u32) -> u32 { self } fn neg(self: u32) -> u32 { panic(array!['not supported']) } fn min_value() -> u32 { 0 } fn max_value() -> u32 { 4294967295 } fn min(self: u32, other: u32) -> u32 { if self < other { self } else { other } } fn max(self: u32, other: u32) -> u32 { if self > other { self } else { other } } fn mag(self: u32) -> u32 { self } fn is_neg(self: u32) -> bool { false } fn xor(lhs: u32, rhs: u32) -> bool { if (lhs == 0 || rhs == 0) && lhs != rhs { true } else { false } } fn or(lhs: u32, rhs: u32) -> bool { if lhs == 0 && rhs == 0 { false } else { true } } fn sign(self: u32) -> u32 { panic(array!['not supported!']) } fn and(lhs: u32, rhs: u32) -> bool { if lhs == 0 || rhs == 0 { false } else { true } } fn where(self: u32, x: u32, y: u32) -> u32 { if self == 0 { y } else { x } } fn NaN() -> u32 { 4242424242 } fn is_nan(self: u32) -> bool { self == 4242424242 } fn INF() -> u32 { 4294967295 } fn is_inf(self: u32) -> bool { self == 4294967295 } fn is_pos_inf(self: u32) -> bool { self == 4294967295 } fn is_neg_inf(self: u32) -> bool { panic(array!['not supported!']) }
fn bitwise_and(lhs: u32, rhs: u32) -> u32 { lhs & rhs } fn bitwise_xor(lhs: u32, rhs: u32) -> u32 { lhs ^ rhs } fn bitwise_or(lhs: u32, rhs: u32) -> u32 { lhs | rhs } fn add(lhs: u32, rhs: u32) -> u32 { lhs + rhs } fn sub(lhs: u32, rhs: u32) -> u32 { lhs - rhs } } use orion::numbers::complex_number::complex_trait::ComplexTrait; use orion::numbers::complex_number::complex64::{ Complex64Impl, complex64, Complex64Add, Complex64Sub }; impl Complex64Number of NumberTrait<complex64, FP64x64> { fn new(mag: FP64x64, sign: bool) -> complex64 { panic(array!['not supported!']) } fn new_unscaled(mag: FP64x64, sign: bool) -> complex64 { panic(array!['not supported!']) } fn from_felt(val: felt252) -> complex64 { panic(array!['not supported!']) } fn ceil(self: complex64) -> complex64 { panic(array!['not supported!']) } fn exp(self: complex64) -> complex64 { Complex64Impl::exp(self) } fn exp2(self: complex64) -> complex64 { Complex64Impl::exp2(self) } fn floor(self: complex64) -> complex64 { panic(array!['not supported!']) } fn ln(self: complex64) -> complex64 { Complex64Impl::ln(self) } fn log2(self: complex64) -> complex64 { Complex64Impl::log2(self) } fn log10(self: complex64) -> complex64 { Complex64Impl::log10(self) } fn pow(self: complex64, b: complex64) -> complex64 { Complex64Impl::pow(self, b) } fn round(self: complex64) -> complex64 { panic(array!['not supported!']) } fn sqrt(self: complex64) -> complex64 { Complex64Impl::sqrt(self) } fn acos(self: complex64) -> complex64 { Complex64Impl::acos(self) } fn asin(self: complex64) -> complex64 { Complex64Impl::asin(self) } fn atan(self: complex64) -> complex64 { Complex64Impl::atan(self) } fn cos(self: complex6
4) -> complex64 { Complex64Impl::cos(self) } fn sin(self: complex64) -> complex64 { Complex64Impl::sin(self) } fn tan(self: complex64) -> complex64 { Complex64Impl::tan(self) } fn acosh(self: complex64) -> complex64 { Complex64Impl::acosh(self) } fn asinh(self: complex64) -> complex64 { Complex64Impl::asinh(self) } fn atanh(self: complex64) -> complex64 { Complex64Impl::atanh(self) } fn cosh(self: complex64) -> complex64 { Complex64Impl::cosh(self) } fn sinh(self: complex64) -> complex64 { Complex64Impl::sinh(self) } fn tanh(self: complex64) -> complex64 { Complex64Impl::tanh(self) } fn zero() -> complex64 { Complex64Impl::zero() } fn is_zero(self: complex64) -> bool { if self == Complex64Impl::zero() { return true; } false } fn half() -> complex64 { panic(array!['not supported!']) } fn one() -> complex64 { Complex64Impl::one() } fn neg_one() -> complex64 { Complex64Impl::new(FP64x64 { mag: core_fp64x64::ONE, sign: true }, FP64x64Impl::ZERO()) } fn is_one(self: complex64) -> bool { if self == Complex64Impl::one() { return true; } false } fn abs(self: complex64) -> complex64 { Complex64Impl::new(Complex64Impl::mag(self), FP64x64Impl::ZERO()) } fn neg(self: complex64) -> complex64 { panic(array!['not supported!']) } fn min_value() -> complex64 { panic(array!['not supported!']) } fn max_value() -> complex64 { panic(array!['not supported!']) } fn min(self: complex64, other: complex64) -> complex64 { panic(array!['not supported!']) } fn max(self: complex64, other: complex64) -> complex64 { panic(array!['not supported!']) } fn mag(self: complex64) -> FP64x64 { Complex64Impl::mag(self) } fn is_ne
g(self: complex64) -> bool { panic(array!['not supported!']) } fn xor(lhs: complex64, rhs: complex64) -> bool { panic(array!['not supported!']) } fn or(lhs: complex64, rhs: complex64) -> bool { panic(array!['not supported!']) } fn sign(self: complex64) -> complex64 { panic(array!['not supported!']) } fn and(lhs: complex64, rhs: complex64) -> bool { panic(array!['not supported!']) } fn where(self: complex64, x: complex64, y: complex64) -> complex64 { panic(array!['not supported!']) } fn NaN() -> complex64 { panic(array!['not supported!']) } fn is_nan(self: complex64) -> bool { panic(array!['not supported!']) } fn INF() -> complex64 { panic(array!['not supported!']) } fn is_inf(self: complex64) -> bool { panic(array!['not supported!']) } fn is_pos_inf(self: complex64) -> bool { panic(array!['not supported!']) } fn is_neg_inf(self: complex64) -> bool { panic(array!['not supported!']) } fn bitwise_and(lhs: complex64, rhs: complex64) -> complex64 { panic(array!['not supported!']) } fn bitwise_xor(lhs: complex64, rhs: complex64) -> complex64 { panic(array!['not supported!']) } fn bitwise_or(lhs: complex64, rhs: complex64) -> complex64 { panic(array!['not supported!']) } fn add(lhs: complex64, rhs: complex64) -> complex64 { Complex64Add::add(lhs, rhs) } fn sub(lhs: complex64, rhs: complex64) -> complex64 { Complex64Sub::sub(lhs, rhs) } } impl U32IntoI32 of Into<u32, i32> { fn into(self: u32) -> i32 { let number_felt: felt252 = self.into(); let number_i32: i32 = number_felt.try_into().unwrap(); number_i32 } }
mod complex_trait; mod complex64;
use core::debug::PrintTrait; use orion::numbers::complex_number::complex_trait::ComplexTrait; use orion::numbers::{FP64x64, FP64x64Impl, FP32x32, FP32x32Impl, FixedTrait};
struct complex64 { real: FP64x64, img: FP64x64, } const PI: u128 = 57952155664616982739; const HALF_PI: u128 = 28976077832308491370; const TWO: u128 = 36893488147419103232; const E: u128 = 50143449208471493718; const HALF: u128 = 9223372036854775808; impl Complex64Impl of ComplexTrait<complex64, FP64x64> { fn new(real: FP64x64, img: FP64x64) -> complex64 { complex64 { real, img } } fn real(self: complex64) -> FP64x64 { self.real } fn img(self: complex64) -> FP64x64 { self.img } fn conjugate(self: complex64) -> complex64 { ComplexTrait::new(self.real, -self.img) } fn zero() -> complex64 { complex64 { real: FixedTrait::ZERO(), img: FP64x64Impl::ZERO() } } fn one() -> complex64 { complex64 { real: FP64x64Impl::ONE(), img: FP64x64Impl::ZERO() } } fn mag(self: complex64) -> FP64x64 { let two = FP64x64Impl::new(TWO, false); (self.real.pow(two) + self.img.pow(two)).sqrt() } fn arg(self: complex64) -> FP64x64 { atan2(self.real, self.img) } fn exp(self: complex64) -> complex64 { let real = self.real.exp() * self.img.cos(); let img = self.real.exp() * self.img.sin(); complex64 { real, img } } fn exp2(self: complex64) -> complex64 { let two = complex64 { real: FP64x64Impl::new(TWO, false), img: FP64x64Impl::ZERO() }; two.pow(self) } fn sqrt(self: complex64) -> complex64 { let x = self.real; let y = self.img; let two = FP64x64Impl::new(TWO, false); let real = (((x.pow(two) + y.pow(two)).sqrt() + x) / two).sqrt(); let img = if y == FP64x64Impl::ZERO() { FP64x64Impl::ZERO() } else { (((x.pow(two) + y.pow(two)).sqrt() - x) / two).sqrt() }; let img = FP64x64Impl::new(img.mag, y.sign); complex64 { real, img } } fn ln(self: complex64) -> complex64 { let real = self.mag().ln(); let img = self.arg
(); complex64 { real, img } } fn log2(self: complex64) -> complex64 { let ln_2 = FP64x64Impl::new(12786309186476892720, false); let ln = self.ln(); complex64 { real: (ln.real / ln_2), img: (ln.img / ln_2) } } fn log10(self: complex64) -> complex64 { let ln_10 = FP64x64Impl::new(42475197399893398429, false); let ln = self.ln(); complex64 { real: (ln.real / ln_10), img: (ln.img / ln_10) } } fn pow(self: complex64, b: complex64) -> complex64 { let two = FP64x64Impl::new(TWO, false); let x = self.real; let y = self.img; if (b.real == two && b.img == FP64x64Impl::new(0, false)) { let real = x.pow(two) - y.pow(two); let img = two * x * y; return complex64 { real, img }; } if (b.img == FP64x64Impl::new(0, false)) { let mag_pow_n = self.mag().pow(b.real); let arg_mul_n = b.real * self.arg(); let real = mag_pow_n * arg_mul_n.cos(); let img = mag_pow_n * arg_mul_n.sin(); return complex64 { real, img }; } let A = FP64x64Impl::new(E, false).pow(b.real * self.mag().ln() - b.img * self.arg()); let B = b.real * self.arg() + b.img * self.mag().ln(); let real = A * B.cos(); let img = A * B.sin(); complex64 { real, img } } fn cos(self: complex64) -> complex64 { let a = self.real; let b = self.img; complex64 { real: FP64x64Impl::cos(a) * FP64x64Impl::cosh(b), img: -FP64x64Impl::sin(a) * FP64x64Impl::sinh(b) } } fn sin(self: complex64) -> complex64 { let a = self.real; let b = self.img; complex64 { real: FP64x64Impl::sin(a) * FP64x64Impl::cosh(b), img: FP64x64Impl::cos(a) * FP64x64Impl::sinh(b) } } fn tan(self: complex64) -> complex64 { let two = FP64
x64Impl::new(TWO, false); let a = self.real; let b = self.img; let den = FP64x64Impl::cosh(two * b) + FP64x64Impl::cos(two * a); complex64 { real: FP64x64Impl::sin(two * a) / den, img: FP64x64Impl::sinh(two * b) / den } } fn acos(self: complex64) -> complex64 { let pi = Complex64Impl::new(FP64x64Impl::new(PI, false), FP64x64Impl::ZERO()); let two = Complex64Impl::new(FP64x64Impl::new(TWO, false), FP64x64Impl::ZERO()); let i = Complex64Impl::new(FP64x64Impl::ZERO(), FP64x64Impl::ONE()); let one = Complex64Impl::new(FP64x64Impl::ONE(), FP64x64Impl::ZERO()); let acos = pi / two + i * Complex64Impl::ln(i * self + Complex64Impl::sqrt(one - (self.pow(two)))); acos } fn asin(self: complex64) -> complex64 { let two = Complex64Impl::new(FP64x64Impl::new(TWO, false), FP64x64Impl::ZERO()); let i = Complex64Impl::new(FP64x64Impl::ZERO(), FP64x64Impl::ONE()); let one = Complex64Impl::new(FP64x64Impl::ONE(), FP64x64Impl::ZERO()); let asin = -i * Complex64Impl::ln(i * self + Complex64Impl::sqrt(one - (self.pow(two)))); asin } fn atan(self: complex64) -> complex64 { let two = Complex64Impl::new(FP64x64Impl::new(TWO, false), FP64x64Impl::ZERO()); let i = Complex64Impl::new(FP64x64Impl::ZERO(), FP64x64Impl::ONE()); let one = Complex64Impl::new(FP64x64Impl::ONE(), FP64x64Impl::ZERO()); let atan = one / two * i * (Complex64Impl::ln(one - i * self) - Complex64Impl::ln(one + i * self)); atan } fn acosh(self: complex64) -> complex64 { let one = Complex64Impl::new(FP64x64Impl::ONE(), FP64x64Impl::ZERO()); let acosh = Complex64Impl::ln( self + Complex64Impl::sqrt(self + one) * Complex64Impl::sqrt(self - one) ); acosh } fn asinh(self: complex64) -> complex64 { let one = Complex64Impl::new(FP64x64Impl::ONE(), F
P64x64Impl::ZERO()); let two = Complex64Impl::new(FP64x64Impl::new(TWO, false), FP64x64Impl::ZERO()); let asinh = Complex64Impl::ln(self + Complex64Impl::sqrt(one + (self.pow(two)))); asinh } fn atanh(self: complex64) -> complex64 { let two = Complex64Impl::new(FP64x64Impl::new(TWO, false), FP64x64Impl::ZERO()); let one = Complex64Impl::new(FP64x64Impl::ONE(), FP64x64Impl::ZERO()); let atanh = (Complex64Impl::ln(one + self) - Complex64Impl::ln(one - self)) / two; atanh } fn cosh(self: complex64) -> complex64 { let a = self.real; let b = self.img; complex64 { real: FP64x64Impl::cosh(a) * FP64x64Impl::cos(b), img: FP64x64Impl::sinh(a) * FP64x64Impl::sin(b) } } fn sinh(self: complex64) -> complex64 { let a = self.real; let b = self.img; complex64 { real: FP64x64Impl::sinh(a) * FP64x64Impl::cos(b), img: FP64x64Impl::cosh(a) * FP64x64Impl::sin(b) } } fn tanh(self: complex64) -> complex64 { let two = FP64x64Impl::new(TWO, false); let a = self.real; let b = self.img; let den = FP64x64Impl::cosh(two * a) + FP64x64Impl::cos(two * b); complex64 { real: FP64x64Impl::sinh(two * a) / den, img: FP64x64Impl::sin(two * b) / den } } fn to_polar(self: complex64) -> (FP64x64, FP64x64) { let mag = self.mag(); let arg = self.arg(); (mag, arg) } fn from_polar(mag: FP64x64, arg: FP64x64) -> complex64 { let real = mag * arg.cos(); let img = mag * arg.sin(); complex64 { real, img } } fn reciprocal(self: complex64) -> complex64 { let two = FP64x64Impl::new(TWO, false); let x = self.real; let y = self.img; let real = x / (x.pow(two) + y.pow(two)); let img = -y / (x.pow(two) + y.pow(two)); complex64 { real, img } } } fn atan2(x: FP64x64, y: FP64x
64) -> FP64x64 { let two = FP64x64Impl::new(TWO, false); if (y != FP64x64Impl::ZERO() || x > FP64x64Impl::ZERO()) { return two * (y / (x + (x.pow(two) + y.pow(two)).sqrt())).atan(); } else if x < FP64x64Impl::ZERO() { return FP64x64Impl::new(PI, false); } else { panic(array!['undifined']) } } impl Complex64Print of PrintTrait<complex64> {
fn print(self: complex64) { self.real.print(); '+'.print(); self.img.print(); 'i'.print(); } } impl Complex64Add of Add<complex64> { fn add(lhs: complex64, rhs: complex64) -> complex64 { complex64_add(lhs, rhs) } } impl Complex64AddEq of AddEq<complex64> {
fn add_eq(ref self: complex64, other: complex64) { self = Add::add(self, other); } } impl Complex64Sub of Sub<complex64> { fn sub(lhs: complex64, rhs: complex64) -> complex64 { complex64_sub(lhs, rhs) } } impl Complex64SubEq of SubEq<complex64> {
fn sub_eq(ref self: complex64, other: complex64) { self = Sub::sub(self, other); } } impl Complex64Mul of Mul<complex64> { fn mul(lhs: complex64, rhs: complex64) -> complex64 { complex64_mul(lhs, rhs) } } impl Complex64MulEq of MulEq<complex64> {
fn mul_eq(ref self: complex64, other: complex64) { self = Mul::mul(self, other); } } impl Complex64Div of Div<complex64> { fn div(lhs: complex64, rhs: complex64) -> complex64 { complex64_div(lhs, rhs) } } impl Complex64DivEq of DivEq<complex64> {
fn div_eq(ref self: complex64, other: complex64) { self = Div::div(self, other); } } impl Complex64PartialEq of PartialEq<complex64> { fn eq(lhs: @complex64, rhs: @complex64) -> bool { complex64_eq(*lhs, *rhs) } fn ne(lhs: @complex64, rhs: @complex64) -> bool { complex64_ne(*lhs, *rhs) } } impl Complex64Neg of Neg<complex64> { fn neg(a: complex64) -> complex64 { complex64_neg(a) } } fn complex64_add(a: complex64, b: complex64) -> complex64 { let real = a.real + b.real; let img = a.img + b.img; ComplexTrait::new(real, img) } fn complex64_sub(a: complex64, b: complex64) -> complex64 { let real = a.real - b.real; let img = a.img - b.img; ComplexTrait::new(real, img) } fn complex64_mul(a: complex64, b: complex64) -> complex64 { let real = a.real * b.real - a.img * b.img; let img = a.real * b.img + a.img * b.real; ComplexTrait::new(real, img) } fn complex64_div(a: complex64, b: complex64) -> complex64 { complex64_mul(a, b.reciprocal()) } fn complex64_eq(a: complex64, b: complex64) -> bool { if a.real == b.real && a.img == b.img { return true; } false } fn complex64_ne(a: complex64, b: complex64) -> bool { !complex64_eq(a, b) } fn complex64_neg(x: complex64) -> complex64 { ComplexTrait::new(-x.real, -x.img) }
trait ComplexTrait<T, F> { fn new(real: F, img: F) -> T; fn real(self: T) -> F; fn img(self: T) -> F; fn conjugate(self: T) -> T; fn zero() -> T; fn one() -> T; fn mag(self: T) -> F; fn arg(self: T) -> F; fn exp(self: T) -> T; fn exp2(self: T) -> T; fn ln(self: T) -> T;
fn log2(self: T) -> T; fn log10(self: T) -> T; fn pow(self: T, b: T) -> T; fn sqrt(self: T) -> T; fn acos(self: T) -> T; fn asin(self: T) -> T; fn atan(self: T) -> T; fn cos(self: T) -> T; fn sin(self: T) -> T; fn tan(self: T) -> T; fn
acosh(self: T) -> T; fn asinh(self: T) -> T; fn atanh(self: T) -> T; fn cosh(self: T) -> T; fn sinh(self: T) -> T; fn tanh(self: T) -> T; fn to_polar(self: T) -> (F, F); fn from_polar(mag: F, arg: F) -> T; fn reciprocal(self: T) -> T; }
//! Fixed-Point implemented from https://github.com/influenceth/cubit and adjusted to Q8.23 mod core; mod implementations; mod utils;
trait FixedTrait<T, MAG> { fn new(mag: MAG, sign: bool) -> T; fn new_unscaled(mag: MAG, sign: bool) -> T; fn from_felt(val: felt252) -> T; fn abs(self: T) -> T; fn ceil(self: T) -> T; fn exp(self: T) -> T; fn exp2(self: T) -> T; fn floor(self: T) -> T; fn ln(self: T) -> T; fn log2(self: T) -> T; fn
log10(self: T) -> T; fn pow(self: T, b: T) -> T; fn round(self: T) -> T; fn sqrt(self: T) -> T; fn acos(self: T) -> T; fn acos_fast(self: T) -> T; fn asin(self: T) -> T; fn asin_fast(self: T) -> T; fn atan(self: T) -> T; fn atan_fast(self: T) -> T; fn cos(self: T) -> T;
fn cos_fast(self: T) -> T; fn sin(self: T) -> T; fn sin_fast(self: T) -> T; fn tan(self: T) -> T; fn tan_fast(self: T) -> T; fn acosh(self: T) -> T; fn asinh(self: T) -> T; fn atanh(self: T) -> T; fn cosh(self: T) -> T; fn sinh(self: T) -> T; fn tanh(self: T) -> T; fn sign(self: T) -> T;
fn erf(self: T) -> T; fn ZERO() -> T; fn HALF() -> T; fn ONE() -> T; fn MAX() -> T; fn NaN() -> T; fn is_nan(self: T) -> bool; fn INF() -> T; fn POS_INF() -> T; fn NEG_INF() -> T; fn is_inf(self: T) -> bool; fn is_pos_inf(self: T) -> bool; fn is_neg_inf(self: T) -> bool; }
mod fp8x23; mod fp16x16; mod fp64x64; mod fp32x32; mod fp16x16wide; mod fp8x23wide;
mod core; mod math; mod helpers;
use core::debug::PrintTrait; use orion::numbers::fixed_point::core::FixedTrait; use orion::numbers::fixed_point::implementations::fp16x16::math::{ core as core_math, trig, hyp, erf }; use orion::numbers::fixed_point::utils;
struct FP16x16 { mag: u32, sign: bool } const TWO: u32 = 131072; const ONE: u32 = 65536; const HALF: u32 = 32768; const MAX: u32 = 2147483648; impl FP16x16Impl of FixedTrait<FP16x16, u32> { fn ZERO() -> FP16x16 { FP16x16 { mag: 0, sign: false } } fn HALF() -> FP16x16 { FP16x16 { mag: HALF, sign: false } } fn ONE() -> FP16x16 { FP16x16 { mag: ONE, sign: false } } fn MAX() -> FP16x16 { FP16x16 { mag: MAX, sign: false } } fn new(mag: u32, sign: bool) -> FP16x16 { FP16x16 { mag: mag, sign: sign } } fn new_unscaled(mag: u32, sign: bool) -> FP16x16 { FP16x16 { mag: mag * ONE, sign: sign } } fn from_felt(val: felt252) -> FP16x16 { let mag = core::integer::u32_try_from_felt252(utils::felt_abs(val)).unwrap(); FixedTrait::new(mag, utils::felt_sign(val)) } fn abs(self: FP16x16) -> FP16x16 { core_math::abs(self) } fn acos(self: FP16x16) -> FP16x16 { trig::acos_fast(self) } fn acos_fast(self: FP16x16) -> FP16x16 { trig::acos_fast(self) } fn acosh(self: FP16x16) -> FP16x16 { hyp::acosh(self) } fn asin(self: FP16x16) -> FP16x16 { trig::asin_fast(self) } fn asin_fast(self: FP16x16) -> FP16x16 { trig::asin_fast(self) } fn asinh(self: FP16x16) -> FP16x16 { hyp::asinh(self) } fn atan(self: FP16x16) -> FP16x16 { trig::atan_fast(self) } fn atan_fast(self: FP16x16) -> FP16x16 { trig::atan_fast(self) } fn atanh(self: FP16x16) -> FP16x16 { hyp::atanh(self) } fn ceil(self: FP16x16) -> FP16x16 { core_math::ceil(self) } fn cos(self: FP16x16) -> FP16x16 { trig::cos_fast(self) } fn cos_fast(self: FP16x16) -> FP16x16 { trig::cos_fast(self) } fn cosh(self: FP16x16) -> FP16x16 { hyp::cosh(self) } fn floor(self: FP16x16) -> FP16x16 { core_math::floor(self)
} fn exp(self: FP16x16) -> FP16x16 { core_math::exp(self) } fn exp2(self: FP16x16) -> FP16x16 { core_math::exp2(self) } fn ln(self: FP16x16) -> FP16x16 { core_math::ln(self) } fn log2(self: FP16x16) -> FP16x16 { core_math::log2(self) } fn log10(self: FP16x16) -> FP16x16 { core_math::log10(self) } fn pow(self: FP16x16, b: FP16x16) -> FP16x16 { core_math::pow(self, b) } fn round(self: FP16x16) -> FP16x16 { core_math::round(self) } fn sin(self: FP16x16) -> FP16x16 { trig::sin_fast(self) } fn sin_fast(self: FP16x16) -> FP16x16 { trig::sin_fast(self) } fn sinh(self: FP16x16) -> FP16x16 { hyp::sinh(self) } fn sqrt(self: FP16x16) -> FP16x16 { core_math::sqrt(self) } fn tan(self: FP16x16) -> FP16x16 { trig::tan_fast(self) } fn tan_fast(self: FP16x16) -> FP16x16 { trig::tan_fast(self) } fn tanh(self: FP16x16) -> FP16x16 { hyp::tanh(self) } fn sign(self: FP16x16) -> FP16x16 { core_math::sign(self) } fn NaN() -> FP16x16 { FP16x16 { mag: 0, sign: true } } fn is_nan(self: FP16x16) -> bool { self == FP16x16 { mag: 0, sign: true } } fn INF() -> FP16x16 { FP16x16 { mag: 4294967295, sign: false } } fn POS_INF() -> FP16x16 { FP16x16 { mag: 4294967295, sign: false } } fn NEG_INF() -> FP16x16 { FP16x16 { mag: 4294967295, sign: true } } fn is_inf(self: FP16x16) -> bool { self.mag == 4294967295 } fn is_pos_inf(self: FP16x16) -> bool { self.is_inf() && !self.sign } fn is_neg_inf(self: FP16x16) -> bool { self.is_inf() && self.sign } fn erf(self: FP16x16) -> FP16x16 { erf::erf(self) } } impl FP16x16Print of PrintTrait<FP16x16> {
fn print(self: FP16x16) { self.sign.print(); self.mag.print(); } } impl FP16x16IntoFelt252 of Into<FP16x16, felt252> { fn into(self: FP16x16) -> felt252 { let mag_felt = self.mag.into(); if self.sign { mag_felt * -1 } else { mag_felt * 1 } } } impl FP16x16IntoI32 of Into<FP16x16, i32> { fn into(self: FP16x16) -> i32 { _i32_into_fp(self) } } impl FP16x16TryIntoI8 of TryInto<FP16x16, i8> { fn try_into(self: FP16x16) -> Option<i8> { _i8_try_from_fp(self) } } impl FP16x16TryIntoU128 of TryInto<FP16x16, u128> { fn try_into(self: FP16x16) -> Option<u128> { if self.sign { Option::None(()) } else { Option::Some((self.mag / ONE).into()) } } } impl FP16x16TryIntoU64 of TryInto<FP16x16, u64> { fn try_into(self: FP16x16) -> Option<u64> { if self.sign { Option::None(()) } else { Option::Some((self.mag / ONE).into()) } } } impl FP16x16TryIntoU32 of TryInto<FP16x16, u32> { fn try_into(self: FP16x16) -> Option<u32> { if self.sign { Option::None(()) } else { Option::Some(self.mag / ONE) } } } impl FP16x16TryIntoU16 of TryInto<FP16x16, u16> { fn try_into(self: FP16x16) -> Option<u16> { if self.sign { Option::None(()) } else { (self.mag / ONE).try_into() } } } impl FP16x16TryIntoU8 of TryInto<FP16x16, u8> { fn try_into(self: FP16x16) -> Option<u8> { if self.sign { Option::None(()) } else { (self.mag / ONE).try_into() } } } impl FP16x16PartialEq of PartialEq<FP16x16> { fn eq(lhs: @FP16x16, rhs: @FP16x16) -> bool { core_math::eq(lhs, rhs) } fn ne(lhs: @FP16x16, rhs: @FP16x16) -> bool { core_math::ne(lhs, rhs) } } impl FP16x16A
dd of Add<FP16x16> { fn add(lhs: FP16x16, rhs: FP16x16) -> FP16x16 { core_math::add(lhs, rhs) } } impl FP16x16AddEq of AddEq<FP16x16> {
fn add_eq(ref self: FP16x16, other: FP16x16) { self = Add::add(self, other); } } impl FP16x16Sub of Sub<FP16x16> { fn sub(lhs: FP16x16, rhs: FP16x16) -> FP16x16 { core_math::sub(lhs, rhs) } } impl FP16x16SubEq of SubEq<FP16x16> {
fn sub_eq(ref self: FP16x16, other: FP16x16) { self = Sub::sub(self, other); } } impl FP16x16Mul of Mul<FP16x16> { fn mul(lhs: FP16x16, rhs: FP16x16) -> FP16x16 { core_math::mul(lhs, rhs) } } impl FP16x16MulEq of MulEq<FP16x16> {
fn mul_eq(ref self: FP16x16, other: FP16x16) { self = Mul::mul(self, other); } } impl FP16x16Div of Div<FP16x16> { fn div(lhs: FP16x16, rhs: FP16x16) -> FP16x16 { core_math::div(lhs, rhs) } } impl FP16x16DivEq of DivEq<FP16x16> {
fn div_eq(ref self: FP16x16, other: FP16x16) { self = Div::div(self, other); } } impl FP16x16PartialOrd of PartialOrd<FP16x16> { fn ge(lhs: FP16x16, rhs: FP16x16) -> bool { core_math::ge(lhs, rhs) } fn gt(lhs: FP16x16, rhs: FP16x16) -> bool { core_math::gt(lhs, rhs) } fn le(lhs: FP16x16, rhs: FP16x16) -> bool { core_math::le(lhs, rhs) } fn lt(lhs: FP16x16, rhs: FP16x16) -> bool { core_math::lt(lhs, rhs) } } impl FP16x16Neg of Neg<FP16x16> { fn neg(a: FP16x16) -> FP16x16 { core_math::neg(a) } } impl FP16x16Rem of Rem<FP16x16> { fn rem(lhs: FP16x16, rhs: FP16x16) -> FP16x16 { core_math::rem(lhs, rhs) } } fn _i32_into_fp(x: FP16x16) -> i32 { let number_felt: felt252 = (x.mag / ONE).into(); let number_i32: i32 = number_felt.try_into().unwrap(); if x.sign { return number_i32 * -1_i32; } number_i32 } fn _i8_try_from_fp(x: FP16x16) -> Option<i8> { let unscaled_mag: Option<u8> = (x.mag / ONE).try_into(); match unscaled_mag { Option::Some => { let number_felt: felt252 = unscaled_mag.unwrap().into(); let mut number_i8: i8 = number_felt.try_into().unwrap(); if x.sign { return Option::Some(number_i8 * -1_i8); } Option::Some(number_i8) }, Option::None => Option::None(()) } }
use core::debug::PrintTrait; use orion::numbers::fixed_point::implementations::fp16x16::core::{ HALF, ONE, TWO, FP16x16, FP16x16Impl, FP16x16Sub, FP16x16Div, FixedTrait, FP16x16Print }; const DEFAULT_PRECISION: u32 = 7; // 1e-4 // To use `DEFAULT_PRECISION`, final arg is: `Option::None(())`. // To use `custom_precision` of 430_u32: `Option::Some(430_u32)`. fn assert_precise(result: FP16x16, expected: felt252, msg: felt252, custom_precision: Option<u32>) { let precision = match custom_precision { Option::Some(val) => val, Option::None => DEFAULT_PRECISION, }; let diff = (result - FixedTrait::from_felt(expected)).mag; if (diff > precision) { result.print(); assert(diff <= precision, msg); } } fn assert_relative( result: FP16x16, expected: felt252, msg: felt252, custom_precision: Option<u32> ) { let precision = match custom_precision { Option::Some(val) => val, Option::None => DEFAULT_PRECISION, }; let diff = result - FixedTrait::from_felt(expected); let rel_diff = (diff / result).mag; if (rel_diff > precision) { result.print(); assert(rel_diff <= precision, msg); } }
mod core; mod comp; mod lut; mod trig; mod hyp; mod erf;
use orion::numbers::fixed_point::implementations::fp16x16::core::{ FP16x16, FixedTrait, FP16x16Impl, FP16x16PartialOrd, FP16x16PartialEq }; fn max(a: FP16x16, b: FP16x16) -> FP16x16 { if a >= b { a } else { b } } fn min(a: FP16x16, b: FP16x16) -> FP16x16 { if a <= b { a } else { b } } fn xor(a: FP16x16, b: FP16x16) -> bool { if (a == FixedTrait::new(0, false) || b == FixedTrait::new(0, false)) && (a != b) { true } else { false } } fn or(a: FP16x16, b: FP16x16) -> bool { let zero = FixedTrait::new(0, false); if a == zero && b == zero { false } else { true } } fn and(a: FP16x16, b: FP16x16) -> bool { let zero = FixedTrait::new(0, false); if a == zero || b == zero { false } else { true } } fn where(a: FP16x16, b: FP16x16, c: FP16x16) -> FP16x16 { if a == FixedTrait::new(0, false) { c } else { b } } fn bitwise_and(a: FP16x16, b: FP16x16) -> FP16x16 { FixedTrait::new(a.mag & b.mag, a.sign & b.sign) } fn bitwise_xor(a: FP16x16, b: FP16x16) -> FP16x16 { FixedTrait::new(a.mag ^ b.mag, a.sign ^ b.sign) } fn bitwise_or(a: FP16x16, b: FP16x16) -> FP16x16 { FixedTrait::new(a.mag | b.mag, a.sign | b.sign) } mod tests { use super::{FixedTrait, max, min, bitwise_and, bitwise_xor, bitwise_or};
fn test_max() { let a = FixedTrait::new_unscaled(1, false); let b = FixedTrait::new_unscaled(0, false); let c = FixedTrait::new_unscaled(1, true); assert(max(a, a) == a, 'max(a, a)'); assert(max(a, b) == a, 'max(a, b)'); assert(max(a, c) == a, 'max(a, c)'); assert(max(b, a) == a, 'max(b, a)'); assert(max(b, b) == b, 'max(b, b)'); assert(max(b, c) == b, 'max(b, c)'); assert(max(c, a) == a, 'max(c, a)'); assert(max(c, b) == b, 'max(c, b)'); assert(max(c, c) == c, 'max(c, c)'); }
fn test_min() { let a = FixedTrait::new_unscaled(1, false); let b = FixedTrait::new_unscaled(0, false); let c = FixedTrait::new_unscaled(1, true); assert(min(a, a) == a, 'min(a, a)'); assert(min(a, b) == b, 'min(a, b)'); assert(min(a, c) == c, 'min(a, c)'); assert(min(b, a) == b, 'min(b, a)'); assert(min(b, b) == b, 'min(b, b)'); assert(min(b, c) == c, 'min(b, c)'); assert(min(c, a) == c, 'min(c, a)'); assert(min(c, b) == c, 'min(c, b)'); assert(min(c, c) == c, 'min(c, c)'); }
fn test_bitwise_and() { let a = FixedTrait::new(225280, false); let b = FixedTrait::new(4160843776, true); let c = FixedTrait::new(94208, false); assert(bitwise_and(a, b) == c, 'bitwise_and(a,b)') }
fn test_bitwise_xor() { let a = FixedTrait::new(225280, false); let b = FixedTrait::new(4160843776, true); let c = FixedTrait::new(4160880640, true); assert(bitwise_xor(a, b) == c, 'bitwise_xor(a,b)') }
fn test_bitwise_or() { let a = FixedTrait::new(225280, false); let b = FixedTrait::new(4160843776, true); let c = FixedTrait::new(4160974848, true); assert(bitwise_or(a, b) == c, 'bitwise_or(a,b)') } }
use core::integer; use orion::numbers::fixed_point::implementations::fp16x16::core::{ HALF, ONE, MAX, FP16x16, FP16x16Impl, FP16x16Add, FP16x16AddEq, FP16x16Sub, FP16x16Mul, FP16x16MulEq, FP16x16TryIntoU128, FP16x16PartialEq, FP16x16PartialOrd, FP16x16SubEq, FP16x16Neg, FP16x16Div, FP16x16IntoFelt252, FixedTrait }; use orion::numbers::fixed_point::implementations::fp16x16::math::lut; fn abs(a: FP16x16) -> FP16x16 { FixedTrait::new(a.mag, false) } fn add(a: FP16x16, b: FP16x16) -> FP16x16 { if a.sign == b.sign { return FixedTrait::new(a.mag + b.mag, a.sign); } if a.mag == b.mag { return FixedTrait::ZERO(); } if (a.mag > b.mag) { FixedTrait::new(a.mag - b.mag, a.sign) } else { FixedTrait::new(b.mag - a.mag, b.sign) } } fn ceil(a: FP16x16) -> FP16x16 { let (div, rem) = integer::u32_safe_divmod(a.mag, integer::u32_as_non_zero(ONE)); if rem == 0 { a } else if !a.sign { FixedTrait::new_unscaled(div + 1, false) } else if div == 0 { FixedTrait::new_unscaled(0, false) } else { FixedTrait::new_unscaled(div, true) } } fn div(a: FP16x16, b: FP16x16) -> FP16x16 { let a_u64 = integer::u32_wide_mul(a.mag, ONE); let res_u64 = a_u64 / b.mag.into(); FixedTrait::new(res_u64.try_into().unwrap(), a.sign ^ b.sign) } fn eq(a: @FP16x16, b: @FP16x16) -> bool { (*a.mag == *b.mag) && (*a.sign == *b.sign) } fn exp(a: FP16x16) -> FP16x16 { exp2(FixedTrait::new(94548, false) * a) } fn exp2(a: FP16x16) -> FP16x16 { if (a.mag == 0) { return FixedTrait::ONE(); } let (int_part, frac_part) = integer::u32_safe_divmod(a.mag, integer::u32_as_non_zero(ONE)); let int_res = FixedTrait::new_unscaled(lut::exp2(int_part), false); let mut res_u = int_res; if frac_part != 0 { let frac = FixedTrait::new(frac_part, false); let r7 = FixedTrait::new(1, false) * frac; let r6 = (r7 + FixedTrait::new(10, false)) * frac; let r5 = (
r6 + FixedTrait::new(87, false)) * frac; let r4 = (r5 + FixedTrait::new(630, false)) * frac; let r3 = (r4 + FixedTrait::new(3638, false)) * frac; let r2 = (r3 + FixedTrait::new(15743, false)) * frac; let r1 = (r2 + FixedTrait::new(45426, false)) * frac; res_u = res_u * (r1 + FixedTrait::ONE()); } if a.sign { FixedTrait::ONE() / res_u } else { res_u } } fn exp2_int(exp: u32) -> FP16x16 { FixedTrait::new_unscaled(lut::exp2(exp), false) } fn floor(a: FP16x16) -> FP16x16 { let (div, rem) = integer::u32_safe_divmod(a.mag, integer::u32_as_non_zero(ONE)); if rem == 0 { a } else if !a.sign { FixedTrait::new_unscaled(div, false) } else { FixedTrait::new_unscaled(div + 1, true) } } fn ge(a: FP16x16, b: FP16x16) -> bool { if a.sign != b.sign { !a.sign } else { (a.mag == b.mag) || ((a.mag > b.mag) ^ a.sign) } } fn gt(a: FP16x16, b: FP16x16) -> bool { if a.sign != b.sign { !a.sign } else { (a.mag != b.mag) && ((a.mag > b.mag) ^ a.sign) } } fn le(a: FP16x16, b: FP16x16) -> bool { if a.sign != b.sign { a.sign } else { (a.mag == b.mag) || ((a.mag < b.mag) ^ a.sign) } } fn ln(a: FP16x16) -> FP16x16 { FixedTrait::new(45426, false) * log2(a) } fn log2(a: FP16x16) -> FP16x16 { assert(a.sign == false, 'must be positive'); if (a.mag == ONE) { return FixedTrait::ZERO(); } else if (a.mag < ONE) { let div = FixedTrait::ONE() / a; return -log2(div); } let whole = a.mag / ONE; let (msb, div) = lut::msb(whole); if a.mag == div * ONE { FixedTrait::new_unscaled(msb, false) } else { let norm = a / FixedTrait::new_unscaled(div, false); let r8 = FixedTrait::new(596, true) * norm; let r7 = (r8 + FixedTrait::new(8116, false)) * norm; let r6 = (r7 + FixedTrait::new(49044, true)) * norm; let r5 = (r6 + FixedTrait::new
(172935, false)) * norm; let r4 = (r5 + FixedTrait::new(394096, true)) * norm; let r3 = (r4 + FixedTrait::new(608566, false)) * norm; let r2 = (r3 + FixedTrait::new(655828, true)) * norm; let r1 = (r2 + FixedTrait::new(534433, false)) * norm; r1 + FixedTrait::new(224487, true) + FixedTrait::new_unscaled(msb, false) } } fn log10(a: FP16x16) -> FP16x16 { FixedTrait::new(19728, false) * log2(a) } fn lt(a: FP16x16, b: FP16x16) -> bool { if a.sign != b.sign { a.sign } else { (a.mag != b.mag) && ((a.mag < b.mag) ^ a.sign) } } fn mul(a: FP16x16, b: FP16x16) -> FP16x16 { let prod_u128 = integer::u32_wide_mul(a.mag, b.mag); FixedTrait::new((prod_u128 / ONE.into()).try_into().unwrap(), a.sign ^ b.sign) } fn ne(a: @FP16x16, b: @FP16x16) -> bool { (*a.mag != *b.mag) || (*a.sign != *b.sign) } fn neg(a: FP16x16) -> FP16x16 { if a.mag == 0 { a } else if !a.sign { FixedTrait::new(a.mag, !a.sign) } else { FixedTrait::new(a.mag, false) } } fn pow(a: FP16x16, b: FP16x16) -> FP16x16 { let (_, rem) = integer::u32_safe_divmod(b.mag, integer::u32_as_non_zero(ONE)); if (rem == 0) { return pow_int(a, b.mag / ONE, b.sign); } exp(b * ln(a)) } fn pow_int(a: FP16x16, b: u32, sign: bool) -> FP16x16 { let mut x = a; let mut n = b; if sign { x = FixedTrait::ONE() / x; } if n == 0 { return FixedTrait::ONE(); } let mut y = FixedTrait::ONE(); let two = integer::u32_as_non_zero(2); while n > 1 { let (div, rem) = integer::u32_safe_divmod(n, two); if rem == 1 { y = x * y; } x = x * x; n = div; }; x * y } fn rem(a: FP16x16, b: FP16x16) -> FP16x16 { a - floor(a / b) * b } fn round(a: FP16x16) -> FP16x16 { let (div, rem) = integer::u32_safe_divmod(a.mag, integer::u32_as_non_zero(ONE)); if (HALF <= rem) { FixedTrait::new_unscaled(div
+ 1, a.sign) } else { FixedTrait::new_unscaled(div, a.sign) } } fn sqrt(a: FP16x16) -> FP16x16 { assert(a.sign == false, 'must be positive'); let root = integer::u64_sqrt(a.mag.into() * ONE.into()); FixedTrait::new(root.into(), false) } fn sub(a: FP16x16, b: FP16x16) -> FP16x16 { add(a, -b) } fn sign(a: FP16x16) -> FP16x16 { if a.mag == 0 { FixedTrait::new(0, false) } else { FixedTrait::new(ONE, a.sign) } } mod tests { use orion::numbers::fixed_point::implementations::fp16x16::helpers::{ assert_precise, assert_relative }; use orion::numbers::fixed_point::implementations::fp16x16::math::trig::{PI, HALF_PI}; use super::{ FixedTrait, ONE, FP16x16, ceil, floor, sqrt, round, lut, pow, exp, exp2, exp2_int, ln, log2, log10, eq, add, ne, HALF };
fn test_into() { let a = FixedTrait::<FP16x16>::new_unscaled(5, false); assert(a.mag == 5 * ONE, 'invalid result'); }
fn test_try_into_u128() { let a = FixedTrait::<FP16x16>::new_unscaled(5, false); assert(a.try_into().unwrap() == 5_u128, 'invalid result'); let b = FixedTrait::<FP16x16>::new(5 * ONE, false); assert(b.try_into().unwrap() == 5_u128, 'invalid result'); let d = FixedTrait::<FP16x16>::new_unscaled(0, false); assert(d.try_into().unwrap() == 0_u128, 'invalid result'); }
fn test_negative_try_into_u128() { let a = FixedTrait::<FP16x16>::new_unscaled(1, true); let _a: u128 = a.try_into().unwrap(); }
fn test_acos() { let a = FixedTrait::<FP16x16>::ONE(); assert(a.acos().into() == 0, 'invalid one'); }
fn test_asin() { let a = FixedTrait::ONE(); assert_precise(a.asin(), HALF_PI.into(), 'invalid one', Option::None(())); }
fn test_atan() { let a = FixedTrait::new(2 * ONE, false); assert_relative(a.atan(), 72558, 'invalid two', Option::None(())); }
fn test_ceil() { let a = FixedTrait::new(190054, false); assert(ceil(a).mag == 3 * ONE, 'invalid pos decimal'); }
fn test_floor() { let a = FixedTrait::new(190054, false); assert(floor(a).mag == 2 * ONE, 'invalid pos decimal'); }
fn test_round() { let a = FixedTrait::new(190054, false); assert(round(a).mag == 3 * ONE, 'invalid pos decimal'); }
fn test_sqrt_fail() { let a = FixedTrait::new_unscaled(25, true); sqrt(a); }
fn test_sqrt() { let mut a = FixedTrait::new_unscaled(0, false); assert(sqrt(a).mag == 0, 'invalid zero root'); a = FixedTrait::new_unscaled(25, false); assert(sqrt(a).mag == 5 * ONE, 'invalid pos root'); }
fn test_msb() { let a = FixedTrait::<FP16x16>::new_unscaled(100, false); let (msb, div) = lut::msb(a.mag / ONE); assert(msb == 6, 'invalid msb'); assert(div == 64, 'invalid msb ceil'); }
fn test_pow() { let a = FixedTrait::new_unscaled(3, false); let b = FixedTrait::new_unscaled(4, false); assert(pow(a, b).mag == 81 * ONE, 'invalid pos base power'); }
fn test_pow_frac() { let a = FixedTrait::new_unscaled(3, false); let b = FixedTrait::new(32768, false); assert_relative( pow(a, b), 113512, 'invalid pos base power', Option::None(()) ); }
fn test_exp() { let a = FixedTrait::new_unscaled(2, false); assert_relative(exp(a), 484249, 'invalid exp of 2', Option::None(())); }
fn test_exp2() { let a = FixedTrait::new_unscaled(5, false); assert(exp2(a).mag == 2097152, 'invalid exp2 of 2'); }
fn test_exp2_int() { assert(exp2_int(5).into() == 2097152, 'invalid exp2 of 2'); }
fn test_ln() { let mut a = FixedTrait::new_unscaled(1, false); assert(ln(a).mag == 0, 'invalid ln of 1'); a = FixedTrait::new(178145, false); assert_relative(ln(a), ONE.into(), 'invalid ln of 2.7...', Option::None(())); }
fn test_log2() { let mut a = FixedTrait::new_unscaled(32, false); assert(log2(a) == FixedTrait::new_unscaled(5, false), 'invalid log2 32'); a = FixedTrait::new_unscaled(10, false); assert_relative(log2(a), 217706, 'invalid log2 10', Option::None(())); }
fn test_log10() { let a = FixedTrait::new_unscaled(100, false); assert_relative(log10(a), 2 * ONE.into(), 'invalid log10', Option::None(())); }
fn test_eq() { let a = FixedTrait::new_unscaled(42, false); let b = FixedTrait::new_unscaled(42, false); let c = eq(@a, @b); assert(c, 'invalid result'); }
fn test_ne() { let a = FixedTrait::new_unscaled(42, false); let b = FixedTrait::new_unscaled(42, false); let c = ne(@a, @b); assert(!c, 'invalid result'); }
fn test_add() { let a = FixedTrait::new_unscaled(1, false); let b = FixedTrait::new_unscaled(2, false); assert(add(a, b) == FixedTrait::new_unscaled(3, false), 'invalid result'); }
fn test_add_eq() { let mut a = FixedTrait::new_unscaled(1, false); let b = FixedTrait::new_unscaled(2, false); a += b; assert(a == FixedTrait::<FP16x16>::new_unscaled(3, false), 'invalid result'); }
fn test_sub() { let a = FixedTrait::new_unscaled(5, false); let b = FixedTrait::new_unscaled(2, false); let c = a - b; assert(c == FixedTrait::<FP16x16>::new_unscaled(3, false), 'false result invalid'); }
fn test_sub_eq() { let mut a = FixedTrait::new_unscaled(5, false); let b = FixedTrait::new_unscaled(2, false); a -= b; assert(a == FixedTrait::<FP16x16>::new_unscaled(3, false), 'invalid result'); }
fn test_mul_pos() { let a = FP16x16 { mag: 190054, sign: false }; let b = FP16x16 { mag: 190054, sign: false }; let c = a * b; assert(c.mag == 551155, 'invalid result'); }
fn test_mul_neg() { let a = FixedTrait::new_unscaled(5, false); let b = FixedTrait::new_unscaled(2, true); let c = a * b; assert(c == FixedTrait::<FP16x16>::new_unscaled(10, true), 'invalid result'); }
fn test_mul_eq() { let mut a = FixedTrait::new_unscaled(5, false); let b = FixedTrait::new_unscaled(2, true); a *= b; assert(a == FixedTrait::<FP16x16>::new_unscaled(10, true), 'invalid result'); }
fn test_div() { let a = FixedTrait::new_unscaled(10, false); let b = FixedTrait::<FP16x16>::new(190054, false); let c = a / b; assert(c.mag == 225986, 'invalid pos decimal'); }
fn test_le() { let a = FixedTrait::new_unscaled(1, false); let b = FixedTrait::new_unscaled(0, false); let c = FixedTrait::<FP16x16>::new_unscaled(1, true); assert(a <= a, 'a <= a'); assert(!(a <= b), 'a <= b'); assert(!(a <= c), 'a <= c'); assert(b <= a, 'b <= a'); assert(b <= b, 'b <= b'); assert(!(b <= c), 'b <= c'); assert(c <= a, 'c <= a'); assert(c <= b, 'c <= b'); assert(c <= c, 'c <= c'); }
fn test_lt() { let a = FixedTrait::new_unscaled(1, false); let b = FixedTrait::new_unscaled(0, false); let c = FixedTrait::<FP16x16>::new_unscaled(1, true); assert(!(a < a), 'a < a'); assert(!(a < b), 'a < b'); assert(!(a < c), 'a < c'); assert(b < a, 'b < a'); assert(!(b < b), 'b < b'); assert(!(b < c), 'b < c'); assert(c < a, 'c < a'); assert(c < b, 'c < b'); assert(!(c < c), 'c < c'); }
fn test_ge() { let a = FixedTrait::new_unscaled(1, false); let b = FixedTrait::new_unscaled(0, false); let c = FixedTrait::<FP16x16>::new_unscaled(1, true); assert(a >= a, 'a >= a'); assert(a >= b, 'a >= b'); assert(a >= c, 'a >= c'); assert(!(b >= a), 'b >= a'); assert(b >= b, 'b >= b'); assert(b >= c, 'b >= c'); assert(!(c >= a), 'c >= a'); assert(!(c >= b), 'c >= b'); assert(c >= c, 'c >= c'); }
fn test_gt() { let a = FixedTrait::new_unscaled(1, false); let b = FixedTrait::new_unscaled(0, false); let c = FixedTrait::<FP16x16>::new_unscaled(1, true); assert(!(a > a), 'a > a'); assert(a > b, 'a > b'); assert(a > c, 'a > c'); assert(!(b > a), 'b > a'); assert(!(b > b), 'b > b'); assert(b > c, 'b > c'); assert(!(c > a), 'c > a'); assert(!(c > b), 'c > b'); assert(!(c > c), 'c > c'); }
fn test_cos() { let a = FixedTrait::<FP16x16>::new(HALF_PI, false); assert(a.cos().into() == 0, 'invalid half pi'); }
fn test_sin() { let a = FixedTrait::new(HALF_PI, false); assert_precise(a.sin(), ONE.into(), 'invalid half pi', Option::None(())); }
fn test_tan() { let a = FixedTrait::<FP16x16>::new(HALF_PI / 2, false); assert(a.tan().mag == 65536, 'invalid quarter pi'); }
fn test_sign() { let a = FixedTrait::<FP16x16>::new(0, false); assert(a.sign().mag == 0 && !a.sign().sign, 'invalid sign (0, true)'); let a = FixedTrait::<FP16x16>::new(HALF, true); assert(a.sign().mag == ONE && a.sign().sign, 'invalid sign (HALF, true)'); let a = FixedTrait::<FP16x16>::new(HALF, false); assert(a.sign().mag == ONE && !a.sign().sign, 'invalid sign (HALF, false)'); let a = FixedTrait::<FP16x16>::new(ONE, true); assert(a.sign().mag == ONE && a.sign().sign, 'invalid sign (ONE, true)'); let a = FixedTrait::<FP16x16>::new(ONE, false); assert(a.sign().mag == ONE && !a.sign().sign, 'invalid sign (ONE, false)'); }
fn test_sign_fail() { let a = FixedTrait::<FP16x16>::new(HALF, true); assert(a.sign().mag != ONE && !a.sign().sign, 'invalid sign (HALF, true)'); } }
use orion::numbers::fixed_point::implementations::fp16x16::core::{ONE, FP16x16, FixedTrait}; use orion::numbers::fixed_point::implementations::fp16x16::math::lut::erf_lut; const ERF_COMPUTATIONAL_ACCURACY: u32 = 100; const ROUND_CHECK_NUMBER: u32 = 10; // Values > MAX_ERF_NUMBER return 1 const MAX_ERF_NUMBER: u32 = 229376; // Values <= ERF_TRUNCATION_NUMBER -> two decimal places, and values > ERF_TRUNCATION_NUMBER -> one decimal place const ERF_TRUNCATION_NUMBER: u32 = 131072; fn erf(x: FP16x16) -> FP16x16 { // Lookup // 1. if x.mag < 3.5 { lookup table } // 2. else{ return 1} let mut erf_value: u32 = 0; if x.mag < MAX_ERF_NUMBER { erf_value = erf_lut(x.mag); } else { erf_value = ONE; } FP16x16 { mag: erf_value, sign: x.sign } }
use orion::numbers::fixed_point::implementations::fp16x16::core::{ HALF, ONE, TWO, FP16x16, FP16x16Impl, FP16x16Add, FP16x16AddEq, FP16x16Sub, FP16x16Mul, FP16x16MulEq, FP16x16TryIntoU128, FP16x16PartialEq, FP16x16PartialOrd, FP16x16SubEq, FP16x16Neg, FP16x16Div, FP16x16IntoFelt252, FixedTrait }; fn cosh(a: FP16x16) -> FP16x16 { let ea = a.exp(); (ea + (FixedTrait::ONE() / ea)) / FixedTrait::new(TWO, false) } fn sinh(a: FP16x16) -> FP16x16 { let ea = a.exp(); (ea - (FixedTrait::ONE() / ea)) / FixedTrait::new(TWO, false) } fn tanh(a: FP16x16) -> FP16x16 { let ea = a.exp(); let ea_i = FixedTrait::ONE() / ea; (ea - ea_i) / (ea + ea_i) } fn acosh(a: FP16x16) -> FP16x16 { let root = (a * a - FixedTrait::ONE()).sqrt(); (a + root).ln() } fn asinh(a: FP16x16) -> FP16x16 { let root = (a * a + FixedTrait::ONE()).sqrt(); (a + root).ln() } fn atanh(a: FP16x16) -> FP16x16 { let one = FixedTrait::ONE(); let ln_arg = (one + a) / (one - a); ln_arg.ln() / FixedTrait::new(TWO, false) } mod tests { use orion::numbers::fixed_point::implementations::fp16x16::helpers::assert_precise; use super::{FixedTrait, TWO, cosh, ONE, sinh, tanh, acosh, asinh, atanh, HALF};
fn test_cosh() { let a = FixedTrait::new(TWO, false); assert_precise(cosh(a), 246550, 'invalid two', Option::None(())); let a = FixedTrait::ONE(); assert_precise(cosh(a), 101127, 'invalid one', Option::None(())); let a = FixedTrait::ZERO(); assert_precise(cosh(a), ONE.into(), 'invalid zero', Option::None(())); let a = FixedTrait::ONE(); assert_precise(cosh(a), 101127, 'invalid neg one', Option::None(())); let a = FixedTrait::new(TWO, true); assert_precise(cosh(a), 246568, 'invalid neg two', Option::None(())); }
fn test_sinh() { let a = FixedTrait::new(TWO, false); assert_precise(sinh(a), 237681, 'invalid two', Option::None(())); let a = FixedTrait::ONE(); assert_precise(sinh(a), 77018, 'invalid one', Option::None(())); let a = FixedTrait::ZERO(); assert(sinh(a).into() == 0, 'invalid zero'); let a = FixedTrait::new(ONE, true); assert_precise(sinh(a), -77018, 'invalid neg one', Option::None(())); let a = FixedTrait::new(TWO, true); assert_precise(sinh(a), -237699, 'invalid neg two', Option::None(())); }
fn test_tanh() { let a = FixedTrait::new(TWO, false); assert_precise(tanh(a), 63179, 'invalid two', Option::None(())); let a = FixedTrait::ONE(); assert_precise(tanh(a), 49912, 'invalid one', Option::None(())); let a = FixedTrait::ZERO(); assert(tanh(a).into() == 0, 'invalid zero'); let a = FixedTrait::new(ONE, true); assert_precise(tanh(a), -49912, 'invalid neg one', Option::None(())); let a = FixedTrait::new(TWO, true); assert_precise(tanh(a), -63179, 'invalid neg two', Option::None(())); }
fn test_acosh() { let a = FixedTrait::new(246559, false); assert_precise(acosh(a), 131072, 'invalid two', Option::None(())); let a = FixedTrait::new(101127, false); assert_precise(acosh(a), ONE.into(), 'invalid one', Option::None(())); let a = FixedTrait::ONE(); assert(acosh(a).into() == 0, 'invalid zero'); }
fn test_asinh() { let a = FixedTrait::new(237690, false); assert_precise(asinh(a), 131072, 'invalid two', Option::None(())); let a = FixedTrait::new(77018, false); assert_precise(asinh(a), ONE.into(), 'invalid one', Option::None(())); let a = FixedTrait::ZERO(); assert(asinh(a).into() == 0, 'invalid zero'); let a = FixedTrait::new(77018, true); assert_precise(asinh(a), -ONE.into(), 'invalid neg one', Option::None(())); let a = FixedTrait::new(237690, true); assert_precise(asinh(a), -131017, 'invalid neg two', Option::None(())); }