tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
sparse.h
1 // -*- mode: c++; fill-column: 80; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  Copyright (c) 2010 Juan Jose Garcia Ripoll
4 
5  Tensor is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published
7  by the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Library General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 
21 #ifndef TENSOR_SPARSE_H
22 #define TENSOR_SPARSE_H
23 
24 #include <tensor/tensor.h>
25 
26 namespace tensor {
27 
36  template<typename elt>
37  class Sparse {
38  public:
39  typedef elt elt_t;
40  typedef Tensor<elt> tensor;
41 
43  Sparse();
45  Sparse(index rows, index cols, index nonzero = 0);
47  Sparse(const Indices &row_indices, const Indices &column_indices, const Vector<elt_t> &data,
48  index rows = 0, index columns = 0);
49  /* Create a sparse matrix from its internal representation. */
50  Sparse(const Indices &dims, const Indices &row_start,
51  const Indices &column, const Vector<elt_t> &data);
53  explicit Sparse(const Tensor<elt_t> &tensor);
55  Sparse(const Sparse<elt_t> &s);
57  Sparse &operator=(const Sparse<elt_t> &s);
58 
60  elt_t operator()(index row, index col) const;
61 
63  const Indices &dimensions() const { return dims_; }
65  index dimension(int which) const;
67  index rows() const { return dims_[0]; }
69  index columns() const { return dims_[1]; }
71  index length() const { index r = rows(); return r? row_start_[r] : 0; }
72 
74  bool is_empty() const { return (rows() == 0)||(columns() == 0); }
75 
77  static Sparse<elt_t> eye(index rows, index cols);
79  static Sparse<elt_t> eye(index rows) { return eye(rows,rows); }
81  static Sparse<elt_t> random(index rows, index columns, double density = 0.2);
82 
83  template<typename t> friend const Tensor<t> full(const Sparse<t> &s);
84 
85  const Indices &priv_dims() const { return dims_; }
86  const Indices &priv_row_start() const { return row_start_; }
87  const Indices &priv_column() const { return column_; }
88  const Vector<elt> &priv_data() const { return data_; }
89 
90  public:
98  Vector<elt_t> data_;
99  };
100 
101  typedef Sparse<double> RSparse;
102  typedef Sparse<cdouble> CSparse;
103  const CSparse to_complex(const RSparse &s);
104  inline const CSparse to_complex(const CSparse &c) { return c; }
105 
107  //
108  // Unary operations
109  //
110 
111  template<typename t>
112  const Sparse<t> operator-(const Sparse<t> &);
113 
114  //
115  // Binary operations
116  //
117 
118  template<typename t>
119  const Sparse<t> operator*(t b, const Sparse<t> &s);
120  template<typename t>
121  const Sparse<t> operator*(const Sparse<t> &s, t b);
122  template<typename t>
123  const Sparse<t> operator/(const Sparse<t> &s, t b);
124 
125  template<typename t>
126  const Sparse<t> operator+(const Sparse<t> &m1, const Sparse<t> &m2);
127  template<typename t>
128  const Sparse<t> operator-(const Sparse<t> &m1, const Sparse<t> &m2);
129  template<typename t>
130  const Sparse<t> operator*(const Sparse<t> &m1, const Sparse<t> &m2);
131 
133  template<typename t>
134  const Sparse<t> kron(const Sparse<t> &s1, const Sparse<t> &s2);
136  template<typename t>
137  const Sparse<t> kron2(const Sparse<t> &s1, const Sparse<t> &s2);
138 
139  //
140  // Comparison
141  //
142  template<typename t1, typename t2>
143  inline bool all_equal(const Sparse<t1> &s1, const Sparse<t2> &s2) {
144  return all_equal(s1.dimensions(), s2.dimensions()) &&
145  all_equal(s1.priv_row_start(), s2.priv_row_start()) &&
146  all_equal(s1.priv_column(), s2.priv_column()) &&
147  all_equal(s1.priv_data(), s2.priv_data());
148  }
149 
150  template<typename t1, typename t2>
151  inline bool all_equal(const Sparse<t1> &s1, const Tensor<t2> &s2) {
152  return all_equal(full(s1), s2);
153  }
154 
155  template<typename t1, typename t2>
156  inline bool all_equal(const Tensor<t1> &s1, const Sparse<t2> &s2) {
157  return all_equal(full(s2), s1);
158  }
159 
160  const RTensor mmult(const RTensor &m1, const RSparse &m2);
161  const CTensor mmult(const CTensor &m1, const CSparse &m2);
162  const RTensor mmult(const RSparse &m1, const RTensor &m2);
163  const CTensor mmult(const CSparse &m1, const CTensor &m2);
164 
165  inline const RSparse &real(const RSparse &A) { return A; }
166  inline const RSparse &conj(const RSparse &A) { return A; }
167  inline const RSparse imag(const RSparse &A) {
168  return RSparse(A.rows(), A.columns());
169  }
170 
171  const RSparse real(const CSparse &A);
172  const CSparse conj(const CSparse &A);
173  const RSparse imag(const CSparse &A);
174 
175 } // namespace tensor
176 
177 #ifdef TENSOR_LOAD_IMPL
178 #include <tensor/detail/sparse_base.hpp>
179 #include <tensor/detail/sparse_kron.hpp>
180 #include <tensor/detail/sparse_ops.hpp>
181 #endif
182 
183 #endif // !TENSOR_SPARSE_H
A sparse matrix.
Definition: sparse.h:37
index rows() const
Number of rows.
Definition: sparse.h:67
Vector< elt_t > data_
The single data entries.
Definition: sparse.h:98
Indices column_
Gives for each data_ entry the column in the matrix.
Definition: sparse.h:96
const Indices & dimensions() const
Return Sparse matrix dimensions.
Definition: sparse.h:63
static Sparse< elt_t > random(index rows, index columns, double density=0.2)
Return a random sparse matrix.
An N-dimensional array of numbers.
Definition: tensor.h:47
static Sparse< elt_t > eye(index rows)
Identity matrix in sparse form.
Definition: sparse.h:79
static Sparse< elt_t > eye(index rows, index cols)
Identity matrix in sparse form.
index dimension(int which) const
Length of a given Sparse matrix index.
index columns() const
Number of columns.
Definition: sparse.h:69
Sparse()
Build an empty matrix.
Sparse & operator=(const Sparse< elt_t > &s)
Assignment operator.
Vector of 'index' type, where 'index' fits the indices of a tensor.
Definition: indices.h:35
Indices dims_
The dimensions (rows and columns) of the sparse matrix.
Definition: sparse.h:92
Indices row_start_
Gives for each row of the matrix at which index the column_/data_ entries start.
Definition: sparse.h:94
const RTensor conj(const RTensor &r)
Complex conjugate of a real tensor.
Definition: tensor.h:461
index length() const
Number of nonzero elements.
Definition: sparse.h:71
elt_t operator()(index row, index col) const
Return an element of the sparse matrix.
bool is_empty() const
Empty matrix?
Definition: sparse.h:74