From 37dafe9fd316adeb80351aacef87a62cbb563681 Mon Sep 17 00:00:00 2001 From: Rajiv Ranjan Singh Date: Mon, 20 Jan 2020 13:31:22 +0530 Subject: [PATCH 1/5] added upper and lower triangular matrix --- lib/nmatrix/numruby.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/nmatrix/numruby.rb b/lib/nmatrix/numruby.rb index aeaa48c..39c5449 100644 --- a/lib/nmatrix/numruby.rb +++ b/lib/nmatrix/numruby.rb @@ -62,4 +62,28 @@ def self.hstack(objs) result end + def self.tri(N, M=None, k=0, nm_dtype=float) + if M is None: + M = N + + m = greater_equal.outer(arange(N, nm_dtype=nm_int(0, N)), + arange(-k, M-k, nm_dtype=nm_int(-k, M - k))) + + # Avoid making a copy if the requested type is already bool + m = m.stype(nm_dtype, copy=False) + return m + end + + def tril(m, k=0) + m = Array(m) + mask = tri(*m.shape[-2:], k=k, nm_dtype=nm_bool) + return where(mask, m, zeros(1, m.nm_dtype)) + end + + def triu(m, k=0) + m = Array(m) + mask = tri(*m.shape[-2:], k=k-1, nm_dtype=nm_bool) + return where(mask, zeros(1, m.nm_dtype), m) + end + end From 00405db9cee0cd842767b94c8fc8d8ffc48f7fc1 Mon Sep 17 00:00:00 2001 From: Rajiv Ranjan Singh Date: Wed, 15 Apr 2020 09:06:24 +0530 Subject: [PATCH 2/5] fixed code as per suggestions --- lib/nmatrix/numruby.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/nmatrix/numruby.rb b/lib/nmatrix/numruby.rb index 39c5449..f6b6791 100644 --- a/lib/nmatrix/numruby.rb +++ b/lib/nmatrix/numruby.rb @@ -73,17 +73,16 @@ def self.tri(N, M=None, k=0, nm_dtype=float) m = m.stype(nm_dtype, copy=False) return m end + end def tril(m, k=0) m = Array(m) mask = tri(*m.shape[-2:], k=k, nm_dtype=nm_bool) - return where(mask, m, zeros(1, m.nm_dtype)) end def triu(m, k=0) m = Array(m) mask = tri(*m.shape[-2:], k=k-1, nm_dtype=nm_bool) - return where(mask, zeros(1, m.nm_dtype), m) end end From cec980d0c89db0d1ff7d5f4e5dae3566a722ff75 Mon Sep 17 00:00:00 2001 From: Rajiv Ranjan Singh Date: Wed, 15 Apr 2020 16:40:38 +0530 Subject: [PATCH 3/5] removed extra indentation --- lib/nmatrix/numruby.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/nmatrix/numruby.rb b/lib/nmatrix/numruby.rb index f6b6791..f1f4e2e 100644 --- a/lib/nmatrix/numruby.rb +++ b/lib/nmatrix/numruby.rb @@ -64,16 +64,16 @@ def self.hstack(objs) def self.tri(N, M=None, k=0, nm_dtype=float) if M is None: - M = N + M = N m = greater_equal.outer(arange(N, nm_dtype=nm_int(0, N)), - arange(-k, M-k, nm_dtype=nm_int(-k, M - k))) + arange(-k, M-k, nm_dtype=nm_int(-k, M - k))) - # Avoid making a copy if the requested type is already bool - m = m.stype(nm_dtype, copy=False) - return m - end + # Avoid making a copy if the requested type is already bool + m = m.stype(nm_dtype, copy=False) + return m end + end def tril(m, k=0) m = Array(m) From a493439616386a7e7e55e3dbf5bf3a287196d240 Mon Sep 17 00:00:00 2001 From: Rajiv Ranjan Singh Date: Wed, 15 Apr 2020 22:31:26 +0530 Subject: [PATCH 4/5] improved syntax --- lib/nmatrix/numruby.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/nmatrix/numruby.rb b/lib/nmatrix/numruby.rb index f1f4e2e..a7e2ce3 100644 --- a/lib/nmatrix/numruby.rb +++ b/lib/nmatrix/numruby.rb @@ -63,8 +63,8 @@ def self.hstack(objs) end def self.tri(N, M=None, k=0, nm_dtype=float) - if M is None: - M = N + if M is nil + M = N m = greater_equal.outer(arange(N, nm_dtype=nm_int(0, N)), arange(-k, M-k, nm_dtype=nm_int(-k, M - k))) From a2281016f21a260d4e536a755e90eb0776709420 Mon Sep 17 00:00:00 2001 From: Rajiv Ranjan Singh Date: Mon, 18 May 2020 03:23:07 +0530 Subject: [PATCH 5/5] change upper case to lower case in parameters --- lib/nmatrix/numruby.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/nmatrix/numruby.rb b/lib/nmatrix/numruby.rb index a7e2ce3..6498dd9 100644 --- a/lib/nmatrix/numruby.rb +++ b/lib/nmatrix/numruby.rb @@ -62,12 +62,12 @@ def self.hstack(objs) result end - def self.tri(N, M=None, k=0, nm_dtype=float) - if M is nil - M = N + def self.tri(n, m=None, k=0, nm_dtype=float) + if m is nil + m = n - m = greater_equal.outer(arange(N, nm_dtype=nm_int(0, N)), - arange(-k, M-k, nm_dtype=nm_int(-k, M - k))) + m = greater_equal.outer(arange(n, nm_dtype=nm_int(0, n)), + arange(-k, m-k, nm_dtype=nm_int(-k, m - k))) # Avoid making a copy if the requested type is already bool m = m.stype(nm_dtype, copy=False)