Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16014 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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    public Matrix(double[,] mat) {
42      if (mat == null) throw new ArgumentNullException("mat");
43      matrix = new igraph_matrix_t();
44      var nrows = mat.GetLength(0);
45      var ncols = mat.GetLength(1);
46      DllImporter.igraph_matrix_init(matrix, nrows, ncols);
47      var colwise = new double[ncols * nrows];
48      for (var j = 0; j < ncols; j++)
49        for (var i = 0; i < nrows; i++)
50          colwise[j * nrows + i] = mat[i, j];
51      DllImporter.igraph_vector_init_copy(matrix.data, colwise);
52    }
53    ~Matrix() {
54      DllImporter.igraph_matrix_destroy(matrix);
55    }
56
57    public void Dispose() {
58      if (matrix == null) return;
59      DllImporter.igraph_matrix_destroy(matrix);
60      matrix = null;
61      GC.SuppressFinalize(this);
62    }
63
64    public void Fill(double v) {
65      DllImporter.igraph_matrix_fill(matrix, v);
66    }
67
68    public void Transpose() {
69      DllImporter.igraph_matrix_transpose(matrix);
70    }
71
72    public void Scale(double by) {
73      DllImporter.igraph_matrix_scale(matrix, by);
74    }
75
76    public double this[int row, int col] {
77      get {
78        if (row < 0 || row > Rows || col < 0 || col > Columns) throw new IndexOutOfRangeException("Trying to get cell(" + row + ";" + col + ") of matrix(" + Rows + ";" + Columns + ").");
79        return DllImporter.igraph_matrix_e(matrix, row, col);
80      }
81      set {
82        if (row < 0 || row > Rows || col < 0 || col > Columns) throw new IndexOutOfRangeException("Trying to set cell(" + row + ";" + col + ") of matrix(" + Rows + ";" + Columns + ").");
83        DllImporter.igraph_matrix_set(matrix, row, col, value);
84      }
85    }
86
87    public double[,] ToMatrix() {
88      var result = new double[Rows, Columns];
89      for (var i = 0; i < Rows; i++) {
90        for (var j = 0; j < Columns; j++) {
91          result[i, j] = DllImporter.igraph_matrix_e(matrix, i, j);
92        }
93      }
94      return result;
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.