GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-flt-cx-diag.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2008-2022 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 (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include "byte-swap.h"
31
32#include "ov-flt-cx-diag.h"
33#include "ov-base-diag.cc"
34#include "ov-flt-re-diag.h"
35#include "ov-flt-complex.h"
36#include "ov-flt-cx-mat.h"
37#include "ls-utils.h"
38
39
41
43 "float complex diagonal matrix", "single");
44
45static octave_base_value *
47{
49 = dynamic_cast<const octave_float_complex_diag_matrix&> (a);
50
52}
53
56{
59}
60
63{
64 octave_base_value *retval = nullptr;
65
66 if (m_matrix.nelem () == 1)
67 {
68 retval = new octave_float_complex (m_matrix (0, 0));
70 if (rv2)
71 {
72 delete retval;
73 retval = rv2;
74 }
75 }
77 {
79 }
80
81 return retval;
82}
83
86{
87 DiagMatrix retval;
88
89 if (! force_conversion)
90 warn_implicit_conversion ("Octave:imag-to-real",
91 type_name (), "real matrix");
92
93 retval = ::real (m_matrix);
94
95 return retval;
96}
97
100{
101 DiagMatrix retval;
102
103 if (! force_conversion)
104 warn_implicit_conversion ("Octave:imag-to-real",
105 type_name (), "real matrix");
106
107 retval = ::real (m_matrix);
108
109 return retval;
110}
111
114{
116}
117
120{
121 return m_matrix;
122}
123
126{
128}
129
132{
133 return m_matrix;
134}
135
138{
139 switch (umap)
140 {
141 case umap_abs:
142 return m_matrix.abs ();
143 case umap_real:
145 case umap_conj:
147 case umap_imag:
149 case umap_sqrt:
150 {
152 (std::sqrt);
153 FloatComplexDiagMatrix retval (tmp);
154 retval.resize (m_matrix.rows (), m_matrix.columns ());
155 return retval;
156 }
157 default:
158 return to_dense ().map (umap);
159 }
160}
161
162bool
164 bool /* save_as_floats */)
165{
166
167 int32_t r = m_matrix.rows ();
168 int32_t c = m_matrix.cols ();
169 os.write (reinterpret_cast<char *> (&r), 4);
170 os.write (reinterpret_cast<char *> (&c), 4);
171
173 save_type st = LS_FLOAT;
174 if (m_matrix.length () > 4096) // FIXME: make this configurable.
175 {
176 float max_val, min_val;
177 if (m.all_integers (max_val, min_val))
178 st = octave::get_save_type (max_val, min_val);
179 }
180
181 const FloatComplex *mtmp = m.data ();
182 write_floats (os, reinterpret_cast<const float *> (mtmp), st, 2 * m.numel ());
183
184 return true;
185}
186
187bool
190{
191 int32_t r, c;
192 char tmp;
193 if (! (is.read (reinterpret_cast<char *> (&r), 4)
194 && is.read (reinterpret_cast<char *> (&c), 4)
195 && is.read (reinterpret_cast<char *> (&tmp), 1)))
196 return false;
197 if (swap)
198 {
199 swap_bytes<4> (&r);
200 swap_bytes<4> (&c);
201 }
202
203 FloatComplexDiagMatrix m (r, c);
204 FloatComplex *re = m.fortran_vec ();
206 read_floats (is, reinterpret_cast<float *> (re),
207 static_cast<save_type> (tmp), 2 * len, swap, fmt);
208
209 if (! is)
210 return false;
211
212 m_matrix = m;
213
214 return true;
215}
216
217bool
219 FloatComplex& x) const
220{
221 bool retval = val.is_complex_scalar () || val.is_real_scalar ();
222 if (retval)
223 x = val.float_complex_value ();
224 return retval;
225}
ComplexColumnVector conj(const ComplexColumnVector &a)
Definition: CColVector.cc:217
void swap_bytes< 4 >(void *ptr)
Definition: byte-swap.h:63
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:411
Array< U, A > map(F fcn) const
Apply function fcn to each element of the Array<T, Alloc>.
Definition: Array.h:799
const T * data(void) const
Size of the specified dimension.
Definition: Array.h:616
T * fortran_vec(void)
Definition: DiagArray2.h:176
octave_idx_type nelem(void) const
Definition: DiagArray2.h:96
octave_idx_type length(void) const
Definition: DiagArray2.h:95
octave_idx_type cols(void) const
Definition: DiagArray2.h:90
octave_idx_type columns(void) const
Definition: DiagArray2.h:91
octave_idx_type rows(void) const
Definition: DiagArray2.h:89
FloatComplexColumnVector extract_diag(octave_idx_type k=0) const
Definition: fCDiagMatrix.h:144
OCTAVE_API bool all_elements_are_real(void) const
OCTAVE_API FloatDiagMatrix abs(void) const
OCTAVE_API bool all_integers(float &max_val, float &min_val) const
Definition: fCNDArray.cc:289
OCTINTERP_API FloatComplexMatrix float_complex_matrix_value(bool=false) const
virtual octave_base_value * try_narrowing_conversion(void)
Definition: ov-base.h:305
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
octave_value as_double(void) const
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
type_conv_info numeric_conversion_function(void) const
octave_value map(unary_mapper_t umap) const
DiagMatrix diag_matrix_value(bool=false) const
std::string type_name(void) const
bool save_binary(std::ostream &os, bool save_as_floats)
FloatDiagMatrix float_diag_matrix_value(bool=false) const
octave_base_value * try_narrowing_conversion(void)
bool chk_valid_scalar(const octave_value &, FloatComplex &) const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
octave_value as_single(void) const
static int static_type_id(void)
FloatComplex float_complex_value(bool frc_str_conv=false) const
Definition: ov.h:913
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1634
bool is_real_scalar(void) const
Definition: ov.h:655
bool is_complex_scalar(void) const
Definition: ov.h:661
ColumnVector real(const ComplexColumnVector &a)
Definition: dColVector.cc:137
ColumnVector imag(const ComplexColumnVector &a)
Definition: dColVector.cc:143
void write_floats(std::ostream &os, const float *data, save_type type, octave_idx_type len)
Definition: data-conv.cc:942
void read_floats(std::istream &is, float *data, save_type type, octave_idx_type len, bool swap, octave::mach_info::float_format fmt)
Definition: data-conv.cc:836
save_type
Definition: data-conv.h:87
@ LS_FLOAT
Definition: data-conv.h:94
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition: errwarn.cc:344
F77_RET_T const F77_DBLE * x
OCTAVE_NAMESPACE_BEGIN save_type get_save_type(double, double)
Definition: ls-utils.cc:40
class OCTAVE_API ComplexDiagMatrix
Definition: mx-fwd.h:60
class OCTAVE_API FloatComplexMatrix
Definition: mx-fwd.h:34
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:222
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
F77_RET_T len
Definition: xerbla.cc:61