source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/Wrappers/Matrix.cs @ 14244

Last change on this file since 14244 was 14244, checked in by abeham, 5 years ago

#2651: worked on igraph integration, additional layout algorithms, density, page rank

  • added unit tests
File size: 2.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23
24namespace HeuristicLab.IGraph.Wrappers {
25  public sealed class Matrix : IDisposable {
26    igraph_matrix_t matrix;
27    internal igraph_matrix_t NativeInstance { get { return matrix; } }
28    public int Rows { get { return matrix.nrow; } }
29    public int Columns { get { return matrix.ncol; } }
30
31    public Matrix(int nrow, int ncol) {
32      if (nrow < 0 || ncol < 0) throw new ArgumentException("Rows and Columns must be >= 0");
33      matrix = new igraph_matrix_t();
34      DllImporter.igraph_matrix_init(matrix, nrow, ncol);
35    }
36    public Matrix(Matrix other) {
37      if (other == null) throw new ArgumentNullException("other");
38      matrix = new igraph_matrix_t();
39      DllImporter.igraph_matrix_copy(matrix, other.NativeInstance);
40    }
41    ~Matrix() {
42      DllImporter.igraph_matrix_destroy(matrix);
43    }
44
45    public void Dispose() {
46      if (matrix == null) return;
47      DllImporter.igraph_matrix_destroy(matrix);
48      matrix = null;
49      GC.SuppressFinalize(this);
50    }
51
52    public double this[int row, int col] {
53      get {
54        if (row < 0 || row > Rows || col < 0 || col > Columns) throw new IndexOutOfRangeException("Trying to get cell(" + row + ";" + col + ") of matrix(" + Rows + ";" + Columns + ").");
55        return DllImporter.igraph_matrix_e(matrix, row, col);
56      }
57      set {
58        if (row < 0 || row > Rows || col < 0 || col > Columns) throw new IndexOutOfRangeException("Trying to set cell(" + row + ";" + col + ") of matrix(" + Rows + ";" + Columns + ").");
59        DllImporter.igraph_matrix_set(matrix, row, col, value);
60      }
61    }
62
63    public double[,] ToMatrix() {
64      var result = new double[Rows, Columns];
65      for (var i = 0; i < Rows; i++) {
66        for (var j = 0; j < Columns; j++) {
67          result[i, j] = DllImporter.igraph_matrix_e(matrix, i, j);
68        }
69      }
70      return result;
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.