mathpaqs_20230121.0.0_773568e5/lin_alg/sparse/sparse.ads

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
------------------------------------------------------------------------------
--  File:            sparse.ads
--  Description:     Sparse matrices, generic package
--                     Compressed Row Storage (CRS) format
--                     doc. @ http://www.netlib.org/
--
--  Date / Version:  01-Jan-2011 ; 21-Nov-2010 ;
--                   08-Jun-2001 ; ... ; 29-Mar-1999
--
--  Author:          Olivier Besson, Universite de Neuchatel & Cray research
--                   Olivier.Besson (at) UniNe.ch
--
--  Ada version:     Gautier de Montmollin
--                   http://gautiersblog.blogspot.com/
--
--  NB: From 8-Jun-2001 version, (bi)conjugate gradient algorithms
--      are detached in a package independent of matrix type
------------------------------------------------------------------------------

with Ada.Unchecked_Deallocation;

generic
  type Real is digits <>;
  type Index is range <>;
  type Vector is array (Index range <>) of Real;

package Sparse is

  type Index_array is array(Index range <>) of Index;

  --------------------------------------------------
  -- Define a matrix with Compressed Rows Storage --
  --------------------------------------------------

  type CRS_matrix( rows_max_p1, nnz_max: Index ) is record
    val       : Vector(1..nnz_max);
    col_ind   : Index_array(1..nnz_max);
    row_ptr   : Index_array(1..rows_max_p1); -- p1 means +1 -> extra row ptr
    symmetric : Boolean;
    -- rows and nnz are effective upper bounds for matrix re-use
    rows      : Index:= rows_max_p1-1; -- must be in [ 1..rows_max_p1-1 ]
    nnz       : Index:= nnz_max;       -- must be in [ 1..nnz_max ]
  end record;

  -- Access (pointer) to matrix for dynamic allocation/deallocation:

  type p_CRS_matrix is access CRS_matrix;

  procedure Dispose is new Ada.Unchecked_Deallocation(CRS_matrix,p_CRS_matrix);

  -- Just returns the symmetric indicator, no verification

  function Defined_symmetric( A: CRS_matrix ) return Boolean;

  function Rows( A: CRS_matrix ) return Index;

  ----------------------------------
  -- Matrix-vector multiplication --
  ----------------------------------

  -- w:= A*u

  procedure Mult( A: in CRS_matrix; u: Vector; w: in out Vector );

  -- operator version - warning: uses stack, not for large/fast usage !
  --   function "*"( A: CRS_matrix; u: vector ) return vector;

  ------------------------------------------
  -- Put/Add/Get data into/in/from matrix --
  ------------------------------------------

  procedure Put( A: in out CRS_matrix; i,j: Index; value: Real );
  procedure Add( A: in out CRS_matrix; i,j: Index; value: Real );
  function  Get( A: in     CRS_matrix; i,j: Index ) return Real;

  -- pragma Inline(Put, Add);

  position_not_found_in_sparse_matrix: exception;

  ------------------------------------------------
  -- Diverse - re-export of BLAS-style routines --
  ------------------------------------------------

  procedure Copy ( u: in Vector; v: out Vector );
  function "*"(u, v: Vector) return Real;
  procedure Add_scaled ( factor: Real; u: in Vector; v: in out Vector );
  procedure Scale ( factor: Real; u: in out Vector );

end Sparse;