Skip to content

Commit

Permalink
Merge pull request lcompilers#227 from namannimmo10/numpy
Browse files Browse the repository at this point in the history
Implement numpy functions
  • Loading branch information
namannimmo10 committed Mar 14, 2022
2 parents 2420169 + 6eda0f1 commit a70257b
Showing 1 changed file with 64 additions and 1 deletion.
65 changes: 64 additions & 1 deletion integration_tests/test_numpy_02.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# This test handles actual LPython implementations of functions from the numpy
# module.
from ltypes import i32, i64, f64, TypeVar
from ltypes import i32, i64, f64, TypeVar, overload
from numpy import empty, int64

e: f64 = 2.718281828459045

n: i32
n = TypeVar("n")

Expand Down Expand Up @@ -30,6 +32,34 @@ def arange(n: i32) -> i64[n]:
A[i] = i
return A

@overload
def sqrt(n: i32) -> f64:
return n**(1/2)

@overload
def sqrt(f: f64) -> f64:
return f**(1/2)

@overload
def exp(n: i32) -> f64:
return e**n

@overload
def exp(f: f64) -> f64:
return e**f

@overload
def fabs(f: f64) -> f64:
if f < 0.0:
return -f
return f

@overload
def fabs(n: i32) -> f64:
if n < 0:
return -1.0*n
return 1.0*n

num: i32
num = TypeVar("num")
def linspace(start: f64, stop: f64, num: i32) -> f64[num]:
Expand Down Expand Up @@ -68,6 +98,36 @@ def test_arange():
assert a[2] == 2
assert a[3] == 3

def test_sqrt():
a: f64
a2: f64
a = sqrt(2)
a2 = sqrt(5.6)
eps: f64
eps = 1e-12
assert abs(a - 1.4142135623730951) < eps
assert abs(a2 - 2.3664319132398464) < eps

def test_exp():
a: f64
a = exp(6)
a2: f64
a2 = exp(5.6)
eps: f64
eps = 1e-12
assert abs(a - 403.4287934927351) < eps
assert abs(a2 - 270.42640742615254) < eps

def test_fabs():
a: f64
a = fabs(-3.7)
a2: f64
a2 = fabs(-3)
eps: f64
eps = 1e-12
assert abs(a - 3.7) < eps
assert abs(a2 - 3.0) < eps

def test_linspace():
a: f64[4]
a = linspace(1., 7., 4)
Expand All @@ -82,6 +142,9 @@ def check():
test_zeros()
test_ones()
test_arange()
test_sqrt()
test_exp()
test_fabs()
test_linspace()

check()

0 comments on commit a70257b

Please sign in to comment.