OOP-Matrices

on Michael Vorburger's Personal Homepage
Home Personal Projects alpha ware

 

This is the third of the five exercises Previous ] Next ] we had to implement in the ADA programming course at EPFL [EXTERNAL]: A package for matrice operatins. I chose to implement it using ADA 's OOP features. (tagged types)

(If you are interested, I would be glad to send you the complete project report: mike@vorburger.ch [E-MAIL])

Matrices.ADS

---------------------------------------------------------------
-- Sample for an object-oriented implementation of Matrices;
-- could easily be extended to full functionality.
---------------------------------------------------------------
--
-- FILE: Matrices.ADS
-- This package defines an abstract Matrix and operations
-- common to all matrices; defined either as abstract primitive
-- operations or as class-wide subprograms.
--
-- AUTHOR:   Michael Vorburger
-- DATE OC:  11/4/96 15:00
-- LAST MOD:
---------------------------------------------------------------

generic
   DIMENSION: POSITIVE;

package MATRICES is

   subtype INDEX is INTEGER range 1..DIMENSION;

   type ABSTRACT_MATRIX is abstract tagged private;

-- Abstract primitive operations:
   function Component (M: in ABSTRACT_MATRIX;
                       Rowa: in index;
                       Column: in index)
            return FLOAT is abstract;

-- Class-wide subprograms:
   procedure Write (M: in ABSTRACT_MATRIX'Class);

private
   type ABSTRACT_MATRIX is abstract tagged null record;

end matrices;
     

Matrices.ADB

---------------------------------------------------------------
-- Sample for an object-oriented implementation of Matrices;
-- could easily be extended to full functionality.
---------------------------------------------------------------
--
-- FILE: Matrices.ADB
-- This package contains the abstract Matrix and operations
-- common to all matrices; defined either as abstract primitive
-- operations or as class-wide subprograms.
--
-- AUTHOR:   Michael Vorburger
-- DATE OC:  11/4/96 15:00
-- LAST MOD:
---------------------------------------------------------------

with TEXT_IO;

package body MATRICES is

   package FIO is new TEXT_IO.FLOAT_IO(FLOAT);

   procedure WRITE(M: in ABSTRACT_MATRIX'CLASS) is
   begin
     for I in INDEX loop
       for J in INDEX loop
         FIO.PUT(COMPONENT(M,I,J));
       end loop;
       TEXT_IO.NEW_LINE;
     end loop;
   end WRITE;

end MATRICES;
    

Matrices-AsArray.ADS

---------------------------------------------------------------
-- Sample for an object-oriented implementation of Matrices;
-- could easily be extended to full functionality.
---------------------------------------------------------------
--
-- FILE: Matrices-AsArray.ADS
-- This package contains a matrix based implementation. Some
-- abstract primitive operations are overriden and new
-- non-abstract one are added.
-- 
--
-- AUTHOR:   Michael Vorburger
-- DATE OC:  11/4/96 15:00
-- LAST MOD:
---------------------------------------------------------------

generic
package MATRICES.ASARRAY is

   type MATRIX_ASARRAY is new ABSTRACT_MATRIX with private;

-- Overriding of inherited primitive operations:
   function Component  (M: in MATRIX_ASARRAY;
                        Rowa: in index;
                        Column: in index) return FLOAT;

-- Introduction of new primitive operations:
   function I return MATRIX_ASARRAY;

private

   type VECTOR is array(INDEX'RANGE,INDEX'range) of float;

   type MATRIX_ASARRAY is new ABSTRACT_MATRIX with
   record
      A : VECTOR;
   end record;

end MATRICES.ASARRAY;
    

Matrices-AsArray.ADB

---------------------------------------------------------------
-- Sample for an object-oriented implementation of Matrices;
-- could easily be extended to full functionality.
---------------------------------------------------------------
--
-- FILE: Matrices-AsArray.ADB
-- This package contains a matrix based implementation. Some
-- abstract primitive operations are overriden and new
-- non-abstract one are added.
--
-- AUTHOR:   Michael Vorburger
-- DATE OC:  11/4/96 15:00
-- LAST MOD:
---------------------------------------------------------------

package body MATRICES.ASARRAY is

   function Component  (M: in MATRIX_ASARRAY;
                        Rowa: in index;
                        Column: in index) return FLOAT is
   begin
      return M.A(ROWA,COLUMN);
   end COMPONENT;

   -- That's just demo; doesn't consider "index" ...
   function I return MATRIX_ASARRAY is
     T: MATRIX_ASARRAY;
   begin
      T.A(1,1):=1.0; T.A(1,2):=0.0; T.A(1,3):=0.0; T.A(2,1):=0.0;
      T.A(2,2):=1.0; T.A(2,3):=0.0; T.A(3,1):=0.0; T.A(3,2):=0.0;
      T.A(3,3):=1.0;
      return T;
   end I;

end MATRICES.ASARRAY;
    

TEST1.ADB

---------------------------------------------------------------
-- Sample for an object-oriented implementation of Matrices;
-- could easily be extended to full functionality.
---------------------------------------------------------------
--
-- FILE: Test1.adb
-- This module shows the usage the packages for abstract
-- matrices and their As-Array implementation.
--
-- AUTHOR:   Michael Vorburger
-- DATE OC:  11/4/96 15:00
-- LAST MOD:
---------------------------------------------------------------

with MATRICES.ASARRAY;

procedure TEST1 is

   package A_MAT_D3 is new MATRICES(4);  -- "signature"
   package MAT is new A_MAT_D3.ASARRAY;  -- defintion of instantiated child

   M1: MAT.MATRIX_ASARRAY;

begin
   M1:=MAT.I;
   A_MAT_D3.WRITE(M1);
end;    

more ADA: Plot ] Sorting ] [ OOP-Matrices ] Symbol Table ] Sym. Derivation ]

Go to TOP of page
Page last modified on 11-Jan-99
© Copyright 1998-99 homepage@vorburger.ch [E-MAIL]

Site hosted by
ItaWeb, Peruggia (Italy)

KISSfp FrontPage Add-On
KISSfp FrontPage

  URL: http://www.vorburger.ch/projects/epfl/ada/ex3_matrices.html