From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,32d9aa9d79729b31 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.228.227 with SMTP id sl3mr14028015pbc.5.1340649367612; Mon, 25 Jun 2012 11:36:07 -0700 (PDT) Path: l9ni19124pbj.0!nntp.google.com!news2.google.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!nx02.iad01.newshosting.com!newshosting.com!213.200.89.86.MISMATCH!news1.as3257.net!feeder.erje.net!news-2.dfn.de!news.dfn.de!news.uni-weimar.de!not-for-mail From: stefan-lucks@see-the.signature Newsgroups: comp.lang.ada Subject: Re: Any easy/build-in construct to extract submatrices from larger matrix? Date: Mon, 25 Jun 2012 20:49:30 +0200 Organization: Bauhaus-Universitaet Weimar Message-ID: References: <1xzo3825h9yt7$.45flobqwlel6.dlg@40tude.net> <4FE896B5.10405@obry.net> Reply-To: stefan-lucks@see-the.signature NNTP-Posting-Host: medsec1.medien.uni-weimar.de Mime-Version: 1.0 X-Trace: tigger.scc.uni-weimar.de 1340649366 19009 141.54.178.228 (25 Jun 2012 18:36:06 GMT) X-Complaints-To: news@tigger.scc.uni-weimar.de NNTP-Posting-Date: Mon, 25 Jun 2012 18:36:06 +0000 (UTC) X-X-Sender: lucks@medsec1.medien.uni-weimar.de In-Reply-To: X-Received-Bytes: 4055 Content-Type: TEXT/PLAIN; charset=US-ASCII Date: 2012-06-25T20:49:30+02:00 List-Id: On Mon, 25 Jun 2012, Nasser M. Abbasi wrote: > Here is another one: Given a square matrix, determine if the > matrix is diagonal or not. This is an easy challenge. Ada 2012 provides a reasonably simple way to do this: 1. Specify the type of the Matrix, e.g., type Matrix is array (Positive range <>, Positive range <>) of Float; 2. Specify the precondition (optional): function Is_Diagonal (M: Matrix) return Boolean with Pre => M'First(1) = M'First(2) and M'Last(1) = M'Last(2); 3. Implement the function: function Is_Diagonal (M: Matrix) return Boolean is (for all I in M'Range(1) => (for all J in M'Range(2) => (if I /= J then M(I, J) = 0.0))); This is four lines -- but the last two or three lines could be squeezed into a single one, if all what matters is the line count, disregarding readability. > In Mathematica, this one line function does it > > diagQ[m_] := m === DiagonalMatrix[Diagonal[m]] The comparison with Mathematica is a bit unfair. It uses some auxiliary functions that are just there in Mathematica, and could be easily be implemented in Ada as well. And your estimate of 20 lines is excessive. Consider the following specifications: type Matrix is array(Positive range <>, Positive range <>) of Float; type Vector is array(Positive range <>) of Float; function Is_Diagonal (M: Matrix) return Boolean with Pre => M'First(1) = M'First(2) and M'Last(1) = M'Last(2); function Diagonal (M: Matrix) return Vector with Pre => M'First(1) = M'First(2) and M'Last(1) = M'Last(2); function Diagonal_Matrix(V: Vector) return Matrix; Implementing all three functions in Ada takes just 19 lines (not counting the empty ones): function Diagonal(M: Matrix) return Vector is V: Vector(M'First(1) .. M'Last(1)); begin for I in V'Range loop V(I) := M(I, I); end loop; return V; end Diagonal; function Diagonal_Matrix(V: Vector) return Matrix is M: Matrix(V'First .. V'Last, V'First .. V'Last) := (others => (others => 0.0)); begin for I in V'Range loop M(I, I) := V(I); end loop; return M; end Diagonal_Matrix; function Is_Diagonal (M: Matrix) return Boolean is (M = Diagonal_Matrix(Diagonal(M))); -- ---- Stefan.Lucks (at) uni-weimar.de, University of Weimar, Germany ---- ------ I love the taste of Cryptanalysis in the morning! ------