GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-flt-cx-diag.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2008-2025 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-flt-re-diag.h"
34#include "ov-flt-complex.h"
35#include "ls-utils.h"
36
38 "float complex diagonal matrix", "single");
39
40static octave_base_value *
41default_numeric_conversion_function (const octave_base_value& a)
42{
44 = dynamic_cast<const octave_float_complex_diag_matrix&> (a);
45
47}
48
55
58{
59 octave_base_value *retval = nullptr;
60
61 if (m_matrix.nelem () == 1)
62 {
63 retval = new octave_float_complex (m_matrix (0, 0));
65 if (rv2)
66 {
67 delete retval;
68 retval = rv2;
69 }
70 }
72 {
74 }
75
76 return retval;
77}
78
81{
82 DiagMatrix retval;
83
84 if (! force_conversion)
85 warn_implicit_conversion ("Octave:imag-to-real",
86 type_name (), "real matrix");
87
88 retval = ::real (m_matrix);
89
90 return retval;
91}
92
95{
96 DiagMatrix retval;
97
98 if (! force_conversion)
99 warn_implicit_conversion ("Octave:imag-to-real",
100 type_name (), "real matrix");
101
102 retval = ::real (m_matrix);
103
104 return retval;
105}
106
112
118
124
130
133{
134 switch (umap)
135 {
136 case umap_abs:
137 return m_matrix.abs ();
138 case umap_real:
139 return ::real (m_matrix);
140 case umap_conj:
141 return ::conj (m_matrix);
142 case umap_imag:
143 return ::imag (m_matrix);
144 case umap_sqrt:
145 {
147 FloatComplexDiagMatrix retval (tmp);
148 retval.resize (m_matrix.rows (), m_matrix.columns ());
149 return retval;
150 }
151 default:
152 return to_dense ().map (umap);
153 }
154}
155
156bool
158 bool /* save_as_floats */)
159{
160
161 int32_t r = m_matrix.rows ();
162 int32_t c = m_matrix.cols ();
163 os.write (reinterpret_cast<char *> (&r), 4);
164 os.write (reinterpret_cast<char *> (&c), 4);
165
167 save_type st = LS_FLOAT;
168 if (m_matrix.length () > 4096) // FIXME: make this configurable.
169 {
170 float max_val, min_val;
171 if (m.all_integers (max_val, min_val))
172 st = octave::get_save_type (max_val, min_val);
173 }
174
175 const FloatComplex *mtmp = m.data ();
176 write_floats (os, reinterpret_cast<const float *> (mtmp), st, 2 * m.numel ());
177
178 return true;
179}
180
181bool
183 octave::mach_info::float_format fmt)
184{
185 int32_t r, c;
186 char tmp;
187 if (! (is.read (reinterpret_cast<char *> (&r), 4)
188 && is.read (reinterpret_cast<char *> (&c), 4)
189 && is.read (reinterpret_cast<char *> (&tmp), 1)))
190 return false;
191 if (swap)
192 {
193 swap_bytes<4> (&r);
194 swap_bytes<4> (&c);
195 }
196
197 FloatComplexDiagMatrix m (r, c);
198 FloatComplex *re = m.rwdata ();
200 read_floats (is, reinterpret_cast<float *> (re),
201 static_cast<save_type> (tmp), 2 * len, swap, fmt);
202
203 if (! is)
204 return false;
205
206 m_matrix = m;
207
208 return true;
209}
210
211bool
212octave_float_complex_diag_matrix::chk_valid_scalar (const octave_value& val,
213 FloatComplex& x) const
214{
215 bool retval = val.is_complex_scalar () || val.is_real_scalar ();
216 if (retval)
217 x = val.float_complex_value ();
218 return retval;
219}
void swap_bytes< 4 >(void *ptr)
Definition byte-swap.h:63
Array< U, A > map(F fcn) const
Apply function fcn to each element of the Array<T, Alloc>.
Definition Array.h:861
const T * data() const
Size of the specified dimension.
Definition Array.h:665
octave_idx_type numel() const
Number of elements in the array.
Definition Array.h:418
octave_idx_type nelem() const
Definition DiagArray2.h:93
octave_idx_type rows() const
Definition DiagArray2.h:86
octave_idx_type length() const
Definition DiagArray2.h:92
octave_idx_type columns() const
Definition DiagArray2.h:88
octave_idx_type cols() const
Definition DiagArray2.h:87
T * rwdata()
Definition DiagArray2.h:168
FloatDiagMatrix abs() const
FloatComplexColumnVector extract_diag(octave_idx_type k=0) const
bool all_elements_are_real() const
bool all_integers(float &max_val, float &min_val) const
Definition fCNDArray.cc:289
FloatComplexMatrix float_complex_matrix_value(bool=false) const
virtual octave_base_value * try_narrowing_conversion()
Definition ov-base.h:332
ComplexDiagMatrix complex_diag_matrix_value(bool=false) const
FloatComplexDiagMatrix float_complex_diag_matrix_value(bool=false) const
octave_value map(unary_mapper_t umap) const
DiagMatrix diag_matrix_value(bool=false) 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()
type_conv_info numeric_conversion_function() const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
bool is_real_scalar() const
Definition ov.h:610
bool is_complex_scalar() const
Definition ov.h:616
FloatComplex float_complex_value(bool frc_str_conv=false) const
Definition ov.h:874
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition ov.h:1528
ColumnVector real(const ComplexColumnVector &a)
void write_floats(std::ostream &os, const float *data, save_type type, octave_idx_type len)
Definition data-conv.cc:968
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:862
save_type
Definition data-conv.h:85
@ LS_FLOAT
Definition data-conv.h:92
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition errwarn.cc:344
F77_RET_T const F77_DBLE * x
std::complex< float > FloatComplex
Definition oct-cmplx.h:34
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition ov-base.h:246
F77_RET_T len
Definition xerbla.cc:61