From ab7df07a04c6f7b4af281746c9f91f4e6e0222db Mon Sep 17 00:00:00 2001 From: Whiteknight Date: Tue, 8 Dec 2009 15:15:54 -0500 Subject: [PATCH] update pi() and e() to use syntax closer to standard M. Add tests --- t/functions/e.t | 12 ++++++++++++ t/functions/pi.t | 12 ++++++++++++ toolbox/e.m | 14 ++++++++++++-- toolbox/pi.m | 16 +++++++++++++--- 4 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 t/functions/e.t create mode 100644 t/functions/pi.t diff --git a/t/functions/e.t b/t/functions/e.t new file mode 100644 index 0000000..cd041d0 --- /dev/null +++ b/t/functions/e.t @@ -0,0 +1,12 @@ +plan(7); +_e = e; +is(parrot_typeof(_e), "Float", "e is a normal float"); +_e = e(2); +is(parrot_typeof(_e), "NumMatrix2D", "e(2) is a matrix"); +is(columns(_e), 2); +is(rows(_e), 2); + +_e = e(3, 4); +is(parrot_typeof(_e), "NumMatrix2D", "e(3, 4) is a matrix"); +is(columns(_e), 4); +is(rows(_e), 3); \ No newline at end of file diff --git a/t/functions/pi.t b/t/functions/pi.t new file mode 100644 index 0000000..d78da16 --- /dev/null +++ b/t/functions/pi.t @@ -0,0 +1,12 @@ +plan(7); +p = pi; +is(parrot_typeof(p), "Float", "pi is a normal float"); +p = pi(2); +is(parrot_typeof(p), "NumMatrix2D", "pi(2) is a matrix"); +is(columns(p), 2); +is(rows(p), 2); + +p = pi(3, 4); +is(parrot_typeof(p), "NumMatrix2D", "pi(3, 4) is a matrix"); +is(columns(p), 4); +is(rows(p), 3); \ No newline at end of file diff --git a/toolbox/e.m b/toolbox/e.m index 1794173..bf5da75 100644 --- a/toolbox/e.m +++ b/toolbox/e.m @@ -1,5 +1,15 @@ -function val = e() +function val = e(rows, cols) %% val = e() %% Returns the value of e, Euler's constant, to several decimal places - val = 2.7182882845904523536; + % TODO: Should take an arbitrary number of args and return an N-dim matrix + _e = 2.7182882845904523536; + if nargin == 0 + val = _e; + else + if nargin == 1 + cols = rows; + end + val = parrot_new("NumMatrix2D"); + parrot_method(val, "fill", _e, rows, cols); + end endfunction \ No newline at end of file diff --git a/toolbox/pi.m b/toolbox/pi.m index 15da1a0..c560b8a 100644 --- a/toolbox/pi.m +++ b/toolbox/pi.m @@ -1,5 +1,15 @@ -function p = pi() -%% p = pi() +function val = pi(rows, cols) +%% val = pi() %% returns the value of pi to about 13 decimal places - p = 3.1415926535898; + % TODO: Should take an arbitrary number of args and return an N-dim matrix + _p = 3.1415926535898; + if nargin == 0 + val = _p; + else + if nargin == 1 + cols = rows; + end + val = parrot_new("NumMatrix2D"); + parrot_method(val, "fill", _p, rows, cols); + end endfunction \ No newline at end of file