1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1998-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_ov_re_sparse_h)
27 #define octave_ov_re_sparse_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cstdlib>
32 
33 #include <iosfwd>
34 #include <string>
35 
36 #include "mx-base.h"
37 #include "str-vec.h"
38 
39 #include "error.h"
40 #include "oct-stream.h"
41 #include "ov-base.h"
42 #include "ov-typeinfo.h"
43 
44 #include "dSparse.h"
45 #include "MatrixType.h"
46 #include "ov-base-sparse.h"
47 #include "ov-cx-sparse.h"
48 
49 class octave_value_list;
50 
51 class
52 OCTINTERP_API
53 octave_sparse_matrix : public octave_base_sparse<SparseMatrix>
54 {
55 public:
56 
octave_sparse_matrix(void)57   octave_sparse_matrix (void)
58     : octave_base_sparse<SparseMatrix> () { }
59 
octave_sparse_matrix(const Matrix & m)60   octave_sparse_matrix (const Matrix& m)
61     : octave_base_sparse<SparseMatrix> (SparseMatrix (m)) { }
62 
octave_sparse_matrix(const NDArray & m)63   octave_sparse_matrix (const NDArray& m)
64     : octave_base_sparse<SparseMatrix> (SparseMatrix (m)) { }
65 
octave_sparse_matrix(const SparseMatrix & m)66   octave_sparse_matrix (const SparseMatrix& m)
67     : octave_base_sparse<SparseMatrix> (m) { }
68 
octave_sparse_matrix(const SparseMatrix & m,const MatrixType & t)69   octave_sparse_matrix (const SparseMatrix& m, const MatrixType& t)
70     : octave_base_sparse<SparseMatrix> (m, t) { }
71 
octave_sparse_matrix(const MSparse<double> & m)72   octave_sparse_matrix (const MSparse<double>& m)
73     : octave_base_sparse<SparseMatrix> (m) { }
74 
octave_sparse_matrix(const MSparse<double> & m,const MatrixType & t)75   octave_sparse_matrix (const MSparse<double>& m, const MatrixType& t)
76     : octave_base_sparse<SparseMatrix> (m, t) { }
77 
octave_sparse_matrix(const Sparse<double> & m)78   octave_sparse_matrix (const Sparse<double>& m)
79     : octave_base_sparse<SparseMatrix> (SparseMatrix (m)) { }
80 
octave_sparse_matrix(const Sparse<double> & m,const MatrixType & t)81   octave_sparse_matrix (const Sparse<double>& m, const MatrixType& t)
82     : octave_base_sparse<SparseMatrix> (SparseMatrix (m), t) { }
83 
octave_sparse_matrix(const octave_sparse_matrix & m)84   octave_sparse_matrix (const octave_sparse_matrix& m)
85     : octave_base_sparse<SparseMatrix> (m) { }
86 
87   ~octave_sparse_matrix (void) = default;
88 
clone(void)89   octave_base_value * clone (void) const
90   { return new octave_sparse_matrix (*this); }
empty_clone(void)91   octave_base_value * empty_clone (void) const
92   { return new octave_sparse_matrix (); }
93 
94   octave_base_value * try_narrowing_conversion (void);
95 
96   idx_vector index_vector (bool require_integers = false) const;
97 
builtin_type(void)98   builtin_type_t builtin_type (void) const { return btyp_double; }
99 
is_real_matrix(void)100   bool is_real_matrix (void) const { return true; }
101 
isreal(void)102   bool isreal (void) const { return true; }
103 
is_double_type(void)104   bool is_double_type (void) const { return true; }
105 
isfloat(void)106   bool isfloat (void) const { return true; }
107 
108   double double_value (bool = false) const;
109 
110   double scalar_value (bool frc_str_conv = false) const
111   { return double_value (frc_str_conv); }
112 
113   Matrix matrix_value (bool = false) const;
114 
115   Complex complex_value (bool = false) const;
116 
117   boolNDArray bool_array_value (bool warn = false) const;
118 
119   charNDArray char_array_value (bool = false) const;
120 
121   ComplexMatrix complex_matrix_value (bool = false) const;
122 
123   ComplexNDArray complex_array_value (bool = false) const;
124 
125   NDArray array_value (bool = false) const;
126 
127   SparseMatrix sparse_matrix_value (bool = false) const
128   { return matrix; }
129 
130   SparseComplexMatrix sparse_complex_matrix_value (bool = false) const
131   { return SparseComplexMatrix (matrix); }
132 
133   SparseBoolMatrix sparse_bool_matrix_value (bool warn = false) const;
134 
135   octave_value convert_to_str_internal (bool pad, bool force, char type) const;
136 
137   octave_value as_double (void) const;
138 
139 #if 0
140   int write (octave::stream& os, int block_size,
141              oct_data_conv::data_type output_type, int skip,
142              octave::mach_info::float_format flt_fmt) const
143   { return os.write (matrix, block_size, output_type, skip, flt_fmt); }
144 #endif
145 
146   bool save_binary (std::ostream& os, bool save_as_floats);
147 
148   bool load_binary (std::istream& is, bool swap,
149                     octave::mach_info::float_format fmt);
150 
151   bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
152 
153   bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
154 
155   mxArray * as_mxArray (void) const;
156 
157   octave_value map (unary_mapper_t umap) const;
158 
159 private:
160   octave_value map (double (*fcn) (double)) const;
161 
162   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
163 };
164 
165 #endif
166