From: stefan-lucks@see-the.signature
Subject: Re: Any easy/build-in construct to extract submatrices from larger matrix?
Date: Mon, 25 Jun 2012 20:49:30 +0200
Date: 2012-06-25T20:49:30+02:00 [thread overview]
Message-ID: <Pine.LNX.4.64.1206252008280.19899@medsec1.medien.uni-weimar.de> (raw)
In-Reply-To: <jsa7ht$dvb$1@speranza.aioe.org>
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 ----
<http://www.uni-weimar.de/cms/medien/mediensicherheit/home.html>
------ I love the taste of Cryptanalysis in the morning! ------
next prev parent reply other threads:[~2012-06-25 18:36 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-24 8:05 Any easy/build-in construct to extract submatrices from larger matrix? Nasser M. Abbasi
2012-06-24 8:24 ` Dmitry A. Kazakov
2012-06-24 8:55 ` Nasser M. Abbasi
2012-06-24 11:10 ` Dmitry A. Kazakov
2012-06-24 12:16 ` Nasser M. Abbasi
2012-06-24 16:11 ` Dmitry A. Kazakov
2012-06-24 17:14 ` Nasser M. Abbasi
2012-06-24 18:33 ` Dmitry A. Kazakov
2012-06-25 5:44 ` J-P. Rosen
2012-06-25 6:32 ` Nasser M. Abbasi
2012-06-25 7:54 ` J-P. Rosen
2012-06-25 8:20 ` Dmitry A. Kazakov
2012-06-25 14:21 ` Nasser M. Abbasi
2012-06-25 8:36 ` Georg Bauhaus
2012-06-25 13:17 ` Nasser M. Abbasi
2012-06-26 11:44 ` Georg Bauhaus
2012-06-25 16:49 ` Pascal Obry
2012-06-25 17:36 ` Nasser M. Abbasi
2012-06-25 18:49 ` stefan-lucks [this message]
2012-06-26 4:41 ` Nasser M. Abbasi
2012-06-26 7:15 ` Jacob Sparre Andersen
2012-06-26 9:51 ` Nasser M. Abbasi
2012-06-25 9:33 ` Simon Wright
2012-06-26 2:48 ` Jerry
2012-06-26 4:19 ` Nasser M. Abbasi
2012-06-26 7:06 ` Nasser M. Abbasi
2012-06-26 12:54 ` Robert A Duff
2012-06-26 15:19 ` Adam Beneschan
2012-06-26 21:14 ` Robert A Duff
2012-06-27 23:42 ` Shark8
2012-06-26 7:10 ` Dmitry A. Kazakov
2012-06-26 15:06 ` Adam Beneschan
2012-06-26 21:19 ` Robert A Duff
2012-06-26 21:40 ` Adam Beneschan
2012-07-03 4:22 ` Randy Brukardt
2012-07-03 8:37 ` Dmitry A. Kazakov
2012-07-05 1:33 ` Randy Brukardt
2012-07-05 7:08 ` Dmitry A. Kazakov
2012-07-06 23:47 ` Randy Brukardt
2012-07-07 8:22 ` Dmitry A. Kazakov
2012-07-05 18:56 ` Adam Beneschan
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox